diff options
| -rw-r--r-- | lib/completion.js | 51 | 
1 files changed, 31 insertions, 20 deletions
| diff --git a/lib/completion.js b/lib/completion.js index b86d2933..1e70297e 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -89,8 +89,10 @@ var completion = (function() {      }    }); -  /** Helper class to construct fuzzy completers for asynchronous data sources like history or bookmark -   * matchers. */ +  /* +   * A generic asynchronous completer which is used by completers which have asynchronous data sources, +   * like history or bookmarks. +   */    var AsyncCompleter = Class.extend({      init: function() {        this.id = utils.createUniqueId(); @@ -103,17 +105,29 @@ var completion = (function() {        this.completions = null;      }, -    /** Convenience function to remove shared code in the completers. Creates an internal representation of -     * a fuzzy completion item that is still independent of the query. The bind function will be called with -     * the actual query as an argument later. */ -    createInternalMatch: function(type, item, action) { +    /* +     * This creates an intermediate representation of a completion which will later be called with a specific +     * query. +     * - type: the type of item we're completing against, e.g. "bookmark", "history", "tab". +     * - item: the item itself. This should include a url and title property (bookmark, history and tab +     *   objects include both of these). +     * - action: the action to take in the Vomnibox frontend  +     * +     * It's used to save us work -- we call this on every bookmark in your bookmarks list when we first fetch +     * them, for instance, and we don't want to do some the same work again every time a new query is +     * processed. +     * +     * TODO(philc): It would be nice if this could be removed; it's confusing. +     * */ +    createUnrankedCompletion: function(type, item, action) {        var url = item.url;        var parts = [type, url, item.title];        var str = parts.join(" ");        action = action || { functionName: "navigateToUrl", args: [url] };        function createLazyCompletion(query) { -        return new LazyCompletionResult(url.length / fuzzyMatcher.calculateRelevancy(query, str), function() { +        var relevancy = url.length / fuzzyMatcher.calculateRelevancy(query, str) +        return new LazyCompletionResult(relevancy, function() {            return {              html:   renderFuzzy(query, createCompletionHtml.apply(null, parts)),              action: action, @@ -124,16 +138,15 @@ var completion = (function() {        // Only after we reduced the number of possible results, we call :bind on them to get        // an actual completion object        return { -        str: parts.join(' '), -        bind: createLazyCompletion, +        completionString: parts.join(" "), +        createLazyCompletion: createLazyCompletion,        }      }, -    // Default to handle results using fuzzy matching. This can be overridden by subclasses.      processResults: function(query, results) { -      results = fuzzyMatcher.filter(query, results, function(match) { return match.str }, this.id); -      // bind the query-agnostic, lazy results to a query -      return results.map(function(result) { return result.bind(query); }); +      results = fuzzyMatcher.filter(query, results, +          function(match) { return match.completionString }, this.id); +      return results.map(function(result) { return result.createLazyCompletion(query); });      },      filter: function(query, callback) { @@ -178,15 +191,14 @@ var completion = (function() {          var results = this.traverseBookmarkTree(bookmarks);          var validResults = results.filter(function(b) { return b.url !== undefined; });          var matches = validResults.map(function(bookmark) { -          return this.asyncCompleter.createInternalMatch("bookmark", bookmark); +          return this.asyncCompleter.createUnrankedCompletion("bookmark", bookmark);          }.proxy(this));          this.asyncCompleter.resultsReady(matches);        }.proxy(this));      }    }); -  /** A fuzzy history completer */ -  var FuzzyHistoryCompletionSource = Class.extend({ +  var FuzzyHistoryCompleter = Class.extend({      init: function(maxResults) {        this.asyncCompleter = new AsyncCompleter();        this.maxResults = maxResults; @@ -199,14 +211,13 @@ var completion = (function() {        historyCache.use(function(history) {          this.asyncCompleter.resultsReady(history.slice(-this.maxResults).map(function(item) { -          return this.asyncCompleter.createInternalMatch("history", item); +          return this.asyncCompleter.createUnrankedCompletion("history", item);          }.proxy(this)));        }.proxy(this));      }    }); -  /** A fuzzy tab completer */ -  var FuzzyTabCompletionSource = Class.extend({ +  var FuzzyTabCompleter = Class.extend({      init: function() { this.asyncCompleter = new AsyncCompleter(); },      filter: function(query, callback) { return this.asyncCompleter.filter(query, callback); }, @@ -215,7 +226,7 @@ var completion = (function() {        this.asyncCompleter.reset();        chrome.tabs.getAllInWindow(null, function(tabs) {          this.asyncCompleter.resultsReady(tabs.map(function(tab) { -          return this.asyncCompleter.createInternalMatch("tab", tab, +          return this.asyncCompleter.createUnrankedCompletion("tab", tab,                { functionName: "switchToTab", args: [tab.id] });          }.proxy(this)));        }.proxy(this)); | 
