Merge #2981
2981: vscode: Add ability to call onEnter without overriding "type". r=matklad a=71 Before this PR, the only way to get enhanced typing (right now, only with `onEnter`) was to override VS Code's `type` command. This leads to issues with extensions like [VsCodeVim](https://github.com/VSCodeVim/Vim) that need to override `type` as well. This PR adds an additional command, `onEnter`. This command can be used with the following keybinding, which allows the user to get smart `onEnter` behavior without overriding `type`. ```json { "key": "enter", "command": "rust-analyzer.onEnter", "when": "editorTextFocus && editorLangId == rust" } ``` Co-authored-by: Gregoire Geis <git@gregoirege.is> Co-authored-by: Grégoire Geis <git@gregoirege.is>
This commit is contained in:
commit
c1a06499fa
@ -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": [
|
||||
@ -136,6 +141,11 @@
|
||||
"command": "rust-analyzer.run",
|
||||
"key": "ctrl+r",
|
||||
"when": "editorTextFocus && editorLangId == rust"
|
||||
},
|
||||
{
|
||||
"command": "rust-analyzer.onEnter",
|
||||
"key": "enter",
|
||||
"when": "editorTextFocus && !suggestWidgetVisible && editorLangId == rust"
|
||||
}
|
||||
],
|
||||
"configuration": {
|
||||
@ -157,11 +167,6 @@
|
||||
"default": {},
|
||||
"description": "Fine grained feature flags to disable annoying features"
|
||||
},
|
||||
"rust-analyzer.enableEnhancedTyping": {
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"description": "Enables enhanced typing. NOTE: If using a VIM extension, you should set this to false"
|
||||
},
|
||||
"rust-analyzer.raLspServerPath": {
|
||||
"type": [
|
||||
"string"
|
||||
|
@ -1,28 +1,35 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as lc from 'vscode-languageclient';
|
||||
|
||||
import { applySourceChange, SourceChange } from '../source_change';
|
||||
import { Cmd, Ctx } from '../ctx';
|
||||
|
||||
async function handleKeypress(ctx: Ctx) {
|
||||
const editor = ctx.activeRustEditor;
|
||||
const client = ctx.client;
|
||||
if (!editor) return false;
|
||||
if (!editor || !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<undefined | SourceChange>(
|
||||
'rust-analyzer/onEnter',
|
||||
request,
|
||||
);
|
||||
if (!change) return false;
|
||||
|
||||
await applySourceChange(ctx, change);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function onEnter(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;
|
||||
return async () => {
|
||||
if (await handleKeypress(ctx)) return;
|
||||
|
||||
const request: lc.TextDocumentPositionParams = {
|
||||
textDocument: { uri: editor.document.uri.toString() },
|
||||
position: client.code2ProtocolConverter.asPosition(
|
||||
editor.selection.active,
|
||||
),
|
||||
};
|
||||
const change = await client.sendRequest<undefined | SourceChange>(
|
||||
'rust-analyzer/onEnter',
|
||||
request,
|
||||
);
|
||||
if (!change) return false;
|
||||
|
||||
await applySourceChange(ctx, change);
|
||||
return true;
|
||||
await vscode.commands.executeCommand('default:type', { text: '\n' });
|
||||
};
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
@ -28,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.onEnter);
|
||||
}
|
||||
activateStatusDisplay(ctx);
|
||||
|
||||
activateHighlighting(ctx);
|
||||
|
Loading…
Reference in New Issue
Block a user