Skip to content

Add regex option to search#43

Open
subhajitlucky wants to merge 2 commits into
zigcBenx:masterfrom
subhajitlucky:add-regex-search-option-11
Open

Add regex option to search#43
subhajitlucky wants to merge 2 commits into
zigcBenx:masterfrom
subhajitlucky:add-regex-search-option-11

Conversation

@subhajitlucky
Copy link
Copy Markdown

Fixes #11.

Summary

  • Adds a .* regex toggle beside the search input.
  • Sends regex mode through the webview search message and runs regex-aware matching in SearchService.
  • Carries actual match length into result/preview rendering so regex matches are highlighted correctly.
  • Adds a small pure matcher test for literal, regex, invalid regex, and zero-length patterns.

Validation

  • npm run test:search-pattern
  • git diff --check

Note: npm run lint still fails because the repository has no ESLint configuration file for the existing lint script.

Copilot AI review requested due to automatic review settings May 13, 2026 03:12
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 threaded isRegex through the webview message to the extension host search.
  • Introduced searchPattern helpers and updated SearchService to use regex-aware matching and to carry matchLength through 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

  • searchPattern is shared across the Promise.all(...) in searchBatch, but findPatternMatches mutates pattern.expression.lastIndex (and increments it for zero-length matches). Because RegExp objects with the g flag are stateful, running exec loops concurrently across multiple files can cause missed/incorrect matches. Consider cloning/compiling a fresh RegExp per file (or per findPatternMatches call) 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 thread src/searchPattern.ts Outdated
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 thread src/WebviewManager.ts
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) {
@subhajitlucky
Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add regex option to search

2 participants