diff options
Diffstat (limited to 'editors/code/src/source_change.ts')
-rw-r--r-- | editors/code/src/source_change.ts | 57 |
1 files changed, 57 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..a336269ba --- /dev/null +++ b/editors/code/src/source_change.ts | |||
@@ -0,0 +1,57 @@ | |||
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 client = ctx.client; | ||
14 | if (!client) return; | ||
15 | |||
16 | const wsEdit = client.protocol2CodeConverter.asWorkspaceEdit( | ||
17 | change.workspaceEdit, | ||
18 | ); | ||
19 | let created; | ||
20 | let moved; | ||
21 | if (change.workspaceEdit.documentChanges) { | ||
22 | for (const docChange of change.workspaceEdit.documentChanges) { | ||
23 | if (lc.CreateFile.is(docChange)) { | ||
24 | created = docChange.uri; | ||
25 | } else if (lc.RenameFile.is(docChange)) { | ||
26 | moved = docChange.newUri; | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | const toOpen = created || moved; | ||
31 | const toReveal = change.cursorPosition; | ||
32 | await vscode.workspace.applyEdit(wsEdit); | ||
33 | if (toOpen) { | ||
34 | const toOpenUri = vscode.Uri.parse(toOpen); | ||
35 | const doc = await vscode.workspace.openTextDocument(toOpenUri); | ||
36 | await vscode.window.showTextDocument(doc); | ||
37 | } else if (toReveal) { | ||
38 | const uri = client.protocol2CodeConverter.asUri( | ||
39 | toReveal.textDocument.uri, | ||
40 | ); | ||
41 | const position = client.protocol2CodeConverter.asPosition( | ||
42 | toReveal.position, | ||
43 | ); | ||
44 | const editor = vscode.window.activeTextEditor; | ||
45 | if (!editor || editor.document.uri.toString() !== uri.toString()) { | ||
46 | return; | ||
47 | } | ||
48 | if (!editor.selection.isEmpty) { | ||
49 | return; | ||
50 | } | ||
51 | editor.selection = new vscode.Selection(position, position); | ||
52 | editor.revealRange( | ||
53 | new vscode.Range(position, position), | ||
54 | vscode.TextEditorRevealType.Default, | ||
55 | ); | ||
56 | } | ||
57 | } | ||