diff options
Diffstat (limited to 'editors/code/src/commands/apply_source_change.ts')
-rw-r--r-- | editors/code/src/commands/apply_source_change.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/editors/code/src/commands/apply_source_change.ts b/editors/code/src/commands/apply_source_change.ts new file mode 100644 index 000000000..67765e5a3 --- /dev/null +++ b/editors/code/src/commands/apply_source_change.ts | |||
@@ -0,0 +1,58 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as lc from 'vscode-languageclient'; | ||
3 | |||
4 | import { Server } from '../server'; | ||
5 | |||
6 | interface FileSystemEdit { | ||
7 | type: string; | ||
8 | uri?: string; | ||
9 | src?: string; | ||
10 | dst?: string; | ||
11 | } | ||
12 | |||
13 | export interface SourceChange { | ||
14 | label: string; | ||
15 | sourceFileEdits: lc.TextDocumentEdit[]; | ||
16 | fileSystemEdits: FileSystemEdit[]; | ||
17 | cursorPosition?: lc.TextDocumentPositionParams; | ||
18 | } | ||
19 | |||
20 | export async function handle(change: SourceChange) { | ||
21 | const wsEdit = new vscode.WorkspaceEdit(); | ||
22 | for (const sourceEdit of change.sourceFileEdits) { | ||
23 | const uri = Server.client.protocol2CodeConverter.asUri(sourceEdit.textDocument.uri); | ||
24 | const edits = Server.client.protocol2CodeConverter.asTextEdits(sourceEdit.edits); | ||
25 | wsEdit.set(uri, edits); | ||
26 | } | ||
27 | let created; | ||
28 | let moved; | ||
29 | for (const fsEdit of change.fileSystemEdits) { | ||
30 | switch (fsEdit.type) { | ||
31 | case 'createFile': | ||
32 | const uri = vscode.Uri.parse(fsEdit.uri!); | ||
33 | wsEdit.createFile(uri); | ||
34 | created = uri; | ||
35 | break; | ||
36 | case 'moveFile': | ||
37 | const src = vscode.Uri.parse(fsEdit.src!); | ||
38 | const dst = vscode.Uri.parse(fsEdit.dst!); | ||
39 | wsEdit.renameFile(src, dst); | ||
40 | moved = dst; | ||
41 | break; | ||
42 | } | ||
43 | } | ||
44 | const toOpen = created || moved; | ||
45 | const toReveal = change.cursorPosition; | ||
46 | await vscode.workspace.applyEdit(wsEdit); | ||
47 | if (toOpen) { | ||
48 | const doc = await vscode.workspace.openTextDocument(toOpen); | ||
49 | await vscode.window.showTextDocument(doc); | ||
50 | } else if (toReveal) { | ||
51 | const uri = Server.client.protocol2CodeConverter.asUri(toReveal.textDocument.uri); | ||
52 | const position = Server.client.protocol2CodeConverter.asPosition(toReveal.position); | ||
53 | const editor = vscode.window.activeTextEditor; | ||
54 | if (!editor || editor.document.uri.toString() !== uri.toString()) { return; } | ||
55 | if (!editor.selection.isEmpty) { return; } | ||
56 | editor!.selection = new vscode.Selection(position, position); | ||
57 | } | ||
58 | } | ||