diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2019-12-30 15:20:13 +0100 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2019-12-30 15:23:55 +0100 |
| commit | 5dd9edaeafde3d5b5975cefe8dc1a65ccd9cd59f (patch) | |
| tree | 12032141b5a113aca0a05ce2ebb3da55ce87f7f9 /editors/code | |
| parent | 57df9bed703bd0f8b7a7cc593152b65b543ad121 (diff) | |
| download | rust-5dd9edaeafde3d5b5975cefe8dc1a65ccd9cd59f.tar.gz rust-5dd9edaeafde3d5b5975cefe8dc1a65ccd9cd59f.zip | |
Move matching brace to new Ctx
Diffstat (limited to 'editors/code')
| -rw-r--r-- | editors/code/src/commands/index.ts | 2 | ||||
| -rw-r--r-- | editors/code/src/commands/matching_brace.ts | 53 | ||||
| -rw-r--r-- | editors/code/src/ctx.ts | 7 | ||||
| -rw-r--r-- | editors/code/src/main.ts | 5 |
4 files changed, 35 insertions, 32 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index ed56f5a4e38..9d9b9c57549 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -1,11 +1,11 @@ import { Ctx, Cmd } from '../ctx' import { analyzerStatus } from './analyzer_status'; +import { matchingBrace } from './matching_brace'; import * as applySourceChange from './apply_source_change'; import * as expandMacro from './expand_macro'; import * as inlayHints from './inlay_hints'; import * as joinLines from './join_lines'; -import * as matchingBrace from './matching_brace'; import * as onEnter from './on_enter'; import * as parentModule from './parent_module'; import * as runnables from './runnables'; diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts index 364208cc758..665b0c33cc4 100644 --- a/editors/code/src/commands/matching_brace.ts +++ b/editors/code/src/commands/matching_brace.ts @@ -1,34 +1,33 @@ import * as vscode from 'vscode'; - import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; -import { Server } from '../server'; +import { Ctx, Cmd } from '../ctx'; + +export function matchingBrace(ctx: Ctx): Cmd { + return async () => { + const editor = ctx.activeRustEditor; + if (!editor) { + return; + } + const request: FindMatchingBraceParams = { + textDocument: { uri: editor.document.uri.toString() }, + offsets: editor.selections.map(s => ctx.client.code2ProtocolConverter.asPosition(s.active)), + }; + const response = await ctx.client.sendRequest<Position[]>( + 'rust-analyzer/findMatchingBrace', + request, + ); + editor.selections = editor.selections.map((sel, idx) => { + const active = ctx.client.protocol2CodeConverter.asPosition( + response[idx], + ); + const anchor = sel.isEmpty ? active : sel.anchor; + return new vscode.Selection(anchor, active); + }); + editor.revealRange(editor.selection); + } +} interface FindMatchingBraceParams { textDocument: TextDocumentIdentifier; offsets: Position[]; } - -export async function handle() { - const editor = vscode.window.activeTextEditor; - if (editor == null || editor.document.languageId !== 'rust') { - return; - } - const request: FindMatchingBraceParams = { - textDocument: { uri: editor.document.uri.toString() }, - offsets: editor.selections.map(s => { - return Server.client.code2ProtocolConverter.asPosition(s.active); - }), - }; - const response = await Server.client.sendRequest<Position[]>( - 'rust-analyzer/findMatchingBrace', - request, - ); - editor.selections = editor.selections.map((sel, idx) => { - const active = Server.client.protocol2CodeConverter.asPosition( - response[idx], - ); - const anchor = sel.isEmpty ? active : sel.anchor; - return new vscode.Selection(anchor, active); - }); - editor.revealRange(editor.selection); -} diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 87f1574d380..712337fe71c 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -13,6 +13,13 @@ export class Ctx { 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) { const fullName = `rust-analyzer.${name}`; const cmd = factory(this); diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index f96fb196257..a4149a05999 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -17,6 +17,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx = new Ctx(context); ctx.registerCommand('analyzerStatus', commands.analyzerStatus); ctx.registerCommand('collectGarbage', commands.collectGarbage); + ctx.registerCommand('matchingBrace', commands.matchingBrace); function disposeOnDeactivation(disposable: vscode.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 - registerCommand( - 'rust-analyzer.matchingBrace', - commands.matchingBrace.handle, - ); registerCommand('rust-analyzer.joinLines', commands.joinLines.handle); registerCommand('rust-analyzer.parentModule', commands.parentModule.handle); registerCommand('rust-analyzer.run', commands.runnables.handle); |
