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‘, {});

 

文章来自:http://www.cnblogs.com/anrainie/p/7808296.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3