提交 819a1af8 编写于 作者: M Matt Bierner

Support running organize imports for background files

Fixes #49405
上级 558635ad
......@@ -8,7 +8,6 @@ import * as nls from 'vscode-nls';
import * as Proto from '../protocol';
import { ITypeScriptServiceClient } from '../typescriptService';
import { Command, CommandManager } from '../utils/commandManager';
import { isSupportedLanguageMode } from '../utils/languageModeIds';
import * as typeconverts from '../utils/typeConverters';
const localize = nls.loadMessageBundle();
......@@ -23,21 +22,11 @@ class OrganizeImportsCommand implements Command {
private readonly client: ITypeScriptServiceClient
) { }
public async execute(): Promise<boolean> {
public async execute(file: string): Promise<boolean> {
if (!this.client.apiVersion.has280Features()) {
return false;
}
const editor = vscode.window.activeTextEditor;
if (!editor || !isSupportedLanguageMode(editor.document)) {
return false;
}
const file = this.client.normalizePath(editor.document.uri);
if (!file) {
return false;
}
const args: Proto.OrganizeImportsRequestArgs = {
scope: {
type: 'file',
......@@ -69,7 +58,7 @@ export class OrganizeImportsCodeActionProvider implements vscode.CodeActionProvi
};
public provideCodeActions(
_document: vscode.TextDocument,
document: vscode.TextDocument,
_range: vscode.Range,
_context: vscode.CodeActionContext,
_token: vscode.CancellationToken
......@@ -78,10 +67,15 @@ export class OrganizeImportsCodeActionProvider implements vscode.CodeActionProvi
return [];
}
const file = this.client.normalizePath(document.uri);
if (!file) {
return [];
}
const action = new vscode.CodeAction(
localize('oraganizeImportsAction.title', "Organize Imports"),
vscode.CodeActionKind.SourceOrganizeImports);
action.command = { title: '', command: OrganizeImportsCommand.Id };
action.command = { title: '', command: OrganizeImportsCommand.Id, arguments: [file] };
return [action];
}
}
\ No newline at end of file
......@@ -135,7 +135,7 @@ export async function applyCodeAction(
action: CodeAction,
bulkEditService: IBulkEditService,
commandService: ICommandService,
editor: ICodeEditor,
editor?: ICodeEditor,
) {
if (action.edit) {
await bulkEditService.apply(action.edit, { editor });
......
......@@ -271,7 +271,6 @@ class CodeActionOnParticipant implements ISaveParticipant {
constructor(
@IBulkEditService private readonly _bulkEditService: IBulkEditService,
@ICommandService private readonly _commandService: ICommandService,
@ICodeEditorService private readonly _codeEditorService: ICodeEditorService,
@IConfigurationService private readonly _configurationService: IConfigurationService
) { }
......@@ -281,10 +280,6 @@ class CodeActionOnParticipant implements ISaveParticipant {
}
const model = editorModel.textEditorModel;
const editor = findEditor(model, this._codeEditorService);
if (!editor) {
return undefined;
}
const settingsOverrides = { overrideIdentifier: model.getLanguageIdentifier().language, resource: editorModel.getResource() };
const setting = this._configurationService.getValue<ICodeActionsOnSaveOptions>('editor.codeActionsOnSave', settingsOverrides);
......@@ -302,12 +297,12 @@ class CodeActionOnParticipant implements ISaveParticipant {
return new Promise<CodeAction[]>((resolve, reject) => {
setTimeout(() => reject(localize('codeActionsOnSave.didTimeout', "Aborted codeActionsOnSave after {0}ms", timeout)), timeout);
this.getActionsToRun(model, codeActionsOnSave).then(resolve);
}).then(actionsToRun => this.applyCodeActions(actionsToRun, editor));
}).then(actionsToRun => this.applyCodeActions(actionsToRun));
}
private async applyCodeActions(actionsToRun: CodeAction[], editor: ICodeEditor) {
private async applyCodeActions(actionsToRun: CodeAction[]) {
for (const action of actionsToRun) {
await applyCodeAction(action, this._bulkEditService, this._commandService, editor);
await applyCodeAction(action, this._bulkEditService, this._commandService);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册