• MonacoEditor 主动触发代码提示功能


    MonacoEditor是微软提供的代码编辑器

    vscode即是使用它作为编辑器。

    它的开发语言是ts,可以嵌入到浏览器中。

    代码提示或者说代码补全功能是我们经常需要定制的部分。

    目前它提供的快捷键是ctrl+space,和win10以下的操作系统的默认中英文切换是冲突的。

    检查源码发现,TriggerSuggestAction的触发快捷键已经写死:

            function TriggerSuggestAction() {
                return _super.call(this, {
                    id: 'editor.action.triggerSuggest',
                    label: nls.localize(0, null),
                    alias: 'Trigger Suggest',
                    precondition: contextkey_1.ContextKeyExpr.and(editorCommon_1.EditorContextKeys.Writable, editorCommon_1.ModeContextKeys.hasCompletionItemProvider),
                    kbOpts: {
                        kbExpr: editorCommon_1.EditorContextKeys.TextFocus,
                        primary: 2048 /* CtrlCmd */ | 10 /* Space */,
                        mac: { primary: 256 /* WinCtrl */ | 10 /* Space */ }
                    }
                }) || this;
            }

    既然没法改快捷键,它的run方法实现如下:

            TriggerSuggestAction.prototype.run = function (accessor, editor) {
                SuggestController.get(editor).triggerSuggest();
            };

    即是说,只要有办法调用这个triggerSuggest即可,但是SuggestControll而是个私有对象,要如何调用呢?

    继续看源码:

            SuggestController.get = function (editor) {
                return editor.getContribution(SuggestController_1.ID);
            };
            SuggestController.prototype.getId = function () {
                return SuggestController_1.ID;
            };
    
        SuggestController.ID = 'editor.contrib.suggestController';
        SuggestController = SuggestController_1 

    可以得出结论

    editor.getContribution('editor.contrib.suggestController').triggerSuggest

    这个就是我们所需要的调用代码。

    当然,还有一种更推荐的形式:

    editor.trigger('随便写点儿啥', 'editor.action.triggerSuggest', {});
  • 相关阅读:
    Redis分布式锁服务(转)
    redis分布式锁(转)
    MySQL+InnoDB semi-consitent read原理及实现分析(转)
    MySQL加锁处理分析(转)
    实战经验丨PHP反序列化漏洞总结
    脚本语言丨Batch入门教程第四章:调用与传参
    福利狂欢已开启,请做好准备!
    脚本语言丨Batch入门教程第三章:逻辑判断
    WinRAR存在严重的安全漏洞影响5亿用户
    Batch入门教程丨第二章:认识变量相关概念
  • 原文地址:https://www.cnblogs.com/anrainie/p/7808296.html
Copyright © 2020-2023  润新知