From ea125af3d33021415a90f4ea48125555bab8e15b Mon Sep 17 00:00:00 2001 From: Josh Mu Date: Sat, 17 Aug 2024 16:31:13 +1000 Subject: [PATCH] feat: support for passing rg parameters directly when search term is enclosed by quotation marks --- README.md | 3 ++- src/lib/ripgrep.ts | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7b28038..1a8f7b2 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,8 @@ For optimal performance, ensure that the VSCode configuration _Editor: Enable Pr - **Search with Regex**: Use regex in your search query to find specific patterns in your codebase. - **Selected Text Search**: Highlight text in the editor and invoke `periscope.search` to have it automatically used as the initial query. -- **Utilise `rgQueryParams`**: Create shortcuts for common ripgrep search queries via regex matching against your current query. This provides a way to map your query to ripgrep parameters via capture groups in the regex. +- **Raw Queries**: Enclosing your `search_term` in quotes will allow additional ripgrep parameters to be passed through. eg: `"foobar" -t js` will search for `foobar` in js files. +- **Utilise `rgQueryParams`**: Create shortcuts for common ripgrep search queries via regex matching against your current query. This provides a way to map your query to ripgrep parameters via capture groups in the regex for faster lookups. ## Configuration diff --git a/src/lib/ripgrep.ts b/src/lib/ripgrep.ts index fcb9863..3aba3d4 100644 --- a/src/lib/ripgrep.ts +++ b/src/lib/ripgrep.ts @@ -40,7 +40,19 @@ function getRgCommand(value: string, extraFlags?: string[]) { ...excludes, ]; - return `"${rgPath}" "${value}" ${rgFlags.join(' ')}`; + const normalizedQuery = handleSearchTermWithAdditionalRgParams(value); + + return `"${rgPath}" ${normalizedQuery} ${rgFlags.join(' ')}`; +} + +/** + * Support for passing raw ripgrep queries by detection of a search_term within quotes within the input query + * if found we can assume the rest of the query are additional ripgrep parameters + */ +function handleSearchTermWithAdditionalRgParams(query: string): string { + const valueWithinQuotes = /".*?"/.exec(query); + if (valueWithinQuotes) return query; + return `"${query}"`; } export function rgSearch(value: string, rgExtraFlags?: string[]) {