From 23ef22dd4880606c4c3dc908d30c2cbeabc37f58 Mon Sep 17 00:00:00 2001 From: Gregoire Geis Date: Sun, 2 Feb 2020 02:21:04 +0100 Subject: Add regular onEnter command, allowing onEnter to be called without overriding the type command. --- editors/code/package.json | 5 ++++ editors/code/src/commands/on_enter.ts | 51 ++++++++++++++++++++++------------- editors/code/src/main.ts | 3 ++- 3 files changed, 40 insertions(+), 19 deletions(-) (limited to 'editors/code') diff --git a/editors/code/package.json b/editors/code/package.json index c0d8f0183..9987a28f5 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -114,6 +114,11 @@ "command": "rust-analyzer.reload", "title": "Restart server", "category": "Rust Analyzer" + }, + { + "command": "rust-analyzer.onEnter", + "title": "Enhanced enter key", + "category": "Rust Analyzer" } ], "keybindings": [ diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 6f61883cd..1b3d3d741 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -1,28 +1,43 @@ +import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; import { applySourceChange, SourceChange } from '../source_change'; import { Cmd, Ctx } from '../ctx'; -export function onEnter(ctx: Ctx): Cmd { +async function handleKeypress(ctx: Ctx) { + const editor = ctx.activeRustEditor; + const client = ctx.client; + if (!editor) return false; + if (!client) return false; + + const request: lc.TextDocumentPositionParams = { + textDocument: { uri: editor.document.uri.toString() }, + position: client.code2ProtocolConverter.asPosition( + editor.selection.active, + ), + }; + const change = await client.sendRequest( + 'rust-analyzer/onEnter', + request, + ); + if (!change) return false; + + await applySourceChange(ctx, change); + return true; +} + +export function onEnterOverride(ctx: Ctx): Cmd { return async (event: { text: string }) => { - const editor = ctx.activeRustEditor; - const client = ctx.client; - if (!editor || event.text !== '\n') return false; - if (!client) return false; + if (event.text === '\n') { + handleKeypress(ctx); + } + }; +} - const request: lc.TextDocumentPositionParams = { - textDocument: { uri: editor.document.uri.toString() }, - position: client.code2ProtocolConverter.asPosition( - editor.selection.active, - ), - }; - const change = await client.sendRequest( - 'rust-analyzer/onEnter', - request, - ); - if (!change) return false; +export function onEnter(ctx: Ctx): Cmd { + return async () => { + if (handleKeypress(ctx)) return; - await applySourceChange(ctx, change); - return true; + await vscode.commands.executeCommand('default:type', { text: '\n' }); }; } diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 6813c3c4c..5c061e72f 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -21,6 +21,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('expandMacro', commands.expandMacro); ctx.registerCommand('run', commands.run); ctx.registerCommand('reload', commands.reload); + ctx.registerCommand('onEnter', commands.onEnter); // Internal commands which are invoked by the server. ctx.registerCommand('runSingle', commands.runSingle); @@ -29,7 +30,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); if (ctx.config.enableEnhancedTyping) { - ctx.overrideCommand('type', commands.onEnter); + ctx.overrideCommand('type', commands.onEnterOverride); } activateStatusDisplay(ctx); -- cgit v1.2.3 From 58c007674b8c746beab371ca13fce951af6cd1f2 Mon Sep 17 00:00:00 2001 From: Gregoire Geis Date: Sun, 2 Feb 2020 15:58:53 +0100 Subject: Change default enhanced typing behavior from using type to using keybindings. --- editors/code/package.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'editors/code') diff --git a/editors/code/package.json b/editors/code/package.json index 9987a28f5..05f55cb35 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -141,6 +141,11 @@ "command": "rust-analyzer.run", "key": "ctrl+r", "when": "editorTextFocus && editorLangId == rust" + }, + { + "command": "rust-analyzer.onEnter", + "key": "enter", + "when": "editorTextFocus && editorLangId == rust" } ], "configuration": { @@ -164,7 +169,7 @@ }, "rust-analyzer.enableEnhancedTyping": { "type": "boolean", - "default": true, + "default": false, "description": "Enables enhanced typing. NOTE: If using a VIM extension, you should set this to false" }, "rust-analyzer.raLspServerPath": { -- cgit v1.2.3 From b70ad7e5f3d524204fab88fe2a8c5a6fbef9e88e Mon Sep 17 00:00:00 2001 From: Gregoire Geis Date: Mon, 3 Feb 2020 20:24:50 +0100 Subject: Remove enableEnhancedTyping and type overriding infrastructure. --- editors/code/package.json | 5 ----- editors/code/src/commands/on_enter.ts | 10 +--------- editors/code/src/ctx.ts | 24 ------------------------ editors/code/src/main.ts | 3 --- 4 files changed, 1 insertion(+), 41 deletions(-) (limited to 'editors/code') diff --git a/editors/code/package.json b/editors/code/package.json index 05f55cb35..421124764 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -167,11 +167,6 @@ "default": {}, "description": "Fine grained feature flags to disable annoying features" }, - "rust-analyzer.enableEnhancedTyping": { - "type": "boolean", - "default": false, - "description": "Enables enhanced typing. NOTE: If using a VIM extension, you should set this to false" - }, "rust-analyzer.raLspServerPath": { "type": [ "string" diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 1b3d3d741..ac582b423 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -26,17 +26,9 @@ async function handleKeypress(ctx: Ctx) { return true; } -export function onEnterOverride(ctx: Ctx): Cmd { - return async (event: { text: string }) => { - if (event.text === '\n') { - handleKeypress(ctx); - } - }; -} - export function onEnter(ctx: Ctx): Cmd { return async () => { - if (handleKeypress(ctx)) return; + if (await handleKeypress(ctx)) return; await vscode.commands.executeCommand('default:type', { text: '\n' }); }; diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 05d21ae56..aa75943bf 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -50,30 +50,6 @@ export class Ctx { this.pushCleanup(d); } - overrideCommand(name: string, factory: (ctx: Ctx) => Cmd) { - const defaultCmd = `default:${name}`; - const override = factory(this); - const original = (...args: unknown[]) => - vscode.commands.executeCommand(defaultCmd, ...args); - try { - const d = vscode.commands.registerCommand( - name, - async (...args: unknown[]) => { - if (!(await override(...args))) { - return await original(...args); - } - }, - ); - this.pushCleanup(d); - } catch (_) { - vscode.window.showWarningMessage( - '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', - ); - } - } - get subscriptions(): Disposable[] { return this.extCtx.subscriptions; } diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 5c061e72f..efc31b2e2 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -29,9 +29,6 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('applySourceChange', commands.applySourceChange); ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); - if (ctx.config.enableEnhancedTyping) { - ctx.overrideCommand('type', commands.onEnterOverride); - } activateStatusDisplay(ctx); activateHighlighting(ctx); -- cgit v1.2.3 From 7fd661f0853ef3a32bfb8a01617de8e5adb3ca01 Mon Sep 17 00:00:00 2001 From: Gregoire Geis Date: Mon, 3 Feb 2020 22:26:20 +0100 Subject: vscode: Only handle enter if the suggest widget is hidden. --- editors/code/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors/code') diff --git a/editors/code/package.json b/editors/code/package.json index 421124764..c9404a4dd 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -145,7 +145,7 @@ { "command": "rust-analyzer.onEnter", "key": "enter", - "when": "editorTextFocus && editorLangId == rust" + "when": "editorTextFocus && !suggestWidgetVisible && editorLangId == rust" } ], "configuration": { -- cgit v1.2.3 From 875dc6d1a4973f70cd48b797ae755d1bd7a83fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Geis?= Date: Tue, 4 Feb 2020 01:44:12 +0100 Subject: Merge two if statements into one in editors/code/src/commands/on_enter.ts. Co-Authored-By: Veetaha --- editors/code/src/commands/on_enter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index ac582b423..c636234da 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -8,7 +8,7 @@ async function handleKeypress(ctx: Ctx) { const editor = ctx.activeRustEditor; const client = ctx.client; if (!editor) return false; - if (!client) return false; + if (!editor || !client) return false; const request: lc.TextDocumentPositionParams = { textDocument: { uri: editor.document.uri.toString() }, -- cgit v1.2.3