mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 13:48:50 +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",
|
"command": "rust-analyzer.reload",
|
||||||
"title": "Restart server",
|
"title": "Restart server",
|
||||||
"category": "Rust Analyzer"
|
"category": "Rust Analyzer"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "rust-analyzer.onEnter",
|
||||||
|
"title": "Enhanced enter key",
|
||||||
|
"category": "Rust Analyzer"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"keybindings": [
|
"keybindings": [
|
||||||
|
|
|
@ -1,28 +1,43 @@
|
||||||
|
import * as vscode from 'vscode';
|
||||||
import * as lc from 'vscode-languageclient';
|
import * as lc from 'vscode-languageclient';
|
||||||
|
|
||||||
import { applySourceChange, SourceChange } from '../source_change';
|
import { applySourceChange, SourceChange } from '../source_change';
|
||||||
import { Cmd, Ctx } from '../ctx';
|
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 }) => {
|
return async (event: { text: string }) => {
|
||||||
const editor = ctx.activeRustEditor;
|
if (event.text === '\n') {
|
||||||
const client = ctx.client;
|
handleKeypress(ctx);
|
||||||
if (!editor || event.text !== '\n') return false;
|
}
|
||||||
if (!client) return false;
|
};
|
||||||
|
}
|
||||||
const request: lc.TextDocumentPositionParams = {
|
|
||||||
textDocument: { uri: editor.document.uri.toString() },
|
export function onEnter(ctx: Ctx): Cmd {
|
||||||
position: client.code2ProtocolConverter.asPosition(
|
return async () => {
|
||||||
editor.selection.active,
|
if (handleKeypress(ctx)) return;
|
||||||
),
|
|
||||||
};
|
await vscode.commands.executeCommand('default:type', { text: '\n' });
|
||||||
const change = await client.sendRequest<undefined | SourceChange>(
|
|
||||||
'rust-analyzer/onEnter',
|
|
||||||
request,
|
|
||||||
);
|
|
||||||
if (!change) return false;
|
|
||||||
|
|
||||||
await applySourceChange(ctx, change);
|
|
||||||
return true;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
ctx.registerCommand('expandMacro', commands.expandMacro);
|
ctx.registerCommand('expandMacro', commands.expandMacro);
|
||||||
ctx.registerCommand('run', commands.run);
|
ctx.registerCommand('run', commands.run);
|
||||||
ctx.registerCommand('reload', commands.reload);
|
ctx.registerCommand('reload', commands.reload);
|
||||||
|
ctx.registerCommand('onEnter', commands.onEnter);
|
||||||
|
|
||||||
// Internal commands which are invoked by the server.
|
// Internal commands which are invoked by the server.
|
||||||
ctx.registerCommand('runSingle', commands.runSingle);
|
ctx.registerCommand('runSingle', commands.runSingle);
|
||||||
|
@ -29,7 +30,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);
|
ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);
|
||||||
|
|
||||||
if (ctx.config.enableEnhancedTyping) {
|
if (ctx.config.enableEnhancedTyping) {
|
||||||
ctx.overrideCommand('type', commands.onEnter);
|
ctx.overrideCommand('type', commands.onEnterOverride);
|
||||||
}
|
}
|
||||||
activateStatusDisplay(ctx);
|
activateStatusDisplay(ctx);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue