diff options
| author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-09 16:52:48 +0000 |
|---|---|---|
| committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-09 16:52:48 +0000 |
| commit | 31c8ebb743572ef07ac4ca77ddd17eddbcf4b24c (patch) | |
| tree | 7a817485cf24ffe6e2acbec607115569bf3d8ac8 /editors/code/src | |
| parent | 14baf11bd41eb17cfee79fd7f9d068c4e785aa71 (diff) | |
| parent | 2b956fd3a83313cee37ff179eae843bc88dd572a (diff) | |
| download | rust-31c8ebb743572ef07ac4ca77ddd17eddbcf4b24c.tar.gz rust-31c8ebb743572ef07ac4ca77ddd17eddbcf4b24c.zip | |
Merge #106
106: Add on-enter handler r=matklad a=matklad Now, typing doc comments is much more pleasant Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
Diffstat (limited to 'editors/code/src')
| -rw-r--r-- | editors/code/src/commands/index.ts | 4 | ||||
| -rw-r--r-- | editors/code/src/commands/on_enter.ts | 29 | ||||
| -rw-r--r-- | editors/code/src/extension.ts | 20 |
3 files changed, 51 insertions, 2 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 2496c7ff8c6..d78a64c3e43 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'; import * as extendSelection from './extend_selection'; import * as joinLines from './join_lines'; import * as matchingBrace from './matching_brace'; +import * as on_enter from './on_enter'; import * as parentModule from './parent_module'; import * as runnables from './runnables'; import * as syntaxTree from './syntaxTree'; @@ -13,5 +14,6 @@ export { matchingBrace, parentModule, runnables, - syntaxTree + syntaxTree, + on_enter, }; diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts new file mode 100644 index 00000000000..2666797fe71 --- /dev/null +++ b/editors/code/src/commands/on_enter.ts @@ -0,0 +1,29 @@ +import * as vscode from 'vscode'; +import * as lc from 'vscode-languageclient'; +import { Server } from '../server'; +import { handle as applySourceChange, SourceChange } from './apply_source_change'; + +interface OnEnterParams { + textDocument: lc.TextDocumentIdentifier; + position: lc.Position; +} + +export async function handle(event: { text: string }): Promise<boolean> { + const editor = vscode.window.activeTextEditor; + if (editor == null || editor.document.languageId !== 'rust' || event.text !== '\n') { + return false; + } + const request: OnEnterParams = { + textDocument: { uri: editor.document.uri.toString() }, + position: Server.client.code2ProtocolConverter.asPosition(editor.selection.active), + }; + const change = await Server.client.sendRequest<undefined | SourceChange>( + 'm/onEnter', + request + ); + if (!change) { + return false; + } + await applySourceChange(change); + return true +} diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts index 81e1107a045..3e576753552 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts @@ -15,6 +15,23 @@ export function activate(context: vscode.ExtensionContext) { function registerCommand(name: string, f: any) { disposeOnDeactivation(vscode.commands.registerCommand(name, f)); } + function overrideCommand( + + name: string, + f: (...args: any[]) => Promise<boolean>, + ) { + const defaultCmd = `default:${name}`; + const original = async (...args: any[]) => await vscode.commands.executeCommand(defaultCmd, ...args); + registerCommand(name, async (...args: any[]) => { + const editor = vscode.window.activeTextEditor; + if (!editor || !editor.document || editor.document.languageId !== 'rust') { + return await original(...args); + } + if (!await f(...args)) { + return await original(...args); + } + }) + } // Commands are requests from vscode to the language server registerCommand('ra-lsp.syntaxTree', commands.syntaxTree.handle); @@ -27,11 +44,12 @@ export function activate(context: vscode.ExtensionContext) { 'ra-lsp.applySourceChange', commands.applySourceChange.handle ); + overrideCommand('type', commands.on_enter.handle) // Notifications are events triggered by the language server const allNotifications: Iterable< [string, lc.GenericNotificationHandler] - > = [['m/publishDecorations', notifications.publishDecorations.handle]]; + > = [['m/publishDecorations', notifications.publishDecorations.handle]]; // The events below are plain old javascript events, triggered and handled by vscode vscode.window.onDidChangeActiveTextEditor( |
