mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 15:14:32 +00:00
Add regular onEnter command, allowing onEnter to be called without overriding the type command.
This commit is contained in:
parent
b090ee5a65
commit
23ef22dd48
3 changed files with 42 additions and 21 deletions
|
@ -114,6 +114,11 @@
|
|||
"command": "rust-analyzer.reload",
|
||||
"title": "Restart server",
|
||||
"category": "Rust Analyzer"
|
||||
},
|
||||
{
|
||||
"command": "rust-analyzer.onEnter",
|
||||
"title": "Enhanced enter key",
|
||||
"category": "Rust Analyzer"
|
||||
}
|
||||
],
|
||||
"keybindings": [
|
||||
|
|
|
@ -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' });
|
||||
};
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
|||
ctx.registerCommand('expandMacro', commands.expandMacro);
|
||||
ctx.registerCommand('run', commands.run);
|
||||
ctx.registerCommand('reload', commands.reload);
|
||||
ctx.registerCommand('onEnter', commands.onEnter);
|
||||
|
||||
// Internal commands which are invoked by the server.
|
||||
ctx.registerCommand('runSingle', commands.runSingle);
|
||||
|
@ -29,7 +30,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
|||
ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);
|
||||
|
||||
if (ctx.config.enableEnhancedTyping) {
|
||||
ctx.overrideCommand('type', commands.onEnter);
|
||||
ctx.overrideCommand('type', commands.onEnterOverride);
|
||||
}
|
||||
activateStatusDisplay(ctx);
|
||||
|
||||
|
|
Loading…
Reference in a new issue