diff --git a/src/index.ts b/src/index.ts index cdd0936..18f2845 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { commands, ExtensionContext, services, workspace } from 'coc.nvim' import TsserverService from './server' import { AutoFixCommand, Command, ConfigurePluginCommand, FileReferencesCommand, OpenTsServerLogCommand, ReloadProjectsCommand, TypeScriptGoToProjectConfigCommand } from './server/commands' -import { OrganizeImportsCommand } from './server/organizeImports' +import { OrganizeImportsCommand, SourceImportsCommand } from './server/organizeImports' import { PluginManager } from './utils/plugins' interface API { @@ -25,6 +25,7 @@ export async function activate(context: ExtensionContext): Promise { registCommand(new OpenTsServerLogCommand(service)) registCommand(new TypeScriptGoToProjectConfigCommand(service)) registCommand(new OrganizeImportsCommand(service)) + registCommand(new SourceImportsCommand(service)) registCommand({ id: 'tsserver.restart', execute: (): void => { diff --git a/src/server/organizeImports.ts b/src/server/organizeImports.ts index 4cca6c7..8f249af 100644 --- a/src/server/organizeImports.ts +++ b/src/server/organizeImports.ts @@ -19,9 +19,10 @@ export class OrganizeImportsCommand implements Command { ) { } - private async _execute(client: TypeScriptServiceClient, document: TextDocument): Promise { + private async _execute(client: TypeScriptServiceClient, document: TextDocument, sortOnly = false): Promise { let file = client.toPath(document.uri) const args: Proto.OrganizeImportsRequestArgs = { + skipDestructiveCodeActions: sortOnly, scope: { type: 'file', args: { @@ -49,7 +50,7 @@ export class OrganizeImportsCommand implements Command { if (edit) await workspace.applyEdit(edit) } - public async execute(document?: TextDocument): Promise { + public async execute(document?: TextDocument, sortOnly = false): Promise { let client = await this.service.getClientHost() if (!document) { let doc = await workspace.document @@ -61,10 +62,14 @@ export class OrganizeImportsCommand implements Command { } document = doc.textDocument } - await this._execute(client.serviceClient, document) + await this._execute(client.serviceClient, document, sortOnly) } } +export class SourceImportsCommand extends OrganizeImportsCommand { + public readonly id = 'tsserver.sortImports' +} + export class OrganizeImportsCodeActionProvider implements CodeActionProvider { // public static readonly minVersion = API.v280 @@ -91,11 +96,16 @@ export class OrganizeImportsCodeActionProvider implements CodeActionProvider { } await this.fileConfigManager.ensureConfigurationForDocument(document, token) - const action = CodeAction.create('Organize Imports', { + const organizeImportsAction = CodeAction.create('Organize Imports', { title: '', command: 'tsserver.organizeImports', arguments: [document] }, CodeActionKind.SourceOrganizeImports) - return [action] + const sortImportsAction = CodeAction.create('Sort Imports', { + title: '', + command: 'tsserver.sortImports', + arguments: [document, true] + }, 'source.sortImports') + return [organizeImportsAction, sortImportsAction] } }