diff options
| author | Phil Crosby | 2012-05-05 17:39:40 -0700 | 
|---|---|---|
| committer | Phil Crosby | 2012-05-05 18:32:14 -0700 | 
| commit | f42cf13daaa7ec39d2fbba905dcec9d4777af306 (patch) | |
| tree | b9307a71c33be6c7761bf1f3399c6f730f27f572 | |
| parent | 98b3d851a74034db8cec07488f7c3ab8c33a9d5f (diff) | |
| download | vimium-f42cf13daaa7ec39d2fbba905dcec9d4777af306.tar.bz2 | |
move the async completer next to the classes which use it.
| -rw-r--r-- | lib/completion.js | 95 | 
1 files changed, 47 insertions, 48 deletions
| diff --git a/lib/completion.js b/lib/completion.js index 0e6b1b8d..268a33d8 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -41,6 +41,53 @@ var completion = (function() {      this.build = buildFunction;    } +  /** 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 a URL or not. */ +  var SmartCompletionSource = Class.extend({ +    init: function(commands) { +      this.commands = commands || {}; +      this.commandKeys = Object.keys(commands); +    }, + +    refresh: function() { }, + +      /** 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 = 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 LazyCompletionResult(-2, function() { +          return { +            html:   createCompletionHtml(desc, term), +            action: { functionName: "navigateToUrl", 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. */ +    getUrlOrSearchSuggestion: function(query, suggestions) { +      // trim query +      query = query.replace(/^\s+|\s+$/g, ''); +      var isUrl = utils.isUrl(query); +      return new LazyCompletionResult(-1, function() { +        return { +            html: createCompletionHtml(isUrl ? "goto" : "search", query), +            action: { functionName: "navigateToUrl", +                args: isUrl ? [utils.createFullUrl(query)] : [utils.createSearchUrl(query)] } +        }}); +    }, + +    filter: function(query, callback) { +      suggestions = this.getCommandSuggestions(query); +      suggestions.push(this.getUrlOrSearchSuggestion(query)); +      callback(suggestions); +    } +  });    /** Helper class to construct fuzzy completers for asynchronous data sources like history or bookmark     * matchers. */ @@ -107,54 +154,6 @@ var completion = (function() {      }    }); -  /** 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 a URL or not. */ -  var SmartCompletionSource = Class.extend({ -    init: function(commands) { -      this.commands = commands || {}; -      this.commandKeys = Object.keys(commands); -    }, - -    refresh: function() { }, - -      /** 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 = 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 LazyCompletionResult(-2, function() { -          return { -            html:   createCompletionHtml(desc, term), -            action: { functionName: "navigateToUrl", 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. */ -    getUrlOrSearchSuggestion: function(query, suggestions) { -      // trim query -      query = query.replace(/^\s+|\s+$/g, ''); -      var isUrl = utils.isUrl(query); -      return new LazyCompletionResult(-1, function() { -        return { -            html: createCompletionHtml(isUrl ? "goto" : "search", query), -            action: { functionName: "navigateToUrl", -                args: isUrl ? [utils.createFullUrl(query)] : [utils.createSearchUrl(query)] } -        }}); -    }, - -    filter: function(query, callback) { -      suggestions = this.getCommandSuggestions(query); -      suggestions.push(this.getUrlOrSearchSuggestion(query)); -      callback(suggestions); -    } -  }); -    /** A fuzzy bookmark completer */    var FuzzyBookmarkCompletionSource = Class.extend({      init: function() { this.asyncCompleter = new AsyncCompleter(); }, | 
