javascript - Values and captions in Ace Editor autocomplete -
i using ace editor v.1.1.8 ext-language_tools. want achieve following behavior auto-complete:
user starts typing, presses ctrl+space , show him list of found captions, when selects 1 of them value inserted.
my code:
var completions = [ {id: 'id1', 'value': 'value1'}, {id: 'id2', 'value': 'value2'} ]; var autocompleter = { getcompletions: function(editor, session, pos, prefix, callback) { if (prefix.length === 0) { callback(null, []); return; } callback( null, completions.map(function(c) { return {value: c.id, caption: c.value}; }) ); } }; editor.setoptions({ enablebasicautocompletion: [autocompleter], enableliveautocompletion: false, enablesnippets: false });
so need above user enters 'val', sees list 'value1' , 'value2', selects 1 of them , 'id1' or 'id2' inserted editor.
at point:
- editor searches value (and need search caption)
- if set 'value' = c.value, editor search correctly insert c.value while need c.id inserted.
here's working code: http://plnkr.co/edit/8zazalpzvhochmi4gwhd?p=preview
upd:
was able achieve behavior adding insertmatch method data:
completions.map(function(c) { return { value: c.name, meta: c.id, completer: { insertmatch: function(editor, data) { if (editor.completer.completions.filtertext) { var ranges = editor.selection.getallranges(); (var = 0, range; range = ranges[i]; i++) { range.start.column -= editor.completer.completions.filtertext.length; editor.session.remove(range); } } editor.execcommand("insertstring", data.meta); } } }; })
you can change
var caption = item.value || item.caption || item.snippet;
line @ https://github.com/ajaxorg/ace/blob/v1.1.8/lib/ace/autocomplete.js#l449
var caption = item.caption || item.value || item.snippet;
you can implement custom insertmatch method on completer https://github.com/ajaxorg/ace/blob/v1.1.8/lib/ace/autocomplete.js#l181, making pull request first option better
Comments
Post a Comment