Skip to content

Commit d5bd9c3

Browse files
committed
feat: Adding a custom importer for the language-server.
1 parent 6bc272d commit d5bd9c3

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

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 path to the block
24+
* file from it's @block directive
2525
*/
2626
importer: Importer;
2727

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

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();

0 commit comments

Comments
 (0)