|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +import { CodeActionKind } from 'vscode-languageserver'; |
| 6 | + |
| 7 | +export interface TSCodeActionKind { |
| 8 | + kind: CodeActionKind; |
| 9 | + matches(refactor: { actionName: string }): boolean; |
| 10 | +} |
| 11 | + |
| 12 | +/* tslint:disable:variable-name */ |
| 13 | +const Extract_Function = Object.freeze<TSCodeActionKind>({ |
| 14 | + kind: CodeActionKind.RefactorExtract + '.function', |
| 15 | + matches: refactor => refactor.actionName.startsWith('function_') |
| 16 | +}); |
| 17 | + |
| 18 | +const Extract_Constant = Object.freeze<TSCodeActionKind>({ |
| 19 | + kind: CodeActionKind.RefactorExtract + '.constant', |
| 20 | + matches: refactor => refactor.actionName.startsWith('constant_') |
| 21 | +}); |
| 22 | + |
| 23 | +const Extract_Type = Object.freeze<TSCodeActionKind>({ |
| 24 | + kind: CodeActionKind.RefactorExtract + '.type', |
| 25 | + matches: refactor => refactor.actionName.startsWith('Extract to type alias') |
| 26 | +}); |
| 27 | + |
| 28 | +const Extract_Interface = Object.freeze<TSCodeActionKind>({ |
| 29 | + kind: CodeActionKind.RefactorExtract + '.interface', |
| 30 | + matches: refactor => refactor.actionName.startsWith('Extract to interface') |
| 31 | +}); |
| 32 | + |
| 33 | +const Move_NewFile = Object.freeze<TSCodeActionKind>({ |
| 34 | + kind: CodeActionKind.Refactor + '.move' + '.newFile', |
| 35 | + matches: refactor => refactor.actionName.startsWith('Move to a new file') |
| 36 | +}); |
| 37 | + |
| 38 | +const Rewrite_Import = Object.freeze<TSCodeActionKind>({ |
| 39 | + kind: CodeActionKind.RefactorRewrite + '.import', |
| 40 | + matches: refactor => |
| 41 | + refactor.actionName.startsWith('Convert namespace import') || |
| 42 | + refactor.actionName.startsWith('Convert named imports') |
| 43 | +}); |
| 44 | + |
| 45 | +const Rewrite_Export = Object.freeze<TSCodeActionKind>({ |
| 46 | + kind: CodeActionKind.RefactorRewrite + '.export', |
| 47 | + matches: refactor => |
| 48 | + refactor.actionName.startsWith('Convert default export') || refactor.actionName.startsWith('Convert named export') |
| 49 | +}); |
| 50 | + |
| 51 | +const Rewrite_Arrow_Braces = Object.freeze<TSCodeActionKind>({ |
| 52 | + kind: CodeActionKind.RefactorRewrite + '.arrow' + '.braces', |
| 53 | + matches: refactor => |
| 54 | + refactor.actionName.startsWith('Convert default export') || refactor.actionName.startsWith('Convert named export') |
| 55 | +}); |
| 56 | + |
| 57 | +const Rewrite_Parameters_ToDestructured = Object.freeze<TSCodeActionKind>({ |
| 58 | + kind: CodeActionKind.RefactorRewrite + '.parameters' + '.toDestructured', |
| 59 | + matches: refactor => refactor.actionName.startsWith('Convert parameters to destructured object') |
| 60 | +}); |
| 61 | + |
| 62 | +const Rewrite_Property_GenerateAccessors = Object.freeze<TSCodeActionKind>({ |
| 63 | + kind: CodeActionKind.RefactorRewrite + '.property' + '.generateAccessors', |
| 64 | + matches: refactor => refactor.actionName.startsWith("Generate 'get' and 'set' accessors") |
| 65 | +}); |
| 66 | +/* tslint:enable:variable-name */ |
| 67 | + |
| 68 | +const allKnownCodeActionKinds = [ |
| 69 | + Extract_Function, |
| 70 | + Extract_Constant, |
| 71 | + Extract_Type, |
| 72 | + Extract_Interface, |
| 73 | + Move_NewFile, |
| 74 | + Rewrite_Import, |
| 75 | + Rewrite_Export, |
| 76 | + Rewrite_Arrow_Braces, |
| 77 | + Rewrite_Parameters_ToDestructured, |
| 78 | + Rewrite_Property_GenerateAccessors |
| 79 | +]; |
| 80 | + |
| 81 | +export function getCodeActionKind(refactor: { actionName: string }): CodeActionKind { |
| 82 | + return allKnownCodeActionKinds.find(kind => kind.matches(refactor))?.kind ?? CodeActionKind.Refactor; |
| 83 | +} |
0 commit comments