Merge pull request #1924 from unional/liveAutocomplete

Improve auto complete
This commit is contained in:
Harutyun Amirjanyan 2014-05-05 17:40:47 +02:00
commit 6779345049
3 changed files with 22 additions and 10 deletions

View file

@ -31,7 +31,7 @@
define(function(require, exports, module) { define(function(require, exports, module) {
var Range = require("../range").Range; var Range = require("../range").Range;
var splitRegex = /[^a-zA-Z_0-9\$\-]+/; var splitRegex = /[^a-zA-Z_0-9\$\-\u00C0-\u1FFF\u2C00-\uD7FF\w]+/;
function getWordIndex(doc, pos) { function getWordIndex(doc, pos) {
var textBefore = doc.getTextRange(Range.fromPoints({row: 0, column:0}, pos)); var textBefore = doc.getTextRange(Range.fromPoints({row: 0, column:0}, pos));

View file

@ -43,9 +43,9 @@ exports.parForEach = function(array, fn, callback) {
callback(result, err); callback(result, err);
}); });
} }
} };
var ID_REGEX = /[a-zA-Z_0-9\$-]/; var ID_REGEX = /[a-zA-Z_0-9\$-\u007F-\uFFFF]/;
exports.retrievePrecedingIdentifier = function(text, pos, regex) { exports.retrievePrecedingIdentifier = function(text, pos, regex) {
regex = regex || ID_REGEX; regex = regex || ID_REGEX;
@ -57,7 +57,7 @@ exports.retrievePrecedingIdentifier = function(text, pos, regex) {
break; break;
} }
return buf.reverse().join(""); return buf.reverse().join("");
} };
exports.retrieveFollowingIdentifier = function(text, pos, regex) { exports.retrieveFollowingIdentifier = function(text, pos, regex) {
regex = regex || ID_REGEX; regex = regex || ID_REGEX;
@ -69,6 +69,6 @@ exports.retrieveFollowingIdentifier = function(text, pos, regex) {
break; break;
} }
return buf; return buf;
} };
}); });

View file

@ -72,6 +72,11 @@ exports.addCompleter = function(completer) {
completers.push(completer); completers.push(completer);
}; };
// Exports existing completer so that user can construct his own set of completers.
exports.textCompleter = textCompleter;
exports.keyWordCompleter = keyWordCompleter;
exports.snippetCompleter = snippetCompleter;
var expandSnippet = { var expandSnippet = {
name: "expandSnippet", name: "expandSnippet",
exec: function(editor) { exec: function(editor) {
@ -126,7 +131,7 @@ var doLiveAutocomplete = function(e) {
//Try to find custom prefixes on the completers //Try to find custom prefixes on the completers
completers.forEach(function(completer) { completers.forEach(function(completer) {
if (completer.identifierRegexps) { if (completer.identifierRegexps) {
completer.identifierRegexps.forEach(function(identifierRegex){ completer.identifierRegexps.forEach(function(identifierRegex) {
if (!prefix) { if (!prefix) {
prefix = util.retrievePrecedingIdentifier(line, pos.column, identifierRegex); prefix = util.retrievePrecedingIdentifier(line, pos.column, identifierRegex);
} }
@ -163,7 +168,7 @@ require("../config").defineOptions(Editor.prototype, "editor", {
enableBasicAutocompletion: { enableBasicAutocompletion: {
set: function(val) { set: function(val) {
if (val) { if (val) {
this.completers = completers; this.completers = Array.isArray(val)? val: completers;
this.commands.addCommand(Autocomplete.startCommand); this.commands.addCommand(Autocomplete.startCommand);
} else { } else {
this.commands.removeCommand(Autocomplete.startCommand); this.commands.removeCommand(Autocomplete.startCommand);
@ -171,10 +176,18 @@ require("../config").defineOptions(Editor.prototype, "editor", {
}, },
value: false value: false
}, },
enableLiveAutocomplete: { /**
* Enable live autocomplete. If the value is an array, it is assumed to be an array of completer
* and will use them instead of the default completers.
* NOTE: Should this be renamed to enableLiveAutocompletion to match enableBasicAutocompletion?
*/
enableLiveAutocompletion: {
set: function(val) { set: function(val) {
if (val) { if (val) {
this.completers = Array.isArray(val)? val: completers;
// On each change automatically trigger the autocomplete // On each change automatically trigger the autocomplete
this.completers = completers;
this.commands.on('afterExec', doLiveAutocomplete); this.commands.on('afterExec', doLiveAutocomplete);
} else { } else {
this.commands.removeListener('afterExec', doLiveAutocomplete); this.commands.removeListener('afterExec', doLiveAutocomplete);
@ -196,5 +209,4 @@ require("../config").defineOptions(Editor.prototype, "editor", {
value: false value: false
} }
}); });
}); });