Add sort imports command

Fixes #122593

Sort imports just sorts imports, vs organize imports which also removes unused imports
This commit is contained in:
Matt Bierner
2021-05-19 18:20:16 -07:00
parent e65a227c82
commit 35eafb7873

View File

@@ -29,7 +29,7 @@ class OrganizeImportsCommand implements Command {
private readonly telemetryReporter: TelemetryReporter,
) { }
public async execute(file: string): Promise<boolean> {
public async execute(file: string, sortOnly = false): Promise<boolean> {
/* __GDPR__
"organizeImports.execute" : {
"${include}": [
@@ -45,7 +45,8 @@ class OrganizeImportsCommand implements Command {
args: {
file
}
}
},
skipDestructiveCodeActions: sortOnly,
};
const response = await this.client.interruptGetErr(() => this.client.execute('organizeImports', args, nulToken));
if (response.type !== 'response' || !response.body) {
@@ -57,23 +58,42 @@ class OrganizeImportsCommand implements Command {
}
}
export class OrganizeImportsCodeActionProvider implements vscode.CodeActionProvider {
public static readonly minVersion = API.v280;
class ImportsCodeActionProvider implements vscode.CodeActionProvider {
static register(
client: ITypeScriptServiceClient,
minVersion: API,
kind: vscode.CodeActionKind,
title: string,
sortOnly: boolean,
commandManager: CommandManager,
fileConfigurationManager: FileConfigurationManager,
telemetryReporter: TelemetryReporter,
selector: DocumentSelector
): vscode.Disposable {
return conditionalRegistration([
requireMinVersion(client, minVersion),
requireSomeCapability(client, ClientCapability.Semantic),
], () => {
const provider = new ImportsCodeActionProvider(client, kind, title, sortOnly, commandManager, fileConfigurationManager, telemetryReporter);
return vscode.languages.registerCodeActionsProvider(selector.semantic, provider, {
providedCodeActionKinds: [kind]
});
});
}
public constructor(
private readonly client: ITypeScriptServiceClient,
private readonly kind: vscode.CodeActionKind,
private readonly title: string,
private readonly sortOnly: boolean,
commandManager: CommandManager,
private readonly fileConfigManager: FileConfigurationManager,
telemetryReporter: TelemetryReporter,
) {
commandManager.register(new OrganizeImportsCommand(client, telemetryReporter));
}
public readonly metadata: vscode.CodeActionProviderMetadata = {
providedCodeActionKinds: [vscode.CodeActionKind.SourceOrganizeImports]
};
public provideCodeActions(
document: vscode.TextDocument,
_range: vscode.Range,
@@ -85,16 +105,14 @@ export class OrganizeImportsCodeActionProvider implements vscode.CodeActionProvi
return [];
}
if (!context.only || !context.only.contains(vscode.CodeActionKind.SourceOrganizeImports)) {
if (!context.only || !context.only.contains(this.kind)) {
return [];
}
this.fileConfigManager.ensureConfigurationForDocument(document, token);
const action = new vscode.CodeAction(
localize('organizeImportsAction.title', "Organize Imports"),
vscode.CodeActionKind.SourceOrganizeImports);
action.command = { title: '', command: OrganizeImportsCommand.Id, arguments: [file] };
const action = new vscode.CodeAction(this.title, this.kind);
action.command = { title: '', command: OrganizeImportsCommand.Id, arguments: [file, this.sortOnly] };
return [action];
}
}
@@ -106,13 +124,28 @@ export function register(
fileConfigurationManager: FileConfigurationManager,
telemetryReporter: TelemetryReporter,
) {
return conditionalRegistration([
requireMinVersion(client, OrganizeImportsCodeActionProvider.minVersion),
requireSomeCapability(client, ClientCapability.Semantic),
], () => {
const organizeImportsProvider = new OrganizeImportsCodeActionProvider(client, commandManager, fileConfigurationManager, telemetryReporter);
return vscode.languages.registerCodeActionsProvider(selector.semantic,
organizeImportsProvider,
organizeImportsProvider.metadata);
});
return vscode.Disposable.from(
ImportsCodeActionProvider.register(
client,
API.v280,
vscode.CodeActionKind.SourceOrganizeImports,
localize('organizeImportsAction.title', "Organize Imports"),
false,
commandManager,
fileConfigurationManager,
telemetryReporter,
selector
),
ImportsCodeActionProvider.register(
client,
API.v430,
vscode.CodeActionKind.Source.append('sortImports'),
localize('sortImportsAction.title', "Sort Imports"),
true,
commandManager,
fileConfigurationManager,
telemetryReporter,
selector
),
);
}