2020-02-02 02:21:04 +01:00
|
|
|
import * as vscode from 'vscode';
|
2020-02-25 00:55:48 +02:00
|
|
|
import * as ra from '../rust-analyzer-api';
|
2019-12-30 19:07:28 +01:00
|
|
|
|
2020-02-25 00:55:48 +02:00
|
|
|
import { applySourceChange } from '../source_change';
|
2019-12-30 16:43:34 +01:00
|
|
|
import { Cmd, Ctx } from '../ctx';
|
2018-10-09 16:00:20 +03:00
|
|
|
|
2020-02-02 02:21:04 +01:00
|
|
|
async function handleKeypress(ctx: Ctx) {
|
|
|
|
|
const editor = ctx.activeRustEditor;
|
|
|
|
|
const client = ctx.client;
|
2020-02-05 00:13:46 +02:00
|
|
|
|
2020-02-04 01:44:12 +01:00
|
|
|
if (!editor || !client) return false;
|
2020-02-02 02:21:04 +01:00
|
|
|
|
2020-02-25 00:55:48 +02:00
|
|
|
const change = await client.sendRequest(ra.onEnter, {
|
2020-02-02 02:21:04 +01:00
|
|
|
textDocument: { uri: editor.document.uri.toString() },
|
|
|
|
|
position: client.code2ProtocolConverter.asPosition(
|
|
|
|
|
editor.selection.active,
|
|
|
|
|
),
|
2020-02-25 00:55:48 +02:00
|
|
|
}).catch(_error => {
|
|
|
|
|
// client.logFailedRequest(OnEnterRequest.type, error);
|
|
|
|
|
return null;
|
|
|
|
|
});
|
2020-02-02 02:21:04 +01:00
|
|
|
if (!change) return false;
|
|
|
|
|
|
|
|
|
|
await applySourceChange(ctx, change);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function onEnter(ctx: Ctx): Cmd {
|
|
|
|
|
return async () => {
|
2020-02-03 20:24:50 +01:00
|
|
|
if (await handleKeypress(ctx)) return;
|
2019-12-30 16:43:34 +01:00
|
|
|
|
2020-02-02 02:21:04 +01:00
|
|
|
await vscode.commands.executeCommand('default:type', { text: '\n' });
|
2019-12-30 18:31:08 +01:00
|
|
|
};
|
2018-10-09 16:00:20 +03:00
|
|
|
}
|