Move matching brace to new Ctx

This commit is contained in:
Aleksey Kladov 2019-12-30 15:20:13 +01:00
parent 57df9bed70
commit 5dd9edaeaf
4 changed files with 35 additions and 32 deletions

View file

@ -1,11 +1,11 @@
import { Ctx, Cmd } from '../ctx' import { Ctx, Cmd } from '../ctx'
import { analyzerStatus } from './analyzer_status'; import { analyzerStatus } from './analyzer_status';
import { matchingBrace } from './matching_brace';
import * as applySourceChange from './apply_source_change'; import * as applySourceChange from './apply_source_change';
import * as expandMacro from './expand_macro'; import * as expandMacro from './expand_macro';
import * as inlayHints from './inlay_hints'; import * as inlayHints from './inlay_hints';
import * as joinLines from './join_lines'; import * as joinLines from './join_lines';
import * as matchingBrace from './matching_brace';
import * as onEnter from './on_enter'; import * as onEnter from './on_enter';
import * as parentModule from './parent_module'; import * as parentModule from './parent_module';
import * as runnables from './runnables'; import * as runnables from './runnables';

View file

@ -1,30 +1,23 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
import { Server } from '../server'; import { Ctx, Cmd } from '../ctx';
interface FindMatchingBraceParams { export function matchingBrace(ctx: Ctx): Cmd {
textDocument: TextDocumentIdentifier; return async () => {
offsets: Position[]; const editor = ctx.activeRustEditor;
} if (!editor) {
export async function handle() {
const editor = vscode.window.activeTextEditor;
if (editor == null || editor.document.languageId !== 'rust') {
return; return;
} }
const request: FindMatchingBraceParams = { const request: FindMatchingBraceParams = {
textDocument: { uri: editor.document.uri.toString() }, textDocument: { uri: editor.document.uri.toString() },
offsets: editor.selections.map(s => { offsets: editor.selections.map(s => ctx.client.code2ProtocolConverter.asPosition(s.active)),
return Server.client.code2ProtocolConverter.asPosition(s.active);
}),
}; };
const response = await Server.client.sendRequest<Position[]>( const response = await ctx.client.sendRequest<Position[]>(
'rust-analyzer/findMatchingBrace', 'rust-analyzer/findMatchingBrace',
request, request,
); );
editor.selections = editor.selections.map((sel, idx) => { editor.selections = editor.selections.map((sel, idx) => {
const active = Server.client.protocol2CodeConverter.asPosition( const active = ctx.client.protocol2CodeConverter.asPosition(
response[idx], response[idx],
); );
const anchor = sel.isEmpty ? active : sel.anchor; const anchor = sel.isEmpty ? active : sel.anchor;
@ -32,3 +25,9 @@ export async function handle() {
}); });
editor.revealRange(editor.selection); editor.revealRange(editor.selection);
} }
}
interface FindMatchingBraceParams {
textDocument: TextDocumentIdentifier;
offsets: Position[];
}

View file

@ -13,6 +13,13 @@ export class Ctx {
return Server.client; return Server.client;
} }
get activeRustEditor(): vscode.TextEditor | undefined {
const editor = vscode.window.activeTextEditor;
return editor && editor.document.languageId === 'rust'
? editor
: undefined;
}
registerCommand(name: string, factory: (ctx: Ctx) => Cmd) { registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
const fullName = `rust-analyzer.${name}`; const fullName = `rust-analyzer.${name}`;
const cmd = factory(this); const cmd = factory(this);

View file

@ -17,6 +17,7 @@ export async function activate(context: vscode.ExtensionContext) {
ctx = new Ctx(context); ctx = new Ctx(context);
ctx.registerCommand('analyzerStatus', commands.analyzerStatus); ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
ctx.registerCommand('collectGarbage', commands.collectGarbage); ctx.registerCommand('collectGarbage', commands.collectGarbage);
ctx.registerCommand('matchingBrace', commands.matchingBrace);
function disposeOnDeactivation(disposable: vscode.Disposable) { function disposeOnDeactivation(disposable: vscode.Disposable) {
context.subscriptions.push(disposable); context.subscriptions.push(disposable);
@ -55,10 +56,6 @@ export async function activate(context: vscode.ExtensionContext) {
} }
// Commands are requests from vscode to the language server // Commands are requests from vscode to the language server
registerCommand(
'rust-analyzer.matchingBrace',
commands.matchingBrace.handle,
);
registerCommand('rust-analyzer.joinLines', commands.joinLines.handle); registerCommand('rust-analyzer.joinLines', commands.joinLines.handle);
registerCommand('rust-analyzer.parentModule', commands.parentModule.handle); registerCommand('rust-analyzer.parentModule', commands.parentModule.handle);
registerCommand('rust-analyzer.run', commands.runnables.handle); registerCommand('rust-analyzer.run', commands.runnables.handle);