Improve autocomplete support for async completers
This commit is contained in:
parent
110eb26f96
commit
09b9348852
1 changed files with 30 additions and 11 deletions
|
|
@ -60,6 +60,7 @@ var Autocomplete = function() {
|
||||||
this.insertMatch();
|
this.insertMatch();
|
||||||
e.stop();
|
e.stop();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
this.gatherCompletionsId = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.openPopup = function(editor, prefix, keepPopupPosition) {
|
this.openPopup = function(editor, prefix, keepPopupPosition) {
|
||||||
|
|
@ -146,6 +147,8 @@ var Autocomplete = function() {
|
||||||
data = this.popup.getData(this.popup.getRow());
|
data = this.popup.getData(this.popup.getRow());
|
||||||
if (!data)
|
if (!data)
|
||||||
return false;
|
return false;
|
||||||
|
this.gatherCompletionsId = this.gatherCompletionsId + 1;
|
||||||
|
|
||||||
if (data.completer && data.completer.insertMatch) {
|
if (data.completer && data.completer.insertMatch) {
|
||||||
data.completer.insertMatch(this.editor);
|
data.completer.insertMatch(this.editor);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -191,16 +194,14 @@ var Autocomplete = function() {
|
||||||
this.base.column -= prefix.length;
|
this.base.column -= prefix.length;
|
||||||
|
|
||||||
var matches = [];
|
var matches = [];
|
||||||
util.parForEach(editor.completers, function(completer, next) {
|
editor.completers.forEach(function(completer) {
|
||||||
completer.getCompletions(editor, session, pos, prefix, function(err, results) {
|
completer.getCompletions(editor, session, pos, prefix, function(err, results) {
|
||||||
if (!err)
|
if (!err)
|
||||||
matches = matches.concat(results);
|
matches = matches.concat(results);
|
||||||
next();
|
callback(null, {
|
||||||
});
|
prefix: prefix,
|
||||||
}, function() {
|
matches: matches
|
||||||
callback(null, {
|
});
|
||||||
prefix: prefix,
|
|
||||||
matches: matches
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -229,6 +230,7 @@ var Autocomplete = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.updateCompletions = function(keepPopupPosition) {
|
this.updateCompletions = function(keepPopupPosition) {
|
||||||
|
var that = this;
|
||||||
if (keepPopupPosition && this.base && this.completions) {
|
if (keepPopupPosition && this.base && this.completions) {
|
||||||
var pos = this.editor.getCursorPosition();
|
var pos = this.editor.getCursorPosition();
|
||||||
var prefix = this.editor.session.getTextRange({start: this.base, end: pos});
|
var prefix = this.editor.session.getTextRange({start: this.base, end: pos});
|
||||||
|
|
@ -240,22 +242,39 @@ var Autocomplete = function() {
|
||||||
this.openPopup(this.editor, prefix, keepPopupPosition);
|
this.openPopup(this.editor, prefix, keepPopupPosition);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save current gatherCompletions session, session is close when a match is insert
|
||||||
|
var _id = this.gatherCompletionsId;
|
||||||
this.gatherCompletions(this.editor, function(err, results) {
|
this.gatherCompletions(this.editor, function(err, results) {
|
||||||
|
// Calcul prefix
|
||||||
|
var session = that.editor.getSession();
|
||||||
|
var pos = that.editor.getCursorPosition();
|
||||||
|
var line = session.getLine(pos.row);
|
||||||
|
var prefix = util.retrievePrecedingIdentifier(line, pos.column);
|
||||||
|
|
||||||
|
// Results matches
|
||||||
var matches = results && results.matches;
|
var matches = results && results.matches;
|
||||||
if (!matches || !matches.length)
|
|
||||||
return this.detach();
|
|
||||||
// TODO reenable this when we have proper change tracking
|
// TODO reenable this when we have proper change tracking
|
||||||
// if (matches.length == 1)
|
// if (matches.length == 1)
|
||||||
// return this.insertMatch(matches[0]);
|
// return this.insertMatch(matches[0]);
|
||||||
|
|
||||||
|
// No prefix or no results -> close
|
||||||
|
if (!prefix || !prefix.length || !matches || !matches.length)
|
||||||
|
return this.detach();
|
||||||
|
|
||||||
|
// Wrong prefx or wrong session -> ignore
|
||||||
|
if (prefix.indexOf(results.prefix) != 0
|
||||||
|
|| _id != this.gatherCompletionsId)
|
||||||
|
return;
|
||||||
|
|
||||||
this.completions = new FilteredList(matches);
|
this.completions = new FilteredList(matches);
|
||||||
this.completions.setFilter(results.prefix);
|
this.completions.setFilter(prefix);
|
||||||
var filtered = this.completions.filtered;
|
var filtered = this.completions.filtered;
|
||||||
if (!filtered.length)
|
if (!filtered.length)
|
||||||
return this.detach();
|
return this.detach();
|
||||||
if (this.autoInsert && filtered.length == 1)
|
if (this.autoInsert && filtered.length == 1)
|
||||||
return this.insertMatch(filtered[0]);
|
return this.insertMatch(filtered[0]);
|
||||||
this.openPopup(this.editor, results.prefix, keepPopupPosition);
|
this.openPopup(this.editor, prefix, keepPopupPosition);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue