1199 lines · 41.1 KB · Rust
RawDownload
use crate::{AgentTool, ToolCallEventStream};use agent_client_protocol as acp;use anyhow::Result;use futures::{FutureExt as _, StreamExt};use gpui::{App, Entity, SharedString, Task};use language::{OffsetRangeExt, ParseStatus, Point};use project::{    Project, SearchResults, WorktreeSettings,    search::{SearchQuery, SearchResult},};use schemars::JsonSchema;use serde::{Deserialize, Serialize};use settings::Settings;use std::{cmp, fmt::Write, sync::Arc};use util::RangeExt;use util::markdown::MarkdownInlineCode;use util::paths::PathMatcher; /// Searches the contents of files in the project with a regular expression////// - Prefer this tool to path search when searching for symbols in the project, because you won't need to guess what path it's in./// - Supports full regex syntax (eg. "log.*Error", "function\\s+\\w+", etc.)/// - Pass an `include_pattern` if you know how to narrow your search on the files system/// - Never use this tool to search for paths. Only search file contents with this tool./// - Use this tool when you need to find files containing specific patterns/// - Results are paginated with 20 matches per page. Use the optional 'offset' parameter to request subsequent pages./// - DO NOT use HTML entities solely to escape characters in the tool parameters.#[derive(Debug, Serialize, Deserialize, JsonSchema)]pub struct GrepToolInput {    /// A regex pattern to search for in the entire project. Note that the regex will be parsed by the Rust `regex` crate.    ///    /// Do NOT specify a path here! This will only be matched against the code **content**.    pub regex: String,    /// A glob pattern for the paths of files to include in the search.    /// Supports standard glob patterns like "**/*.rs" or "frontend/src/**/*.ts".    /// If omitted, all files in the project will be searched.    ///    /// The glob pattern is matched against the full path including the project root directory.    ///    /// <example>    /// If the project has the following root directories:    ///    /// - /a/b/backend    /// - /c/d/frontend    ///    /// Use "backend/**/*.rs" to search only Rust files in the backend root directory.    /// Use "frontend/src/**/*.ts" to search TypeScript files only in the frontend root directory (sub-directory "src").    /// Use "**/*.rs" to search Rust files across all root directories.    /// </example>    pub include_pattern: Option<String>,    /// Optional starting position for paginated results (0-based).    /// When not provided, starts from the beginning.    #[serde(default)]    pub offset: u32,    /// Whether the regex is case-sensitive. Defaults to false (case-insensitive).    #[serde(default)]    pub case_sensitive: bool,} impl GrepToolInput {    /// Which page of search results this is.    pub fn page(&self) -> u32 {        1 + (self.offset / RESULTS_PER_PAGE)    }} const RESULTS_PER_PAGE: u32 = 20; pub struct GrepTool {    project: Entity<Project>,} impl GrepTool {    pub fn new(project: Entity<Project>) -> Self {        Self { project }    }} impl AgentTool for GrepTool {    type Input = GrepToolInput;    type Output = String;     const NAME: &'static str = "grep";     fn kind() -> acp::ToolKind {        acp::ToolKind::Search    }     fn initial_title(        &self,        input: Result<Self::Input, serde_json::Value>,        _cx: &mut App,    ) -> SharedString {        match input {            Ok(input) => {                let page = input.page();                let regex_str = MarkdownInlineCode(&input.regex);                let case_info = if input.case_sensitive {                    " (case-sensitive)"                } else {                    ""                };                 if page > 1 {                    format!("Get page {page} of search results for regex {regex_str}{case_info}")                } else {                    format!("Search files for regex {regex_str}{case_info}")                }            }            Err(_) => "Search with regex".into(),        }        .into()    }     fn run(        self: Arc<Self>,        input: Self::Input,        event_stream: ToolCallEventStream,        cx: &mut App,    ) -> Task<Result<Self::Output, Self::Output>> {        const CONTEXT_LINES: u32 = 2;        const MAX_ANCESTOR_LINES: u32 = 10;         let path_style = self.project.read(cx).path_style(cx);         let include_matcher = match PathMatcher::new(            input                .include_pattern                .as_ref()                .into_iter()                .collect::<Vec<_>>(),            path_style,        ) {            Ok(matcher) => matcher,            Err(error) => {                return Task::ready(Err(format!("invalid include glob pattern: {error}")));            }        };         // Exclude global file_scan_exclusions and private_files settings        let exclude_matcher = {            let global_settings = WorktreeSettings::get_global(cx);            let exclude_patterns = global_settings                .file_scan_exclusions                .sources()                .chain(global_settings.private_files.sources());             match PathMatcher::new(exclude_patterns, path_style) {                Ok(matcher) => matcher,                Err(error) => {                    return Task::ready(Err(format!("invalid exclude pattern: {error}")));                }            }        };         let query = match SearchQuery::regex(            &input.regex,            false,            input.case_sensitive,            false,            false,            include_matcher,            exclude_matcher,            true, // Always match file include pattern against *full project paths* that start with a project root.            None,        ) {            Ok(query) => query,            Err(error) => return Task::ready(Err(error.to_string())),        };         let results = self            .project            .update(cx, |project, cx| project.search(query, cx));         let project = self.project.downgrade();        cx.spawn(async move |cx|  {            // Keep the search alive for the duration of result iteration. Dropping this task is the            // cancellation mechanism; we intentionally do not detach it.            let SearchResults {rx, _task_handle}  = results;            futures::pin_mut!(rx);             let mut output = String::new();            let mut skips_remaining = input.offset;            let mut matches_found = 0;            let mut has_more_matches = false;             'outer: loop {                let search_result = futures::select! {                    result = rx.next().fuse() => result,                    _ = event_stream.cancelled_by_user().fuse() => {                        return Err("Search cancelled by user".to_string());                    }                };                let Some(SearchResult::Buffer { buffer, ranges }) = search_result else {                    break;                };                if ranges.is_empty() {                    continue;                }                 let (Some(path), mut parse_status) = buffer.read_with(cx, |buffer, cx| {                    (buffer.file().map(|file| file.full_path(cx)), buffer.parse_status())                }) else {                    continue;                };                 // Check if this file should be excluded based on its worktree settings                if let Ok(Some(project_path)) = project.read_with(cx, |project, cx| {                    project.find_project_path(&path, cx)                }) {                    if cx.update(|cx| {                        let worktree_settings = WorktreeSettings::get(Some((&project_path).into()), cx);                        worktree_settings.is_path_excluded(&project_path.path)                            || worktree_settings.is_path_private(&project_path.path)                    }) {                        continue;                    }                }                 while *parse_status.borrow() != ParseStatus::Idle {                    parse_status.changed().await.map_err(|e| e.to_string())?;                }                 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());                 let mut ranges = ranges                    .into_iter()                    .map(|range| {                        let matched = range.to_point(&snapshot);                        let matched_end_line_len = snapshot.line_len(matched.end.row);                        let full_lines = Point::new(matched.start.row, 0)..Point::new(matched.end.row, matched_end_line_len);                        let symbols = snapshot.symbols_containing(matched.start, None);                         if let Some(ancestor_node) = snapshot.syntax_ancestor(full_lines.clone()) {                            let full_ancestor_range = ancestor_node.byte_range().to_point(&snapshot);                            let end_row = full_ancestor_range.end.row.min(full_ancestor_range.start.row + MAX_ANCESTOR_LINES);                            let end_col = snapshot.line_len(end_row);                            let capped_ancestor_range = Point::new(full_ancestor_range.start.row, 0)..Point::new(end_row, end_col);                             if capped_ancestor_range.contains_inclusive(&full_lines) {                                return (capped_ancestor_range, Some(full_ancestor_range), symbols)                            }                        }                         let mut matched = matched;                        matched.start.column = 0;                        matched.start.row =                            matched.start.row.saturating_sub(CONTEXT_LINES);                        matched.end.row = cmp::min(                            snapshot.max_point().row,                            matched.end.row + CONTEXT_LINES,                        );                        matched.end.column = snapshot.line_len(matched.end.row);                         (matched, None, symbols)                    })                    .peekable();                 let mut file_header_written = false;                 while let Some((mut range, ancestor_range, parent_symbols)) = ranges.next(){                    if skips_remaining > 0 {                        skips_remaining -= 1;                        continue;                    }                     // We'd already found a full page of matches, and we just found one more.                    if matches_found >= RESULTS_PER_PAGE {                        has_more_matches = true;                        break 'outer;                    }                     while let Some((next_range, _, _)) = ranges.peek() {                        if range.end.row >= next_range.start.row {                            range.end = next_range.end;                            ranges.next();                        } else {                            break;                        }                    }                     if !file_header_written {                        writeln!(output, "\n## Matches in {}", path.display())                            .ok();                        file_header_written = true;                    }                     let end_row = range.end.row;                    output.push_str("\n### ");                     for symbol in parent_symbols {                        write!(output, "{} › ", symbol.text)                            .ok();                    }                     if range.start.row == end_row {                        writeln!(output, "L{}", range.start.row + 1)                            .ok();                    } else {                        writeln!(output, "L{}-{}", range.start.row + 1, end_row + 1)                            .ok();                    }                     output.push_str("```\n");                    output.extend(snapshot.text_for_range(range));                    output.push_str("\n```\n");                     if let Some(ancestor_range) = ancestor_range                        && end_row < ancestor_range.end.row {                            let remaining_lines = ancestor_range.end.row - end_row;                            writeln!(output, "\n{} lines remaining in ancestor node. Read the file to see all.", remaining_lines)                                .ok();                        }                     matches_found += 1;                }            }             if matches_found == 0 {                Ok("No matches found".into())            } else if has_more_matches {                Ok(format!(                    "Showing matches {}-{} (there were more matches found; use offset: {} to see next page):\n{output}",                    input.offset + 1,                    input.offset + matches_found,                    input.offset + RESULTS_PER_PAGE,                ))            } else {                Ok(format!("Found {matches_found} matches:\n{output}"))            }        })    }} #[cfg(test)]mod tests {    use crate::ToolCallEventStream;     use super::*;    use gpui::{TestAppContext, UpdateGlobal};    use project::{FakeFs, Project};    use serde_json::json;    use settings::SettingsStore;    use unindent::Unindent;    use util::path;     #[gpui::test]    async fn test_grep_tool_with_include_pattern(cx: &mut TestAppContext) {        init_test(cx);        cx.executor().allow_parking();         let fs = FakeFs::new(cx.executor());        fs.insert_tree(            path!("/root"),            serde_json::json!({                "src": {                    "main.rs": "fn main() {\n    println!(\"Hello, world!\");\n}",                    "utils": {                        "helper.rs": "fn helper() {\n    println!(\"I'm a helper!\");\n}",                    },                },                "tests": {                    "test_main.rs": "fn test_main() {\n    assert!(true);\n}",                }            }),        )        .await;         let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;         // Test with include pattern for Rust files inside the root of the project        let input = GrepToolInput {            regex: "println".to_string(),            include_pattern: Some("root/**/*.rs".to_string()),            offset: 0,            case_sensitive: false,        };         let result = run_grep_tool(input, project.clone(), cx).await;        assert!(result.contains("main.rs"), "Should find matches in main.rs");        assert!(            result.contains("helper.rs"),            "Should find matches in helper.rs"        );        assert!(            !result.contains("test_main.rs"),            "Should not include test_main.rs even though it's a .rs file (because it doesn't have the pattern)"        );         // Test with include pattern for src directory only        let input = GrepToolInput {            regex: "fn".to_string(),            include_pattern: Some("root/**/src/**".to_string()),            offset: 0,            case_sensitive: false,        };         let result = run_grep_tool(input, project.clone(), cx).await;        assert!(            result.contains("main.rs"),            "Should find matches in src/main.rs"        );        assert!(            result.contains("helper.rs"),            "Should find matches in src/utils/helper.rs"        );        assert!(            !result.contains("test_main.rs"),            "Should not include test_main.rs as it's not in src directory"        );         // Test with empty include pattern (should default to all files)        let input = GrepToolInput {            regex: "fn".to_string(),            include_pattern: None,            offset: 0,            case_sensitive: false,        };         let result = run_grep_tool(input, project.clone(), cx).await;        assert!(result.contains("main.rs"), "Should find matches in main.rs");        assert!(            result.contains("helper.rs"),            "Should find matches in helper.rs"        );        assert!(            result.contains("test_main.rs"),            "Should include test_main.rs"        );    }     #[gpui::test]    async fn test_grep_tool_with_case_sensitivity(cx: &mut TestAppContext) {        init_test(cx);        cx.executor().allow_parking();         let fs = FakeFs::new(cx.executor());        fs.insert_tree(            path!("/root"),            serde_json::json!({                "case_test.txt": "This file has UPPERCASE and lowercase text.\nUPPERCASE patterns should match only with case_sensitive: true",            }),        )        .await;         let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;         // Test case-insensitive search (default)        let input = GrepToolInput {            regex: "uppercase".to_string(),            include_pattern: Some("**/*.txt".to_string()),            offset: 0,            case_sensitive: false,        };         let result = run_grep_tool(input, project.clone(), cx).await;        assert!(            result.contains("UPPERCASE"),            "Case-insensitive search should match uppercase"        );         // Test case-sensitive search        let input = GrepToolInput {            regex: "uppercase".to_string(),            include_pattern: Some("**/*.txt".to_string()),            offset: 0,            case_sensitive: true,        };         let result = run_grep_tool(input, project.clone(), cx).await;        assert!(            !result.contains("UPPERCASE"),            "Case-sensitive search should not match uppercase"        );         // Test case-sensitive search        let input = GrepToolInput {            regex: "LOWERCASE".to_string(),            include_pattern: Some("**/*.txt".to_string()),            offset: 0,            case_sensitive: true,        };         let result = run_grep_tool(input, project.clone(), cx).await;         assert!(            !result.contains("lowercase"),            "Case-sensitive search should match lowercase"        );         // Test case-sensitive search for lowercase pattern        let input = GrepToolInput {            regex: "lowercase".to_string(),            include_pattern: Some("**/*.txt".to_string()),            offset: 0,            case_sensitive: true,        };         let result = run_grep_tool(input, project.clone(), cx).await;        assert!(

Lines 1–500 of 1199.