Skip to content

Commit

Permalink
Fix #2574 and better CodeAction
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Jan 21, 2021
1 parent b13e601 commit 901f6cc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 13 deletions.
79 changes: 79 additions & 0 deletions server/src/modes/script/CodeActionKindConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CodeActionKind } from 'vscode-languageserver';

export interface TSCodeActionKind {
kind: CodeActionKind;
matches(refactor: { actionName: string }): boolean;
}

/* tslint:disable:variable-name */
const Extract_Function = Object.freeze<TSCodeActionKind>({
kind: CodeActionKind.RefactorExtract + '.function',
matches: refactor => refactor.actionName.startsWith('function_')
});

const Extract_Constant = Object.freeze<TSCodeActionKind>({
kind: CodeActionKind.RefactorExtract + '.constant',
matches: refactor => refactor.actionName.startsWith('constant_')
});

const Extract_Type = Object.freeze<TSCodeActionKind>({
kind: CodeActionKind.RefactorExtract + '.type',
matches: refactor => refactor.actionName.startsWith('Extract to type alias')
});

const Extract_Interface = Object.freeze<TSCodeActionKind>({
kind: CodeActionKind.RefactorExtract + '.interface',
matches: refactor => refactor.actionName.startsWith('Extract to interface')
});

const Move_NewFile = Object.freeze<TSCodeActionKind>({
kind: CodeActionKind.Refactor + '.move' + '.newFile',
matches: refactor => refactor.actionName.startsWith('Move to a new file')
});

const Rewrite_Import = Object.freeze<TSCodeActionKind>({
kind: CodeActionKind.RefactorRewrite + '.import',
matches: refactor => refactor.actionName.startsWith('Convert namespace import') || refactor.actionName.startsWith('Convert named imports')
});

const Rewrite_Export = Object.freeze<TSCodeActionKind>({
kind: CodeActionKind.RefactorRewrite + '.export',
matches: refactor => refactor.actionName.startsWith('Convert default export') || refactor.actionName.startsWith('Convert named export')
});

const Rewrite_Arrow_Braces = Object.freeze<TSCodeActionKind>({
kind: CodeActionKind.RefactorRewrite + '.arrow' + '.braces',
matches: refactor => refactor.actionName.startsWith('Convert default export') || refactor.actionName.startsWith('Convert named export')
});

const Rewrite_Parameters_ToDestructured = Object.freeze<TSCodeActionKind>({
kind: CodeActionKind.RefactorRewrite + '.parameters' + '.toDestructured',
matches: refactor => refactor.actionName.startsWith('Convert parameters to destructured object')
});

const Rewrite_Property_GenerateAccessors = Object.freeze<TSCodeActionKind>({
kind: CodeActionKind.RefactorRewrite + '.property' + '.generateAccessors',
matches: refactor => refactor.actionName.startsWith('Generate \'get\' and \'set\' accessors')
});
/* tslint:enable:variable-name */

const allKnownCodeActionKinds = [
Extract_Function,
Extract_Constant,
Extract_Type,
Extract_Interface,
Move_NewFile,
Rewrite_Import,
Rewrite_Export,
Rewrite_Arrow_Braces,
Rewrite_Parameters_ToDestructured,
Rewrite_Property_GenerateAccessors
];

export function getCodeActionKind (refactor: { actionName: string }): CodeActionKind {
return allKnownCodeActionKinds.find((kind) => kind.matches(refactor))?.kind ?? CodeActionKind.Refactor;
}
21 changes: 12 additions & 9 deletions server/src/modes/script/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { toCompletionItemKind, toSymbolKind } from '../../services/typescriptSer
import * as Previewer from './previewer';
import { isVCancellationRequested, VCancellationToken } from '../../utils/cancellationToken';
import { EnvironmentService } from '../../services/EnvironmentService';
import { getCodeActionKind } from './CodeActionKindConverter';

// Todo: After upgrading to LS server 4.0, use CompletionContext for filtering trigger chars
// https://microsoft.github.io/language-server-protocol/specification#completion-request-leftwards_arrow_with_hook
Expand Down Expand Up @@ -747,15 +748,19 @@ function provideRefactoringCodeActions(
CodeActionKind.Refactor,
CodeActionKind.RefactorExtract,
CodeActionKind.RefactorInline,
CodeActionKind.RefactorRewrite,
CodeActionKind.Source
CodeActionKind.RefactorRewrite
].includes(el)
)
) {
return;
}

const refactorings = service.getApplicableRefactors(fileName, textRange, preferences);
const refactorings = service.getApplicableRefactors(
fileName,
textRange,
preferences,
!context.only ? undefined : 'invoked'
);

const actions: RefactorActionData[] = [];
for (const refactoring of refactorings) {
Expand All @@ -779,15 +784,17 @@ function provideRefactoringCodeActions(
textRange,
refactorName,
actionName: action.name,
description: action.description
description: action.description,
notApplicableReason: action.notApplicableReason
}))
);
}
}
for (const action of actions) {
result.push({
title: action.description,
kind: CodeActionKind.Refactor,
kind: getCodeActionKind(action),
disabled: action.notApplicableReason ? { reason: action.notApplicableReason } : undefined,
data: action
});
}
Expand Down Expand Up @@ -834,10 +841,6 @@ function provideQuickFixCodeActions(
}
});

if (context.only && !context.only.includes(CodeActionKind.SourceFixAll)) {
continue;
}

if (fix.fixAllDescription && fix.fixId) {
result.push({
title: fix.fixAllDescription,
Expand Down
8 changes: 4 additions & 4 deletions server/src/services/vls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,9 @@ export class VLS {
}

async onCodeActionResolve(action: CodeAction) {
if (!action.data) return action;
if (!action.data) {
return action;
}
const project = await this.getProjectService((action.data as { uri: string })?.uri);

return project?.onCodeActionResolve(action) ?? action;
Expand Down Expand Up @@ -656,9 +658,7 @@ export class VLS {
CodeActionKind.RefactorExtract,
CodeActionKind.RefactorInline,
CodeActionKind.RefactorRewrite,
CodeActionKind.Source,
CodeActionKind.SourceOrganizeImports,
CodeActionKind.SourceFixAll
CodeActionKind.SourceOrganizeImports
],
resolveProvider: true
},
Expand Down
1 change: 1 addition & 0 deletions server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface RefactorActionData extends BaseCodeActionData {
refactorName: string;
actionName: string;
description: string;
notApplicableReason?: string;
}

export interface CombinedFixActionData extends BaseCodeActionData {
Expand Down

0 comments on commit 901f6cc

Please sign in to comment.