I am experimenting with CM’s autocomplete feature. I am having an issue in such a scenario: when a dot (’.’) is typed after an object, I trigger the autocomplete process which calls ‘someurl’ to return the completion results from the server, in async mode; then call the callback to pass the data to CM once the request returns. I expect the hint widget (the list of completion results) to show up immediately. However, the hint widget doesn’t show, until I enter some letter (like ‘a’).
My code is like below:
CodeMirror.registerHelper('hint', 'ajax', (mirror, callback) => {
var cur = mirror.getCursor();
var range = mirror.findWordAt(cur);
callServer('someurl', function() {
callback({
list: ['a', 'b'],
from: range.anchor,
to: range.head
});
});
});
CodeMirror.hint.ajax.async = true;
CodeMirror.commands.autocomplete = function(mirror) {
mirror.showHint({
hint: CodeMirror.hint.ajax
});
};
myCodeMirror.on("keyup", function (cm, event) {
if (!cm.state.completionActive && /*Enables keyboard navigation in autocomplete list*/
event.keyCode == 190) { // when key is .
CodeMirror.commands.autocomplete(cm, null, {completeSingle: true});
}
});
The issue seems to have something to do with the aync part of code – if I return {list,from,to} in CodeMirror.hint.ajax sync and comment out this line:
CodeMirror.hint.ajax.async = true;
the hint widget containing ‘a’ and ‘b’ shows up immediately.
How can I have the hint widget show up immediately for async results?
Thanks!