Skip to content

Commit

Permalink
Fix services initialization
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Jan 27, 2021
1 parent d35e49a commit 39b111e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 51 deletions.
106 changes: 55 additions & 51 deletions src/yamlServerInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@ import { NotificationHandlers } from './languageserver/handlers/notificationHand
import { RequestHandlers } from './languageserver/handlers/requestHandlers';
import { ValidationHandler } from './languageserver/handlers/validationHandlers';
import { SettingsHandler } from './languageserver/handlers/settingsHandlers';
import { ClientCapabilities } from 'vscode-json-languageservice';

export class YAMLServerInit {
private yamlSettings: SettingsState;
languageService: LanguageService;
languageHandler: LanguageHandlers;
validationHandler: ValidationHandler;

constructor(
private readonly connection: IConnection,
yamlSettings: SettingsState,
workspaceContext: WorkspaceContextService,
schemaRequestService: SchemaRequestService
private yamlSettings: SettingsState,
private workspaceContext: WorkspaceContextService,
private schemaRequestService: SchemaRequestService
) {
this.yamlSettings = yamlSettings;

this.languageService = getCustomLanguageService(schemaRequestService, workspaceContext, ClientCapabilities.LATEST);

this.yamlSettings.documents.listen(this.connection);

/**
Expand All @@ -39,48 +33,7 @@ export class YAMLServerInit {
*/
this.connection.onInitialize(
(params: InitializeParams): InitializeResult => {
this.yamlSettings.capabilities = params.capabilities;
this.languageService = getCustomLanguageService(schemaRequestService, workspaceContext, 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.rangeFormatting &&
this.yamlSettings.capabilities.textDocument.rangeFormatting.dynamicRegistration
);
this.yamlSettings.hasWorkspaceFolderCapability =
this.yamlSettings.capabilities.workspace && !!this.yamlSettings.capabilities.workspace.workspaceFolders;
return {
capabilities: {
textDocumentSync: this.yamlSettings.documents.syncKind,
completionProvider: { resolveProvider: false },
hoverProvider: true,
documentSymbolProvider: true,
documentFormattingProvider: false,
documentOnTypeFormattingProvider: {
firstTriggerCharacter: '\n',
},
documentRangeFormattingProvider: false,
documentLinkProvider: {},
foldingRangeProvider: true,
workspace: {
workspaceFolders: {
changeNotifications: true,
supported: true,
},
},
},
};
return this.connectionInitialized(params);
}
);
this.connection.onInitialized(() => {
Expand All @@ -90,7 +43,58 @@ export class YAMLServerInit {
});
}
});
}

// public for test setup
connectionInitialized(params: InitializeParams): InitializeResult {
this.yamlSettings.capabilities = params.capabilities;
this.languageService = getCustomLanguageService(this.schemaRequestService, this.workspaceContext, 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.rangeFormatting &&
this.yamlSettings.capabilities.textDocument.rangeFormatting.dynamicRegistration
);
this.yamlSettings.hasWorkspaceFolderCapability =
this.yamlSettings.capabilities.workspace && !!this.yamlSettings.capabilities.workspace.workspaceFolders;

this.registerHandlers();

return {
capabilities: {
textDocumentSync: this.yamlSettings.documents.syncKind,
completionProvider: { resolveProvider: false },
hoverProvider: true,
documentSymbolProvider: true,
documentFormattingProvider: false,
documentOnTypeFormattingProvider: {
firstTriggerCharacter: '\n',
},
documentRangeFormattingProvider: false,
documentLinkProvider: {},
foldingRangeProvider: true,
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);
const settingsHandler = new SettingsHandler(this.connection, this.languageService, this.yamlSettings, this.validationHandler);
Expand Down
7 changes: 7 additions & 0 deletions test/utils/testHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { YAMLServerInit } from '../../src/yamlServerInit';
import { LanguageService, LanguageSettings } from '../../src';
import { ValidationHandler } from '../../src/languageserver/handlers/validationHandlers';
import { LanguageHandlers } from '../../src/languageserver/handlers/languageHandlers';
import { ClientCapabilities } from 'vscode-json-languageservice';

export function toFsPath(str: unknown): string {
if (typeof str !== 'string') {
Expand Down Expand Up @@ -63,6 +64,12 @@ export function setupLanguageService(languageSettings: LanguageSettings): TestLa
};
const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection);
const serverInit = new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService);
serverInit.connectionInitialized({
processId: null,
capabilities: ClientCapabilities.LATEST,
rootUri: null,
workspaceFolders: null,
});
const languageService = serverInit.languageService;
const validationHandler = serverInit.validationHandler;
const languageHandler = serverInit.languageHandler;
Expand Down

0 comments on commit 39b111e

Please sign in to comment.