Skip to content

Commit

Permalink
refactor: use standard token types and modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Oct 23, 2022
1 parent 440d6fc commit 158ec1f
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 208 deletions.
3 changes: 1 addition & 2 deletions examples/svelte-language-server/src/index.ts
@@ -1,11 +1,10 @@
import { languageModule } from '@volar-examples/svelte-language-core';
import useTsPlugin, { getSemanticTokenLegend } from '@volar-plugins/typescript';
import useTsPlugin from '@volar-plugins/typescript';
import { createLanguageServer, LanguageServerPlugin } from '@volar/language-server/node';

const plugin: LanguageServerPlugin = () => ({
extraFileExtensions: [{ extension: 'svelte', isMixedContent: true, scriptKind: 7 }],
semanticService: {
semanticTokenLegend: getSemanticTokenLegend(),
getLanguageModules(host) {
return [languageModule];
},
Expand Down
31 changes: 0 additions & 31 deletions examples/vscode-svelte/package.json
Expand Up @@ -33,37 +33,6 @@
".svelte"
]
}
],
"semanticTokenScopes": [
{
"language": "svelte",
"scopes": {
"property": [
"variable.other.property.svelte"
],
"property.readonly": [
"variable.other.constant.property.svelte"
],
"variable": [
"variable.other.readwrite.svelte"
],
"variable.readonly": [
"variable.other.constant.object.svelte"
],
"function": [
"entity.name.function.svelte"
],
"namespace": [
"entity.name.type.module.svelte"
],
"variable.defaultLibrary": [
"support.variable.svelte"
],
"function.defaultLibrary": [
"support.function.svelte"
]
}
}
]
},
"scripts": {
Expand Down
3 changes: 1 addition & 2 deletions examples/vue-and-svelte-language-server/src/index.ts
@@ -1,5 +1,5 @@
import { languageModule as svelteLanguageModule } from '@volar-examples/svelte-language-core';
import useTsPlugin, { getSemanticTokenLegend } from '@volar-plugins/typescript';
import useTsPlugin from '@volar-plugins/typescript';
import { createLanguageServer, LanguageModule, LanguageServerInitializationOptions, LanguageServerPlugin } from '@volar/language-server/node';
import * as shared from '@volar/shared';
import * as vue from '@volar/vue-language-core';
Expand All @@ -11,7 +11,6 @@ const plugin: LanguageServerPlugin<LanguageServerInitializationOptions, vue.Lang
{ extension: 'svelte', isMixedContent: true, scriptKind: 7 },
],
semanticService: {
semanticTokenLegend: getSemanticTokenLegend(),
resolveLanguageServiceHost(ts, sys, tsConfig, host) {
let vueOptions: vue.VueCompilerOptions = {};
if (typeof tsConfig === 'string') {
Expand Down
98 changes: 0 additions & 98 deletions extensions/vscode-vue-language-features/package.json
Expand Up @@ -542,104 +542,6 @@
}
}
},
"semanticTokenScopes": [
{
"language": "vue",
"scopes": {
"property": [
"variable.other.property.vue"
],
"property.readonly": [
"variable.other.constant.property.vue"
],
"variable": [
"variable.other.readwrite.vue"
],
"variable.readonly": [
"variable.other.constant.object.vue"
],
"function": [
"entity.name.function.vue"
],
"namespace": [
"entity.name.type.module.vue"
],
"variable.defaultLibrary": [
"support.variable.vue"
],
"function.defaultLibrary": [
"support.function.vue"
],
"componentTag": [
"support.class.component.vue"
]
}
},
{
"language": "markdown",
"scopes": {
"property": [
"variable.other.property.vue"
],
"property.readonly": [
"variable.other.constant.property.vue"
],
"variable": [
"variable.other.readwrite.vue"
],
"variable.readonly": [
"variable.other.constant.object.vue"
],
"function": [
"entity.name.function.vue"
],
"namespace": [
"entity.name.type.module.vue"
],
"variable.defaultLibrary": [
"support.variable.vue"
],
"function.defaultLibrary": [
"support.function.vue"
],
"componentTag": [
"support.class.component.vue"
]
}
},
{
"language": "html",
"scopes": {
"property": [
"variable.other.property.vue"
],
"property.readonly": [
"variable.other.constant.property.vue"
],
"variable": [
"variable.other.readwrite.vue"
],
"variable.readonly": [
"variable.other.constant.object.vue"
],
"function": [
"entity.name.function.vue"
],
"namespace": [
"entity.name.type.module.vue"
],
"variable.defaultLibrary": [
"support.variable.vue"
],
"function.defaultLibrary": [
"support.function.vue"
],
"componentTag": [
"support.class.component.vue"
]
}
}
],
"commands": [
{
"command": "volar.action.restartServer",
Expand Down
3 changes: 3 additions & 0 deletions packages/language-server/src/features/languageFeatures.ts
Expand Up @@ -4,6 +4,7 @@ import { AutoInsertRequest, FindFileReferenceRequest, ShowReferencesNotification
import { CancellationTokenHost } from '../utils/cancellationPipe';
import type { Workspaces } from '../utils/workspaces';
import * as shared from '@volar/shared';
import { semanticTokensLegend } from '../registerFeatures';

export function register(
connection: vscode.Connection,
Expand Down Expand Up @@ -187,6 +188,7 @@ export function register(
const result = await vueLs?.getSemanticTokens(
params.textDocument.uri,
undefined,
semanticTokensLegend,
token,
tokens => resultProgress?.report(buildTokens(tokens)),
) ?? [];
Expand All @@ -202,6 +204,7 @@ export function register(
const result = await vueLs?.getSemanticTokens(
params.textDocument.uri,
params.range,
semanticTokensLegend,
token,
tokens => resultProgress?.report(buildTokens(tokens)),
) ?? [];
Expand Down
52 changes: 42 additions & 10 deletions packages/language-server/src/registerFeatures.ts
Expand Up @@ -3,6 +3,47 @@ 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 @@ -135,17 +176,8 @@ export function setupSemanticCapabilities(
server.semanticTokensProvider = {
range: true,
full: false,
legend: {
tokenModifiers: [],
tokenTypes: [],
},
legend: semanticTokensLegend,
};
for (const plugin of plugins) {
if (plugin.semanticService?.semanticTokenLegend) {
server.semanticTokensProvider.legend.tokenModifiers = server.semanticTokensProvider.legend.tokenModifiers.concat(plugin.semanticService.semanticTokenLegend.tokenModifiers);
server.semanticTokensProvider.legend.tokenTypes = server.semanticTokensProvider.legend.tokenTypes.concat(plugin.semanticService.semanticTokenLegend.tokenTypes);
}
}
}
if (params.textDocument?.codeAction) {
server.codeActionProvider = {
Expand Down
2 changes: 0 additions & 2 deletions packages/language-server/src/types.ts
Expand Up @@ -48,8 +48,6 @@ export type LanguageServerPlugin<

semanticService?: {

semanticTokenLegend?: vscode.SemanticTokensLegend,

resolveLanguageServiceHost?(
ts: typeof import('typescript/lib/tsserverlibrary'),
sys: FileSystem,
Expand Down
Expand Up @@ -6,7 +6,13 @@ import { languageFeatureWorker } from '../utils/featureWorkers';

export function register(context: LanguageServiceRuntimeContext) {

return (uri: string, range: vscode.Range | undefined, cancelToken: vscode.CancellationToken, reportProgress?: (tokens: SemanticToken[]) => void) => {
return (
uri: string,
range: vscode.Range | undefined,
legend: vscode.SemanticTokensLegend,
cancelToken: vscode.CancellationToken,
reportProgress?: (tokens: SemanticToken[],) => void,
) => {

const document = context.getTextDocument(uri);

Expand Down Expand Up @@ -56,6 +62,7 @@ export function register(context: LanguageServiceRuntimeContext) {
(plugin, document, offsetRange) => plugin.findDocumentSemanticTokens?.(
document,
vscode.Range.create(document.positionAt(offsetRange[0]), document.positionAt(offsetRange[1])),
legend,
),
(tokens, sourceMap) => tokens.map<SemanticToken | undefined>(_token => {

Expand Down
2 changes: 1 addition & 1 deletion packages/language-service/src/types.ts
Expand Up @@ -88,7 +88,7 @@ export interface LanguageServicePlugin {
findDocumentHighlights?(document: TextDocument, position: vscode.Position): NullableResult<vscode.DocumentHighlight[]>;
findDocumentLinks?(document: TextDocument): NullableResult<vscode.DocumentLink[]>;
findDocumentSymbols?(document: TextDocument): NullableResult<vscode.SymbolInformation[]>;
findDocumentSemanticTokens?(document: TextDocument, range: vscode.Range): NullableResult<SemanticToken[]>;
findDocumentSemanticTokens?(document: TextDocument, range: vscode.Range, legend: vscode.SemanticTokensLegend): NullableResult<SemanticToken[]>;
findWorkspaceSymbols?(query: string): NullableResult<vscode.SymbolInformation[]>;
doExecuteCommand?(command: string, args: any[], context: ExecuteCommandContext): NotNullableResult<void>;
findDocumentColors?(document: TextDocument): NullableResult<vscode.ColorInformation[]>;
Expand Down
1 change: 0 additions & 1 deletion plugins/typescript/src/createLanguageService.ts
Expand Up @@ -31,7 +31,6 @@ import { URI } from 'vscode-uri';
import * as _ from 'vscode-languageserver-protocol';

export interface LanguageService extends ReturnType<typeof createLanguageService> { }
export { getSemanticTokenLegend } from './services/semanticTokens';
export * from './configs/getFormatCodeSettings';
export * from './configs/getUserPreferences';

Expand Down
6 changes: 2 additions & 4 deletions plugins/typescript/src/index.ts
Expand Up @@ -6,8 +6,6 @@ import * as vscode from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as shared from '@volar/shared';

export { getSemanticTokenLegend } from './createLanguageService';

function getBasicTriggerCharacters(tsVersion: string) {

const triggerCharacters = ['.', '"', '\'', '`', '/', '<'];
Expand Down Expand Up @@ -248,9 +246,9 @@ export default function (): LanguageServicePlugin {
}
},

findDocumentSemanticTokens(document, range) {
findDocumentSemanticTokens(document, range, legend) {
if (isTsDocument(document)) {
return tsLs2.getDocumentSemanticTokens(document.uri, range);
return tsLs2.getDocumentSemanticTokens(document.uri, range, legend);
}
},

Expand Down

0 comments on commit 158ec1f

Please sign in to comment.