Skip to content

Commit

Permalink
refactor(language-server): improve error handling for unsupported cap…
Browse files Browse the repository at this point in the history
…abilities
  • Loading branch information
johnsoncodehk committed Sep 4, 2024
1 parent f384d55 commit 2254579
Showing 1 changed file with 48 additions and 36 deletions.
84 changes: 48 additions & 36 deletions packages/language-server/lib/features/languageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { AutoInsertRequest, DocumentDrop_DataTransferItemAsStringRequest, Docume
import type { LanguageServerProject, LanguageServerState } from '../types.js';
import { SnapshotDocument } from '../utils/snapshotDocument';

const reportedCapabilities = new Set<string>();

export function register(
server: LanguageServerState,
documents: ReturnType<typeof import('./textDocuments')['register']>,
Expand Down Expand Up @@ -331,14 +333,14 @@ export function register(
});
}

if (initializeParams.capabilities.workspace?.inlayHint?.refreshSupport) {
refreshHandlers.push(() => {
refreshHandlers.push(() => {
if (initializeParams.capabilities.workspace?.inlayHint?.refreshSupport) {
server.connection.languages.inlayHint.refresh();
});
}
else {
console.warn('Inlay hint refresh is not supported by the client.');
}
}
else {
wranCapabilitiesNotSupported('workspace.inlayHint.refreshSupport');
}
});
}

if (languageServicePlugins.some(({ capabilities }) => capabilities.signatureHelpProvider)) {
Expand Down Expand Up @@ -419,14 +421,14 @@ export function register(
}) ?? { data: [] };
});

if (initializeParams.capabilities.workspace?.semanticTokens?.refreshSupport) {
refreshHandlers.push(() => {
refreshHandlers.push(() => {
if (initializeParams.capabilities.workspace?.semanticTokens?.refreshSupport) {
server.connection.languages.semanticTokens.refresh();
});
}
else {
console.warn('Semantic tokens refresh is not supported by the client.');
}
}
else {
wranCapabilitiesNotSupported('workspace.semanticTokens.refreshSupport');
}
});
}

if (languageServicePlugins.some(({ capabilities }) => capabilities.codeActionProvider)) {
Expand All @@ -448,8 +450,12 @@ export function register(
codeAction.data = { uri: params.textDocument.uri };
}
}
if (!initializeParams.capabilities.textDocument?.codeAction?.disabledSupport) {
if (
!initializeParams.capabilities.textDocument?.codeAction?.disabledSupport
&& codeActions.some(codeAction => !codeAction.disabled)
) {
codeActions = codeActions.filter(codeAction => !codeAction.disabled);
wranCapabilitiesNotSupported('textDocument.codeAction.disabledSupport');
}
return codeActions;
});
Expand Down Expand Up @@ -515,14 +521,14 @@ export function register(
return languageService.getInlineValue(uri, params.range, params.context, token);
});
});
if (initializeParams.capabilities.workspace?.inlineValue?.refreshSupport) {
refreshHandlers.push(() => {
refreshHandlers.push(() => {
if (initializeParams.capabilities.workspace?.inlineValue?.refreshSupport) {
server.connection.languages.inlineValue.refresh();
});
}
else {
console.warn('Inline value refresh is not supported by the client.');
}
}
else {
wranCapabilitiesNotSupported('workspace.inlineValue.refreshSupport');
}
});
}

if (languageServicePlugins.some(({ capabilities }) => capabilities.autoInsertionProvider)) {
Expand Down Expand Up @@ -655,14 +661,14 @@ export function register(
workspaceDiagnostics,
};

if (initializeParams.capabilities.workspace?.diagnostics?.refreshSupport) {
refreshHandlers.push(() => {
refreshHandlers.push(() => {
if (initializeParams.capabilities.workspace?.diagnostics?.refreshSupport) {
server.connection.languages.diagnostics.refresh();
});
}
else {
console.warn('Diagnostics refresh is not supported by the client.');
}
}
else {
wranCapabilitiesNotSupported('workspace.diagnostics.refreshSupport');
}
});
}
else {
documents.onDidChangeContent(({ document }) => {
Expand Down Expand Up @@ -804,26 +810,32 @@ export function register(

function handleCompletionItem(initializeParams: vscode.InitializeParams, item: vscode.CompletionItem) {
const insertReplaceSupport = initializeParams.capabilities.textDocument?.completion?.completionItem?.insertReplaceSupport ?? false;
if (!insertReplaceSupport) {
if (item.textEdit && vscode.InsertReplaceEdit.is(item.textEdit)) {
item.textEdit = vscode.TextEdit.replace(item.textEdit.insert, item.textEdit.newText);
}
if (!insertReplaceSupport && item.textEdit && vscode.InsertReplaceEdit.is(item.textEdit)) {
item.textEdit = vscode.TextEdit.replace(item.textEdit.insert, item.textEdit.newText);
wranCapabilitiesNotSupported('textDocument.completion.completionItem.insertReplaceSupport');
}
return item;
}

function handleDefinitions(initializeParams: vscode.InitializeParams, type: 'declaration' | 'definition' | 'typeDefinition' | 'implementation', items: vscode.LocationLink[]) {
const linkSupport = initializeParams.capabilities.textDocument?.[type]?.linkSupport ?? false;
if (!linkSupport) {
wranCapabilitiesNotSupported(`textDocument.${type}.linkSupport`);
return items.map<vscode.Location>(item => ({
uri: item.targetUri,
range: item.targetRange,
}));
}
else {
return items;
}
return items;
}
}

function wranCapabilitiesNotSupported(path: string) {
if (reportedCapabilities.has(path)) {
return;
}
reportedCapabilities.add(path);
console.warn(`${path} is not supported by the client but could be used by the server.`);
}

function sleep(ms: number) {
Expand Down

0 comments on commit 2254579

Please sign in to comment.