2013-11-15 64 views
7

我想知道是否可以在用戶在編輯器ace.js中輸入時啓用自動完成功能。 目前在我的項目中,當用戶鍵入:ctrl +空格時啓用自動完成功能。 然後,是否可以在自動完成列表中添加一些關鍵字?在ace.js編輯器中自動完成

感謝

回答

15

對於觸發自動完成use

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

對於addidng一些關鍵詞,你可以另completer添加到編輯或覆蓋的模式getCompletions方法。

+0

謝謝!它在用戶輸入時顯示自動完成,但當自動完成選項中只有一個匹配時,它也會自動替換輸入的單詞。有一種避免這種情況的方法? – Proghero

+2

@Proghero你有沒有找到你的問題的答案?我有同樣的問題。 –

+1

最近的ace版本已經內置支持這個看到https://github.com/ajaxorg/ace/blob/4599dc6/lib/ace/ext/language_tools.js#L150 –

3

它已經內置!請參閱我在editor.setOptions下選擇的選項:

var langTools = ace.require("ace/ext/language_tools"); 
    var editor = ace.edit("editor"); 

    editor.setTheme("ace/theme/monokai"); 
    editor.getSession().setMode("ace/mode/yaml"); 

    editor.setOptions({ 
     enableBasicAutocompletion: true, 
     enableSnippets: true, 
     enableLiveAutocompletion: true 
    });