Update VS Code refactoring support for new TS 4.0 api

Adopts changes from https://github.com/microsoft/TypeScript/pull/37871/
This commit is contained in:
Matt Bierner 2020-07-02 16:41:23 -07:00
parent f3280356e4
commit 2ce03eed5a
2 changed files with 18 additions and 10 deletions

View file

@ -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<boolean>('renameShorthandProperties', true) === false ? false : preferencesConfig.get<boolean>('useAliasesForRenames', true),
allowRenameOfImportPath: true,
includeAutomaticOptionalChainCompletions: config.get<boolean>('suggest.includeAutomaticOptionalChainCompletions', true),
provideRefactorNotApplicableReason: true,
};
return preferences;

View file

@ -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;
}