rust-analyzer/editors/code/src/commands/matching_brace.ts

35 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-10-07 20:44:25 +00:00
import * as vscode from 'vscode';
2018-10-07 20:59:02 +00:00
import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
2018-10-07 20:44:25 +00:00
import { Server } from '../server';
interface FindMatchingBraceParams {
textDocument: TextDocumentIdentifier;
offsets: Position[];
}
export async function handle() {
2018-10-07 20:59:02 +00:00
const editor = vscode.window.activeTextEditor;
2018-10-08 21:38:33 +00:00
if (editor == null || editor.document.languageId !== 'rust') {
return;
}
2018-10-07 20:59:02 +00:00
const request: FindMatchingBraceParams = {
2018-10-07 20:44:25 +00:00
textDocument: { uri: editor.document.uri.toString() },
2018-10-08 21:38:33 +00:00
offsets: editor.selections.map(s => {
2018-10-07 20:59:02 +00:00
return Server.client.code2ProtocolConverter.asPosition(s.active);
2018-10-08 21:38:33 +00:00
})
2018-10-07 20:59:02 +00:00
};
2018-10-08 21:38:33 +00:00
const response = await Server.client.sendRequest<Position[]>(
2019-01-28 11:43:07 +00:00
'rust-analyzer/findMatchingBrace',
2018-10-08 21:38:33 +00:00
request
);
2018-10-07 20:44:25 +00:00
editor.selections = editor.selections.map((sel, idx) => {
2018-10-08 21:38:33 +00:00
const active = Server.client.protocol2CodeConverter.asPosition(
response[idx]
);
2018-10-07 20:59:02 +00:00
const anchor = sel.isEmpty ? active : sel.anchor;
return new vscode.Selection(anchor, active);
});
editor.revealRange(editor.selection);
}