]> git.lizzy.rs Git - rust.git/blobdiff - editors/code/src/commands/on_enter.ts
Add boolean literals to package.json
[rust.git] / editors / code / src / commands / on_enter.ts
index 16dcb70c813ed36fa7f5e8500d5e3b4bad6b9160..a7871c31eed0bde9d209833eb2e9ff82b06df673 100644 (file)
@@ -1,33 +1,35 @@
 import * as vscode from 'vscode';
-import * as lc from 'vscode-languageclient';
-import { Server } from '../server';
-import {
-    handle as applySourceChange,
-    SourceChange
-} from './apply_source_change';
+import * as ra from '../rust-analyzer-api';
 
-export async function handle(event: { text: string }): Promise<boolean> {
-    const editor = vscode.window.activeTextEditor;
-    if (
-        editor == null ||
-        editor.document.languageId !== 'rust' ||
-        event.text !== '\n'
-    ) {
-        return false;
-    }
-    const request: lc.TextDocumentPositionParams = {
+import { Cmd, Ctx } from '../ctx';
+import { applySnippetWorkspaceEdit } from '.';
+
+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: Server.client.code2ProtocolConverter.asPosition(
-            editor.selection.active
-        )
-    };
-    const change = await Server.client.sendRequest<undefined | SourceChange>(
-        'rust-analyzer/onEnter',
-        request
-    );
-    if (!change) {
-        return false;
-    }
-    await applySourceChange(change);
+        position: client.code2ProtocolConverter.asPosition(
+            editor.selection.active,
+        ),
+    }).catch(_error => {
+        // client.logFailedRequest(OnEnterRequest.type, error);
+        return null;
+    });
+    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 vscode.commands.executeCommand('default:type', { text: '\n' });
+    };
+}