feat(search): support wildcard patterns across search fields (#1571)

* feat(search): add wildcard matching to search fields

* fix(search): harden wildcard matching and file globs

* fix(search): align file matching with shared wildcard semantics

* fix(search): bound file wildcard search in runner

* fix(search): normalize outline queries through shared matcher

* fix(web): remove duplicate markdown test context field
This commit is contained in:
Ananovo
2026-08-16 22:41:24 +08:00
committed by GitHub
parent 8dc4a50fee
commit 7909c46fff
14 changed files with 377 additions and 19 deletions
+5 -2
View File
@@ -1,13 +1,14 @@
import { logger } from '@/ui/logger'
import { RPC_METHODS } from '@hapi/protocol/rpcMethods'
import type { RpcHandlerManager } from '@/api/rpc/RpcHandlerManager'
import { run as runRipgrep } from '@/modules/ripgrep/index'
import { run as runRipgrep, runFileSearch, type FileSearchOptions } from '@/modules/ripgrep/index'
import { validatePath } from '../pathSecurity'
import { getErrorMessage, rpcError } from '../rpcResponses'
interface RipgrepRequest {
args: string[]
cwd?: string
fileSearch?: FileSearchOptions
}
interface RipgrepResponse {
@@ -30,7 +31,9 @@ export function registerRipgrepHandlers(rpcHandlerManager: RpcHandlerManager, wo
}
try {
const result = await runRipgrep(data.args, { cwd: data.cwd })
const result = data.fileSearch
? await runFileSearch(data.args, { ...data.fileSearch, cwd: data.cwd })
: await runRipgrep(data.args, { cwd: data.cwd })
return {
success: true,
exitCode: result.exitCode,
+23 -2
View File
@@ -3,7 +3,7 @@
*/
import { describe, it, expect } from 'vitest'
import { run } from './index'
import { matchesFileSearchPath, run, runFileSearch, selectFileSearchPaths } from './index'
describe('ripgrep low-level wrapper', () => {
it('should get version', async () => {
@@ -39,4 +39,25 @@ describe('ripgrep low-level wrapper', () => {
expect(result.exitCode).toBe(0)
expect(result.stdout).toContain('describe')
})
})
it('should apply shared wildcard semantics before enforcing the result limit', () => {
expect(selectFileSearchPaths([
'src/file.ts',
'other.ts',
'src/deep/file.ts'
], 'src*.ts', 2)).toEqual([
'src/file.ts',
'src/deep/file.ts'
])
})
it('should bound runner-side wildcard file-search output', async () => {
const query = 'src/modules/ripgrep/*.ts'
const result = await runFileSearch(['--files'], { query, limit: 1 })
const paths = result.stdout.trim().split(/\r?\n/).filter(Boolean)
expect(result.exitCode).toBe(0)
expect(paths).toHaveLength(1)
expect(matchesFileSearchPath(paths[0], query)).toBe(true)
})
})
+75
View File
@@ -3,8 +3,10 @@
*/
import { spawn } from 'child_process';
import { createInterface } from 'readline';
import { join, resolve } from 'path';
import { platform } from 'os';
import { matchesSearchQuery } from '@hapi/protocol';
import { runtimePath } from '@/projectPath';
import { withBunRuntimeEnv } from '@/utils/bunRuntime';
@@ -18,6 +20,12 @@ export interface RipgrepOptions {
cwd?: string
}
export interface FileSearchOptions {
cwd?: string
query: string
limit: number
}
function getBinaryPath(): string {
const platformName = platform();
const binaryName = platformName === 'win32' ? 'rg.exe' : 'rg';
@@ -58,3 +66,70 @@ export function run(args: string[], options?: RipgrepOptions): Promise<RipgrepRe
});
});
}
export function matchesFileSearchPath(path: string, query: string): boolean {
const normalizedPath = platform() === 'win32' ? path.replaceAll('\\', '/') : path
return matchesSearchQuery(normalizedPath, query)
}
export function selectFileSearchPaths(paths: Iterable<string>, query: string, limit: number): string[] {
const matches: string[] = []
const boundedLimit = Math.max(1, limit)
for (const path of paths) {
if (matchesFileSearchPath(path, query)) {
matches.push(path)
if (matches.length >= boundedLimit) break
}
}
return matches
}
export function runFileSearch(args: string[], options: FileSearchOptions): Promise<RipgrepResult> {
const binaryPath = getBinaryPath();
const limit = Math.max(1, options.limit)
return new Promise((resolve, reject) => {
const child = spawn(binaryPath, args, {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: options.cwd,
env: withBunRuntimeEnv(),
windowsHide: process.platform === 'win32'
});
const lines = createInterface({ input: child.stdout });
const matchedPaths: string[] = [];
let stderr = '';
let settled = false;
child.stderr.on('data', (data) => {
stderr += data.toString();
});
lines.on('line', (line) => {
if (matchedPaths.length >= limit) return;
if (matchesFileSearchPath(line, options.query)) {
matchedPaths.push(line);
if (matchedPaths.length >= limit) {
child.kill();
}
}
});
child.on('close', (code) => {
if (settled) return;
settled = true;
lines.close();
resolve({
exitCode: code || 0,
stdout: matchedPaths.length > 0 ? `${matchedPaths.join('\n')}\n` : '',
stderr
});
});
child.on('error', (err) => {
if (settled) return;
settled = true;
lines.close();
reject(err);
});
});
}