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. * Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { workspace, CodeActionProvider, CodeActionProviderMetadata } from 'coc.nvim' 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 { TextDocument } from 'vscode-languageserver-textdocument'
import { Command } from './commands' import { Command } from './commands'
import Proto from './protocol' import Proto from './protocol'
import { standardLanguageDescriptions } from './utils/languageDescription'
import * as typeconverts from './utils/typeConverters' import * as typeconverts from './utils/typeConverters'
import FileConfigurationManager from './features/fileConfigurationManager' import FileConfigurationManager from './features/fileConfigurationManager'
import TypeScriptServiceClient from './typescriptServiceClient' 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) let file = client.toPath(document.uri)
const args: Proto.OrganizeImportsRequestArgs = { const args: Proto.OrganizeImportsRequestArgs = {
scope: { scope: {
@ -40,9 +39,15 @@ export class OrganizeImportsCommand implements Command {
client, client,
response.body response.body
) )
let desc = standardLanguageDescriptions.find(o => o.modeIds.indexOf(document.languageId) !== -1) let keys = Object.keys(edit.changes)
if (!desc) return null if (keys.length == 1) {
return edit 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> { public async execute(document?: TextDocument): Promise<void> {
@ -57,9 +62,7 @@ export class OrganizeImportsCommand implements Command {
} }
document = doc.textDocument document = doc.textDocument
} }
let edit = await this.getTextEdits(client.serviceClient, document) await this._execute(client.serviceClient, document)
if (edit) await workspace.applyEdit(edit)
return
} }
} }

View file

@ -15,11 +15,11 @@
"strictPropertyInitialization": false, "strictPropertyInitialization": false,
"declaration": false, "declaration": false,
"outDir": "lib", "outDir": "lib",
"target": "es2015", "target": "es2017",
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
"noImplicitThis": true, "noImplicitThis": true,
"lib": ["es2018"], "lib": ["ES2018", "ES2019"],
"plugins": [] "plugins": []
}, },
"include": ["src"], "include": ["src"],