Skip to content

Commit

Permalink
Merge pull request #4485 from YAL-Forks/master
Browse files Browse the repository at this point in the history
Immediate-return optimization for Autocomplete.updateCompletions (#4482)
  • Loading branch information
nightwing authored May 26, 2021
2 parents bfde34b + d945f5f commit 9992db9
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions lib/ace/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,22 +293,16 @@ var Autocomplete = function() {

// Save current gatherCompletions session, session is close when a match is insert
var _id = this.gatherCompletionsId;
this.gatherCompletions(this.editor, function(err, results) {
// Only detach if result gathering is finished
var detachIfFinished = function() {
if (!results.finished) return;
return this.detach();
}.bind(this);

var prefix = results.prefix;
var matches = results && results.matches;
// Only detach if result gathering is finished
var detachIfFinished = function(results) {
if (!results.finished) return;
return this.detach();
}.bind(this);

if (!matches || !matches.length)
return detachIfFinished();

// Wrong prefix or wrong session -> ignore
if (prefix.indexOf(results.prefix) !== 0 || _id != this.gatherCompletionsId)
return;
var processResults = function(results) {
var prefix = results.prefix;
var matches = results.matches;

this.completions = new FilteredList(matches);

Expand All @@ -320,18 +314,47 @@ var Autocomplete = function() {

// No results
if (!filtered.length)
return detachIfFinished();
return detachIfFinished(results);

// One result equals to the prefix
if (filtered.length == 1 && filtered[0].value == prefix && !filtered[0].snippet)
return detachIfFinished();
return detachIfFinished(results);

// Autoinsert if one result
if (this.autoInsert && filtered.length == 1 && results.finished)
return this.insertMatch(filtered[0]);

this.openPopup(this.editor, prefix, keepPopupPosition);
}.bind(this);

var isImmediate = true;
var immediateResults = null;
this.gatherCompletions(this.editor, function(err, results) {
var prefix = results.prefix;
var matches = results && results.matches;

if (!matches || !matches.length)
return detachIfFinished(results);

// Wrong prefix or wrong session -> ignore
if (prefix.indexOf(results.prefix) !== 0 || _id != this.gatherCompletionsId)
return;

// If multiple completers return their results immediately, we want to process them together
if (isImmediate) {
immediateResults = results;
return;
}

processResults(results);
}.bind(this));

isImmediate = false;
if (immediateResults) {
var results = immediateResults;
immediateResults = null;
processResults(results);
}
};

this.cancelContextMenu = function() {
Expand Down

0 comments on commit 9992db9

Please sign in to comment.