From 2ce03eed5a61e9a7cb7837861d80b62a83cf6841 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 2 Jul 2020 16:41:23 -0700 Subject: [PATCH] Update VS Code refactoring support for new TS 4.0 api Adopts changes from https://github.com/microsoft/TypeScript/pull/37871/ --- .../src/features/fileConfigurationManager.ts | 10 +++++++++- .../src/features/refactor.ts | 18 +++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/extensions/typescript-language-features/src/features/fileConfigurationManager.ts b/extensions/typescript-language-features/src/features/fileConfigurationManager.ts index 0d54ccea22a..55d5324469b 100644 --- a/extensions/typescript-language-features/src/features/fileConfigurationManager.ts +++ b/extensions/typescript-language-features/src/features/fileConfigurationManager.ts @@ -13,6 +13,13 @@ import { isTypeScriptDocument } from '../utils/languageModeIds'; import { equals } from '../utils/objects'; import { ResourceMap } from '../utils/resourceMap'; +namespace Experimental { + // https://github.com/microsoft/TypeScript/pull/37871/ + export interface UserPreferences extends Proto.UserPreferences { + readonly provideRefactorNotApplicableReason?: boolean; + } +} + interface FileConfiguration { readonly formatOptions: Proto.FormatCodeSettings; readonly preferences: Proto.UserPreferences; @@ -170,7 +177,7 @@ export default class FileConfigurationManager extends Disposable { isTypeScriptDocument(document) ? 'typescript.preferences' : 'javascript.preferences', document.uri); - const preferences: Proto.UserPreferences = { + const preferences: Experimental.UserPreferences = { quotePreference: this.getQuoteStylePreference(preferencesConfig), importModuleSpecifierPreference: getImportModuleSpecifierPreference(preferencesConfig), importModuleSpecifierEnding: getImportModuleSpecifierEndingPreference(preferencesConfig), @@ -178,6 +185,7 @@ export default class FileConfigurationManager extends Disposable { providePrefixAndSuffixTextForRename: preferencesConfig.get('renameShorthandProperties', true) === false ? false : preferencesConfig.get('useAliasesForRenames', true), allowRenameOfImportPath: true, includeAutomaticOptionalChainCompletions: config.get('suggest.includeAutomaticOptionalChainCompletions', true), + provideRefactorNotApplicableReason: true, }; return preferences; diff --git a/extensions/typescript-language-features/src/features/refactor.ts b/extensions/typescript-language-features/src/features/refactor.ts index b3b9376af20..ca56c6d6902 100644 --- a/extensions/typescript-language-features/src/features/refactor.ts +++ b/extensions/typescript-language-features/src/features/refactor.ts @@ -22,7 +22,7 @@ const localize = nls.loadMessageBundle(); namespace Experimental { export interface RefactorActionInfo extends Proto.RefactorActionInfo { - readonly error?: string + readonly notApplicableReason?: string; } export type RefactorTriggerReason = 'implicit' | 'invoked'; @@ -312,16 +312,16 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider { const codeAction = new vscode.CodeAction(action.description, TypeScriptRefactorProvider.getKind(action)); // https://github.com/microsoft/TypeScript/pull/37871 - if (action.error) { - codeAction.disabled = { reason: action.error }; - return codeAction; + if (action.notApplicableReason) { + codeAction.disabled = { reason: action.notApplicableReason }; + } else { + codeAction.command = { + title: action.description, + command: ApplyRefactoringCommand.ID, + arguments: [document, info.name, action.name, rangeOrSelection], + }; } - codeAction.command = { - title: action.description, - command: ApplyRefactoringCommand.ID, - arguments: [document, info.name, action.name, rangeOrSelection], - }; codeAction.isPreferred = TypeScriptRefactorProvider.isPreferred(action, allActions); return codeAction; }