Ace编辑器自动完成功能

8

好的,这是问题:

  • 我正在使用Ace Editor
  • 编辑器集成在Objective-C/Cocoa编写的应用程序中
  • 我需要自动完成(针对一组关键字)

现在,问题来了:

  • 我知道自动完成尚未得到本地支持
  • 我知道其他人有一些尝试(例如Codiad IDEGherkinAlloy-UI),其中一些利用了Jquery UI Autocomplete - 但我仍然无法弄清楚如何将其适配到现有的Ace设置中
  • 我仍然不确定是否应该选择JS为导向的解决方案还是只使用Objective-C/Cocoa

任何帮助都将不胜感激。


可能是ACE编辑器中的自动完成的重复问题。 - endorama
3个回答

19

在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


5
要在Ace中添加实时自动完成功能: 在你的HTML中引入ace/ext-language_tools.js。 目前“.”调用还不够完善,所以你可能需要按下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中的错误。 - 1.21 gigawatts

1
自动完成的难点在于确定关键词,其余部分很容易实现。
  1. 您需要一个弹出窗口和listView来显示完成内容,最好使用基于Cocoa的弹出窗口。
  2. 一些过滤函数,可以使用简单的startsWith检查,但也可以使用更好的flex匹配,如sublime。
  3. 通过调用editor.session.replace来插入选择的完成。
对于2-3,您应该在https://github.com/ajaxorg/ace/issues/110中针对您特定的用例进行评论,因为有工作正在进行以获得原生支持AutoCompletion。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接