ace-editor中的自定义自动完成功能在"之后不起作用。"

时间:2018-05-21 15:31:10

标签: javascript ace-editor

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

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

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])

如果我删除identifierRegexpsif子句,则自动完成功能有效但不会在"。"。

之后。

我也阅读了这个解决方案,但它不再起作用了:Custom autocompleter and periods (.)

1 个答案:

答案 0 :(得分:1)

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

    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()
            }
        }
   });