Skip to content

Commit 1af1f6d

Browse files
testforstephenfbricon
authored andcommitted
preview the updates before apply the changes caused by rename file
Signed-off-by: Jinbo Wang <[email protected]>
1 parent 2a839a2 commit 1af1f6d

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

src/fileEventHandler.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22

33
import * as path from 'path';
4-
import { workspace, FileCreateEvent, ExtensionContext, window, TextDocument, SnippetString, commands, Uri, FileRenameEvent, ProgressLocation } from 'vscode';
5-
import { LanguageClient } from 'vscode-languageclient';
4+
import { workspace, FileCreateEvent, ExtensionContext, window, TextDocument, SnippetString, commands, Uri, FileRenameEvent, ProgressLocation, WorkspaceEdit as CodeWorkspaceEdit } from 'vscode';
5+
import { LanguageClient, WorkspaceEdit as LsWorkspaceEdit, CreateFile, RenameFile, DeleteFile, TextDocumentEdit } from 'vscode-languageclient';
66
import { ListCommandResult } from './buildpath';
77
import { Commands } from './commands';
88
import { DidRenameFiles } from './protocol';
9+
import { Converter as ProtocolConverter } from 'vscode-languageclient/lib/protocolConverter';
910

1011
let serverReady: boolean = false;
1112

@@ -98,7 +99,8 @@ async function handleRenameFiles(e: FileRenameEvent, client: LanguageClient) {
9899
const edit = await client.sendRequest(DidRenameFiles.type, {
99100
files: javaRenameEvents
100101
});
101-
const codeEdit = client.protocol2CodeConverter.asWorkspaceEdit(edit);
102+
103+
const codeEdit = asPreviewWorkspaceEdit(edit, client.protocol2CodeConverter, "Rename updates");
102104
if (codeEdit) {
103105
workspace.applyEdit(codeEdit);
104106
}
@@ -173,3 +175,58 @@ async function isVersionLessThan(fileUri: string, targetVersion: number): Promis
173175

174176
return javaVersion < targetVersion;
175177
}
178+
179+
/**
180+
* This function reference the implementation of asWorkspaceEdit() from 'vscode-languageclient/lib/protocolConverter'.
181+
*/
182+
function asPreviewWorkspaceEdit(item: LsWorkspaceEdit, converter: ProtocolConverter, label: string) {
183+
if (!item) {
184+
return undefined;
185+
}
186+
187+
const result = new CodeWorkspaceEdit();
188+
if (item.documentChanges) {
189+
item.documentChanges.forEach(change => {
190+
if (CreateFile.is(change)) {
191+
result.createFile(converter.asUri(change.uri), change.options, {
192+
needsConfirmation: true,
193+
label,
194+
});
195+
} else if (RenameFile.is(change)) {
196+
result.renameFile(converter.asUri(change.oldUri), converter.asUri(change.newUri), change.options, {
197+
needsConfirmation: true,
198+
label,
199+
});
200+
} else if (DeleteFile.is(change)) {
201+
result.deleteFile(converter.asUri(change.uri), change.options, {
202+
needsConfirmation: true,
203+
label,
204+
});
205+
} else if (TextDocumentEdit.is(change)) {
206+
if (change.edits) {
207+
change.edits.forEach(edit => {
208+
result.replace(converter.asUri(change.textDocument.uri), converter.asRange(edit.range), edit.newText, {
209+
needsConfirmation: true,
210+
label,
211+
});
212+
});
213+
}
214+
} else {
215+
console.error(`Unknown workspace edit change received:\n${JSON.stringify(change, undefined, 4)}`);
216+
}
217+
});
218+
} else if (item.changes) {
219+
Object.keys(item.changes).forEach(key => {
220+
if (item.changes[key]) {
221+
item.changes[key].forEach(edit => {
222+
result.replace(converter.asUri(key), converter.asRange(edit.range), edit.newText, {
223+
needsConfirmation: true,
224+
label,
225+
});
226+
});
227+
}
228+
});
229+
}
230+
231+
return result;
232+
}

0 commit comments

Comments
 (0)