From 3ad16ac68682ea1e65dde64b62cde878c6b0ccff Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Wed, 16 Dec 2020 11:30:19 +0800 Subject: [PATCH] improve organizeImports for single document --- src/server/organizeImports.ts | 21 ++++++++++++--------- tsconfig.json | 4 ++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/server/organizeImports.ts b/src/server/organizeImports.ts index 2308932..38a9eb7 100644 --- a/src/server/organizeImports.ts +++ b/src/server/organizeImports.ts @@ -3,11 +3,10 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { workspace, CodeActionProvider, CodeActionProviderMetadata } from 'coc.nvim' -import { CancellationToken, Range, CodeActionContext, WorkspaceEdit, CodeActionKind, CodeAction } from 'vscode-languageserver-protocol' +import { CancellationToken, Range, CodeActionContext, WorkspaceEdit, CodeActionKind, CodeAction, TextEdit } from 'vscode-languageserver-protocol' import { TextDocument } from 'vscode-languageserver-textdocument' import { Command } from './commands' import Proto from './protocol' -import { standardLanguageDescriptions } from './utils/languageDescription' import * as typeconverts from './utils/typeConverters' import FileConfigurationManager from './features/fileConfigurationManager' import TypeScriptServiceClient from './typescriptServiceClient' @@ -21,7 +20,7 @@ export class OrganizeImportsCommand implements Command { ) { } - private async getTextEdits(client: TypeScriptServiceClient, document: TextDocument): Promise { + private async _execute(client: TypeScriptServiceClient, document: TextDocument): Promise { let file = client.toPath(document.uri) const args: Proto.OrganizeImportsRequestArgs = { scope: { @@ -40,9 +39,15 @@ export class OrganizeImportsCommand implements Command { client, response.body ) - let desc = standardLanguageDescriptions.find(o => o.modeIds.indexOf(document.languageId) !== -1) - if (!desc) return null - return edit + let keys = Object.keys(edit.changes) + if (keys.length == 1) { + let doc = workspace.getDocument(keys[0]) + if (doc) { + await doc.applyEdits(edit.changes[keys[0]]) + return + } + } + if (edit) await workspace.applyEdit(edit) } public async execute(document?: TextDocument): Promise { @@ -57,9 +62,7 @@ export class OrganizeImportsCommand implements Command { } document = doc.textDocument } - let edit = await this.getTextEdits(client.serviceClient, document) - if (edit) await workspace.applyEdit(edit) - return + await this._execute(client.serviceClient, document) } } diff --git a/tsconfig.json b/tsconfig.json index 403fb43..6464e65 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,11 +15,11 @@ "strictPropertyInitialization": false, "declaration": false, "outDir": "lib", - "target": "es2015", + "target": "es2017", "module": "commonjs", "moduleResolution": "node", "noImplicitThis": true, - "lib": ["es2018"], + "lib": ["ES2018", "ES2019"], "plugins": [] }, "include": ["src"],