首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过键盘快捷键调用quickFix

通过键盘快捷键调用quickFix
EN

Stack Overflow用户
提问于 2022-04-04 09:06:23
回答 1查看 807关注 0票数 2

我有一个由摩纳哥编辑建造的编辑器。对于某些文本,它返回某些代码操作和快速修复。

然而,快捷的Command + .在Mac和Chrome中不起作用;Ctrl + .在Windows和Chrome中也不工作(我使用法语键盘)。(是由其他人提供的quickFix的工作示例,其中的快捷方式也不起作用。)

有人知道原因是什么吗?

然后我试图覆盖快捷键。我在index.tsx中有以下代码

代码语言:javascript
运行
复制
  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 *)

EN

回答 1

Stack Overflow用户

发布于 2022-04-07 18:20:43

键盘快捷键对我来说工作正常,在摩纳哥游乐场中使用摩纳哥游乐场。需要注意的一点是,当您加载摩纳哥游乐场、粘贴代码片段并按> Run时,右侧的编辑器没有焦点(尽管在编辑器中的错误下划线上盘旋会弹出标记)。要使键盘快捷键工作,您将需要放置光标,以便编辑器有焦点,光标正在闪烁。

如果单击下划线文本所在的位置(第1行,第3列),然后按键盘快捷方式,它将显示快速修复列表(在本例中是一个名为My quickfix的建议)。

如果光标没有定位在错误的文本上(例如光标位于第1行的末尾),按下快捷方式将导致显示消息No code actions available

如果您确实希望重新绑定任何现有快捷方式,可以使用摩纳哥游乐场中的以下代码来解除现有键盘快捷方式的绑定并绑定新的快捷键:

代码语言:javascript
运行
复制
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);

这一行代码将解除当前快捷方式的绑定:

代码语言:javascript
运行
复制
ed._standaloneKeybindingService.addDynamicKeybinding("-editor.action.quickFix", undefined, () => {});

这些行将新的行绑定到Ctrl+P

代码语言:javascript
运行
复制
const newKeyBinding = monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyP;
ed._standaloneKeybindingService.addDynamicKeybinding("editor.action.quickFix", newKeyBinding, handler, when);

正如您在问题中发布的这句话链接中提到的那样,该解决方案使用非官方API (因为似乎还没有公共API)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71734607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档