-import * as lc from 'vscode-languageclient';
+import * as vscode from 'vscode';
+import * as ra from '../rust-analyzer-api';
-import { applySourceChange, SourceChange } from '../source_change';
import { Cmd, Ctx } from '../ctx';
+import { applySnippetWorkspaceEdit } from '.';
-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;
+async function handleKeypress(ctx: Ctx) {
+ const editor = ctx.activeRustEditor;
+ const client = ctx.client;
+
+ if (!editor || !client) return false;
+
+ const change = await client.sendRequest(ra.onEnter, {
+ textDocument: { uri: editor.document.uri.toString() },
+ position: client.code2ProtocolConverter.asPosition(
+ editor.selection.active,
+ ),
+ }).catch(_error => {
+ // client.logFailedRequest(OnEnterRequest.type, error);
+ return null;
+ });
+ if (!change) 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;
+ const workspaceEdit = client.protocol2CodeConverter.asWorkspaceEdit(change);
+ await applySnippetWorkspaceEdit(workspaceEdit);
+ return true;
+}
+
+export function onEnter(ctx: Ctx): Cmd {
+ return async () => {
+ if (await handleKeypress(ctx)) return;
- await applySourceChange(ctx, change);
- return true;
+ await vscode.commands.executeCommand('default:type', { text: '\n' });
};
}