Skip to content

Commit c266a7b

Browse files
committed
Fix autocomplete prefix for custom identifierRegexes
Hi, Ace team! My team has been using a fork of Ace, and I'm hoping to merge some of our changes upstream so that we can return to using your official releases. The engineer who originally accomplished this work has long since dparted my team, so forgive me for this somewhat rote transcription of his working notes. > This was sent upstream in #2352, > but closed because fcebd0f > seemed to address the same issue. That fix unfortunately does not > work as we need, and fails the behavior described in our test named > `test leading @ not duplicated on autocomplete` > > The root cause: Autocomplete prefix can be wrong for completions with > a custom identifierRegex because `Autocompleter.base` is computed > relative to a prefix computed for the default identifierRegex. Below, I reproduce the test my colleague referred to. ``` 'test leading @ not duplicated on autocomplete': function (test) { editor.setValue(''); editor.navigateFileStart(); var text = 'view: users { derived_table: { sql: @{;; } }'; exec('insertstring', 1, text); editor.moveCursorTo(0, 38); editor.getValue(); attachedSymbolsToSession(text, editor.session, { const: 'value' }); editor.execCommand('startAutocomplete'); var completion = editor.completer.completions.filtered.filter(function(completion) { return completion.caption === '@{const}'; }); test.ok(completion[0]); editor.completer.insertMatch(completion[0]); test.equal(editor.getValue(), 'view: users { derived_table: { sql: @{const};; } }'); test.done(); }, ```
1 parent ff3dd69 commit c266a7b

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/autocomplete.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ var Autocomplete = function() {
190190
"PageDown": function(editor) { editor.completer.popup.gotoPageDown(); }
191191
};
192192

193+
this.getPrefix = function(session, pos, prefixRegex) {
194+
var line = session.getLine(pos.row);
195+
var prefix = util.retrievePrecedingIdentifier(line, pos.column, prefixRegex);
196+
197+
this.base = session.doc.createAnchor(pos.row, pos.column - prefix.length);
198+
this.base.$insertRight = true;
199+
return prefix;
200+
}
201+
193202
this.gatherCompletions = function(editor, callback) {
194203
var session = editor.getSession();
195204
var pos = editor.getCursorPosition();
@@ -206,13 +215,14 @@ var Autocomplete = function() {
206215
if (!err && results)
207216
matches = matches.concat(results);
208217
// Fetch prefix again, because they may have changed by now
218+
var prefix = this.getPrefix(session, pos, results[0] && results[0].identifierRegex);
209219
callback(null, {
210-
prefix: util.getCompletionPrefix(editor),
220+
prefix: prefix,
211221
matches: matches,
212222
finished: (--total === 0)
213223
});
214-
});
215-
});
224+
}.bind(this));
225+
}, this);
216226
return true;
217227
};
218228

0 commit comments

Comments
 (0)