Skip to content

Commit

Permalink
feat(language-service): add support for executeCommand handler (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jun 26, 2024
1 parent 336ca38 commit 2f90b0c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/language-server/lib/register/registerLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function registerLanguageFeatures(server: LanguageServer) {
semanticTokensProvider,
diagnosticProvider,
inlayHintProvider,
executeCommandProvider,
experimental,
} = server.initializeResult.capabilities;

Expand Down Expand Up @@ -401,6 +402,17 @@ export function registerLanguageFeatures(server: LanguageServer) {
});
});
}
if (executeCommandProvider) {
server.connection.onExecuteCommand(async (params, token) => {
for (const languageService of await server.project.getExistingLanguageServices()) {
if (languageService.executeCommand && languageService.getCommands().includes(params.command)) {
try {
return await languageService.executeCommand(params.command, params.arguments ?? [], token);
} catch { }
}
}
});
}
if (typeof inlayHintProvider === 'object' && inlayHintProvider.resolveProvider) {
server.connection.languages.inlayHint.resolve(async (hint, token) => {
return await lastInlayHintLs?.resolveInlayHint(hint, token) ?? hint;
Expand Down
5 changes: 5 additions & 0 deletions packages/language-server/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ export function createServerBase(
moreTriggerCharacter: [...new Set(capabilitiesArr.map(data => data.documentOnTypeFormattingProvider?.triggerCharacters ?? []).flat())].slice(1),
}
: undefined,
executeCommandProvider: capabilitiesArr.some(data => data.executeCommandProvider)
? {
commands: [...new Set(capabilitiesArr.map(data => data.executeCommandProvider?.commands ?? []).flat())],
}
: undefined,
};

if (!status.pullModelDiagnostics && status.initializeResult.capabilities.diagnosticProvider) {
Expand Down
14 changes: 14 additions & 0 deletions packages/language-service/lib/languageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import * as documentLinkResolve from './features/resolveDocumentLink';
import * as inlayHintResolve from './features/resolveInlayHint';
import * as workspaceSymbolResolve from './features/resolveWorkspaceSymbol';
import type { LanguageServiceContext, LanguageServiceEnvironment, LanguageServicePlugin } from './types';
import { NoneCancellationToken } from './utils/cancellation';
import { UriMap, createUriMap } from './utils/uriMap';

export type LanguageService = ReturnType<typeof createLanguageServiceBase>;
Expand Down Expand Up @@ -188,11 +189,24 @@ function createLanguageServiceBase(
tokenTypes: [...new Set(tokenTypes)],
};
},
getCommands: () => plugins.map(plugin => plugin.capabilities.executeCommandProvider?.commands ?? []).flat(),
getTriggerCharacters: () => plugins.map(plugin => plugin.capabilities.completionProvider?.triggerCharacters ?? []).flat(),
getAutoFormatTriggerCharacters: () => plugins.map(plugin => plugin.capabilities.documentOnTypeFormattingProvider?.triggerCharacters ?? []).flat(),
getSignatureHelpTriggerCharacters: () => plugins.map(plugin => plugin.capabilities.signatureHelpProvider?.triggerCharacters ?? []).flat(),
getSignatureHelpRetriggerCharacters: () => plugins.map(plugin => plugin.capabilities.signatureHelpProvider?.retriggerCharacters ?? []).flat(),

executeCommand(command: string, args: any[], token = NoneCancellationToken) {
for (const plugin of context.plugins) {
if (context.disabledServicePlugins.has(plugin[1])) {
continue;
}
if (!plugin[1].executeCommand || !plugin[0].capabilities.executeCommandProvider?.commands.includes(command)) {
continue;
}
return plugin[1].executeCommand(command, args, token);
}
},

getDocumentFormattingEdits: format.register(context),
getFoldingRanges: foldingRanges.register(context),
getSelectionRanges: selectionRanges.register(context),
Expand Down
6 changes: 5 additions & 1 deletion packages/language-service/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export type SemanticToken = [
export interface LanguageServicePlugin<P = any> {
name?: string;
capabilities: {
executeCommandProvider?: {
commands: string[];
};
selectionRangeProvider?: boolean;
foldingRangeProvider?: boolean;
linkedEditingRangeProvider?: boolean;
Expand Down Expand Up @@ -133,8 +136,8 @@ export interface LanguageServicePlugin<P = any> {
codeActionKinds?: string[];
resolveProvider?: boolean;
};
// TODO: interFileDependencies
diagnosticProvider?: {
// TODO: interFileDependencies
workspaceDiagnostics?: boolean;
};
fileReferencesProvider?: boolean;
Expand All @@ -151,6 +154,7 @@ export interface EmbeddedCodeFormattingOptions {
export interface LanguageServicePluginInstance<P = any> {
provide?: P;
isAdditionalCompletion?: boolean; // volar specific
executeCommand?(command: string, args: any[], token: vscode.CancellationToken): ProviderResult<any>;
provideHover?(document: TextDocument, position: vscode.Position, token: vscode.CancellationToken): NullableProviderResult<vscode.Hover>;
provideDocumentSymbols?(document: TextDocument, token: vscode.CancellationToken): NullableProviderResult<vscode.DocumentSymbol[]>;
provideDocumentHighlights?(document: TextDocument, position: vscode.Position, token: vscode.CancellationToken): NullableProviderResult<vscode.DocumentHighlight[]>;
Expand Down

0 comments on commit 2f90b0c

Please sign in to comment.