前端代碼編輯器ace 語法提示 代碼提示

本文主要是介紹ace編輯器的語法提示,自動完成。其實沒什麼可特別介紹的,善始善終吧,把項目中使用到的ace的功能都介紹下。html

{
    enableBasicAutocompletion: false, //boolea 或 completer數組,
    enableLiveAutocompletion: false, //boolean 或 completer數組,
    enableSnippets: false, // boolean
}

  completer,就是擁有一個getCompletions(editor, session, pos, prefix, callback)方法的objectgit

  相關的配置邏輯,能夠看下源代碼https://github.com/ajaxorg/ace/blob/v1.1.4/lib/ace/ext/language_tools.jsgithub

  若是enableBasicAutocompletion, enableLiveAutocompletion的值爲數組,就會覆蓋編輯器默認的completers,不推薦使用。ajax

  enableBasicAutocompletion 數組

  設置enableBasicAutocompletion = true,就會增長Autocomplete.startCommand命令。可是快捷鍵默認是以下配置,和如今的輸入法存在衝突。session

startCommand.bindKey = "Ctrl-Space|Ctrl-Shift-Space|Alt-Space"

  enableLiveAutocompletion 編輯器

  設置enableLiveAutocompletion = true,就會在輸入內容時,彈出語法提示框,可是邏輯代碼中忽略了一些狀況,如刪除。
  因此若是交互要求變更就彈出提示的話,能夠editor綁定change事件,觸發命令ui

editor.on("change", function(e){
  editor.execCommand("startAutocomplete");
})

  項目中還有可能,變量在別的地方預設的,也但願能自動完成,須要經過language_tools,增長自定義的completerspa

var langTools = ace.acequire("ace/ext/language_tools");
langTools.addCompleter({
    getCompletions: function(editor, session, pos, prefix, callback) {
        console.log(editor, session, pos, prefix, callback);
        if (prefix.length === 0) { callback(null, []); return }
        callback(null, [{
            name: word, //顯示的名稱,‘獎金’
            value: word, //插入的值,‘100’
            score: 1000, //分數,越大的排在越上面
            meta: type //描述,‘個人常量’
        }]);
    }
});                

  enableSnippets 調試

  設置enableSnippets = true;啓用代碼塊提示的功能。
  若是是給本身新增的mode增長snippets,參照下面的文件配置下。

ace.define("ace/snippets/modeName",["require","exports","module"],function(e,t,n){"use strict";t.snippetText=undefined,t.scope="modeName"})

  snippets配置說明: https://cloud9-sdk.readme.io/docs/snippets
  snippets在線調試:https://ace.c9.io/build/kitchen-sink.html

  經過[TAB]在定義的${1},${2},${3}。。。跳轉,最後回到${0},詳細的就參照《snippets配置說明》配置,提到的Triggers & Guards,試了幾種狀況,也沒搞明白具體有什麼限制。

相關文章
相關標籤/搜索