         #{relevancyHtml}
       </div>"
 
-  shortenUrl: (url) ->
-    @stripTrailingSlash(url).replace(/^http:\/\//, "")
+  shortenUrl: (url) -> @stripTrailingSlash(url).replace(/^http:\/\//, "")
 
   stripTrailingSlash: (url) ->
     url = url.substring(url, url.length - 1) if url[url.length - 1] == "/"
     url
+
+  # Wraps each occurence of the query terms in the given string in a <span>.
+  highlightTerms: (string) ->
+    ranges = []
+    for term in @queryTerms
+      i = string.search(RegexpCache.get(term))
+      ranges.push([i, i + term.length]) if i >= 0
+
+    return string if ranges.length == 0
+
+    ranges = @mergeRanges(ranges.sort (a, b) -> a[0] - b[0])
+    # Replace portions of the string from right to left.
+    ranges = ranges.sort (a, b) -> b[0] - a[0]
+    for [start, end] in ranges
+      string =
+        string.substring(0, start) +
+        "<span class='match'>" + string.substring(start, end) + "</span>" +
+        string.substring(end)
+    string
 
   # Merges the given list of ranges such that any overlapping regions are combined. E.g.
   #   mergeRanges([0, 4], [3, 6]) => [0, 6].  A range is [startIndex, endIndex].

        suggestion
      onComplete(suggestions)

  computeRelevancy: (queryTerms, suggestion) ->
    RankingUtils.wordRelevancy(queryTerms, suggestion.url, suggestion.title)

class MultiCompleter
  constructor: (@completers) ->
    @maxResults = 10 # TODO(philc): Should this be configurable?

  refresh: -> completer.refresh() for completer in @completers when completer.refresh

  filter: (queryTerms, onComplete) ->
    # Only allow one query to run at a time.
    if @filterInProgress
      @mostRecentQuery = { queryTerms: queryTerms, onComplete: onComplete }
      return

    return 1 if a.url > b.url
    -1

  # When a page we've seen before has been visited again, be sure to replace our History item so it has the
  # correct "lastVisitTime". That's crucial for ranking Vomnibar suggestions.
  onPageVisited: (newPage) ->
    i = HistoryCache.binarySearch(newPage, @history, @compareHistoryByUrl)
    pageWasFound = (@history[i].url == newPage.url)
