improve organizeImports for single document

This commit is contained in:
Qiming Zhao 2020-12-16 11:30:19 +08:00
parent 366475a119
commit 3ad16ac686
2 changed files with 14 additions and 11 deletions

View file

@ -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<WorkspaceEdit | null> {
private async _execute(client: TypeScriptServiceClient, document: TextDocument): Promise<WorkspaceEdit | TextEdit[] | null> {
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<void> {
@ -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)
}
}

View file

@ -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"],