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.
* 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<WorkspaceEdit> {
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
}
}

View file

@ -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 {

View file

@ -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) {

View file

@ -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<Proto.FileCodeEdits>
): 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 }
}
}