aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-30 15:43:34 +0000
committerAleksey Kladov <[email protected]>2019-12-30 18:07:59 +0000
commit5aebf1081dced95a71c674aba65fb5b3e40e6ff1 (patch)
treebacedf66912eee5a366a3c93c60a39ba29744f92
parent83d2527880d86653ce00940c65620319b36afcff (diff)
Refactor applySourceChange
-rw-r--r--editors/code/src/commands/analyzer_status.ts8
-rw-r--r--editors/code/src/commands/index.ts4
-rw-r--r--editors/code/src/commands/join_lines.ts12
-rw-r--r--editors/code/src/commands/on_enter.ts49
-rw-r--r--editors/code/src/ctx.ts22
-rw-r--r--editors/code/src/main.ts52
-rw-r--r--editors/code/src/source_change.ts (renamed from editors/code/src/commands/apply_source_change.ts)10
7 files changed, 68 insertions, 89 deletions
diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts
index c9d32fe07..849c2ec6c 100644
--- a/editors/code/src/commands/analyzer_status.ts
+++ b/editors/code/src/commands/analyzer_status.ts
@@ -40,11 +40,10 @@ export function analyzerStatus(ctx: Ctx): Cmd {
40class TextDocumentContentProvider 40class TextDocumentContentProvider
41 implements vscode.TextDocumentContentProvider { 41 implements vscode.TextDocumentContentProvider {
42 42
43 ctx: Ctx
43 uri = vscode.Uri.parse('rust-analyzer-status://status'); 44 uri = vscode.Uri.parse('rust-analyzer-status://status');
44 eventEmitter = new vscode.EventEmitter<vscode.Uri>(); 45 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
45 46
46 ctx: Ctx
47
48 constructor(ctx: Ctx) { 47 constructor(ctx: Ctx) {
49 this.ctx = ctx 48 this.ctx = ctx
50 } 49 }
@@ -53,9 +52,8 @@ class TextDocumentContentProvider
53 _uri: vscode.Uri, 52 _uri: vscode.Uri,
54 ): vscode.ProviderResult<string> { 53 ): vscode.ProviderResult<string> {
55 const editor = vscode.window.activeTextEditor; 54 const editor = vscode.window.activeTextEditor;
56 if (editor == null) { 55 if (editor == null) return '';
57 return ''; 56
58 }
59 return this.ctx.client.sendRequest<string>( 57 return this.ctx.client.sendRequest<string>(
60 'rust-analyzer/analyzerStatus', 58 'rust-analyzer/analyzerStatus',
61 null, 59 null,
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 8090c7e5b..0a0a36e23 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -3,10 +3,9 @@ import { Ctx, Cmd } from '../ctx'
3import { analyzerStatus } from './analyzer_status'; 3import { analyzerStatus } from './analyzer_status';
4import { matchingBrace } from './matching_brace'; 4import { matchingBrace } from './matching_brace';
5import { joinLines } from './join_lines'; 5import { joinLines } from './join_lines';
6import * as applySourceChange from './apply_source_change'; 6import { onEnter } from './on_enter';
7import * as expandMacro from './expand_macro'; 7import * as expandMacro from './expand_macro';
8import * as inlayHints from './inlay_hints'; 8import * as inlayHints from './inlay_hints';
9import * as onEnter from './on_enter';
10import * as parentModule from './parent_module'; 9import * as parentModule from './parent_module';
11import * as runnables from './runnables'; 10import * as runnables from './runnables';
12import * as syntaxTree from './syntaxTree'; 11import * as syntaxTree from './syntaxTree';
@@ -17,7 +16,6 @@ function collectGarbage(ctx: Ctx): Cmd {
17 16
18export { 17export {
19 analyzerStatus, 18 analyzerStatus,
20 applySourceChange,
21 expandMacro, 19 expandMacro,
22 joinLines, 20 joinLines,
23 matchingBrace, 21 matchingBrace,
diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts
index 7952fb0c0..1a4b8a2d8 100644
--- a/editors/code/src/commands/join_lines.ts
+++ b/editors/code/src/commands/join_lines.ts
@@ -1,16 +1,14 @@
1import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; 1import { Range, TextDocumentIdentifier } from 'vscode-languageclient';
2import { Ctx, Cmd } from '../ctx'; 2import { Ctx, Cmd } from '../ctx';
3import { 3import {
4 handle as applySourceChange, 4 applySourceChange, SourceChange
5 SourceChange, 5} from '../source_change';
6} from './apply_source_change';
7 6
8export function joinLines(ctx: Ctx): Cmd { 7export function joinLines(ctx: Ctx): Cmd {
9 return async () => { 8 return async () => {
10 const editor = ctx.activeRustEditor; 9 const editor = ctx.activeRustEditor;
11 if (!editor) { 10 if (!editor) return;
12 return; 11
13 }
14 const request: JoinLinesParams = { 12 const request: JoinLinesParams = {
15 range: ctx.client.code2ProtocolConverter.asRange(editor.selection), 13 range: ctx.client.code2ProtocolConverter.asRange(editor.selection),
16 textDocument: { uri: editor.document.uri.toString() }, 14 textDocument: { uri: editor.document.uri.toString() },
@@ -19,7 +17,7 @@ export function joinLines(ctx: Ctx): Cmd {
19 'rust-analyzer/joinLines', 17 'rust-analyzer/joinLines',
20 request, 18 request,
21 ); 19 );
22 await applySourceChange(change); 20 await applySourceChange(ctx, change);
23 } 21 }
24} 22}
25 23
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts
index 772c64b3c..4503e13f0 100644
--- a/editors/code/src/commands/on_enter.ts
+++ b/editors/code/src/commands/on_enter.ts
@@ -1,33 +1,28 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 1import * as lc from 'vscode-languageclient';
3import { Server } from '../server';
4import { 2import {
5 handle as applySourceChange, 3 applySourceChange,
6 SourceChange, 4 SourceChange,
7} from './apply_source_change'; 5} from '../source_change';
6import { Cmd, Ctx } from '../ctx';
8 7
9export async function handle(event: { text: string }): Promise<boolean> { 8export function onEnter(ctx: Ctx): Cmd {
10 const editor = vscode.window.activeTextEditor; 9 return async (event: { text: string }) => {
11 if ( 10 const editor = ctx.activeRustEditor;
12 editor == null || 11 if (!editor || event.text !== '\n') return false;
13 editor.document.languageId !== 'rust' || 12
14 event.text !== '\n' 13 const request: lc.TextDocumentPositionParams = {
15 ) { 14 textDocument: { uri: editor.document.uri.toString() },
16 return false; 15 position: ctx.client.code2ProtocolConverter.asPosition(
17 } 16 editor.selection.active,
18 const request: lc.TextDocumentPositionParams = { 17 ),
19 textDocument: { uri: editor.document.uri.toString() }, 18 };
20 position: Server.client.code2ProtocolConverter.asPosition( 19 const change = await ctx.client.sendRequest<undefined | SourceChange>(
21 editor.selection.active, 20 'rust-analyzer/onEnter',
22 ), 21 request,
23 }; 22 );
24 const change = await Server.client.sendRequest<undefined | SourceChange>( 23 if (!change) return false;
25 'rust-analyzer/onEnter', 24
26 request, 25 await applySourceChange(ctx, change);
27 ); 26 return true;
28 if (!change) {
29 return false;
30 } 27 }
31 await applySourceChange(change);
32 return true;
33} 28}
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
index 712337fe7..22af5ef32 100644
--- a/editors/code/src/ctx.ts
+++ b/editors/code/src/ctx.ts
@@ -27,6 +27,28 @@ export class Ctx {
27 this.pushCleanup(d); 27 this.pushCleanup(d);
28 } 28 }
29 29
30 overrideCommand(name: string, factory: (ctx: Ctx) => Cmd) {
31 const defaultCmd = `default:${name}`;
32 const override = factory(this);
33 const original = (...args: any[]) =>
34 vscode.commands.executeCommand(defaultCmd, ...args);
35 try {
36 const d = vscode.commands.registerCommand(
37 name,
38 async (...args: any[]) => {
39 if (!(await override(...args))) {
40 return await original(...args);
41 }
42 },
43 );
44 this.pushCleanup(d);
45 } catch (_) {
46 vscode.window.showWarningMessage(
47 'Enhanced typing feature is disabled because of incompatibility with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings',
48 );
49 }
50 }
51
30 pushCleanup(d: { dispose(): any }) { 52 pushCleanup(d: { dispose(): any }) {
31 this.extCtx.subscriptions.push(d); 53 this.extCtx.subscriptions.push(d);
32 } 54 }
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 95beb2d8f..c3f280630 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -27,34 +27,6 @@ export async function activate(context: vscode.ExtensionContext) {
27 function registerCommand(name: string, f: any) { 27 function registerCommand(name: string, f: any) {
28 disposeOnDeactivation(vscode.commands.registerCommand(name, f)); 28 disposeOnDeactivation(vscode.commands.registerCommand(name, f));
29 } 29 }
30 function overrideCommand(
31 name: string,
32 f: (...args: any[]) => Promise<boolean>,
33 ) {
34 const defaultCmd = `default:${name}`;
35 const original = (...args: any[]) =>
36 vscode.commands.executeCommand(defaultCmd, ...args);
37
38 try {
39 registerCommand(name, async (...args: any[]) => {
40 const editor = vscode.window.activeTextEditor;
41 if (
42 !editor ||
43 !editor.document ||
44 editor.document.languageId !== 'rust'
45 ) {
46 return await original(...args);
47 }
48 if (!(await f(...args))) {
49 return await original(...args);
50 }
51 });
52 } catch (_) {
53 vscode.window.showWarningMessage(
54 'Enhanced typing feature is disabled because of incompatibility with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings',
55 );
56 }
57 }
58 30
59 // Commands are requests from vscode to the language server 31 // Commands are requests from vscode to the language server
60 registerCommand('rust-analyzer.parentModule', commands.parentModule.handle); 32 registerCommand('rust-analyzer.parentModule', commands.parentModule.handle);
@@ -62,10 +34,6 @@ export async function activate(context: vscode.ExtensionContext) {
62 // Unlike the above this does not send requests to the language server 34 // Unlike the above this does not send requests to the language server
63 registerCommand('rust-analyzer.runSingle', commands.runnables.handleSingle); 35 registerCommand('rust-analyzer.runSingle', commands.runnables.handleSingle);
64 registerCommand( 36 registerCommand(
65 'rust-analyzer.applySourceChange',
66 commands.applySourceChange.handle,
67 );
68 registerCommand(
69 'rust-analyzer.showReferences', 37 'rust-analyzer.showReferences',
70 (uri: string, position: lc.Position, locations: lc.Location[]) => { 38 (uri: string, position: lc.Position, locations: lc.Location[]) => {
71 vscode.commands.executeCommand( 39 vscode.commands.executeCommand(
@@ -78,7 +46,7 @@ export async function activate(context: vscode.ExtensionContext) {
78 ); 46 );
79 47
80 if (Server.config.enableEnhancedTyping) { 48 if (Server.config.enableEnhancedTyping) {
81 overrideCommand('type', commands.onEnter.handle); 49 ctx.overrideCommand('type', commands.onEnter);
82 } 50 }
83 51
84 const watchStatus = new StatusDisplay( 52 const watchStatus = new StatusDisplay(
@@ -91,15 +59,15 @@ export async function activate(context: vscode.ExtensionContext) {
91 string, 59 string,
92 lc.GenericNotificationHandler, 60 lc.GenericNotificationHandler,
93 ]> = [ 61 ]> = [
94 [ 62 [
95 'rust-analyzer/publishDecorations', 63 'rust-analyzer/publishDecorations',
96 notifications.publishDecorations.handle, 64 notifications.publishDecorations.handle,
97 ], 65 ],
98 [ 66 [
99 '$/progress', 67 '$/progress',
100 params => watchStatus.handleProgressNotification(params), 68 params => watchStatus.handleProgressNotification(params),
101 ], 69 ],
102 ]; 70 ];
103 const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); 71 const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
104 const expandMacroContentProvider = new ExpandMacroContentProvider(); 72 const expandMacroContentProvider = new ExpandMacroContentProvider();
105 73
diff --git a/editors/code/src/commands/apply_source_change.ts b/editors/code/src/source_change.ts
index 8167398b1..a4f9068b2 100644
--- a/editors/code/src/commands/apply_source_change.ts
+++ b/editors/code/src/source_change.ts
@@ -1,7 +1,7 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
3 3
4import { Server } from '../server'; 4import { Ctx } from './ctx';
5 5
6export interface SourceChange { 6export interface SourceChange {
7 label: string; 7 label: string;
@@ -9,8 +9,8 @@ export interface SourceChange {
9 cursorPosition?: lc.TextDocumentPositionParams; 9 cursorPosition?: lc.TextDocumentPositionParams;
10} 10}
11 11
12export async function handle(change: SourceChange) { 12export async function applySourceChange(ctx: Ctx, change: SourceChange) {
13 const wsEdit = Server.client.protocol2CodeConverter.asWorkspaceEdit( 13 const wsEdit = ctx.client.protocol2CodeConverter.asWorkspaceEdit(
14 change.workspaceEdit, 14 change.workspaceEdit,
15 ); 15 );
16 let created; 16 let created;
@@ -32,10 +32,10 @@ export async function handle(change: SourceChange) {
32 const doc = await vscode.workspace.openTextDocument(toOpenUri); 32 const doc = await vscode.workspace.openTextDocument(toOpenUri);
33 await vscode.window.showTextDocument(doc); 33 await vscode.window.showTextDocument(doc);
34 } else if (toReveal) { 34 } else if (toReveal) {
35 const uri = Server.client.protocol2CodeConverter.asUri( 35 const uri = ctx.client.protocol2CodeConverter.asUri(
36 toReveal.textDocument.uri, 36 toReveal.textDocument.uri,
37 ); 37 );
38 const position = Server.client.protocol2CodeConverter.asPosition( 38 const position = ctx.client.protocol2CodeConverter.asPosition(
39 toReveal.position, 39 toReveal.position,
40 ); 40 );
41 const editor = vscode.window.activeTextEditor; 41 const editor = vscode.window.activeTextEditor;