Skip to content

Commit 38d194e

Browse files
committed
Merge remote-tracking branch 'origin/vs-code-importer'
2 parents 00985b8 + 19a2543 commit 38d194e

File tree

8 files changed

+78
-39
lines changed

8 files changed

+78
-39
lines changed

Diff for: packages/@css-blocks/core/src/configuration/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export interface Configuration {
2020
preprocessors: Preprocessors;
2121

2222
/**
23-
* An importer is an object that is in charge of findi
24-
*
23+
* An importer is an object that is in charge of finding the contents of a
24+
* block file from its @block directive
2525
*/
2626
importer: Importer;
2727

Diff for: packages/@css-blocks/language-server/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
This is the language server for css-blocks that can plug into any client, such
2+
as, vs code.
3+
4+
Contributing
5+
1. `yarn run compile` in the root folder of `css-blocks`. This will compile all
6+
the code, including this package.
7+
2. Open css-blocks from the workspace. Locate `css-blocks.code-workspace` file
8+
within `css-blocks` project. It should bring a pop up at the bottom that says
9+
something like “use workspace”. Click on it. If it does not, try the
10+
following steps:
11+
1. from command palette, choose “Close workspace” - that should close
12+
everything and take you to the vscode start screen
13+
2. choose “Open folder”
14+
3. navigate to css-blocks and choose to open the css-blocks.code-workspace
15+
file directly
16+
3. Open the debug panel by pressing `cmd+shift+D`
17+
4. At the top there should be a drop down. select Client + Server
18+
5. After that, pressing f5 or clicking the “play” icon next to the selected
19+
configuration should run the extension
20+
6. If youre making changes to the server and you need to compile them as the
21+
changes are made, go to `css-blocks/packages/@css-blocks/language-server` and
22+
run `yarn watch`
23+

Diff for: packages/@css-blocks/language-server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@
3535
"watch": "watch 'yarn run test' './src' --wait=3"
3636
},
3737
"volta": {
38-
"node": "10.11.0"
38+
"node": "10.13.0"
3939
}
4040
}

Diff for: packages/@css-blocks/language-server/src/Importer.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Configuration, ImportedFile, NodeJsImporter } from "@css-blocks/core";
2+
import { TextDocuments } from "vscode-languageserver";
3+
import { URI } from "vscode-uri";
4+
5+
/**
6+
* Imports the contents of the file from the language server client if its
7+
* already opened on the client. Otherwise, it proxies to the NodeJSImporter to
8+
* read the file from the disk
9+
*/
10+
export class LSImporter extends NodeJsImporter {
11+
documents: TextDocuments;
12+
constructor(documents: TextDocuments) {
13+
super();
14+
this.documents = documents;
15+
}
16+
17+
async import(identifier: string, config: Configuration): Promise<ImportedFile> {
18+
// the uri expected is that of a file
19+
let clientDocument = this.documents.get(URI.file(identifier).toString());
20+
// if the document is opened on the client, read from there
21+
// this will allow us to access the contents of an unsaved file
22+
if (clientDocument) {
23+
return {
24+
syntax: this.syntax(identifier, config),
25+
identifier,
26+
defaultName: this.defaultName(identifier, config),
27+
contents: clientDocument.getText(),
28+
};
29+
}
30+
// else import from the defaultImporter (which is the NodeJSImporter) as before
31+
return super.import(identifier, config);
32+
}
33+
}

Diff for: packages/@css-blocks/language-server/src/Server.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ export class Server {
5252

5353
async onDidChangeContent(e: TextDocumentChangeEvent) {
5454
// only track incremental changes within block files
55-
// NOTE: this does seem to cause a little bit of weirdness when editing a
56-
// template with errors since the error locations do not get updated until
57-
// saving the file. We may want to validate the open template files on
58-
// every change?
55+
this.blockFactory.reset();
5956
if (isBlockFile(e.document.uri)) {
60-
const cssBlockErrors = await documentContentChange(e, this.blockParser);
57+
const cssBlockErrors = await documentContentChange(e, this.blockFactory);
6158
this.sendDiagnostics(cssBlockErrors, e.document.uri);
6259

6360
} else if (isTemplateFile(e.document.uri)) {
6461
// Validate template
62+
// NOTE: this does seem to cause a little bit of weirdness when editing a
63+
// template with errors since the error locations do not get updated until
64+
// saving the file. We may want to validate the open template files on
65+
// every change?
6566
}
6667

6768
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import { CssBlockError } from "@css-blocks/core/dist/src";
2-
import { BlockParser } from "@css-blocks/core/dist/src/BlockParser/BlockParser";
1+
import { BlockFactory, CssBlockError } from "@css-blocks/core/dist/src";
32
import { TextDocumentChangeEvent } from "vscode-languageserver";
43
import { URI } from "vscode-uri";
54

6-
import { isBlockFile, parseBlockErrors } from "../util/blockUtils";
5+
import { isBlockFile } from "../util/blockUtils";
76

8-
export async function documentContentChange(e: TextDocumentChangeEvent, parser: BlockParser): Promise<CssBlockError[]> {
7+
export async function documentContentChange(e: TextDocumentChangeEvent, blockFactory: BlockFactory): Promise<CssBlockError[]> {
98
const { uri } = e.document;
109

1110
if (isBlockFile(uri)) {
12-
return await parseBlockErrors(parser, URI.parse(uri).fsPath, e.document.getText());
11+
try {
12+
// parses the block file to get the block and errors if there's a problem
13+
// along the way. The importer ensures that we're getting live contents if
14+
// the block file is opened
15+
await blockFactory.getBlockFromPath(URI.parse(uri).fsPath);
16+
} catch (error) {
17+
return error instanceof CssBlockError ? [error] : [];
18+
}
1319
}
14-
1520
return [];
1621
}

Diff for: packages/@css-blocks/language-server/src/run.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Syntax } from "@css-blocks/core/dist/src";
22
import { ProposedFeatures, TextDocuments, createConnection } from "vscode-languageserver";
33

4+
import { LSImporter } from "./Importer";
45
import { EmberClassicTransformer } from "./pathTransformers/EmberClassicTransformer";
56
import { Server } from "./Server";
67

@@ -11,6 +12,6 @@ const pathTransformer = new EmberClassicTransformer(Syntax.css);
1112
const connection = createConnection(ProposedFeatures.all);
1213
const documents = new TextDocuments();
1314

14-
let server = new Server(connection, documents, pathTransformer);
15+
let server = new Server(connection, documents, pathTransformer, {importer: new LSImporter(documents)});
1516

1617
server.listen();

Diff for: packages/@css-blocks/language-server/src/util/blockUtils.ts

-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { CssBlockError, Syntax } from "@css-blocks/core/dist/src";
2-
import { BlockParser } from "@css-blocks/core/dist/src/BlockParser/BlockParser";
31
import * as fs from "fs";
4-
import { postcss } from "opticss";
52
import * as path from "path";
63
import { CompletionItem, CompletionItemKind, Position, TextDocument } from "vscode-languageserver";
74
import { URI } from "vscode-uri";
@@ -14,27 +11,6 @@ export function isBlockFile(uriOrFsPath: string) {
1411
return uriOrFsPath.endsWith(".block.css");
1512
}
1613

17-
export async function parseBlockErrors(parser: BlockParser, blockFsPath: string, sourceText: string): Promise<CssBlockError[]> {
18-
let errors: CssBlockError[] = [];
19-
20-
try {
21-
await parser.parseSource({
22-
identifier: blockFsPath,
23-
defaultName: path.parse(blockFsPath).name.replace(/\.block/, ""),
24-
originalSource: sourceText,
25-
originalSyntax: Syntax.css,
26-
parseResult: postcss.parse(sourceText, { from: blockFsPath }),
27-
dependencies: [],
28-
});
29-
} catch (error) {
30-
if (error instanceof CssBlockError) {
31-
errors = errors.concat(error);
32-
}
33-
}
34-
35-
return errors;
36-
}
37-
3814
/**
3915
* If the cursor line has an import path, we check to see if the current position
4016
* of the cursor in the line is within the bounds of the import path to decide

0 commit comments

Comments
 (0)