Skip to content

Commit fcebd0f

Browse files
author
Sherry Zhou
committed
Fix custom identifierRegexps (issue #2532)
- Add util.getCompletionPrefix and use it instead of util.retrievePrecedingIdentifier in gatherCompletions and doLiveAutocomplete functions as suggested at #2532
1 parent 5635526 commit fcebd0f

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

lib/ace/autocomplete.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ var Autocomplete = function() {
226226
var pos = editor.getCursorPosition();
227227

228228
var line = session.getLine(pos.row);
229-
var prefix = util.retrievePrecedingIdentifier(line, pos.column);
229+
var prefix = util.getCompletionPrefix(editor);
230230

231231
this.base = session.doc.createAnchor(pos.row, pos.column - prefix.length);
232232
this.base.$insertRight = true;
@@ -241,7 +241,7 @@ var Autocomplete = function() {
241241
var pos = editor.getCursorPosition();
242242
var line = session.getLine(pos.row);
243243
callback(null, {
244-
prefix: util.retrievePrecedingIdentifier(line, pos.column, results && results[0] && results[0].identifierRegex),
244+
prefix: prefix,
245245
matches: matches,
246246
finished: (--total === 0)
247247
});

lib/ace/autocomplete/util.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,19 @@ exports.retrieveFollowingIdentifier = function(text, pos, regex) {
7171
return buf;
7272
};
7373

74+
exports.getCompletionPrefix = function (editor) {
75+
var pos = editor.getCursorPosition();
76+
var line = editor.session.getLine(pos.row);
77+
var prefix;
78+
editor.completers.forEach(function(completer) {
79+
if (completer.identifierRegexps) {
80+
completer.identifierRegexps.forEach(function(identifierRegex) {
81+
if (!prefix && identifierRegex)
82+
prefix = this.retrievePrecedingIdentifier(line, pos.column, identifierRegex);
83+
}.bind(this));
84+
}
85+
}.bind(this));
86+
return prefix || this.retrievePrecedingIdentifier(line, pos.column);
87+
};
88+
7489
});

lib/ace/ext/language_tools.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,17 @@ var loadSnippetFile = function(id) {
137137
});
138138
};
139139

140-
function getCompletionPrefix(editor) {
141-
var pos = editor.getCursorPosition();
142-
var line = editor.session.getLine(pos.row);
143-
var prefix;
144-
// Try to find custom prefixes on the completers
145-
editor.completers.forEach(function(completer) {
146-
if (completer.identifierRegexps) {
147-
completer.identifierRegexps.forEach(function(identifierRegex) {
148-
if (!prefix && identifierRegex)
149-
prefix = util.retrievePrecedingIdentifier(line, pos.column, identifierRegex);
150-
});
151-
}
152-
});
153-
return prefix || util.retrievePrecedingIdentifier(line, pos.column);
154-
}
155-
156140
var doLiveAutocomplete = function(e) {
157141
var editor = e.editor;
158142
var hasCompleter = editor.completer && editor.completer.activated;
159143

160144
// We don't want to autocomplete with no prefix
161145
if (e.command.name === "backspace") {
162-
if (hasCompleter && !getCompletionPrefix(editor))
146+
if (hasCompleter && !util.getCompletionPrefix(editor))
163147
editor.completer.detach();
164148
}
165149
else if (e.command.name === "insertstring") {
166-
var prefix = getCompletionPrefix(editor);
150+
var prefix = util.getCompletionPrefix(editor);
167151
// Only autocomplete if there's a prefix that can be matched
168152
if (prefix && !hasCompleter) {
169153
if (!editor.completer) {

0 commit comments

Comments
 (0)