From a00b640dad1acb8e059f884ac18fcb2006010399 Mon Sep 17 00:00:00 2001 From: travertexg Date: Tue, 10 Jun 2025 04:21:40 +0000 Subject: [PATCH] refactor: Improve file search tool descriptions and reliability - Updates the description for the regex_search_files tool to dynamically indicate the regex syntax used based on the user's settings (ECMAScript or Rust). - Refactors the core plugin match search to wait for the search view to load before attempting to access results, addressing a timing issue. - Adds new settings for regexSearchBackend and matchSearchBackend in settings.test.ts to reflect the refactored backend options for file searching. --- src/core/prompts/tools/search-files.ts | 18 ++++++++++++++++-- src/core/search/match/coreplugin-match.ts | 17 +++++++++++------ src/types/settings.test.ts | 4 ++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/core/prompts/tools/search-files.ts b/src/core/prompts/tools/search-files.ts index 8ed702b..3008b6f 100644 --- a/src/core/prompts/tools/search-files.ts +++ b/src/core/prompts/tools/search-files.ts @@ -1,4 +1,5 @@ import { ToolArgs } from "./types" +import { useSettings } from '../../../contexts/SettingsContext' export function getSearchFilesDescription(args: ToolArgs): string { if (args.searchTool === 'match') { @@ -17,7 +18,7 @@ export function getMatchSearchFilesDescription(args: ToolArgs): string { Description: Request to perform a match/fuzzy search across files in a specified directory, providing context-rich results. This tool searches for specific content across multiple files, displaying each match with encapsulating context. Parameters: - path: (required) The path of the directory to search in (relative to the current working directory ${args.cwd}). This directory will be recursively searched. -- query: (required) The keyword, phrase to search for. The system will find documents with similar keywords/phrases. +- query: (required) The keyword/phrase to search for. The system will find documents with similar keywords/phrases. Usage: @@ -33,11 +34,24 @@ Example: Requesting to search for all Markdown files containing 'test' in the cu } export function getRegexSearchFilesDescription(args: ToolArgs): string { + const { settings } = useSettings() + let regex_syntax: string; + switch (settings.regexSearchBackend) { + case 'coreplugin': + regex_syntax = "ECMAScript (JavaScript)"; + break; + case 'ripgrep': + regex_syntax = "Rust"; + break; + default: + regex_syntax = "ECMAScript (JavaScript)"; + } + return `## regex_search_files Description: Request to perform a regex search across files in a specified directory, providing context-rich results. This tool searches for patterns or specific content across multiple files, displaying each match with encapsulating context. Parameters: - path: (required) The path of the directory to search in (relative to the current working directory ${args.cwd}). This directory will be recursively searched. -- regex: (required) The regular expression pattern to search for. Uses Rust regex syntax, **but should not include word boundaries (\b)**. +- regex: (required) The regular expression pattern to search for. Uses ${regex_syntax} regex syntax, **but should not include word boundaries (\b)**. Usage: diff --git a/src/core/search/match/coreplugin-match.ts b/src/core/search/match/coreplugin-match.ts index c58b5b2..d32e955 100644 --- a/src/core/search/match/coreplugin-match.ts +++ b/src/core/search/match/coreplugin-match.ts @@ -28,18 +28,23 @@ export async function matchSearchUsingCorePlugin( // It does not return the results directly. searchPlugin.openGlobalSearch(query); - // We must wait for the search to execute and the UI to update. - await new Promise(resolve => setTimeout(resolve, 500)); - const searchLeaf = app.workspace.getLeavesOfType('search')[0]; if (!searchLeaf) { throw new Error("No active search pane found after triggering search."); } - // @ts-ignore - const searchResultsMap = (searchLeaf.view as any).dom.resultDomLookup; + // Ensure the view is fully loaded before we try to access its properties. + const view = await searchLeaf.open(searchLeaf.view); + const searchResultsMap = await new Promise>(resolve => { + setTimeout(() => { + // @ts-ignore + const results = (view as any).dom?.resultDomLookup; + resolve(results || new Map()); + }, 5000) + }); + if (!searchResultsMap || searchResultsMap.size === 0) { - console.error("No results found."); + console.error("No results found or search results map is not available."); return "No results found." } diff --git a/src/types/settings.test.ts b/src/types/settings.test.ts index cec6c6c..3d73299 100644 --- a/src/types/settings.test.ts +++ b/src/types/settings.test.ts @@ -13,6 +13,8 @@ describe('parseSmartCopilotSettings', () => { openAIApiKey: '', anthropicApiKey: '', filesSearchMethod: 'auto', + regexSearchBackend: 'ripgrep', + matchSearchBackend: 'coreplugin', fuzzyMatchThreshold: 0.85, geminiApiKey: '', groqApiKey: '', @@ -196,6 +198,8 @@ describe('settings migration', () => { openAIApiKey: '', anthropicApiKey: '', filesSearchMethod: 'auto', + regexSearchBackend: 'ripgrep', + matchSearchBackend: 'coreplugin', fuzzyMatchThreshold: 0.85, geminiApiKey: '', groqApiKey: '',