use documentChanges for workspaceEdit

This commit is contained in:
Qiming Zhao 2022-01-30 18:00:29 +08:00
parent ddcab27979
commit e5f19b47b4
No known key found for this signature in database
GPG key ID: 9722CD0E8D4DCB8C
4 changed files with 28 additions and 12 deletions

View file

@ -1,8 +1,8 @@
import { CodeActionProvider, CodeActionProviderMetadata, commands, TextDocument, window, workspace } from 'coc.nvim'
/*--------------------------------------------------------------------------------------------- /*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved. * Copyright (c) Microsoft Corporation. All rights reserved.
* 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 { CodeActionProvider, Uri, CodeActionProviderMetadata, commands, TextDocument, window, workspace } from 'coc.nvim'
import { CancellationToken, CodeAction, CodeActionContext, CodeActionKind, Range, WorkspaceEdit } from 'vscode-languageserver-protocol' import { CancellationToken, CodeAction, CodeActionContext, CodeActionKind, Range, WorkspaceEdit } from 'vscode-languageserver-protocol'
import { Command, registCommand } from '../commands' import { Command, registCommand } from '../commands'
import Proto from '../protocol' import Proto from '../protocol'
@ -55,13 +55,22 @@ class ApplyRefactoringCommand implements Command {
} }
private async toWorkspaceEdit(body: Proto.RefactorEditInfo): Promise<WorkspaceEdit> { private async toWorkspaceEdit(body: Proto.RefactorEditInfo): Promise<WorkspaceEdit> {
for (const edit of body.edits) {
await workspace.createFile(edit.fileName, { ignoreIfExists: true })
}
let workspaceEdit = typeConverters.WorkspaceEdit.fromFileCodeEdits( let workspaceEdit = typeConverters.WorkspaceEdit.fromFileCodeEdits(
this.client, this.client,
body.edits 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 return workspaceEdit
} }
} }

View file

@ -16,7 +16,7 @@ export default class TypeScriptRenameProvider implements RenameProvider {
public constructor( public constructor(
private readonly client: ITypeScriptServiceClient, private readonly client: ITypeScriptServiceClient,
private readonly fileConfigurationManager: FileConfigurationManager private readonly fileConfigurationManager: FileConfigurationManager
) { } ) {}
public async prepareRename( public async prepareRename(
document: TextDocument, document: TextDocument,
@ -60,8 +60,8 @@ export default class TypeScriptRenameProvider implements RenameProvider {
} }
if (this.client.apiVersion.gte(API.v310)) { if (this.client.apiVersion.gte(API.v310)) {
if ((renameInfo as any).fileToRename) { if (renameInfo.fileToRename) {
const edits = await this.renameFile((renameInfo as any).fileToRename, newName, token) const edits = await this.renameFile(renameInfo.fileToRename, newName, token)
if (edits) { if (edits) {
return edits return edits
} else { } else {

View file

@ -39,7 +39,7 @@ export class OrganizeImportsCommand implements Command {
client, client,
response.body response.body
) )
let keys = Object.keys(edit.changes) let keys = Object.keys(edit.changes || {})
if (keys.length == 1) { if (keys.length == 1) {
let doc = workspace.getDocument(keys[0]) let doc = workspace.getDocument(keys[0])
if (doc) { if (doc) {

View file

@ -6,6 +6,7 @@
* Helpers for converting FROM LanguageServer types language-server ts types * Helpers for converting FROM LanguageServer types language-server ts types
*/ */
import * as language from 'vscode-languageserver-protocol' import * as language from 'vscode-languageserver-protocol'
import { TextDocumentEdit } from 'vscode-languageserver-protocol'
import Proto from '../protocol' import Proto from '../protocol'
import * as PConst from '../protocol.const' import * as PConst from '../protocol.const'
import { ITypeScriptServiceClient } from '../typescriptService' import { ITypeScriptServiceClient } from '../typescriptService'
@ -98,14 +99,20 @@ export namespace WorkspaceEdit {
client: ITypeScriptServiceClient, client: ITypeScriptServiceClient,
edits: Iterable<Proto.FileCodeEdits> edits: Iterable<Proto.FileCodeEdits>
): language.WorkspaceEdit { ): language.WorkspaceEdit {
let changes = {} let documentChanges: TextDocumentEdit[] = []
for (const edit of edits) { for (const edit of edits) {
let uri = client.toResource(edit.fileName) let uri = client.toResource(edit.fileName)
changes[uri] = edit.textChanges.map(change => { documentChanges.push({
return TextEdit.fromCodeEdit(change) textDocument: {
uri,
version: null
},
edits: edit.textChanges.map(change => {
return TextEdit.fromCodeEdit(change)
})
}) })
} }
return { changes } return { documentChanges }
} }
} }