Skip to content

Commit d71c54a

Browse files
committed
feat: update configuration and revalidate files when extension config is changed
1 parent eaeaf5f commit d71c54a

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

packages/server/src/sampleServer.ts

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
createConnection,
1212
Diagnostic,
1313
DiagnosticSeverity,
14+
DidChangeConfigurationNotification,
1415
InitializeParams,
16+
InitializeResult,
1517
Position,
1618
ProposedFeatures,
1719
Range,
@@ -21,8 +23,13 @@ import {
2123
TextEdit,
2224
} from 'vscode-languageserver/node';
2325
import { TextDocument } from 'vscode-languageserver-textdocument';
24-
import { configService } from './services/configuration.service';
25-
import { validateFilePaths } from './validations/validate';
26+
27+
import {
28+
configService,
29+
CortexCommandLanguageSupportConfiguration,
30+
} from './services/configuration.service';
31+
import { validateFilePaths } from './validations/validateFilePath';
32+
import { fsService } from './services/fileSystem.service';
2633

2734
const connection = createConnection(ProposedFeatures.all);
2835

@@ -33,7 +40,7 @@ connection.console.info(
3340
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
3441
documents.listen(connection);
3542

36-
connection.onInitialize((params: InitializeParams) => {
43+
connection.onInitialize((params: InitializeParams): InitializeResult => {
3744
const capabilities = params.capabilities;
3845

3946
// Does the client support the `workspace/configuration` request?
@@ -47,7 +54,9 @@ connection.onInitialize((params: InitializeParams) => {
4754
configService.hasDiagnosticRelatedInformationCapability =
4855
!!capabilities.textDocument?.publishDiagnostics?.relatedInformation;
4956

50-
return {
57+
fsService.workspaceFolders = params.workspaceFolders ?? [];
58+
59+
const result: InitializeResult = {
5160
capabilities: {
5261
// codeActionProvider: true,
5362
textDocumentSync: {
@@ -59,6 +68,45 @@ connection.onInitialize((params: InitializeParams) => {
5968
// },
6069
},
6170
};
71+
72+
if (configService.hasWorkspaceFolderCapability) {
73+
result.capabilities.workspace = {
74+
workspaceFolders: {
75+
supported: true,
76+
},
77+
};
78+
}
79+
80+
return result;
81+
});
82+
83+
connection.onInitialized(() => {
84+
if (configService.hasConfigurationCapability) {
85+
// Register for all configuration changes.
86+
connection.client.register(
87+
DidChangeConfigurationNotification.type,
88+
undefined
89+
);
90+
}
91+
if (configService.hasWorkspaceFolderCapability) {
92+
connection.workspace.onDidChangeWorkspaceFolders((_event) => {
93+
connection.console.log('Workspace folder change event received.');
94+
});
95+
}
96+
});
97+
98+
connection.onDidChangeConfiguration((change) => {
99+
if (configService.hasConfigurationCapability) {
100+
// Reset all cached document settings
101+
configService.documentSettings.clear();
102+
} else if (change.settings['cortexCommandLanguageSupport']) {
103+
configService.globalSettings = <CortexCommandLanguageSupportConfiguration>(
104+
change.settings['cortexCommandLanguageSupport']
105+
);
106+
}
107+
108+
// Revalidate all open text documents
109+
documents.all().forEach(validate);
62110
});
63111

64112
function validate(document: TextDocument): void {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1+
import { Uri } from 'vscode';
2+
3+
export interface CortexCommandLanguageSupportConfiguration {
4+
maxNumberOfProblems: number;
5+
gameDirectoryPath: Uri;
6+
}
7+
18
class ConfigurationService {
29
public hasConfigurationCapability = false;
310
public hasWorkspaceFolderCapability = false;
411
public hasDiagnosticRelatedInformationCapability = false;
12+
13+
// The global settings, used when the `workspace/configuration` request is not supported by the client.
14+
// Please note that this is not the case when using this server with the client provided in this example
15+
// but could happen with other clients.
16+
public globalSettings: CortexCommandLanguageSupportConfiguration = {
17+
maxNumberOfProblems: 100,
18+
gameDirectoryPath: Uri.parse('.'),
19+
};
20+
21+
// Cache the settings of all open documents
22+
public documentSettings: Map<
23+
string,
24+
Thenable<CortexCommandLanguageSupportConfiguration>
25+
> = new Map();
526
}
627

728
export const configService = new ConfigurationService();

0 commit comments

Comments
 (0)