Add source code action kind

Fixes #47621

Adds the concept of a source code action that applies to an entire file. Does not show these actions in the lightbulb menu by default
This commit is contained in:
Matt Bierner
2018-04-11 15:34:20 -07:00
parent c1b9138a23
commit 556fa03cf3
9 changed files with 141 additions and 39 deletions

View File

@@ -13,6 +13,10 @@ import { isSupportedLanguageMode } from '../utils/languageModeIds';
import API from '../utils/api';
import { Lazy } from '../utils/lazy';
import TypeScriptServiceClientHost from '../typeScriptServiceClientHost';
import { ITypeScriptServiceClient } from '../typescriptService';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
export class OrganizeImportsCommand implements Command {
public static readonly Ids = ['javascript.organizeImports', 'typescript.organizeImports'];
@@ -84,3 +88,30 @@ export class OrganizeImportsContextManager {
this.currentValue = newValue;
}
}
export class OrganizeImportsCodeActionProvider implements vscode.CodeActionProvider {
public constructor(
private readonly client: ITypeScriptServiceClient
) { }
public provideCodeActions(
document: vscode.TextDocument,
_range: vscode.Range,
_context: vscode.CodeActionContext,
_token: vscode.CancellationToken
): vscode.CodeAction[] {
if (!isSupportedLanguageMode(document)) {
return [];
}
if (!this.client.apiVersion.has280Features()) {
return [];
}
const action = new vscode.CodeAction(localize('oraganizeImportsAction.title', "Organize Imports"), vscode.CodeActionKind.Source.append('organizeImports'));
action.command = { title: '', command: OrganizeImportsCommand.Ids[0] };
return [action];
}
}

View File

@@ -120,6 +120,7 @@ export default class LanguageProvider {
this.disposables.push(languages.registerRenameProvider(selector, new (await import('./features/renameProvider')).default(client)));
this.disposables.push(languages.registerCodeActionsProvider(selector, new (await import('./features/quickFixProvider')).default(client, this.formattingOptionsManager, commandManager, this.diagnosticsManager, this.bufferSyncSupport)));
this.disposables.push(languages.registerCodeActionsProvider(selector, new (await import('./features/refactorProvider')).default(client, this.formattingOptionsManager, commandManager)));
this.disposables.push(languages.registerCodeActionsProvider(selector, new (await import('./features/organizeImports')).OrganizeImportsCodeActionProvider(client)));
await this.initFoldingProvider();
this.disposables.push(workspace.onDidChangeConfiguration(c => {