aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/apply_source_change.ts
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-10-08 20:39:52 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-10-08 20:39:52 +0100
commitf4ad36e972989c3feed8671d6d6fca0aed37cd8f (patch)
treef60e1aa4703c3e176315ecd886206848028b8cbf /editors/code/src/commands/apply_source_change.ts
parenta05e09e9c514878148ddf26aa76d6b9183583d0f (diff)
parentbbf38b9e722e8d6455828ff22242c92219da346d (diff)
Merge #103
103: WIP: refactor vscode extension r=aochagavia a=aochagavia Todo: - [x] Add more comments, so other people can find their way in the codebase - [x] Resolve remaining tslint suggestions - [ ] Integrate with CI @matklad The standard configuration of tslint forbids using `console.log` and `console.error`. Is there any reason we are using those or can I remove them? If they are used for debugging purposes I would prefer to remove them and rely on vscode's excellent debugger. Co-authored-by: Adolfo OchagavĂ­a <[email protected]>
Diffstat (limited to 'editors/code/src/commands/apply_source_change.ts')
-rw-r--r--editors/code/src/commands/apply_source_change.ts58
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 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient';
3
4import { Server } from '../server';
5
6interface FileSystemEdit {
7 type: string;
8 uri?: string;
9 src?: string;
10 dst?: string;
11}
12
13export interface SourceChange {
14 label: string;
15 sourceFileEdits: lc.TextDocumentEdit[];
16 fileSystemEdits: FileSystemEdit[];
17 cursorPosition?: lc.TextDocumentPositionParams;
18}
19
20export 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}