我正在尝试为 VSCode 编写一个扩展,其中编辑器屏幕始终以光标为中心。还有其他扩展程序可以添加将屏幕居中放置在光标上的命令,但您必须按下该命令才能激活它。
目前我发现实现这一点的唯一方法是重写 cursorUp、cursorDown、enter、pageUp、pageDown——任何基本上向上和向下移动光标的命令,然后使用带有光标行位置的“revealLine”命令并将“at”属性设为“center”。
有没有更好的办法?重新实现默认的编辑器命令似乎非常低效。
这是我目前所拥有的:
"use strict";
import * as vscode from "vscode";
export function activate(context: vscode.ExtensionContext) {
let disposable1 = vscode.commands.registerCommand("cursorUp",() => {
centralizar();
vscode.commands.executeCommand("cursorMove", {
to: "up",
});
}
);
let disposable2 = vscode.commands.registerCommand("cursorDown",() => {
centralizar();
vscode.commands.executeCommand("cursorMove", {
to: "down",
});
}
);
context.subscriptions.push(disposable1);
context.subscriptions.push(disposable2);
}
function centralizar() {
let currentLineNumber = vscode.window.activeTextEditor.selection.start.line;
vscode.commands.executeCommand("revealLine", {
lineNumber: currentLineNumber,
at: "center"
});
}
export function deactivate() {}
Run Code Online (Sandbox Code Playgroud)