aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/source_change.ts
blob: a336269baa8bdec26658993a97eb709bb145b090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';

import { Ctx } from './ctx';

export interface SourceChange {
    label: string;
    workspaceEdit: lc.WorkspaceEdit;
    cursorPosition?: lc.TextDocumentPositionParams;
}

export async function applySourceChange(ctx: Ctx, change: SourceChange) {
    const client = ctx.client;
    if (!client) return;

    const wsEdit = client.protocol2CodeConverter.asWorkspaceEdit(
        change.workspaceEdit,
    );
    let created;
    let moved;
    if (change.workspaceEdit.documentChanges) {
        for (const docChange of change.workspaceEdit.documentChanges) {
            if (lc.CreateFile.is(docChange)) {
                created = docChange.uri;
            } else if (lc.RenameFile.is(docChange)) {
                moved = docChange.newUri;
            }
        }
    }
    const toOpen = created || moved;
    const toReveal = change.cursorPosition;
    await vscode.workspace.applyEdit(wsEdit);
    if (toOpen) {
        const toOpenUri = vscode.Uri.parse(toOpen);
        const doc = await vscode.workspace.openTextDocument(toOpenUri);
        await vscode.window.showTextDocument(doc);
    } else if (toReveal) {
        const uri = client.protocol2CodeConverter.asUri(
            toReveal.textDocument.uri,
        );
        const position = client.protocol2CodeConverter.asPosition(
            toReveal.position,
        );
        const editor = vscode.window.activeTextEditor;
        if (!editor || editor.document.uri.toString() !== uri.toString()) {
            return;
        }
        if (!editor.selection.isEmpty) {
            return;
        }
        editor.selection = new vscode.Selection(position, position);
        editor.revealRange(
            new vscode.Range(position, position),
            vscode.TextEditorRevealType.Default,
        );
    }
}