diff options
Diffstat (limited to 'background_scripts')
| -rw-r--r-- | background_scripts/completion.coffee | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index c8a06b27..ab24377a 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -46,19 +46,32 @@ class Suggestion url = url.substring(url, url.length - 1) if url[url.length - 1] == "/" url + # Push the ranges within `string` which match `term` onto `ranges`. + pushMatchingRanges: (string,term,ranges) -> + textPosition = 0 + # Split `string` into a (flat) list of pairs: + # - splits[i%2] is unmatched text + # - splits[(i%2)+1] is the following matched text (matching `term`) + # (except for the final element, for which there is no following matched text). + # Example: + # - string = "Abacab" + # - term = "a" + # - splits = [ "", "A", "b", "a", "c", "a", b" ] + # UM M UM M UM M UM (M=Matched, UM=Unmatched) + splits = string.split(RegexpCache.get(term, "(", ")")) + for index in [0..splits.length-2] by 2 + unmatchedText = splits[index] + matchedText = splits[index+1] + # Add the indices spanning `matchedText` to `ranges`. + textPosition += unmatchedText.length + ranges.push([textPosition, textPosition + matchedText.length]) + textPosition += matchedText.length + # Wraps each occurence of the query terms in the given string in a <span>. highlightTerms: (string) -> ranges = [] for term in @queryTerms - textPosition = 0 - splits = string.split(RegexpCache.get(term, "(", ")")).reverse() - while 0 < splits.length - unmatchedText = splits.pop() - textPosition += unmatchedText.length - matchedText = if 0 < splits.length then splits.pop() else null - if matchedText - ranges.push([textPosition, textPosition + matchedText.length]) - textPosition += matchedText.length + @pushMatchingRanges string, term, ranges return string if ranges.length == 0 @@ -314,9 +327,14 @@ RegexpCache = clear: -> @cache = {} - # Get rexexp for string from cache, creating the regexp if necessary. - # Regexp meta-characters in string are escaped. - # Regexp is wrapped in prefix/suffix, which may contain meta-characters. + # Get rexexp for `string` from cache, creating it if necessary. + # Regexp meta-characters in `string` are escaped. + # Regexp is wrapped in `prefix`/`suffix`, which may contain meta-characters (these are not escaped). + # With their default values, `prefix` and `suffix` have no effect. + # Example: + # - string="go", prefix="\b", suffix="" + # - this returns regexp matching "google", but not "agog" (the "go" must occur at the start of a word) + # TODO: `prefix` and `suffix` might be useful in richer word-relevancy scoring. get: (string, prefix="", suffix="") -> @init() unless @initialized regexpString = string.replace(@escapeRegExp, "\\$&") |
