diff options
author | Aleksey Kladov <[email protected]> | 2019-12-30 14:20:13 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-12-30 14:23:55 +0000 |
commit | 5dd9edaeafde3d5b5975cefe8dc1a65ccd9cd59f (patch) | |
tree | 12032141b5a113aca0a05ce2ebb3da55ce87f7f9 /editors/code/src | |
parent | 57df9bed703bd0f8b7a7cc593152b65b543ad121 (diff) |
Move matching brace to new Ctx
Diffstat (limited to 'editors/code/src')
-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 ed56f5a4e..9d9b9c575 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts | |||
@@ -1,11 +1,11 @@ | |||
1 | import { Ctx, Cmd } from '../ctx' | 1 | import { Ctx, Cmd } from '../ctx' |
2 | 2 | ||
3 | import { analyzerStatus } from './analyzer_status'; | 3 | import { analyzerStatus } from './analyzer_status'; |
4 | import { matchingBrace } from './matching_brace'; | ||
4 | import * as applySourceChange from './apply_source_change'; | 5 | import * as applySourceChange from './apply_source_change'; |
5 | import * as expandMacro from './expand_macro'; | 6 | import * as expandMacro from './expand_macro'; |
6 | import * as inlayHints from './inlay_hints'; | 7 | import * as inlayHints from './inlay_hints'; |
7 | import * as joinLines from './join_lines'; | 8 | import * as joinLines from './join_lines'; |
8 | import * as matchingBrace from './matching_brace'; | ||
9 | import * as onEnter from './on_enter'; | 9 | import * as onEnter from './on_enter'; |
10 | import * as parentModule from './parent_module'; | 10 | import * as parentModule from './parent_module'; |
11 | import * as runnables from './runnables'; | 11 | import * as runnables from './runnables'; |
diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts index 364208cc7..665b0c33c 100644 --- a/editors/code/src/commands/matching_brace.ts +++ b/editors/code/src/commands/matching_brace.ts | |||
@@ -1,34 +1,33 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | |||
3 | import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; | 2 | import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; |
4 | import { Server } from '../server'; | 3 | import { Ctx, Cmd } from '../ctx'; |
4 | |||
5 | export function matchingBrace(ctx: Ctx): Cmd { | ||
6 | return async () => { | ||
7 | const editor = ctx.activeRustEditor; | ||
8 | if (!editor) { | ||
9 | return; | ||
10 | } | ||
11 | const request: FindMatchingBraceParams = { | ||
12 | textDocument: { uri: editor.document.uri.toString() }, | ||
13 | offsets: editor.selections.map(s => ctx.client.code2ProtocolConverter.asPosition(s.active)), | ||
14 | }; | ||
15 | const response = await ctx.client.sendRequest<Position[]>( | ||
16 | 'rust-analyzer/findMatchingBrace', | ||
17 | request, | ||
18 | ); | ||
19 | editor.selections = editor.selections.map((sel, idx) => { | ||
20 | const active = ctx.client.protocol2CodeConverter.asPosition( | ||
21 | response[idx], | ||
22 | ); | ||
23 | const anchor = sel.isEmpty ? active : sel.anchor; | ||
24 | return new vscode.Selection(anchor, active); | ||
25 | }); | ||
26 | editor.revealRange(editor.selection); | ||
27 | } | ||
28 | } | ||
5 | 29 | ||
6 | interface FindMatchingBraceParams { | 30 | interface FindMatchingBraceParams { |
7 | textDocument: TextDocumentIdentifier; | 31 | textDocument: TextDocumentIdentifier; |
8 | offsets: Position[]; | 32 | offsets: Position[]; |
9 | } | 33 | } |
10 | |||
11 | export async function handle() { | ||
12 | const editor = vscode.window.activeTextEditor; | ||
13 | if (editor == null || editor.document.languageId !== 'rust') { | ||
14 | return; | ||
15 | } | ||
16 | const request: FindMatchingBraceParams = { | ||
17 | textDocument: { uri: editor.document.uri.toString() }, | ||
18 | offsets: editor.selections.map(s => { | ||
19 | return Server.client.code2ProtocolConverter.asPosition(s.active); | ||
20 | }), | ||
21 | }; | ||
22 | const response = await Server.client.sendRequest<Position[]>( | ||
23 | 'rust-analyzer/findMatchingBrace', | ||
24 | request, | ||
25 | ); | ||
26 | editor.selections = editor.selections.map((sel, idx) => { | ||
27 | const active = Server.client.protocol2CodeConverter.asPosition( | ||
28 | response[idx], | ||
29 | ); | ||
30 | const anchor = sel.isEmpty ? active : sel.anchor; | ||
31 | return new vscode.Selection(anchor, active); | ||
32 | }); | ||
33 | editor.revealRange(editor.selection); | ||
34 | } | ||
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 87f1574d3..712337fe7 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts | |||
@@ -13,6 +13,13 @@ export class Ctx { | |||
13 | return Server.client; | 13 | return Server.client; |
14 | } | 14 | } |
15 | 15 | ||
16 | get activeRustEditor(): vscode.TextEditor | undefined { | ||
17 | const editor = vscode.window.activeTextEditor; | ||
18 | return editor && editor.document.languageId === 'rust' | ||
19 | ? editor | ||
20 | : undefined; | ||
21 | } | ||
22 | |||
16 | registerCommand(name: string, factory: (ctx: Ctx) => Cmd) { | 23 | registerCommand(name: string, factory: (ctx: Ctx) => Cmd) { |
17 | const fullName = `rust-analyzer.${name}`; | 24 | const fullName = `rust-analyzer.${name}`; |
18 | const cmd = factory(this); | 25 | const cmd = factory(this); |
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index f96fb1962..a4149a059 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -17,6 +17,7 @@ export async function activate(context: vscode.ExtensionContext) { | |||
17 | ctx = new Ctx(context); | 17 | ctx = new Ctx(context); |
18 | ctx.registerCommand('analyzerStatus', commands.analyzerStatus); | 18 | ctx.registerCommand('analyzerStatus', commands.analyzerStatus); |
19 | ctx.registerCommand('collectGarbage', commands.collectGarbage); | 19 | ctx.registerCommand('collectGarbage', commands.collectGarbage); |
20 | ctx.registerCommand('matchingBrace', commands.matchingBrace); | ||
20 | 21 | ||
21 | function disposeOnDeactivation(disposable: vscode.Disposable) { | 22 | function disposeOnDeactivation(disposable: vscode.Disposable) { |
22 | context.subscriptions.push(disposable); | 23 | context.subscriptions.push(disposable); |
@@ -55,10 +56,6 @@ export async function activate(context: vscode.ExtensionContext) { | |||
55 | } | 56 | } |
56 | 57 | ||
57 | // Commands are requests from vscode to the language server | 58 | // Commands are requests from vscode to the language server |
58 | registerCommand( | ||
59 | 'rust-analyzer.matchingBrace', | ||
60 | commands.matchingBrace.handle, | ||
61 | ); | ||
62 | registerCommand('rust-analyzer.joinLines', commands.joinLines.handle); | 59 | registerCommand('rust-analyzer.joinLines', commands.joinLines.handle); |
63 | registerCommand('rust-analyzer.parentModule', commands.parentModule.handle); | 60 | registerCommand('rust-analyzer.parentModule', commands.parentModule.handle); |
64 | registerCommand('rust-analyzer.run', commands.runnables.handle); | 61 | registerCommand('rust-analyzer.run', commands.runnables.handle); |