Skip to content

Commit

Permalink
perf(typescript): check command resolve capability only for specifi…
Browse files Browse the repository at this point in the history
…c refactors (#94)
  • Loading branch information
johnsoncodehk committed May 10, 2024
1 parent 4a333b8 commit f143f57
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/typescript/lib/semanticFeatures/codeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,28 @@ export interface OrganizeImportsData {

export type Data = FixAllData | RefactorData | OrganizeImportsData;

const renameCommandRefactors = new Set([
'refactor.rewrite.property.generateAccessors',
'refactor.extract.type',
'refactor.extract.interface',
'refactor.extract.typedef',
'refactor.extract.constant',
'refactor.extract.function',
]);

export function register(ctx: SharedContext) {

let resolveCommandSupport = ctx.env.clientCapabilities?.textDocument?.codeAction?.resolveSupport?.properties?.includes('command');
let resolveEditSupport = ctx.env.clientCapabilities?.textDocument?.codeAction?.resolveSupport?.properties?.includes('edit');
let loged = false;

const wranUnsupportResolve = () => {
if (loged) {
return;
}
loged = true;
console.warn('[volar-service-typescript] The language client lacks support for the command/edit properties in the resolve code action. Therefore, the code action resolve is pre-calculated.');
};

if (!ctx.env.clientCapabilities) {
resolveCommandSupport = true;
Expand Down Expand Up @@ -115,6 +133,7 @@ export function register(ctx: SharedContext) {
action.data = data;
}
else {
wranUnsupportResolve();
resolveOrganizeImportsCodeAction(ctx, action, data, formatOptions, preferences);
}
result.push(action);
Expand All @@ -140,6 +159,7 @@ export function register(ctx: SharedContext) {
action.data = data;
}
else {
wranUnsupportResolve();
resolveFixAllCodeAction(ctx, action, data, formatOptions, preferences);
}
result.push(action);
Expand Down Expand Up @@ -169,6 +189,7 @@ export function register(ctx: SharedContext) {
action.data = data;
}
else {
wranUnsupportResolve();
resolveFixAllCodeAction(ctx, action, data, formatOptions, preferences);
}
result.push(action);
Expand All @@ -195,6 +216,7 @@ export function register(ctx: SharedContext) {
action.data = data;
}
else {
wranUnsupportResolve();
resolveFixAllCodeAction(ctx, action, data, formatOptions, preferences);
}
result.push(action);
Expand Down Expand Up @@ -257,6 +279,7 @@ export function register(ctx: SharedContext) {
fixAll.data = data;
}
else {
wranUnsupportResolve();
resolveFixAllCodeAction(ctx, fixAll, data, formatOptions, preferences);
}
fixAll.diagnostics = diagnostics;
Expand Down Expand Up @@ -285,10 +308,15 @@ export function register(ctx: SharedContext) {
refactorName: refactor.name,
actionName: action.name,
};
if (resolveCommandSupport && resolveEditSupport) {
const hasCommand = renameCommandRefactors.has(action.kind!);
if (hasCommand && resolveCommandSupport && resolveEditSupport) {
codeAction.data = data;
}
else if (!hasCommand && resolveEditSupport) {
codeAction.data = data;
}
else if (!codeAction.disabled) {
wranUnsupportResolve();
resolveRefactorCodeAction(ctx, codeAction, data, document, formatOptions, preferences);
}
codeActions.push(codeAction);
Expand Down

0 comments on commit f143f57

Please sign in to comment.