From d0157d93d24c8c7f1a86289efe29e203d98bb072 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Mon, 5 Nov 2012 09:01:39 +0000 Subject: Factor pushMatchingRanges, improve comments/tests 1. Factor out `pushMatchingRanges`: This then allows us to ... 2. Add unit tests for `pushMatchingRanges` In effect, these tests verify where matches are highlighted in suggestions. 3. Added Utils.zip. This helps simplify `pushMatchingRanges` unit tests. 4. Improve comments. --- background_scripts/completion.coffee | 42 +++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'background_scripts/completion.coffee') 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 . 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, "\\$&") -- cgit v1.2.3