Files
rust/editors/code/src/commands/on_enter.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
2019-12-30 19:07:28 +01:00
2019-12-30 18:31:08 +01:00
import { applySourceChange, SourceChange } from '../source_change';
2019-12-30 16:43:34 +01:00
import { Cmd, Ctx } from '../ctx';
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 {
2019-12-30 16:43:34 +01:00
return async (event: { text: string }) => {
if (event.text === '\n') {
handleKeypress(ctx);
}
};
}
2019-12-30 16:43:34 +01:00
export function onEnter(ctx: Ctx): Cmd {
return async () => {
if (handleKeypress(ctx)) return;
2019-12-30 16:43:34 +01:00
await vscode.commands.executeCommand('default:type', { text: '\n' });
2019-12-30 18:31:08 +01:00
};
}