MCPcopy
hub / github.com/philc/vimium / pushMatchingRanges

Method pushMatchingRanges

background_scripts/completion/completers.js:135–156  ·  view source on GitHub ↗
(string, term, ranges)

Source from the content-addressed store, hash-verified

133
134 // Push the ranges within `string` which match `term` onto `ranges`.
135 pushMatchingRanges(string, term, ranges) {
136 let textPosition = 0;
137 // Split `string` into a (flat) list of pairs:
138 // - for i=0,2,4,6,...
139 // - splits[i] is unmatched text
140 // - splits[i+1] is the following matched text (matching `term`)
141 // (except for the final element, for which there is no following matched text).
142 // Example:
143 // - string = "Abacab"
144 // - term = "a"
145 // - splits = [ "", "A", "b", "a", "c", "a", b" ]
146 // UM M UM M UM M UM (M=Matched, UM=Unmatched)
147 const splits = string.split(RegexpCache.get(term, "(", ")"));
148 for (let index = 0, end = splits.length - 2; index <= end; index += 2) {
149 const unmatchedText = splits[index];
150 const matchedText = splits[index + 1];
151 // Add the indices spanning `matchedText` to `ranges`.
152 textPosition += unmatchedText.length;
153 ranges.push([textPosition, textPosition + matchedText.length]);
154 textPosition += matchedText.length;
155 }
156 }
157
158 // Wraps each occurence of the query terms in the given string in a <span>.
159 highlightQueryTerms(string) {

Callers 2

highlightQueryTermsMethod · 0.95
completers_test.jsFile · 0.80

Calls 2

getMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected