MCPcopy Index your code
hub / github.com/philc/vimium / updateQuery

Method updateQuery

content_scripts/mode_find.js:156–212  ·  view source on GitHub ↗
(query)

Source from the content-addressed store, hash-verified

154 }
155
156 static updateQuery(query) {
157 let pattern;
158 if (!this.query) {
159 this.query = {};
160 }
161 this.query.rawQuery = query;
162 // the query can be treated differently (e.g. as a plain string versus regex) depending on the
163 // presence of escape sequences. '\' is the escape character and needs to be escaped itself to
164 // be used as a normal character. here we grep for the relevant escape sequences.
165 this.query.isRegex = Settings.get("regexFindMode");
166 this.query.parsedQuery = this.query.rawQuery.replace(
167 /(\\{1,2})([rRI]?)/g,
168 (match, slashes, flag) => {
169 if ((flag === "") || (slashes.length !== 1)) {
170 return match;
171 }
172
173 switch (flag) {
174 case "r":
175 this.query.isRegex = true;
176 break;
177 case "R":
178 this.query.isRegex = false;
179 break;
180 }
181 return "";
182 },
183 );
184
185 // Implement smartcase.
186 this.query.ignoreCase = !Utils.hasUpperCase(this.query.parsedQuery);
187
188 const regexPattern = this.query.isRegex
189 ? this.query.parsedQuery
190 : Utils.escapeRegexSpecialCharacters(this.query.parsedQuery);
191
192 // Grep for all matches in every text node,
193 // so we can show a the number of results.
194 try {
195 pattern = new RegExp(regexPattern, `g${this.query.ignoreCase ? "i" : ""}`);
196 } catch {
197 // If we catch a SyntaxError, assume the user is not done typing yet and return quietly.
198 return;
199 }
200
201 const textNodes = getAllTextNodes();
202 const matchedNodes = textNodes.filter((node) => {
203 return node.textContent.match(pattern);
204 });
205 const regexMatches = matchedNodes.map((node) => node.textContent.match(pattern));
206 this.query.regexMatches = regexMatches;
207 this.query.regexPattern = pattern;
208 this.query.regexMatchedNodes = matchedNodes;
209 this.updateActiveRegexIndices();
210
211 return this.query.matchCount = regexMatches != null ? regexMatches.flat().length : null;
212 }
213

Callers 3

getQueryMethod · 0.95
findSelectedHelperFunction · 0.80
findInPlaceMethod · 0.80

Calls 5

getAllTextNodesFunction · 0.85
getMethod · 0.80
matchMethod · 0.80
filterMethod · 0.45

Tested by

no test coverage detected