Skip to content

Commit

Permalink
feat: provide extra semanticTokensLegend by language client
Browse files Browse the repository at this point in the history
close #2252
  • Loading branch information
johnsoncodehk committed Dec 25, 2022
1 parent bf32696 commit 49acaa4
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 45 deletions.
26 changes: 26 additions & 0 deletions extensions/vscode-vue-language-features/package.json
Expand Up @@ -118,6 +118,32 @@
"url": "./dist/schemas/vue-tsconfig.schema.json"
}
],
"semanticTokenScopes": [
{
"language": "vue",
"scopes": {
"component": [
"support.class.component.vue"
]
}
},
{
"language": "markdown",
"scopes": {
"component": [
"support.class.component.vue"
]
}
},
{
"language": "html",
"scopes": {
"component": [
"support.class.component.vue"
]
}
}
],
"languages": [
{
"id": "vue",
Expand Down
4 changes: 4 additions & 0 deletions extensions/vscode-vue-language-features/src/common.ts
Expand Up @@ -328,6 +328,10 @@ function getInitializationOptions(
customBlockSchemaUrls: vscode.workspace.getConfiguration('volar').get<Record<string, string>>('vueserver.json.customBlockSchemaUrls'),
},
additionalExtensions: additionalExtensions(),
semanticTokensLegend: {
tokenTypes: ['component'],
tokenModifiers: [],
},
};
return initializationOptions;
}
Expand Up @@ -4,13 +4,13 @@ import { AutoInsertRequest, FindFileReferenceRequest, ShowReferencesNotification
import { CancellationTokenHost } from '../cancellationPipe';
import type { Workspaces } from '../workspaces';
import * as shared from '@volar/shared';
import { semanticTokensLegend } from '../utils/registerFeatures';

export function register(
connection: vscode.Connection,
projects: Workspaces,
initParams: vscode.InitializeParams,
cancelHost: CancellationTokenHost,
semanticTokensLegend: vscode.SemanticTokensLegend,
) {

let lastCompleteUri: string;
Expand Down
55 changes: 53 additions & 2 deletions packages/language-server/src/common/server.ts
Expand Up @@ -59,7 +59,7 @@ export function createCommonLanguageServer(params: ServerParams) {
await _createDocumentServiceHost();

if (serverMode === ServerMode.Semantic) {
setupSemanticCapabilities(initParams.capabilities, result.capabilities, options, plugins);
setupSemanticCapabilities(initParams.capabilities, result.capabilities, options, plugins, getSemanticTokensLegend());
await createLanguageServiceHost();
}

Expand Down Expand Up @@ -175,7 +175,7 @@ export function createCommonLanguageServer(params: ServerParams) {
}

(await import('./features/customFeatures')).register(connection, projects);
(await import('./features/languageFeatures')).register(connection, projects, initParams, cancelTokenHost);
(await import('./features/languageFeatures')).register(connection, projects, initParams, cancelTokenHost, getSemanticTokensLegend());

for (const plugin of plugins) {
plugin.semanticService?.onInitialize?.(connection, getLanguageService as any);
Expand All @@ -186,4 +186,55 @@ export function createCommonLanguageServer(params: ServerParams) {
return project?.getLanguageService();
}
}

function getSemanticTokensLegend() {
if (!options.semanticTokensLegend) {
return standardSemanticTokensLegend;
}
return {
tokenTypes: [...standardSemanticTokensLegend.tokenTypes, ...options.semanticTokensLegend.tokenTypes],
tokenModifiers: [...standardSemanticTokensLegend.tokenModifiers, ...options.semanticTokensLegend.tokenModifiers],
};
}
}

// https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide#standard-token-types-and-modifiers
const standardSemanticTokensLegend: vscode.SemanticTokensLegend = {
tokenTypes: [
'namespace',
'class',
'enum',
'interface',
'struct',
'typeParameter',
'type',
'parameter',
'variable',
'property',
'enumMember',
'decorator',
'event',
'function',
'method',
'macro',
'label',
'comment',
'string',
'keyword',
'number',
'regexp',
'operator',
],
tokenModifiers: [
'declaration',
'definition',
'readonly',
'static',
'deprecated',
'abstract',
'async',
'modification',
'documentation',
'defaultLibrary',
],
};
42 changes: 1 addition & 41 deletions packages/language-server/src/common/utils/registerFeatures.ts
Expand Up @@ -3,47 +3,6 @@ import { DiagnosticModel, LanguageServerPlugin, LanguageServerInitializationOpti
import * as vscode from 'vscode-languageserver';
import { ClientCapabilities } from 'vscode-languageserver';

// https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide#standard-token-types-and-modifiers
export const semanticTokensLegend: vscode.SemanticTokensLegend = {
tokenTypes: [
'namespace',
'class',
'enum',
'interface',
'struct',
'typeParameter',
'type',
'parameter',
'variable',
'property',
'enumMember',
'decorator',
'event',
'function',
'method',
'macro',
'label',
'comment',
'string',
'keyword',
'number',
'regexp',
'operator',
],
tokenModifiers: [
'declaration',
'definition',
'readonly',
'static',
'deprecated',
'abstract',
'async',
'modification',
'documentation',
'defaultLibrary',
],
};

export function setupSyntacticCapabilities(
params: ClientCapabilities,
server: vscode.ServerCapabilities,
Expand Down Expand Up @@ -84,6 +43,7 @@ export function setupSemanticCapabilities(
server: vscode.ServerCapabilities,
initOptions: LanguageServerInitializationOptions,
plugins: ReturnType<LanguageServerPlugin>[],
semanticTokensLegend: vscode.SemanticTokensLegend,
) {
if (!initOptions.respectClientCapabilities || params.textDocument?.references) {
server.referencesProvider = true;
Expand Down
4 changes: 4 additions & 0 deletions packages/language-server/src/types.ts
Expand Up @@ -117,4 +117,8 @@ export interface LanguageServerInitializationOptions {
respectClientCapabilities?: boolean;
maxFileSize?: number;
configFilePath?: string;
/**
* Extra semantic token types and modifiers that are supported by the client.
*/
semanticTokensLegend?: vscode.SemanticTokensLegend;
}
Expand Up @@ -202,7 +202,11 @@ export default function useVueTemplateLanguagePlugin<T extends ReturnType<typeof
const tokenPosition = document.positionAt(tokenOffset);

if (components.has(tokenText)) {
result.push([tokenPosition.line, tokenPosition.character, tokenLength, legend.tokenTypes.indexOf('class'), 0]);
let tokenType = legend.tokenTypes.indexOf('component');
if (tokenType === -1) {
tokenType = legend.tokenTypes.indexOf('class');
}
result.push([tokenPosition.line, tokenPosition.character, tokenLength, tokenType, 0]);
}
}
}
Expand Down

0 comments on commit 49acaa4

Please sign in to comment.