aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-10-09 14:00:20 +0100
committerAleksey Kladov <[email protected]>2018-10-09 17:52:06 +0100
commit2b956fd3a83313cee37ff179eae843bc88dd572a (patch)
treea1010728cfc8018e5f62152f4c45b9523f8d5e25 /editors/code/src
parent82447ecacef9129a44d3c17b3db7a0e60a7ec92b (diff)
Add on-enter handler
Now, typing doc comments is much more pleasant
Diffstat (limited to 'editors/code/src')
-rw-r--r--editors/code/src/commands/index.ts4
-rw-r--r--editors/code/src/commands/on_enter.ts29
-rw-r--r--editors/code/src/extension.ts20
3 files changed, 51 insertions, 2 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 2496c7ff8..d78a64c3e 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -2,6 +2,7 @@ import * as applySourceChange from './apply_source_change';
2import * as extendSelection from './extend_selection'; 2import * as extendSelection from './extend_selection';
3import * as joinLines from './join_lines'; 3import * as joinLines from './join_lines';
4import * as matchingBrace from './matching_brace'; 4import * as matchingBrace from './matching_brace';
5import * as on_enter from './on_enter';
5import * as parentModule from './parent_module'; 6import * as parentModule from './parent_module';
6import * as runnables from './runnables'; 7import * as runnables from './runnables';
7import * as syntaxTree from './syntaxTree'; 8import * as syntaxTree from './syntaxTree';
@@ -13,5 +14,6 @@ export {
13 matchingBrace, 14 matchingBrace,
14 parentModule, 15 parentModule,
15 runnables, 16 runnables,
16 syntaxTree 17 syntaxTree,
18 on_enter,
17}; 19};
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts
new file mode 100644
index 000000000..2666797fe
--- /dev/null
+++ b/editors/code/src/commands/on_enter.ts
@@ -0,0 +1,29 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient';
3import { Server } from '../server';
4import { handle as applySourceChange, SourceChange } from './apply_source_change';
5
6interface OnEnterParams {
7 textDocument: lc.TextDocumentIdentifier;
8 position: lc.Position;
9}
10
11export async function handle(event: { text: string }): Promise<boolean> {
12 const editor = vscode.window.activeTextEditor;
13 if (editor == null || editor.document.languageId !== 'rust' || event.text !== '\n') {
14 return false;
15 }
16 const request: OnEnterParams = {
17 textDocument: { uri: editor.document.uri.toString() },
18 position: Server.client.code2ProtocolConverter.asPosition(editor.selection.active),
19 };
20 const change = await Server.client.sendRequest<undefined | SourceChange>(
21 'm/onEnter',
22 request
23 );
24 if (!change) {
25 return false;
26 }
27 await applySourceChange(change);
28 return true
29}
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts
index 81e1107a0..3e5767535 100644
--- a/editors/code/src/extension.ts
+++ b/editors/code/src/extension.ts
@@ -15,6 +15,23 @@ export function activate(context: vscode.ExtensionContext) {
15 function registerCommand(name: string, f: any) { 15 function registerCommand(name: string, f: any) {
16 disposeOnDeactivation(vscode.commands.registerCommand(name, f)); 16 disposeOnDeactivation(vscode.commands.registerCommand(name, f));
17 } 17 }
18 function overrideCommand(
19
20 name: string,
21 f: (...args: any[]) => Promise<boolean>,
22 ) {
23 const defaultCmd = `default:${name}`;
24 const original = async (...args: any[]) => await vscode.commands.executeCommand(defaultCmd, ...args);
25 registerCommand(name, async (...args: any[]) => {
26 const editor = vscode.window.activeTextEditor;
27 if (!editor || !editor.document || editor.document.languageId !== 'rust') {
28 return await original(...args);
29 }
30 if (!await f(...args)) {
31 return await original(...args);
32 }
33 })
34 }
18 35
19 // Commands are requests from vscode to the language server 36 // Commands are requests from vscode to the language server
20 registerCommand('ra-lsp.syntaxTree', commands.syntaxTree.handle); 37 registerCommand('ra-lsp.syntaxTree', commands.syntaxTree.handle);
@@ -27,11 +44,12 @@ export function activate(context: vscode.ExtensionContext) {
27 'ra-lsp.applySourceChange', 44 'ra-lsp.applySourceChange',
28 commands.applySourceChange.handle 45 commands.applySourceChange.handle
29 ); 46 );
47 overrideCommand('type', commands.on_enter.handle)
30 48
31 // Notifications are events triggered by the language server 49 // Notifications are events triggered by the language server
32 const allNotifications: Iterable< 50 const allNotifications: Iterable<
33 [string, lc.GenericNotificationHandler] 51 [string, lc.GenericNotificationHandler]
34 > = [['m/publishDecorations', notifications.publishDecorations.handle]]; 52 > = [['m/publishDecorations', notifications.publishDecorations.handle]];
35 53
36 // The events below are plain old javascript events, triggered and handled by vscode 54 // The events below are plain old javascript events, triggered and handled by vscode
37 vscode.window.onDidChangeActiveTextEditor( 55 vscode.window.onDidChangeActiveTextEditor(