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

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