Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对象的ACE编辑器自动完成_Javascript_Reactjs_Ace Editor - Fatal编程技术网

Javascript 对象的ACE编辑器自动完成

Javascript 对象的ACE编辑器自动完成,javascript,reactjs,ace-editor,Javascript,Reactjs,Ace Editor,假设我有一个对象foo,它有两个键bar,baz。我想创建一个自定义的getCompletions,这样当我键入foo时。然后它显示了bar和baz。我该怎么做?回调中的prefix变量只包含最后一个按下的键 在我的真实示例中,在执行此操作之前,我需要进行AJAX调用以获取foo的键,这就是为什么我需要自定义自动完成 您可以绑定密钥,而不是在getCompletions中使用前缀。然后建立你的词表。您可以将您的单词列表设置为全局,并在getCompletions内部使用,或者在绑定后使用。使用此

假设我有一个对象foo,它有两个键bar,baz。我想创建一个自定义的getCompletions,这样当我键入foo时。然后它显示了bar和baz。我该怎么做?回调中的prefix变量只包含最后一个按下的键


在我的真实示例中,在执行此操作之前,我需要进行AJAX调用以获取foo的键,这就是为什么我需要自定义自动完成

您可以绑定密钥,而不是在getCompletions中使用前缀。然后建立你的词表。您可以将您的单词列表设置为全局,并在getCompletions内部使用,或者在绑定后使用。使用此代码获取before项ie foo,然后将该值插入编辑器

例如,如果我们将wordList作为一个全局数组,那么当用户输入foo时。我们可以将这两个单词都添加到单词列表中,然后在自动补全器中使用

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 (bar, baz) to the global word list so that the autocomplete can show both bar, baz
           // Append the words to wordList here
        }
    }
});
在自动补全器中,我们可以映射单词列表

var staticWordCompleter = {
getCompletions: function(editor, session, pos, prefix, callback) {

    callback(null, wordList.map(function(word) {
        return {
            caption: word,
            value: word,
            meta: "static",
            completer: {
                insertMatch: function(editor, data) {
                    var insertedValue = data.value;
                    if(insertedValue === "bar" || insertedValue === "baz") {
                        // You can clear the world list here
                    }
                }
            }
        };
    }));
  }
}
editor.completers = [staticWordCompleter];
为了始终使用自动补全,而不是等待用户键入下一个字符来调用自动补全列表,您可以在初始化期间使用以下代码段

editor.commands.on("afterExec", function (e) {
    if (e.command.name == "insertstring" && /^[\w.]$/.test(e.args)) {
        editor.execCommand("startAutocomplete");
    }
});

当用户输入foo时,是否要用bar、baz显示自动完成?@Harshapps是。我还启用了LiveAutoCompletion,所以每当用户按下键时,autocomplete就会激活。我如何删除单词列表?好像一旦我到了福。然后永远吧,baz会出现在单词列表中。我是否需要不断更新wordList并删除自动完成选项??您可以在完成程序中使用insertMatch选项,一旦您从自动完成列表中单击工具栏或baz,就会调用该选项,然后您可以清除该列表。通过添加InsertMatchThank更新了答案。这非常有效。最后一个问题。无论如何,要真正在上自动打开建议框。按现在,您仍然需要键入下一个单词才能打开“自动完成”菜单。我想在福。一旦按下菜单打开upI我很高兴解决方案成功了:没有选项调用自动补全器实际上,它在用户实际键入内容时被调用。好吧,这总比什么都没有好。另外,我注意到,在使用您的解决方案时,我实际上无法选择完成建议,即,如果我键入foo.b,然后使用键盘向下滚动到bar并按enter键,则不会发生任何事情;建议栏刚刚关闭