Add regular onEnter command, allowing onEnter to be called without overriding the type command.

This commit is contained in:
Gregoire Geis 2020-02-02 02:21:04 +01:00
parent b090ee5a65
commit 23ef22dd48
3 changed files with 42 additions and 21 deletions

View file

@ -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": [

View file

@ -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<undefined | SourceChange>(
'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;
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;
if (event.text === '\n') {
handleKeypress(ctx);
}
};
}
export function onEnter(ctx: Ctx): Cmd {
return async () => {
if (handleKeypress(ctx)) return;
await vscode.commands.executeCommand('default:type', { text: '\n' });
};
}

View file

@ -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);