我有一个由摩纳哥编辑建造的编辑器。对于某些文本,它返回某些代码操作和快速修复。
然而,快捷的Command + .
在Mac和Chrome中不起作用;Ctrl + .
在Windows和Chrome中也不工作(我使用法语键盘)。(这是由其他人提供的quickFix的工作示例,其中的快捷方式也不起作用。)
有人知道原因是什么吗?
然后我试图覆盖快捷键。我在index.tsx
中有以下代码
handleEditorDidMount(editor, monaco) {
// I tried to follow `updateKeyBinding` of https://github.com/microsoft/monaco-editor/issues/102#issuecomment-822981429
const id = "editor.action.quickFix"
const newKeyBinding = monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyP
const { handler, when } = CommandsRegistry.getCommand(id) ?? {}
editor._standaloneKeybindingService.addDynamicKeybinding(id, newKeyBinding, handler)
//@ts-ignore
editor._standaloneKeybindingService.addDynamicKeybinding('write-command' , monaco.KeyMod.Alt | monaco.KeyMod.Shift | monaco.KeyCode.KeyS , this.writeShortcutKey.bind(this));
}
writeShortcutKey
可以由Alt+Shift+S调用,而quickFix不能被Command+P调用
有人知道给editor.action.quickFix
分配快捷方式的正确方法吗?
(*链接到Github:https://github.com/microsoft/monaco-editor/issues/3055 *)
发布于 2022-04-07 18:20:43
键盘快捷键对我来说工作正常,在摩纳哥游乐场中使用摩纳哥游乐场。需要注意的一点是,当您加载摩纳哥游乐场、粘贴代码片段并按> Run
时,右侧的编辑器没有焦点(尽管在编辑器中的错误下划线上盘旋会弹出标记)。要使键盘快捷键工作,您将需要放置光标,以便编辑器有焦点,光标正在闪烁。
如果单击下划线文本所在的位置(第1行,第3列),然后按键盘快捷方式,它将显示快速修复列表(在本例中是一个名为My quickfix
的建议)。
如果光标没有定位在错误的文本上(例如光标位于第1行的末尾),按下快捷方式将导致显示消息No code actions available
。
如果您确实希望重新绑定任何现有快捷方式,可以使用摩纳哥游乐场中的以下代码来解除现有键盘快捷方式的绑定并绑定新的快捷键:
monaco.languages.register({ id: 'myLanguage' });
monaco.editor.onDidCreateModel(function(model) {
function validate() {
var textToValidate = model.getValue();
var markers = [{
severity: monaco.MarkerSeverity.Error,
startLineNumber: 1,
startColumn: 3,
endLineNumber: 1,
endColumn: 5,
message: 'Lets correct it'
}];
monaco.editor.setModelMarkers(model, 'myLanguage', markers);
}
var handle = null;
model.onDidChangeContent(() => {
// debounce
clearTimeout(handle);
handle = setTimeout(() => validate(), 500);
});
validate();
});
monaco.languages.registerCodeActionProvider("myLanguage", {
provideCodeActions: (model, range, context, token) => {
const actions = context.markers.map(error => {
return {
title: `My quickfix`, // Name of quickfix
diagnostics: [error],
kind: "quickfix",
edit: {
edits: [
{
resource: model.uri,
edit: {
range: error,
text: "replacement text" // text to replace with
}
}
]
},
isPreferred: true
};
});
return {
actions: actions,
dispose: () => {}
}
}
});
var ed = monaco.editor.create(document.getElementById("container"), {
value: "cont foo = 1;",
language: "myLanguage",
lightbulb: { enabled: false },
});
/**
* Code to rebind the keyboard shortcut to quickFix
*/
// Get access to CommandsRegistry
const CommandsRegistry = require('vs/platform/commands/common/commands').CommandsRegistry;
// Get the handler and when properties for editor.action.quickFix
const { handler, when } = CommandsRegistry.getCommand("editor.action.quickFix") ?? {};
// Unbind the current keyboard shortcut
ed._standaloneKeybindingService.addDynamicKeybinding("-editor.action.quickFix", undefined, () => {});
// Add the new keyboard shortcut (bound to CtrlCmd+P)
const newKeyBinding = monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyP;
ed._standaloneKeybindingService.addDynamicKeybinding("editor.action.quickFix", newKeyBinding, handler, when);
这一行代码将解除当前快捷方式的绑定:
ed._standaloneKeybindingService.addDynamicKeybinding("-editor.action.quickFix", undefined, () => {});
这些行将新的行绑定到Ctrl+P
const newKeyBinding = monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyP;
ed._standaloneKeybindingService.addDynamicKeybinding("editor.action.quickFix", newKeyBinding, handler, when);
正如您在问题中发布的这句话链接中提到的那样,该解决方案使用非官方API (因为似乎还没有公共API)。
https://stackoverflow.com/questions/71734607
复制相似问题