Ace 编辑器自动完成

5 autocomplete ace-editor

我正在 sql 模式下使用 ace 编辑器。我点击此链接启用自动完成:https://github.com/ajaxorg/ace/blob/master/demo/autocompletion.html。一般情况下效果很好。但是,我想稍微调整一下自动完成功能,以满足我的进一步要求。这是愿望清单:

  1. 我希望建议的sql关键字都是大写的。默认都是小写;
  2. 我发现当我输入一些单词时,我以前输入的单词会添加到建议词典中,这很好。即使在编辑器中输入任何内容之前,我是否可以以编程方式在建议词典中添加更多单词。我需要这个的原因是我想将一些表名和字段名预加载到字典中。

我是这个很棒的编辑器的新手。我希望获得有关如何调整自动完成功能的一些指导。谢谢。

a u*_*ser 3

有一个拉取请求为 sql server 模式添加更好的完成https://github.com/ajaxorg/ace/pull/2460,sql 模式可以以相同的方式处理。

要添加更多单词,您需要实现一个自定义完成器,这很简单:

<!DOCTYPE html>
<html>
<head>  
  <script src="http://ajaxorg.github.io/ace-builds/src/ace.js">
  </script> 
  <script src="http://ajaxorg.github.io/ace-builds/src/ext-language_tools.js">
  </script> 
  <style>
    #editor { position: absolute; top: 0; left: 0; right: 0; bottom: 0;}
  </style>
</head>
<body>
 <div id="editor">
press ctrl+space</div> 
</body>
<script>
  editor = ace.edit("editor")
  editor.setOptions({
    // mode: "ace/mode/javascript",
    enableBasicAutocompletion: true
  });
  editor.completers.push({
    getCompletions: function(editor, session, pos, prefix, callback) {
      callback(null, [
        {value: "foo", score: 1000, meta: "custom"},
        {value: "bar", score: 1000, meta: "custom"}
      ]);
    }
  })
</script>
</html>
Run Code Online (Sandbox Code Playgroud)

另请参阅https://github.com/ajaxorg/ace/wiki/How-to-enable-Autocomplete-in-the-Ace-editor