Skip to content

Commit

Permalink
Fix custom identifierRegexps (issue #2532)
Browse files Browse the repository at this point in the history
- Add util.getCompletionPrefix and use it instead of util.retrievePrecedingIdentifier in gatherCompletions and doLiveAutocomplete functions as suggested at #2532
  • Loading branch information
Sherry Zhou committed Dec 11, 2015
1 parent 5635526 commit fcebd0f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/ace/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ var Autocomplete = function() {
var pos = editor.getCursorPosition();

var line = session.getLine(pos.row);
var prefix = util.retrievePrecedingIdentifier(line, pos.column);
var prefix = util.getCompletionPrefix(editor);

this.base = session.doc.createAnchor(pos.row, pos.column - prefix.length);
this.base.$insertRight = true;
Expand All @@ -241,7 +241,7 @@ var Autocomplete = function() {
var pos = editor.getCursorPosition();
var line = session.getLine(pos.row);
callback(null, {
prefix: util.retrievePrecedingIdentifier(line, pos.column, results && results[0] && results[0].identifierRegex),
prefix: prefix,
matches: matches,
finished: (--total === 0)
});
Expand Down
15 changes: 15 additions & 0 deletions lib/ace/autocomplete/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,19 @@ exports.retrieveFollowingIdentifier = function(text, pos, regex) {
return buf;
};

exports.getCompletionPrefix = function (editor) {
var pos = editor.getCursorPosition();
var line = editor.session.getLine(pos.row);
var prefix;
editor.completers.forEach(function(completer) {
if (completer.identifierRegexps) {
completer.identifierRegexps.forEach(function(identifierRegex) {
if (!prefix && identifierRegex)
prefix = this.retrievePrecedingIdentifier(line, pos.column, identifierRegex);
}.bind(this));
}
}.bind(this));
return prefix || this.retrievePrecedingIdentifier(line, pos.column);
};

});
20 changes: 2 additions & 18 deletions lib/ace/ext/language_tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,33 +137,17 @@ var loadSnippetFile = function(id) {
});
};

function getCompletionPrefix(editor) {
var pos = editor.getCursorPosition();
var line = editor.session.getLine(pos.row);
var prefix;
// Try to find custom prefixes on the completers
editor.completers.forEach(function(completer) {
if (completer.identifierRegexps) {
completer.identifierRegexps.forEach(function(identifierRegex) {
if (!prefix && identifierRegex)
prefix = util.retrievePrecedingIdentifier(line, pos.column, identifierRegex);
});
}
});
return prefix || util.retrievePrecedingIdentifier(line, pos.column);
}

var doLiveAutocomplete = function(e) {
var editor = e.editor;
var hasCompleter = editor.completer && editor.completer.activated;

// We don't want to autocomplete with no prefix
if (e.command.name === "backspace") {
if (hasCompleter && !getCompletionPrefix(editor))
if (hasCompleter && !util.getCompletionPrefix(editor))
editor.completer.detach();
}
else if (e.command.name === "insertstring") {
var prefix = getCompletionPrefix(editor);
var prefix = util.getCompletionPrefix(editor);
// Only autocomplete if there's a prefix that can be matched
if (prefix && !hasCompleter) {
if (!editor.completer) {
Expand Down

0 comments on commit fcebd0f

Please sign in to comment.