【问题标题】:Autocompletion for Ace EditorAce 编辑器的自动完成功能
【发布时间】:2013-02-22 08:11:31
【问题描述】:

好的,这就是交易:

  • 我正在使用Ace Editor
  • 集成编辑器的应用,用Objective-C/Cocoa编写
  • 我需要自动补全(对于给定的一组关键字)

现在,问题来了:

  • 我知道 AutoCompletion 尚未得到原生支持
  • 我知道其他人的一些尝试(例如Codiad IDEGherkinAlloy-UI),一些使用Jquery UI Autocomplete - 但我仍然无法弄清楚如何将其适应现有的 Ace 设置
  • 我仍然不确定我是应该选择面向 JS 的解决方案还是只使用 Objective-C/Cocoa 来解决这个问题

任何帮助都将不胜感激。

【问题讨论】:

标签: javascript objective-c cocoa autocomplete ace-editor


【解决方案1】:

可以在ace编辑器中实现自动补全..

代码:

    var editor = ace.edit('editor');
    editor.setTheme("ace/theme/eclipse");
    editor.getSession().setMode("ace/mode/java");
    editor.setShowInvisibles(true);
    editor.setDisplayIndentGuides(true);
    editor.getSession().setUseWrapMode(true);    
    var jsonUrl = "JSON/Components/proce.json";
    //the url where the json file with the suggestions is present
    var langTools = ace.require("ace/ext/language_tools");
    editor.setOptions({enableBasicAutocompletion: true});
    var rhymeCompleter = {
        getCompletions: function(editor, session, pos, prefix, callback) {
            if (prefix.length === 0) { callback(null, []); return }
            $.getJSON(jsonUrl, function(wordList) {
                callback(null, wordList.map(function(ea)  {           
                    return {name: ea.word, value: ea.word, meta: "optional text"}
                }));
            })
        }
    }

    langTools.addCompleter(rhymeCompleter);

Json 文件格式:

   [ {"word":"hello"}, 
   {"word":"good morning"},
   {"word":"suggestions"},
   {"word":"auto suggest"},
   {"word":"try this"}]

参考/演示:

http://plnkr.co/edit/6MVntVmXYUbjR0DI82Cr?p=preview

【讨论】:

    【解决方案2】:

    现在要在 Ace 中添加实时自动完成功能: 在您的 HTML 中包含 ace/ext-language_tools.js。 这 。 call 还不能很好地工作,因此您可能必须为此输入 ctrl-space 或 alt-space,但现在将显示标准语法内容,例如写入函数。 那么:

    var editor = ace.edit("editor");
    
    ace.require("ace/ext/language_tools");
    editor.setOptions({
        enableBasicAutocompletion: true,
        enableSnippets: true,
        enableLiveAutocompletion: true
    });
    

    【讨论】:

    • 嵌入应用程序包装器时对我不起作用。我不得不使用editor.setOptions("enableBasicAutocompletion", true);。我认为这是应用程序包装代理接口中的错误,而不是 Ace 中的错误。
    【解决方案3】:

    自动完成的难点在于找出关键字,其余的很容易做到。

    1. 您需要一个弹出窗口和 listView 来显示完成,它可能 最好使用基于 Cocoa 的弹出窗口。
    2. 一些过滤功能,简单的startsWith检查就可以了,但是你可以使用更好的弹性匹配 像崇高的
    3. 对 editor.session.replace 的简单调用以插入 选择完成

    对于 2-3,您应该在 https://github.com/ajaxorg/ace/issues/110 评论您的特定用例,因为有一项工作可以获得对 AutoCompletion 的本机支持。

    【讨论】:

      猜你喜欢
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2015-07-14
      相关资源
      最近更新 更多