diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-30 18:08:23 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-30 18:08:23 +0000 |
commit | 7c1634a9c2d76ea8c152c368775391090d62db8f (patch) | |
tree | 3879a92160f2313f54e738812d698d62e298f1a0 /editors/code/src/source_change.ts | |
parent | b42d3ee3cc22aaa892d15c4ba2219a3bc53907a1 (diff) | |
parent | 260df66b7742e76c76184388253552c5055b1945 (diff) |
Merge #2691
2691: Cleanup imports r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'editors/code/src/source_change.ts')
-rw-r--r-- | editors/code/src/source_change.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/editors/code/src/source_change.ts b/editors/code/src/source_change.ts new file mode 100644 index 000000000..a4f9068b2 --- /dev/null +++ b/editors/code/src/source_change.ts | |||
@@ -0,0 +1,54 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as lc from 'vscode-languageclient'; | ||
3 | |||
4 | import { Ctx } from './ctx'; | ||
5 | |||
6 | export interface SourceChange { | ||
7 | label: string; | ||
8 | workspaceEdit: lc.WorkspaceEdit; | ||
9 | cursorPosition?: lc.TextDocumentPositionParams; | ||
10 | } | ||
11 | |||
12 | export async function applySourceChange(ctx: Ctx, change: SourceChange) { | ||
13 | const wsEdit = ctx.client.protocol2CodeConverter.asWorkspaceEdit( | ||
14 | change.workspaceEdit, | ||
15 | ); | ||
16 | let created; | ||
17 | let moved; | ||
18 | if (change.workspaceEdit.documentChanges) { | ||
19 | for (const docChange of change.workspaceEdit.documentChanges) { | ||
20 | if (lc.CreateFile.is(docChange)) { | ||
21 | created = docChange.uri; | ||
22 | } else if (lc.RenameFile.is(docChange)) { | ||
23 | moved = docChange.newUri; | ||
24 | } | ||
25 | } | ||
26 | } | ||
27 | const toOpen = created || moved; | ||
28 | const toReveal = change.cursorPosition; | ||
29 | await vscode.workspace.applyEdit(wsEdit); | ||
30 | if (toOpen) { | ||
31 | const toOpenUri = vscode.Uri.parse(toOpen); | ||
32 | const doc = await vscode.workspace.openTextDocument(toOpenUri); | ||
33 | await vscode.window.showTextDocument(doc); | ||
34 | } else if (toReveal) { | ||
35 | const uri = ctx.client.protocol2CodeConverter.asUri( | ||
36 | toReveal.textDocument.uri, | ||
37 | ); | ||
38 | const position = ctx.client.protocol2CodeConverter.asPosition( | ||
39 | toReveal.position, | ||
40 | ); | ||
41 | const editor = vscode.window.activeTextEditor; | ||
42 | if (!editor || editor.document.uri.toString() !== uri.toString()) { | ||
43 | return; | ||
44 | } | ||
45 | if (!editor.selection.isEmpty) { | ||
46 | return; | ||
47 | } | ||
48 | editor.selection = new vscode.Selection(position, position); | ||
49 | editor.revealRange( | ||
50 | new vscode.Range(position, position), | ||
51 | vscode.TextEditorRevealType.Default, | ||
52 | ); | ||
53 | } | ||
54 | } | ||