fix organizeImports not working sometimes
This commit is contained in:
parent
68a9e822e9
commit
e4dbb9f68c
5 changed files with 106 additions and 94 deletions
|
@ -1,6 +1,7 @@
|
|||
import { commands, ExtensionContext, services, workspace } from 'coc.nvim'
|
||||
import TsserverService from './server'
|
||||
import { Command, OpenTsServerLogCommand, ReloadProjectsCommand, TypeScriptGoToProjectConfigCommand } from './server/commands'
|
||||
import OrganizeImportsCommand from './server/organizeImports'
|
||||
|
||||
export async function activate(context: ExtensionContext): Promise<void> {
|
||||
let { subscriptions } = context
|
||||
|
@ -24,6 +25,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
|
|||
registCommand(new ReloadProjectsCommand(service.clientHost))
|
||||
registCommand(new OpenTsServerLogCommand(service.clientHost))
|
||||
registCommand(new TypeScriptGoToProjectConfigCommand(service.clientHost))
|
||||
registCommand(new OrganizeImportsCommand(service.clientHost))
|
||||
registCommand(commands.register({
|
||||
id: 'tsserver.restart',
|
||||
execute: (): void => {
|
||||
|
|
|
@ -18,7 +18,6 @@ import Folding from './features/folding'
|
|||
import FormattingProvider from './features/formatting'
|
||||
import HoverProvider from './features/hover'
|
||||
import ImplementationsCodeLensProvider from './features/implementationsCodeLens'
|
||||
import OrganizeImportsProvider from './features/organizeImports'
|
||||
// import TagCompletionProvider from './features/tagCompletion'
|
||||
import QuickfixProvider from './features/quickfix'
|
||||
import RefactorProvider from './features/refactor'
|
||||
|
@ -213,10 +212,6 @@ export default class LanguageProvider {
|
|||
// )
|
||||
|
||||
if (this.client.apiVersion.gte(API.v280)) {
|
||||
this.disposables.push(
|
||||
new OrganizeImportsProvider(client, commands, this.fileConfigurationManager, this.description.id)
|
||||
)
|
||||
|
||||
this.disposables.push(
|
||||
languages.registerFoldingRangeProvider(languageIds, new Folding(this.client))
|
||||
)
|
||||
|
|
|
@ -2,24 +2,22 @@
|
|||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import { disposeAll, TextDocumentWillSaveEvent, workspace } from 'coc.nvim'
|
||||
import { Command, CommandManager } from 'coc.nvim/lib/commands'
|
||||
import { Disposable, TextDocument, TextEdit, WorkspaceEdit } from 'vscode-languageserver-protocol'
|
||||
import Proto from '../protocol'
|
||||
import { ITypeScriptServiceClient } from '../typescriptService'
|
||||
import { standardLanguageDescriptions } from '../utils/languageDescription'
|
||||
import * as typeconverts from '../utils/typeConverters'
|
||||
import FileConfigurationManager from './fileConfigurationManager'
|
||||
import { TextDocumentWillSaveEvent, workspace } from 'coc.nvim'
|
||||
import { TextDocument, TextEdit, WorkspaceEdit } from 'vscode-languageserver-protocol'
|
||||
import { Command } from './commands'
|
||||
import Proto from './protocol'
|
||||
import TypeScriptServiceClientHost from './typescriptServiceClientHost'
|
||||
import { standardLanguageDescriptions } from './utils/languageDescription'
|
||||
import { languageIds } from './utils/languageModeIds'
|
||||
import * as typeconverts from './utils/typeConverters'
|
||||
|
||||
class OrganizeImportsCommand implements Command {
|
||||
export default class OrganizeImportsCommand implements Command {
|
||||
public readonly id: string = 'tsserver.organizeImports'
|
||||
|
||||
constructor(
|
||||
private readonly client: ITypeScriptServiceClient,
|
||||
private noSemicolons: boolean,
|
||||
private modeIds: string[]
|
||||
private readonly client: TypeScriptServiceClientHost
|
||||
) {
|
||||
workspace.onWillSaveUntil(this.onWillSaveUntil, this, 'tsserver-organizeImports')
|
||||
workspace.onWillSaveUntil(this.onWillSaveUntil, this, 'tsserver')
|
||||
}
|
||||
|
||||
private onWillSaveUntil(event: TextDocumentWillSaveEvent): void {
|
||||
|
@ -27,7 +25,7 @@ class OrganizeImportsCommand implements Command {
|
|||
let format = config.get('orgnizeImportOnSave', false)
|
||||
if (!format) return
|
||||
let { document } = event
|
||||
if (this.modeIds.indexOf(document.languageId) == -1) return
|
||||
if (languageIds.indexOf(document.languageId) == -1) return
|
||||
let willSaveWaitUntil = async (): Promise<TextEdit[]> => {
|
||||
let edit = await this.getTextEdits(document)
|
||||
if (!edit) return []
|
||||
|
@ -37,7 +35,8 @@ class OrganizeImportsCommand implements Command {
|
|||
}
|
||||
|
||||
private async getTextEdits(document: TextDocument): Promise<WorkspaceEdit | null> {
|
||||
let file = this.client.toPath(document.uri)
|
||||
let client = this.client.serviceClient
|
||||
let file = client.toPath(document.uri)
|
||||
const args: Proto.OrganizeImportsRequestArgs = {
|
||||
scope: {
|
||||
type: 'file',
|
||||
|
@ -46,16 +45,21 @@ class OrganizeImportsCommand implements Command {
|
|||
}
|
||||
}
|
||||
}
|
||||
const response = await this.client.execute('organizeImports', args)
|
||||
const response = await client.execute('organizeImports', args)
|
||||
if (!response || !response.success) {
|
||||
return
|
||||
}
|
||||
|
||||
const edit = typeconverts.WorkspaceEdit.fromFileCodeEdits(
|
||||
this.client,
|
||||
client,
|
||||
response.body
|
||||
)
|
||||
if (this.noSemicolons) {
|
||||
let desc = standardLanguageDescriptions.find(o => o.modeIds.indexOf(document.languageId) !== -1)
|
||||
if (!desc) return null
|
||||
const config = workspace.getConfiguration(`${desc.id}.preferences`)
|
||||
let noSemicolons = config.get<boolean>('noSemicolons', false)
|
||||
|
||||
if (noSemicolons) {
|
||||
let { changes } = edit
|
||||
if (changes) {
|
||||
for (let c of Object.keys(changes)) {
|
||||
|
@ -70,32 +74,9 @@ class OrganizeImportsCommand implements Command {
|
|||
|
||||
public async execute(): Promise<void> {
|
||||
let document = await workspace.document
|
||||
if (this.modeIds.indexOf(document.filetype) == -1) return
|
||||
if (languageIds.indexOf(document.filetype) == -1) return
|
||||
let edit = await this.getTextEdits(document.textDocument)
|
||||
if (edit) await workspace.applyEdit(edit)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
export default class OrganizeImports {
|
||||
private disposables: Disposable[] = []
|
||||
public constructor(
|
||||
client: ITypeScriptServiceClient,
|
||||
commandManager: CommandManager,
|
||||
fileConfigurationManager: FileConfigurationManager,
|
||||
languageId: string
|
||||
) {
|
||||
let description = standardLanguageDescriptions.find(o => o.id == languageId)
|
||||
let modeIds = description ? description.modeIds : []
|
||||
let noSemicolons = fileConfigurationManager.removeSemicolons(languageId)
|
||||
let cmd = new OrganizeImportsCommand(client, noSemicolons, modeIds)
|
||||
commandManager.register(cmd)
|
||||
this.disposables.push(Disposable.create(() => {
|
||||
commandManager.unregister(cmd.id)
|
||||
}))
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
disposeAll(this.disposables)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue