Add regex option to search#43
Open
subhajitlucky wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an optional regex search mode to the extension’s webview search flow, including correct match-length-based highlighting for regex matches and a small matcher-focused test to validate literal/regex behavior.
Changes:
- Added a regex toggle (
.*) in the webview and threadedisRegexthrough the webview message to the extension host search. - Introduced
searchPatternhelpers and updatedSearchServiceto use regex-aware matching and to carrymatchLengththrough to the UI for accurate highlighting. - Added a pure Node test (
tests/searchPattern.test.js) plus an npm script to run it.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/searchPattern.test.js | Adds basic tests for literal vs regex matching, invalid regex handling, and zero-length match behavior. |
| src/searchPattern.ts | New helper module to build a search pattern and enumerate matches (index + length). |
| src/services/SearchService.ts | Switches search implementation to pattern-based matching and forwards match length into SearchMatch. |
| src/types.ts | Extends search result types with matchLength and adds isRegex to the search webview message. |
| src/WebviewManager.ts | Threads isRegex into search calls and adjusts search abort tracking. |
| src/webview/webviewContent.ts | Adds the .* regex toggle button beside the search input. |
| src/webview/script.ts | Implements regex toggle behavior, includes isRegex in search messages, and updates highlighting to use matchLength. |
| media/styles.css | Styles the regex toggle alongside the existing filter toggle button. |
| package.json | Adds test:search-pattern script for running the new matcher test. |
Comments suppressed due to low confidence (1)
src/services/SearchService.ts:178
searchPatternis shared across thePromise.all(...)insearchBatch, butfindPatternMatchesmutatespattern.expression.lastIndex(and increments it for zero-length matches). BecauseRegExpobjects with thegflag are stateful, running exec loops concurrently across multiple files can cause missed/incorrect matches. Consider cloning/compiling a fresh RegExp per file (or perfindPatternMatchescall) so each async task has its own non-shared matcher state.
private async searchBatch(
batch: vscode.Uri[],
searchPattern: SearchPattern
): Promise<Array<{ filePath: string; matches: SearchMatch[] } | null>> {
return Promise.all(batch.map(async (file) => {
try {
const stat = await vscode.workspace.fs.stat(file);
if (stat.size > this.options.maxFileSize) {
return null;
}
const uint8Array = await vscode.workspace.fs.readFile(file);
const text = new TextDecoder('utf-8', { fatal: false }).decode(uint8Array);
const matches = this.findMatchesInFile(file, text, searchPattern);
return matches.length > 0 ? { filePath: file.fsPath, matches } : null;
} catch (error) {
return null;
}
}));
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+28
to
+33
| pattern.expression.lastIndex = 0; | ||
|
|
||
| let match: RegExpExecArray | null; | ||
| while ((match = pattern.expression.exec(text)) !== null) { | ||
| if (match[0].length === 0) { | ||
| pattern.expression.lastIndex++; |
Comment on lines
+177
to
+193
| private async handleSearch(panelId: string, panel: vscode.WebviewPanel, query: string, includePattern?: string, excludePattern?: string, isRegex: boolean = false): Promise<void> { | ||
| try { | ||
| this.panelSearches.set(panelId, query); | ||
| const searchId = `${isRegex ? 'regex' : 'literal'}:${query}`; | ||
| this.panelSearches.set(panelId, searchId); | ||
|
|
||
| const searchableFiles = await this.searchService.getSearchableFiles(); | ||
| const searchOptions = this.searchService.getSearchOptions(); | ||
| let resultCount = 0; | ||
|
|
||
| for (let i = 0; i < searchableFiles.length; i += searchOptions.batchSize) { | ||
| if (this.panelSearches.get(panelId) !== query) { | ||
| if (this.panelSearches.get(panelId) !== searchId) { | ||
| // A new search has been initiated in this panel, abort current search | ||
| return; | ||
| } | ||
|
|
||
| const batch = searchableFiles.slice(i, i + searchOptions.batchSize); | ||
| const results = await this.searchService.search(batch, query, includePattern, excludePattern); | ||
| const results = await this.searchService.search(batch, query, includePattern, excludePattern, isRegex); |
Comment on lines
169
to
175
|
|
||
| const uint8Array = await vscode.workspace.fs.readFile(file); | ||
| const text = new TextDecoder('utf-8', { fatal: false }).decode(uint8Array); | ||
| const textLower = text.toLowerCase(); | ||
|
|
||
| if (!textLower.includes(queryLower)) { | ||
| return null; | ||
| } | ||
|
|
||
| const matches = this.findMatchesInFile(file, text, textLower, queryLower); | ||
| const matches = this.findMatchesInFile(file, text, searchPattern); | ||
| return matches.length > 0 ? { filePath: file.fsPath, matches } : null; | ||
| } catch (error) { |
Author
|
Pushed a follow-up commit (85b96cd) that removes the shared mutable RegExp from SearchPattern. findPatternMatches now creates a fresh RegExp per scan, and the matcher test covers reusable patterns without exposing shared RegExp state. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #11.
Summary
.*regex toggle beside the search input.SearchService.Validation
npm run test:search-patterngit diff --checkNote:
npm run lintstill fails because the repository has no ESLint configuration file for the existing lint script.