From fcebd0f662e0434fadf15faaaf4b59ab8950217a Mon Sep 17 00:00:00 2001 From: Sherry Zhou Date: Thu, 10 Dec 2015 23:21:36 -0800 Subject: [PATCH] Fix custom identifierRegexps (issue #2532) - Add util.getCompletionPrefix and use it instead of util.retrievePrecedingIdentifier in gatherCompletions and doLiveAutocomplete functions as suggested at https://github.com/ajaxorg/ace/issues/2532 --- lib/ace/autocomplete.js | 4 ++-- lib/ace/autocomplete/util.js | 15 +++++++++++++++ lib/ace/ext/language_tools.js | 20 ++------------------ 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/ace/autocomplete.js b/lib/ace/autocomplete.js index 07149cf5fb2..092b76cf26a 100644 --- a/lib/ace/autocomplete.js +++ b/lib/ace/autocomplete.js @@ -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; @@ -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) }); diff --git a/lib/ace/autocomplete/util.js b/lib/ace/autocomplete/util.js index 767b983ee9f..7e1d7f5eef9 100644 --- a/lib/ace/autocomplete/util.js +++ b/lib/ace/autocomplete/util.js @@ -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); +}; + }); diff --git a/lib/ace/ext/language_tools.js b/lib/ace/ext/language_tools.js index 563fe58a8f3..c72391386c5 100644 --- a/lib/ace/ext/language_tools.js +++ b/lib/ace/ext/language_tools.js @@ -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) {