Skip to content

Commit e564970

Browse files
committed
feat: Load css-blocks configuration from the config file for vscode.
1 parent 9dbacfb commit e564970

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed

packages/@css-blocks/language-server/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"dependencies": {
1616
"@css-blocks/core": "^0.24.0",
17+
"@css-blocks/config": "^0.24.0",
1718
"@glimmer/syntax": "^0.42.1",
1819
"opticss": "^0.6.2",
1920
"vscode-languageserver": "^5.2.1",

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

+50-14
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
1-
import { BlockFactory, CssBlockError, Options, resolveConfiguration } from "@css-blocks/core/dist/src";
2-
import { BlockParser } from "@css-blocks/core/dist/src/BlockParser/BlockParser";
1+
import { search as searchForConfig } from "@css-blocks/config";
2+
import { BlockFactory, Configuration, CssBlockError, resolveConfiguration } from "@css-blocks/core";
33
import { CompletionItem, Definition, DidChangeConfigurationNotification, DocumentLink, DocumentLinkParams, IConnection, InitializeParams, InitializeResult, TextDocumentChangeEvent, TextDocumentPositionParams, TextDocuments } from "vscode-languageserver";
4+
import { URI } from "vscode-uri";
45

56
import { emberCompletionProvider } from "./completionProviders/emberCompletionProvider";
67
import { createBlockFactory } from "./createBlockFactory";
7-
import { createBlockParser } from "./createBlockParser";
88
import { emberDefinitionProvider } from "./definitionProviders/emberDefinitionProvider";
99
import { blockLinksProvider } from "./documentLinksProviders/blockLinkProvider";
1010
import { documentContentChange } from "./eventHandlers/documentContentChange";
11+
import { LSImporter } from "./Importer";
1112
import { PathTransformer } from "./pathTransformers/PathTransformer";
1213
import { SERVER_CAPABILITIES } from "./serverCapabilities";
1314
import { isBlockFile } from "./util/blockUtils";
1415
import { convertErrorsToDiagnostics } from "./util/diagnosticsUtils";
1516
import { isTemplateFile, validateTemplates } from "./util/hbsUtils";
1617

1718
export class Server {
19+
_blockFactory: BlockFactory | undefined;
20+
_config: Readonly<Configuration> | undefined;
1821
connection: IConnection;
1922
documents: TextDocuments;
20-
blockFactory: BlockFactory;
21-
blockParser: BlockParser;
2223
pathTransformer: PathTransformer;
2324
hasConfigurationCapability = false;
2425
hasWorkspaceFolderCapability = false;
2526

26-
constructor(connection: IConnection, documents: TextDocuments, pathTransformer: PathTransformer, cssBlocksOptions?: Options) {
27+
constructor(connection: IConnection, documents: TextDocuments, pathTransformer: PathTransformer) {
2728
this.connection = connection;
2829
this.documents = documents;
2930
this.pathTransformer = pathTransformer;
3031

31-
// NOTE: creating these instances directly in the constructor because we
32-
// don't currently have a need to expose them for mocking in testing.
33-
const config = resolveConfiguration(cssBlocksOptions);
34-
let blockFactory = createBlockFactory(config);
35-
this.blockFactory = blockFactory;
36-
this.blockParser = createBlockParser(config, blockFactory);
37-
3832
this.registerDocumentEvents();
3933
this.registerConnectionEvents();
4034
}
4135

36+
get config(): Readonly<Configuration> {
37+
if (!this._config) {
38+
this._config = resolveConfiguration({});
39+
return this._config;
40+
}
41+
return this._config;
42+
}
43+
44+
get blockFactory(): BlockFactory {
45+
if (!this._blockFactory) {
46+
this._blockFactory = createBlockFactory(this.config);
47+
}
48+
return this._blockFactory;
49+
}
50+
4251
listen() {
4352
this.documents.listen(this.connection);
4453
this.connection.listen();
@@ -91,8 +100,35 @@ export class Server {
91100
});
92101
}
93102

94-
private onConnectionInitialize(params: InitializeParams): InitializeResult {
103+
private async onConnectionInitialize(params: InitializeParams): Promise<InitializeResult> {
95104
let capabilities = params.capabilities;
105+
let options: Partial<Configuration>;
106+
107+
// TODO #1: We need to spin up a server per workspace folder.
108+
// TODO #1: Then the rootUri should come in as part of the constructor params.
109+
// TODO #1: But the rest of the config lookup logic should remain basically the same.
110+
// TODO #2: There should be a configuration option to set the rootDir for CSS Blocks
111+
// TODO #2: as well as a configuration option to set the configuration file explicitly
112+
// TODO #2: instead of only doing a search for the configuration file.
113+
let result: Partial<Configuration> | null = null;
114+
let rootDir: string | null = null;
115+
if (params.workspaceFolders) {
116+
for (let wsFolder of params.workspaceFolders) {
117+
let folderPath = URI.parse(wsFolder.uri).fsPath;
118+
result = await searchForConfig(folderPath);
119+
if (result) {
120+
rootDir = folderPath;
121+
break;
122+
}
123+
}
124+
} else if (params.rootPath) {
125+
rootDir = params.rootPath;
126+
result = await searchForConfig(params.rootPath);
127+
}
128+
options = result || (rootDir ? {rootDir} : {});
129+
options.importer = new LSImporter(this.documents, options.importer);
130+
this._config = resolveConfiguration(options);
131+
this._blockFactory = createBlockFactory(this._config);
96132

97133
// Does the client support the `workspace/configuration` request?
98134
// If not, we will fall back using global settings

packages/@css-blocks/language-server/src/createBlockParser.ts

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

4-
import { LSImporter } from "./Importer";
54
import { EmberClassicTransformer } from "./pathTransformers/EmberClassicTransformer";
65
import { Server } from "./Server";
76

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

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

1716
server.listen();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
disablePreprocessChaining: true,
3+
};

0 commit comments

Comments
 (0)