aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/join_lines.ts
blob: 0bf1ee6e671e12ba46b06663b20d9c20ee4face4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import * as ra from '../rust-analyzer-api';
import * as lc from 'vscode-languageclient';

import { Ctx, Cmd } from '../ctx';

export function joinLines(ctx: Ctx): Cmd {
    return async () => {
        const editor = ctx.activeRustEditor;
        const client = ctx.client;
        if (!editor || !client) return;

        const items: lc.TextEdit[] = await client.sendRequest(ra.joinLines, {
            ranges: editor.selections.map((it) => client.code2ProtocolConverter.asRange(it)),
            textDocument: { uri: editor.document.uri.toString() },
        });
        editor.edit((builder) => {
            client.protocol2CodeConverter.asTextEdits(items).forEach((edit) => {
                builder.replace(edit.range, edit.newText);
            });
        });
    };
}