aboutsummaryrefslogtreecommitdiffstats
path: root/lib/completion.js
diff options
context:
space:
mode:
authorPhil Crosby2012-04-29 23:24:53 -0700
committerPhil Crosby2012-04-30 00:13:07 -0700
commit152a4a6ce19b515a17455e3d545d20aaea14d0c5 (patch)
tree2654b112b4d63f7c933982ca0651be8f12330e6c /lib/completion.js
parentfd4d81c276accbe8612e17ff5467c762c5c0c277 (diff)
downloadvimium-152a4a6ce19b515a17455e3d545d20aaea14d0c5.tar.bz2
shorten the declaration of SmartCompletionSource
Diffstat (limited to 'lib/completion.js')
-rw-r--r--lib/completion.js46
1 files changed, 21 insertions, 25 deletions
diff --git a/lib/completion.js b/lib/completion.js
index 165aa86d..1a8779a1 100644
--- a/lib/completion.js
+++ b/lib/completion.js
@@ -50,10 +50,7 @@ var completion = (function() {
filter: function(query, callback) {
var self = this;
-
- var handler = function(results) {
- callback(self.processResults(query, results));
- }
+ var handler = function(results) { callback(self.processResults(query, results)); }
// are the results ready?
if (this.completions !== null) {
@@ -70,23 +67,23 @@ var completion = (function() {
},
}
- //========== Completer implementations ===========//
-
/** A simple completer that suggests to open the input string as an URL or to trigger a web search for the
- * given term, depending on whether it thinks the input is an URL or not. */
- var SmartCompletionSource = function(commands) {
- commands = commands || {};
- var commandKeys = Object.keys(commands);
+ * given term, depending on whether it thinks the input is a URL or not. */
+ var SmartCompletionSource = Class.extend({
+ init: function(commands) {
+ this.commands = commands || {};
+ this.commandKeys = Object.keys(commands);
+ },
- this.refresh = function() { };
+ refresh: function() { },
- /** Returns the suggestions matching the user-defined commands */
- this.getCommandSuggestions = function(query, suggestions) {
- return commandKeys.filter(function(cmd) { return query.indexOf(cmd) == 0 }).map(function(cmd) {
+ /** Returns the suggestions matching the user-defined commands */
+ getCommandSuggestions: function(query, suggestions) {
+ return this.commandKeys.filter(function(cmd) { return query.indexOf(cmd) == 0 }).map(function(cmd) {
var term = query.slice(cmd.length);
- var desc = commands[cmd][0];
- var pattern = commands[cmd][1];
- var url = typeof pattern == 'function' ? pattern(term) : pattern.replace(/%s/g, term);
+ var desc = this.commands[cmd][0];
+ var pattern = this.commands[cmd][1];
+ var url = (typeof pattern == "function") ? pattern(term) : pattern.replace(/%s/g, term);
// this will appear even before the URL/search suggestion
return new LazyCompletion(-2, function() {
@@ -94,30 +91,29 @@ var completion = (function() {
html: createCompletionHtml(desc, term),
action: {func: 'completion.createActionOpenUrl', args: [utils.createFullUrl(url)]},
}})
- });
- }
+ }.proxy(this));
+ },
/** Checks if the input is a URL. If yes, returns a suggestion to visit it. If no, returns a suggestion
* to start a web search. */
- this.getUrlOrSearchSuggestion = function(query, suggestions) {
+ getUrlOrSearchSuggestion: function(query, suggestions) {
// trim query
query = query.replace(/^\s+|\s+$/g, '');
var isUrl = utils.isUrl(query);
-
- return new LazyCompletion(-1, function() {
+ return new LazyCompletion(-1, function() {
return {
html: createCompletionHtml(isUrl ? 'goto' : 'search', query),
action: {func: 'completion.createActionOpenUrl', args: isUrl ? [utils.createFullUrl(query)]
: [utils.createSearchUrl(query)]},
}});
- }
+ },
- this.filter = function(query, callback) {
+ filter: function(query, callback) {
suggestions = this.getCommandSuggestions(query);
suggestions.push(this.getUrlOrSearchSuggestion(query));
callback(suggestions);
}
- }
+ });
/** A fuzzy bookmark completer */
var FuzzyBookmarkCompletionSource = function() {