diff --git a/src/clientHandler.ts b/src/clientHandler.ts index 484ef6ad1..e0b4aeeb9 100644 --- a/src/clientHandler.ts +++ b/src/clientHandler.ts @@ -11,10 +11,10 @@ import { State, } from 'vscode-languageclient/node'; import { ServerPath } from './serverPath'; -import { ShowReferencesFeature } from './showReferences'; -import { TelemetryFeature } from './telemetry'; +import { ShowReferencesFeature } from './features/showReferences'; +import { TelemetryFeature } from './features/telemetry'; import { config } from './vscodeUtils'; -import { CustomSemanticTokens } from './semanticTokens'; +import { CustomSemanticTokens, PartialManifest } from './features/semanticTokens'; export interface TerraformLanguageClient { commandPrefix: string; @@ -37,6 +37,7 @@ export class ClientHandler { private lsPath: ServerPath, private outputChannel: vscode.OutputChannel, private reporter: TelemetryReporter, + private manifest: PartialManifest, ) { if (lsPath.hasCustomBinPath()) { this.reporter.sendTelemetryEvent('usePathToBinary'); @@ -90,9 +91,7 @@ export class ClientHandler { const id = `terraform`; const client = new LanguageClient(id, serverOptions, clientOptions); - client.registerFeature( - new CustomSemanticTokens(client, this.extSemanticTokenTypes, this.extSemanticTokenModifiers), - ); + client.registerFeature(new CustomSemanticTokens(client, this.manifest)); const codeLensReferenceCount = config('terraform').get('codelens.referenceCount'); if (codeLensReferenceCount) { diff --git a/src/extension.ts b/src/extension.ts index 8d713b1b6..a569ced60 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -43,9 +43,7 @@ export async function activate(context: vscode.ExtensionContext): Promise } const lsPath = new ServerPath(context); - clientHandler = new ClientHandler(lsPath, outputChannel, reporter); - clientHandler.extSemanticTokenTypes = tokenTypesFromExtManifest(manifest); - clientHandler.extSemanticTokenModifiers = tokenModifiersFromExtManifest(manifest); + clientHandler = new ClientHandler(lsPath, outputChannel, reporter, manifest); // get rid of pre-2.0.0 settings if (config('terraform').has('languageServer.enabled')) { @@ -314,32 +312,6 @@ async function terraformCommand(command: string, languageServerExec = true): Pro } } -interface PartialManifest { - contributes: { - semanticTokenTypes?: ObjectWithId[]; - semanticTokenModifiers?: ObjectWithId[]; - }; -} - -interface ObjectWithId { - id: string; -} - -function tokenTypesFromExtManifest(manifest: PartialManifest): string[] { - if (!manifest.contributes.semanticTokenTypes) { - return []; - } - return manifest.contributes.semanticTokenTypes.map((token: ObjectWithId) => token.id); -} - -function tokenModifiersFromExtManifest(manifest: PartialManifest): string[] { - if (!manifest.contributes.semanticTokenModifiers) { - return []; - } - - return manifest.contributes.semanticTokenModifiers.map((modifier: ObjectWithId) => modifier.id); -} - function enabled(): boolean { return config('terraform').get('languageServer.external', false); } diff --git a/src/features/semanticTokens.ts b/src/features/semanticTokens.ts new file mode 100644 index 000000000..1ae5b87ff --- /dev/null +++ b/src/features/semanticTokens.ts @@ -0,0 +1,55 @@ +import { BaseLanguageClient, ClientCapabilities, ServerCapabilities, StaticFeature } from 'vscode-languageclient'; + +export interface PartialManifest { + contributes: { + semanticTokenTypes?: ObjectWithId[]; + semanticTokenModifiers?: ObjectWithId[]; + }; +} + +interface ObjectWithId { + id: string; +} + +export class CustomSemanticTokens implements StaticFeature { + constructor(private _client: BaseLanguageClient, private manifest: PartialManifest) {} + + public fillClientCapabilities(capabilities: ClientCapabilities): void { + if (!capabilities.textDocument || !capabilities.textDocument.semanticTokens) { + return; + } + + const extSemanticTokenTypes = this.tokenTypesFromExtManifest(this.manifest); + const extSemanticTokenModifiers = this.tokenModifiersFromExtManifest(this.manifest); + + const tokenTypes = capabilities.textDocument.semanticTokens.tokenTypes; + capabilities.textDocument.semanticTokens.tokenTypes = tokenTypes.concat(extSemanticTokenTypes); + + const tokenModifiers = capabilities.textDocument.semanticTokens.tokenModifiers; + capabilities.textDocument.semanticTokens.tokenModifiers = tokenModifiers.concat(extSemanticTokenModifiers); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public initialize(capabilities: ServerCapabilities): void { + return; + } + + public dispose(): void { + return; + } + + tokenTypesFromExtManifest(manifest: PartialManifest): string[] { + if (!manifest.contributes.semanticTokenTypes) { + return []; + } + return manifest.contributes.semanticTokenTypes.map((token: ObjectWithId) => token.id); + } + + tokenModifiersFromExtManifest(manifest: PartialManifest): string[] { + if (!manifest.contributes.semanticTokenModifiers) { + return []; + } + + return manifest.contributes.semanticTokenModifiers.map((modifier: ObjectWithId) => modifier.id); + } +} diff --git a/src/showReferences.ts b/src/features/showReferences.ts similarity index 100% rename from src/showReferences.ts rename to src/features/showReferences.ts diff --git a/src/telemetry.ts b/src/features/telemetry.ts similarity index 100% rename from src/telemetry.ts rename to src/features/telemetry.ts diff --git a/src/types.ts b/src/features/types.ts similarity index 100% rename from src/types.ts rename to src/features/types.ts diff --git a/src/semanticTokens.ts b/src/semanticTokens.ts deleted file mode 100644 index 3ffb7558a..000000000 --- a/src/semanticTokens.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { BaseLanguageClient, ClientCapabilities, ServerCapabilities, StaticFeature } from 'vscode-languageclient'; - -export class CustomSemanticTokens implements StaticFeature { - constructor( - private _client: BaseLanguageClient, - private extTokenTypes: string[], - private extTokenModifiers: string[], - ) {} - - public fillClientCapabilities(capabilities: ClientCapabilities): void { - if (!capabilities.textDocument || !capabilities.textDocument.semanticTokens) { - return; - } - - const tokenTypes = capabilities.textDocument.semanticTokens.tokenTypes; - capabilities.textDocument.semanticTokens.tokenTypes = tokenTypes.concat(this.extTokenTypes); - - const tokenModifiers = capabilities.textDocument.semanticTokens.tokenModifiers; - capabilities.textDocument.semanticTokens.tokenModifiers = tokenModifiers.concat(this.extTokenModifiers); - } - - // eslint-disable-next-line @typescript-eslint/no-unused-vars - public initialize(capabilities: ServerCapabilities): void { - return; - } - - public dispose(): void { - return; - } -}