diff --git a/src/server/features/refactor.ts b/src/server/features/refactor.ts index 9a912f1..6c9fc4f 100644 --- a/src/server/features/refactor.ts +++ b/src/server/features/refactor.ts @@ -1,8 +1,8 @@ -import { CodeActionProvider, CodeActionProviderMetadata, commands, TextDocument, window, workspace } from 'coc.nvim' /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ +import { CodeActionProvider, Uri, CodeActionProviderMetadata, commands, TextDocument, window, workspace } from 'coc.nvim' import { CancellationToken, CodeAction, CodeActionContext, CodeActionKind, Range, WorkspaceEdit } from 'vscode-languageserver-protocol' import { Command, registCommand } from '../commands' import Proto from '../protocol' @@ -55,13 +55,22 @@ class ApplyRefactoringCommand implements Command { } private async toWorkspaceEdit(body: Proto.RefactorEditInfo): Promise { - for (const edit of body.edits) { - await workspace.createFile(edit.fileName, { ignoreIfExists: true }) - } let workspaceEdit = typeConverters.WorkspaceEdit.fromFileCodeEdits( this.client, body.edits ) + let documentChanges = workspaceEdit.documentChanges = workspaceEdit.documentChanges || [] + for (const edit of body.edits) { + let resource = this.client.toResource(edit.fileName) + if (Uri.parse(resource).scheme === 'file') { + // should create file first. + documentChanges.unshift({ + kind: 'create', + uri: resource, + options: { ignoreIfExists: true } + }) + } + } return workspaceEdit } } diff --git a/src/server/features/rename.ts b/src/server/features/rename.ts index 8df0966..cdb04dd 100644 --- a/src/server/features/rename.ts +++ b/src/server/features/rename.ts @@ -16,7 +16,7 @@ export default class TypeScriptRenameProvider implements RenameProvider { public constructor( private readonly client: ITypeScriptServiceClient, private readonly fileConfigurationManager: FileConfigurationManager - ) { } + ) {} public async prepareRename( document: TextDocument, @@ -60,8 +60,8 @@ export default class TypeScriptRenameProvider implements RenameProvider { } if (this.client.apiVersion.gte(API.v310)) { - if ((renameInfo as any).fileToRename) { - const edits = await this.renameFile((renameInfo as any).fileToRename, newName, token) + if (renameInfo.fileToRename) { + const edits = await this.renameFile(renameInfo.fileToRename, newName, token) if (edits) { return edits } else { diff --git a/src/server/organizeImports.ts b/src/server/organizeImports.ts index 8f249af..c5d4ff7 100644 --- a/src/server/organizeImports.ts +++ b/src/server/organizeImports.ts @@ -39,7 +39,7 @@ export class OrganizeImportsCommand implements Command { client, response.body ) - let keys = Object.keys(edit.changes) + let keys = Object.keys(edit.changes || {}) if (keys.length == 1) { let doc = workspace.getDocument(keys[0]) if (doc) { diff --git a/src/server/utils/typeConverters.ts b/src/server/utils/typeConverters.ts index ace1aa1..6a07813 100644 --- a/src/server/utils/typeConverters.ts +++ b/src/server/utils/typeConverters.ts @@ -6,6 +6,7 @@ * Helpers for converting FROM LanguageServer types language-server ts types */ import * as language from 'vscode-languageserver-protocol' +import { TextDocumentEdit } from 'vscode-languageserver-protocol' import Proto from '../protocol' import * as PConst from '../protocol.const' import { ITypeScriptServiceClient } from '../typescriptService' @@ -98,14 +99,20 @@ export namespace WorkspaceEdit { client: ITypeScriptServiceClient, edits: Iterable ): language.WorkspaceEdit { - let changes = {} + let documentChanges: TextDocumentEdit[] = [] for (const edit of edits) { let uri = client.toResource(edit.fileName) - changes[uri] = edit.textChanges.map(change => { - return TextEdit.fromCodeEdit(change) + documentChanges.push({ + textDocument: { + uri, + version: null + }, + edits: edit.textChanges.map(change => { + return TextEdit.fromCodeEdit(change) + }) }) } - return { changes } + return { documentChanges } } }