ace编辑器中的自定义自动完成功能在“”之后不起作用。 [英] Custom autocomplete in ace-editor does not work after "."

查看:452
本文介绍了ace编辑器中的自定义自动完成功能在“”之后不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ace编辑器中使用自动完成功能。在用户键入 foo。之后,我建议使用 foo.bar

I want to use autocomplete in the ace editor. After the user types foo. I want to suggest foo.bar.

实际上我使用了以下代码:

Actually I used the following code:

var langTools = ace.require("ace/ext/language_tools");

var staticWordCompleter = {
    identifierRegexps: [/[\.]/],
    getCompletions: function(editor, session, pos, prefix, callback) {
        console.log(prefix);
        if (prefix == "foo.") {
            var wordList = ["baar", "bar", "baz"];
            callback(null, wordList.map(function(word) {
                return {
                    caption: word,
                    value: word,
                    meta: "static"
                };
        }
        }));

    }
}

langTools.setCompleters([staticWordCompleter])

如果删除 identifierRegexps if 子句,则自动完成功能有效,但之后。。

If I remove identifierRegexps and the if clause, the autocomplete works but not after ".".

我也阅读了此解决方案,但是它不再起作用:自定义自动完成程序和句点(。)

I also read this solution but it does not work anymore: Custom autocompleter and periods (.)

推荐答案

您可以绑定。然后建立您的wordList。您可以将wordList设为全局,并在 getCompletions 中使用,或在绑定之后使用。使用此代码获取前一项(即foo),然后将值插入编辑器。

You can bind the "." and then build your wordList. You can make your wordList global and use in the getCompletions or once you bind the "." use this code to get the before item ie foo, and then insert the value into the editor.

    self.editor.commands.addCommand({
        name: "dotCommand1",
        bindKey: { win: ".", mac: "." },
        exec: function () {
            var pos = editor.selection.getCursor();
            var session = editor.session;

            var curLine = (session.getDocument().getLine(pos.row)).trim();
            var curTokens = curLine.slice(0, pos.column).split(/\s+/);
            var curCmd = curTokens[0];
            if (!curCmd) return;
            var lastToken = curTokens[curTokens.length - 1];

            editor.insert(".");                

            if (lastToken === "foo") {
                // Add your words to the list or then insert into the editor using editor.insert()
            }
        }
   });

这篇关于ace编辑器中的自定义自动完成功能在“”之后不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆