diff options
| author | Stephen Blott | 2015-05-10 08:59:14 +0100 | 
|---|---|---|
| committer | Stephen Blott | 2015-05-10 09:55:06 +0100 | 
| commit | 1a337a261a6dd6deffa836cbd949bb036e103f36 (patch) | |
| tree | 0db259e68e86f399ff6c448eb7929e4f95569eb4 | |
| parent | 8d51ccbb01fe2a4e7b548cc14617a05048a8d68c (diff) | |
| download | vimium-1a337a261a6dd6deffa836cbd949bb036e103f36.tar.bz2 | |
Search completion; reuse previous query.
| -rw-r--r-- | background_scripts/completion_engines.coffee | 19 | ||||
| -rw-r--r-- | lib/utils.coffee | 10 | 
2 files changed, 28 insertions, 1 deletions
| diff --git a/background_scripts/completion_engines.coffee b/background_scripts/completion_engines.coffee index 85062c49..425ff47e 100644 --- a/background_scripts/completion_engines.coffee +++ b/background_scripts/completion_engines.coffee @@ -176,7 +176,7 @@ CompletionEngines =      # to generate a key.  We mix in some junk generated by pwgen. A key clash might be possible, but      # vanishingly unlikely.      junk = "//Zi?ei5;o//" -    completionCacheKey = searchUrl + junk + queryTerms.join junk +    completionCacheKey = searchUrl + junk + queryTerms.map((s) -> s.toLowerCase()).join junk      @completionCache ?= new SimpleCache 60 * 60 * 1000, 2000 # One hour, 2000 entries.      if @completionCache.has completionCacheKey        # We add a short delay, even for a cache hit.  This avoids an ugly flicker when the additional @@ -186,6 +186,21 @@ CompletionEngines =          callback @completionCache.get completionCacheKey        return +    if @mostRecentQuery? and @mostRecentSuggestions? +      # If the user appears to be typing a continuation of the characters in all of the most recent query, +      # then we can re-use the results of the previous query. +      reusePreviousSuggestions = do (query) => +        query = queryTerms.join(" ").toLowerCase() +        return false unless 0 == query.indexOf @mostRecentQuery.toLowerCase() +        previousSuggestions = @mostRecentSuggestions.map (s) -> s.toLowerCase() +        return false unless query.length <= Utils.longestCommonPrefix previousSuggestions +        true + +      if reusePreviousSuggestions +        console.log "reuse previous query", @mostRecentQuery if @debug +        @mostRecentQuery = queryTerms.join " " +        return callback @completionCache.set completionCacheKey, @mostRecentSuggestions +      fetchSuggestions = (engine, callback) =>        url = engine.getUrl queryTerms        query = queryTerms.join(" ").toLowerCase() @@ -227,6 +242,8 @@ CompletionEngines =          queue = @inTransit[completionCacheKey] = []          engine = @lookupEngine searchUrl          fetchSuggestions engine, (suggestions) => +          @mostRecentQuery = queryTerms.join " " +          @mostRecentSuggestions = suggestions            @completionCache.set completionCacheKey, suggestions unless engine.doNotCache            callback suggestions            delete @inTransit[completionCacheKey] diff --git a/lib/utils.coffee b/lib/utils.coffee index 16adc305..a1ed23c2 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -184,6 +184,16 @@ Utils =        return true if re.test string      false +  # Calculate the length of the longest shared prefix of a list of strings. +  longestCommonPrefix: (strings) -> +    return 0 unless 0 < strings.length +    strings.sort (a,b) -> a.length - b.length +    [ shortest, strings... ] = strings +    for ch, index in shortest.split "" +      for str in strings +        return index if ch != str[index] +    return shortest.length +    # Convenience wrapper for setTimeout (with the arguments around the other way).    setTimeout: (ms, func) -> setTimeout func, ms | 
