forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyamlServerInit.ts
More file actions
175 lines (163 loc) · 7.15 KB
/
yamlServerInit.ts
File metadata and controls
175 lines (163 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { Connection, InitializeParams, InitializeResult, TextDocumentSyncKind } from 'vscode-languageserver';
import {
getLanguageService as getCustomLanguageService,
LanguageService,
SchemaRequestService,
WorkspaceContextService,
} from './languageservice/yamlLanguageService';
import { workspaceFoldersChanged } from './languageservice/utils/paths';
import { URI } from 'vscode-uri';
import { SettingsState } from './yamlSettings';
import { LanguageHandlers } from './languageserver/handlers/languageHandlers';
import { NotificationHandlers } from './languageserver/handlers/notificationHandlers';
import { RequestHandlers } from './languageserver/handlers/requestHandlers';
import { ValidationHandler } from './languageserver/handlers/validationHandlers';
import { SettingsHandler } from './languageserver/handlers/settingsHandlers';
import { YamlCommands } from './commands';
import { WorkspaceHandlers } from './languageserver/handlers/workspaceHandlers';
import { commandExecutor } from './languageserver/commandExecutor';
import { Telemetry } from './languageservice/telemetry';
import { registerCommands } from './languageservice/services/yamlCommands';
import * as l10n from '@vscode/l10n';
import * as path from 'path';
import * as fs from 'fs';
export class YAMLServerInit {
languageService: LanguageService;
languageHandler: LanguageHandlers;
validationHandler: ValidationHandler;
settingsHandler: SettingsHandler;
constructor(
private readonly connection: Connection,
private yamlSettings: SettingsState,
private workspaceContext: WorkspaceContextService,
private schemaRequestService: SchemaRequestService,
private telemetry: Telemetry
) {
this.yamlSettings.documents.listen(this.connection);
/**
* Run when the client connects to the server after it is activated.
* The server receives the root path(s) of the workspace and the client capabilities.
*/
this.connection.onInitialize(async (params: InitializeParams): Promise<InitializeResult> => {
return this.connectionInitialized(params);
});
this.connection.onInitialized(() => {
if (this.yamlSettings.hasWsChangeWatchedFileDynamicRegistration) {
this.connection.workspace.onDidChangeWorkspaceFolders((changedFolders) => {
this.yamlSettings.workspaceFolders = workspaceFoldersChanged(this.yamlSettings.workspaceFolders, changedFolders);
});
}
// need to call this after connection initialized
this.settingsHandler.registerHandlers();
this.settingsHandler.pullConfiguration();
});
}
public async setupl10nBundle(params: InitializeParams): Promise<void> {
const __dirname = path.dirname(__filename);
const l10nPath: string = params.initializationOptions?.l10nPath || path.join(__dirname, '../../../l10n');
const locale: string = params.locale || 'en';
if (l10nPath) {
const bundleFile = !fs.existsSync(path.join(l10nPath, `bundle.l10n.${locale}.json`))
? `bundle.l10n.json`
: `bundle.l10n.${locale}.json`;
const baseBundleFile = path.join(l10nPath, bundleFile);
process.env.VSCODE_NLS_CONFIG = JSON.stringify({
locale,
_languagePackSupport: true,
});
await l10n.config({
uri: URI.file(baseBundleFile).toString(),
});
}
}
// public for test setup
async connectionInitialized(params: InitializeParams): Promise<InitializeResult> {
this.yamlSettings.capabilities = params.capabilities;
this.languageService = getCustomLanguageService({
schemaRequestService: this.schemaRequestService,
workspaceContext: this.workspaceContext,
connection: this.connection,
yamlSettings: this.yamlSettings,
telemetry: this.telemetry,
clientCapabilities: params.capabilities,
});
// Only try to parse the workspace root if its not null. Otherwise initialize will fail
if (params.rootUri) {
this.yamlSettings.workspaceRoot = URI.parse(params.rootUri);
}
this.yamlSettings.workspaceFolders = params.workspaceFolders || [];
this.yamlSettings.hierarchicalDocumentSymbolSupport = !!(
this.yamlSettings.capabilities.textDocument &&
this.yamlSettings.capabilities.textDocument.documentSymbol &&
this.yamlSettings.capabilities.textDocument.documentSymbol.hierarchicalDocumentSymbolSupport
);
this.yamlSettings.clientDynamicRegisterSupport = !!(
this.yamlSettings.capabilities.textDocument &&
this.yamlSettings.capabilities.textDocument.formatting &&
this.yamlSettings.capabilities.textDocument.formatting.dynamicRegistration
);
this.yamlSettings.hasWorkspaceFolderCapability =
this.yamlSettings.capabilities.workspace && !!this.yamlSettings.capabilities.workspace.workspaceFolders;
this.yamlSettings.hasConfigurationCapability = !!(
this.yamlSettings.capabilities.workspace && !!this.yamlSettings.capabilities.workspace.configuration
);
this.yamlSettings.hasWsChangeWatchedFileDynamicRegistration = !!(
this.yamlSettings.capabilities.workspace &&
this.yamlSettings.capabilities.workspace.didChangeWatchedFiles &&
this.yamlSettings.capabilities.workspace.didChangeWatchedFiles.dynamicRegistration
);
this.registerHandlers();
registerCommands(commandExecutor, this.connection);
await this.setupl10nBundle(params);
return {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Incremental,
completionProvider: { resolveProvider: false },
hoverProvider: true,
documentSymbolProvider: true,
documentFormattingProvider: !this.yamlSettings.clientDynamicRegisterSupport,
documentOnTypeFormattingProvider: {
firstTriggerCharacter: '\n',
},
documentRangeFormattingProvider: false,
definitionProvider: true,
documentLinkProvider: {},
foldingRangeProvider: true,
selectionRangeProvider: true,
codeActionProvider: true,
codeLensProvider: {
resolveProvider: false,
},
executeCommandProvider: {
commands: Object.keys(YamlCommands).map((k) => YamlCommands[k]),
},
workspace: {
workspaceFolders: {
changeNotifications: true,
supported: true,
},
},
},
};
}
private registerHandlers(): void {
// Register all features that the language server has
this.validationHandler = new ValidationHandler(this.connection, this.languageService, this.yamlSettings);
this.settingsHandler = new SettingsHandler(
this.connection,
this.languageService,
this.yamlSettings,
this.validationHandler,
this.telemetry
);
// this.settingsHandler.registerHandlers();
this.languageHandler = new LanguageHandlers(this.connection, this.languageService, this.yamlSettings, this.validationHandler);
this.languageHandler.registerHandlers();
new NotificationHandlers(this.connection, this.languageService, this.yamlSettings, this.settingsHandler).registerHandlers();
new RequestHandlers(this.connection, this.languageService).registerHandlers();
new WorkspaceHandlers(this.connection, commandExecutor).registerHandlers();
}
start(): void {
this.connection.listen();
}
}