grep_tool.rs

   1use crate::{AgentTool, ToolCallEventStream};
   2use agent_client_protocol as acp;
   3use anyhow::{Result, anyhow};
   4use futures::StreamExt;
   5use gpui::{App, Entity, SharedString, Task};
   6use language::{OffsetRangeExt, ParseStatus, Point};
   7use project::{
   8    Project, WorktreeSettings,
   9    search::{SearchQuery, SearchResult},
  10};
  11use schemars::JsonSchema;
  12use serde::{Deserialize, Serialize};
  13use settings::Settings;
  14use std::{cmp, fmt::Write, sync::Arc};
  15use util::RangeExt;
  16use util::markdown::MarkdownInlineCode;
  17use util::paths::PathMatcher;
  18
  19/// Searches the contents of files in the project with a regular expression
  20///
  21/// - 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.
  22/// - Supports full regex syntax (eg. "log.*Error", "function\\s+\\w+", etc.)
  23/// - Pass an `include_pattern` if you know how to narrow your search on the files system
  24/// - Never use this tool to search for paths. Only search file contents with this tool.
  25/// - Use this tool when you need to find files containing specific patterns
  26/// - Results are paginated with 20 matches per page. Use the optional 'offset' parameter to request subsequent pages.
  27/// - DO NOT use HTML entities solely to escape characters in the tool parameters.
  28#[derive(Debug, Serialize, Deserialize, JsonSchema)]
  29pub struct GrepToolInput {
  30    /// A regex pattern to search for in the entire project. Note that the regex
  31    /// will be parsed by the Rust `regex` crate.
  32    ///
  33    /// Do NOT specify a path here! This will only be matched against the code **content**.
  34    pub regex: String,
  35    /// A glob pattern for the paths of files to include in the search.
  36    /// Supports standard glob patterns like "**/*.rs" or "src/**/*.ts".
  37    /// If omitted, all files in the project will be searched.
  38    pub include_pattern: Option<String>,
  39    /// Optional starting position for paginated results (0-based).
  40    /// When not provided, starts from the beginning.
  41    #[serde(default)]
  42    pub offset: u32,
  43    /// Whether the regex is case-sensitive. Defaults to false (case-insensitive).
  44    #[serde(default)]
  45    pub case_sensitive: bool,
  46}
  47
  48impl GrepToolInput {
  49    /// Which page of search results this is.
  50    pub fn page(&self) -> u32 {
  51        1 + (self.offset / RESULTS_PER_PAGE)
  52    }
  53}
  54
  55const RESULTS_PER_PAGE: u32 = 20;
  56
  57pub struct GrepTool {
  58    project: Entity<Project>,
  59}
  60
  61impl GrepTool {
  62    pub fn new(project: Entity<Project>) -> Self {
  63        Self { project }
  64    }
  65}
  66
  67impl AgentTool for GrepTool {
  68    type Input = GrepToolInput;
  69    type Output = String;
  70
  71    fn name(&self) -> SharedString {
  72        "grep".into()
  73    }
  74
  75    fn kind(&self) -> acp::ToolKind {
  76        acp::ToolKind::Search
  77    }
  78
  79    fn initial_title(&self, input: Result<Self::Input, serde_json::Value>) -> SharedString {
  80        match input {
  81            Ok(input) => {
  82                let page = input.page();
  83                let regex_str = MarkdownInlineCode(&input.regex);
  84                let case_info = if input.case_sensitive {
  85                    " (case-sensitive)"
  86                } else {
  87                    ""
  88                };
  89
  90                if page > 1 {
  91                    format!("Get page {page} of search results for regex {regex_str}{case_info}")
  92                } else {
  93                    format!("Search files for regex {regex_str}{case_info}")
  94                }
  95            }
  96            Err(_) => "Search with regex".into(),
  97        }
  98        .into()
  99    }
 100
 101    fn run(
 102        self: Arc<Self>,
 103        input: Self::Input,
 104        event_stream: ToolCallEventStream,
 105        cx: &mut App,
 106    ) -> Task<Result<Self::Output>> {
 107        const CONTEXT_LINES: u32 = 2;
 108        const MAX_ANCESTOR_LINES: u32 = 10;
 109
 110        let include_matcher = match PathMatcher::new(
 111            input
 112                .include_pattern
 113                .as_ref()
 114                .into_iter()
 115                .collect::<Vec<_>>(),
 116        ) {
 117            Ok(matcher) => matcher,
 118            Err(error) => {
 119                return Task::ready(Err(anyhow!("invalid include glob pattern: {error}")));
 120            }
 121        };
 122
 123        // Exclude global file_scan_exclusions and private_files settings
 124        let exclude_matcher = {
 125            let global_settings = WorktreeSettings::get_global(cx);
 126            let exclude_patterns = global_settings
 127                .file_scan_exclusions
 128                .sources()
 129                .iter()
 130                .chain(global_settings.private_files.sources().iter());
 131
 132            match PathMatcher::new(exclude_patterns) {
 133                Ok(matcher) => matcher,
 134                Err(error) => {
 135                    return Task::ready(Err(anyhow!("invalid exclude pattern: {error}")));
 136                }
 137            }
 138        };
 139
 140        let query = match SearchQuery::regex(
 141            &input.regex,
 142            false,
 143            input.case_sensitive,
 144            false,
 145            false,
 146            include_matcher,
 147            exclude_matcher,
 148            true, // Always match file include pattern against *full project paths* that start with a project root.
 149            None,
 150        ) {
 151            Ok(query) => query,
 152            Err(error) => return Task::ready(Err(error)),
 153        };
 154
 155        let results = self
 156            .project
 157            .update(cx, |project, cx| project.search(query, cx));
 158
 159        let project = self.project.downgrade();
 160        cx.spawn(async move |cx|  {
 161            futures::pin_mut!(results);
 162
 163            let mut output = String::new();
 164            let mut skips_remaining = input.offset;
 165            let mut matches_found = 0;
 166            let mut has_more_matches = false;
 167
 168            'outer: while let Some(SearchResult::Buffer { buffer, ranges }) = results.next().await {
 169                if ranges.is_empty() {
 170                    continue;
 171                }
 172
 173                let Ok((Some(path), mut parse_status)) = buffer.read_with(cx, |buffer, cx| {
 174                    (buffer.file().map(|file| file.full_path(cx)), buffer.parse_status())
 175                }) else {
 176                    continue;
 177                };
 178
 179                // Check if this file should be excluded based on its worktree settings
 180                if let Ok(Some(project_path)) = project.read_with(cx, |project, cx| {
 181                    project.find_project_path(&path, cx)
 182                }) {
 183                    if cx.update(|cx| {
 184                        let worktree_settings = WorktreeSettings::get(Some((&project_path).into()), cx);
 185                        worktree_settings.is_path_excluded(&project_path.path)
 186                            || worktree_settings.is_path_private(&project_path.path)
 187                    }).unwrap_or(false) {
 188                        continue;
 189                    }
 190                }
 191
 192                while *parse_status.borrow() != ParseStatus::Idle {
 193                    parse_status.changed().await?;
 194                }
 195
 196                let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot())?;
 197
 198                let mut ranges = ranges
 199                    .into_iter()
 200                    .map(|range| {
 201                        let matched = range.to_point(&snapshot);
 202                        let matched_end_line_len = snapshot.line_len(matched.end.row);
 203                        let full_lines = Point::new(matched.start.row, 0)..Point::new(matched.end.row, matched_end_line_len);
 204                        let symbols = snapshot.symbols_containing(matched.start, None);
 205
 206                        if let Some(ancestor_node) = snapshot.syntax_ancestor(full_lines.clone()) {
 207                            let full_ancestor_range = ancestor_node.byte_range().to_point(&snapshot);
 208                            let end_row = full_ancestor_range.end.row.min(full_ancestor_range.start.row + MAX_ANCESTOR_LINES);
 209                            let end_col = snapshot.line_len(end_row);
 210                            let capped_ancestor_range = Point::new(full_ancestor_range.start.row, 0)..Point::new(end_row, end_col);
 211
 212                            if capped_ancestor_range.contains_inclusive(&full_lines) {
 213                                return (capped_ancestor_range, Some(full_ancestor_range), symbols)
 214                            }
 215                        }
 216
 217                        let mut matched = matched;
 218                        matched.start.column = 0;
 219                        matched.start.row =
 220                            matched.start.row.saturating_sub(CONTEXT_LINES);
 221                        matched.end.row = cmp::min(
 222                            snapshot.max_point().row,
 223                            matched.end.row + CONTEXT_LINES,
 224                        );
 225                        matched.end.column = snapshot.line_len(matched.end.row);
 226
 227                        (matched, None, symbols)
 228                    })
 229                    .peekable();
 230
 231                let mut file_header_written = false;
 232
 233                while let Some((mut range, ancestor_range, parent_symbols)) = ranges.next(){
 234                    if skips_remaining > 0 {
 235                        skips_remaining -= 1;
 236                        continue;
 237                    }
 238
 239                    // We'd already found a full page of matches, and we just found one more.
 240                    if matches_found >= RESULTS_PER_PAGE {
 241                        has_more_matches = true;
 242                        break 'outer;
 243                    }
 244
 245                    while let Some((next_range, _, _)) = ranges.peek() {
 246                        if range.end.row >= next_range.start.row {
 247                            range.end = next_range.end;
 248                            ranges.next();
 249                        } else {
 250                            break;
 251                        }
 252                    }
 253
 254                    if !file_header_written {
 255                        writeln!(output, "\n## Matches in {}", path.display())?;
 256                        file_header_written = true;
 257                    }
 258
 259                    let end_row = range.end.row;
 260                    output.push_str("\n### ");
 261
 262                    if let Some(parent_symbols) = &parent_symbols {
 263                        for symbol in parent_symbols {
 264                            write!(output, "{} › ", symbol.text)?;
 265                        }
 266                    }
 267
 268                    if range.start.row == end_row {
 269                        writeln!(output, "L{}", range.start.row + 1)?;
 270                    } else {
 271                        writeln!(output, "L{}-{}", range.start.row + 1, end_row + 1)?;
 272                    }
 273
 274                    output.push_str("```\n");
 275                    output.extend(snapshot.text_for_range(range));
 276                    output.push_str("\n```\n");
 277
 278                    if let Some(ancestor_range) = ancestor_range {
 279                        if end_row < ancestor_range.end.row {
 280                            let remaining_lines = ancestor_range.end.row - end_row;
 281                            writeln!(output, "\n{} lines remaining in ancestor node. Read the file to see all.", remaining_lines)?;
 282                        }
 283                    }
 284
 285                    event_stream.update_fields(acp::ToolCallUpdateFields {
 286                        content: Some(vec![output.clone().into()]),
 287                        ..Default::default()
 288                    });
 289                    matches_found += 1;
 290                }
 291            }
 292
 293            let output = if matches_found == 0 {
 294                "No matches found".to_string()
 295            } else if has_more_matches {
 296                format!(
 297                    "Showing matches {}-{} (there were more matches found; use offset: {} to see next page):\n{output}",
 298                    input.offset + 1,
 299                    input.offset + matches_found,
 300                    input.offset + RESULTS_PER_PAGE,
 301                )
 302            } else {
 303                format!("Found {matches_found} matches:\n{output}")
 304            };
 305
 306            event_stream.update_fields(acp::ToolCallUpdateFields {
 307                content: Some(vec![output.clone().into()]),
 308                ..Default::default()
 309            });
 310
 311            Ok(output)
 312        })
 313    }
 314}
 315
 316#[cfg(test)]
 317mod tests {
 318    use crate::ToolCallEventStream;
 319
 320    use super::*;
 321    use gpui::{TestAppContext, UpdateGlobal};
 322    use language::{Language, LanguageConfig, LanguageMatcher};
 323    use project::{FakeFs, Project, WorktreeSettings};
 324    use serde_json::json;
 325    use settings::SettingsStore;
 326    use unindent::Unindent;
 327    use util::path;
 328
 329    #[gpui::test]
 330    async fn test_grep_tool_with_include_pattern(cx: &mut TestAppContext) {
 331        init_test(cx);
 332        cx.executor().allow_parking();
 333
 334        let fs = FakeFs::new(cx.executor().clone());
 335        fs.insert_tree(
 336            path!("/root"),
 337            serde_json::json!({
 338                "src": {
 339                    "main.rs": "fn main() {\n    println!(\"Hello, world!\");\n}",
 340                    "utils": {
 341                        "helper.rs": "fn helper() {\n    println!(\"I'm a helper!\");\n}",
 342                    },
 343                },
 344                "tests": {
 345                    "test_main.rs": "fn test_main() {\n    assert!(true);\n}",
 346                }
 347            }),
 348        )
 349        .await;
 350
 351        let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
 352
 353        // Test with include pattern for Rust files inside the root of the project
 354        let input = GrepToolInput {
 355            regex: "println".to_string(),
 356            include_pattern: Some("root/**/*.rs".to_string()),
 357            offset: 0,
 358            case_sensitive: false,
 359        };
 360
 361        let result = run_grep_tool(input, project.clone(), cx).await;
 362        assert!(result.contains("main.rs"), "Should find matches in main.rs");
 363        assert!(
 364            result.contains("helper.rs"),
 365            "Should find matches in helper.rs"
 366        );
 367        assert!(
 368            !result.contains("test_main.rs"),
 369            "Should not include test_main.rs even though it's a .rs file (because it doesn't have the pattern)"
 370        );
 371
 372        // Test with include pattern for src directory only
 373        let input = GrepToolInput {
 374            regex: "fn".to_string(),
 375            include_pattern: Some("root/**/src/**".to_string()),
 376            offset: 0,
 377            case_sensitive: false,
 378        };
 379
 380        let result = run_grep_tool(input, project.clone(), cx).await;
 381        assert!(
 382            result.contains("main.rs"),
 383            "Should find matches in src/main.rs"
 384        );
 385        assert!(
 386            result.contains("helper.rs"),
 387            "Should find matches in src/utils/helper.rs"
 388        );
 389        assert!(
 390            !result.contains("test_main.rs"),
 391            "Should not include test_main.rs as it's not in src directory"
 392        );
 393
 394        // Test with empty include pattern (should default to all files)
 395        let input = GrepToolInput {
 396            regex: "fn".to_string(),
 397            include_pattern: None,
 398            offset: 0,
 399            case_sensitive: false,
 400        };
 401
 402        let result = run_grep_tool(input, project.clone(), cx).await;
 403        assert!(result.contains("main.rs"), "Should find matches in main.rs");
 404        assert!(
 405            result.contains("helper.rs"),
 406            "Should find matches in helper.rs"
 407        );
 408        assert!(
 409            result.contains("test_main.rs"),
 410            "Should include test_main.rs"
 411        );
 412    }
 413
 414    #[gpui::test]
 415    async fn test_grep_tool_with_case_sensitivity(cx: &mut TestAppContext) {
 416        init_test(cx);
 417        cx.executor().allow_parking();
 418
 419        let fs = FakeFs::new(cx.executor().clone());
 420        fs.insert_tree(
 421            path!("/root"),
 422            serde_json::json!({
 423                "case_test.txt": "This file has UPPERCASE and lowercase text.\nUPPERCASE patterns should match only with case_sensitive: true",
 424            }),
 425        )
 426        .await;
 427
 428        let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
 429
 430        // Test case-insensitive search (default)
 431        let input = GrepToolInput {
 432            regex: "uppercase".to_string(),
 433            include_pattern: Some("**/*.txt".to_string()),
 434            offset: 0,
 435            case_sensitive: false,
 436        };
 437
 438        let result = run_grep_tool(input, project.clone(), cx).await;
 439        assert!(
 440            result.contains("UPPERCASE"),
 441            "Case-insensitive search should match uppercase"
 442        );
 443
 444        // Test case-sensitive search
 445        let input = GrepToolInput {
 446            regex: "uppercase".to_string(),
 447            include_pattern: Some("**/*.txt".to_string()),
 448            offset: 0,
 449            case_sensitive: true,
 450        };
 451
 452        let result = run_grep_tool(input, project.clone(), cx).await;
 453        assert!(
 454            !result.contains("UPPERCASE"),
 455            "Case-sensitive search should not match uppercase"
 456        );
 457
 458        // Test case-sensitive search
 459        let input = GrepToolInput {
 460            regex: "LOWERCASE".to_string(),
 461            include_pattern: Some("**/*.txt".to_string()),
 462            offset: 0,
 463            case_sensitive: true,
 464        };
 465
 466        let result = run_grep_tool(input, project.clone(), cx).await;
 467
 468        assert!(
 469            !result.contains("lowercase"),
 470            "Case-sensitive search should match lowercase"
 471        );
 472
 473        // Test case-sensitive search for lowercase pattern
 474        let input = GrepToolInput {
 475            regex: "lowercase".to_string(),
 476            include_pattern: Some("**/*.txt".to_string()),
 477            offset: 0,
 478            case_sensitive: true,
 479        };
 480
 481        let result = run_grep_tool(input, project.clone(), cx).await;
 482        assert!(
 483            result.contains("lowercase"),
 484            "Case-sensitive search should match lowercase text"
 485        );
 486    }
 487
 488    /// Helper function to set up a syntax test environment
 489    async fn setup_syntax_test(cx: &mut TestAppContext) -> Entity<Project> {
 490        use unindent::Unindent;
 491        init_test(cx);
 492        cx.executor().allow_parking();
 493
 494        let fs = FakeFs::new(cx.executor().clone());
 495
 496        // Create test file with syntax structures
 497        fs.insert_tree(
 498            path!("/root"),
 499            serde_json::json!({
 500                "test_syntax.rs": r#"
 501                    fn top_level_function() {
 502                        println!("This is at the top level");
 503                    }
 504
 505                    mod feature_module {
 506                        pub mod nested_module {
 507                            pub fn nested_function(
 508                                first_arg: String,
 509                                second_arg: i32,
 510                            ) {
 511                                println!("Function in nested module");
 512                                println!("{first_arg}");
 513                                println!("{second_arg}");
 514                            }
 515                        }
 516                    }
 517
 518                    struct MyStruct {
 519                        field1: String,
 520                        field2: i32,
 521                    }
 522
 523                    impl MyStruct {
 524                        fn method_with_block() {
 525                            let condition = true;
 526                            if condition {
 527                                println!("Inside if block");
 528                            }
 529                        }
 530
 531                        fn long_function() {
 532                            println!("Line 1");
 533                            println!("Line 2");
 534                            println!("Line 3");
 535                            println!("Line 4");
 536                            println!("Line 5");
 537                            println!("Line 6");
 538                            println!("Line 7");
 539                            println!("Line 8");
 540                            println!("Line 9");
 541                            println!("Line 10");
 542                            println!("Line 11");
 543                            println!("Line 12");
 544                        }
 545                    }
 546
 547                    trait Processor {
 548                        fn process(&self, input: &str) -> String;
 549                    }
 550
 551                    impl Processor for MyStruct {
 552                        fn process(&self, input: &str) -> String {
 553                            format!("Processed: {}", input)
 554                        }
 555                    }
 556                "#.unindent().trim(),
 557            }),
 558        )
 559        .await;
 560
 561        let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
 562
 563        project.update(cx, |project, _cx| {
 564            project.languages().add(rust_lang().into())
 565        });
 566
 567        project
 568    }
 569
 570    #[gpui::test]
 571    async fn test_grep_top_level_function(cx: &mut TestAppContext) {
 572        let project = setup_syntax_test(cx).await;
 573
 574        // Test: Line at the top level of the file
 575        let input = GrepToolInput {
 576            regex: "This is at the top level".to_string(),
 577            include_pattern: Some("**/*.rs".to_string()),
 578            offset: 0,
 579            case_sensitive: false,
 580        };
 581
 582        let result = run_grep_tool(input, project.clone(), cx).await;
 583        let expected = r#"
 584            Found 1 matches:
 585
 586            ## Matches in root/test_syntax.rs
 587
 588            ### fn top_level_function › L1-3
 589            ```
 590            fn top_level_function() {
 591                println!("This is at the top level");
 592            }
 593            ```
 594            "#
 595        .unindent();
 596        assert_eq!(result, expected);
 597    }
 598
 599    #[gpui::test]
 600    async fn test_grep_function_body(cx: &mut TestAppContext) {
 601        let project = setup_syntax_test(cx).await;
 602
 603        // Test: Line inside a function body
 604        let input = GrepToolInput {
 605            regex: "Function in nested module".to_string(),
 606            include_pattern: Some("**/*.rs".to_string()),
 607            offset: 0,
 608            case_sensitive: false,
 609        };
 610
 611        let result = run_grep_tool(input, project.clone(), cx).await;
 612        let expected = r#"
 613            Found 1 matches:
 614
 615            ## Matches in root/test_syntax.rs
 616
 617            ### mod feature_module › pub mod nested_module › pub fn nested_function › L10-14
 618            ```
 619                    ) {
 620                        println!("Function in nested module");
 621                        println!("{first_arg}");
 622                        println!("{second_arg}");
 623                    }
 624            ```
 625            "#
 626        .unindent();
 627        assert_eq!(result, expected);
 628    }
 629
 630    #[gpui::test]
 631    async fn test_grep_function_args_and_body(cx: &mut TestAppContext) {
 632        let project = setup_syntax_test(cx).await;
 633
 634        // Test: Line with a function argument
 635        let input = GrepToolInput {
 636            regex: "second_arg".to_string(),
 637            include_pattern: Some("**/*.rs".to_string()),
 638            offset: 0,
 639            case_sensitive: false,
 640        };
 641
 642        let result = run_grep_tool(input, project.clone(), cx).await;
 643        let expected = r#"
 644            Found 1 matches:
 645
 646            ## Matches in root/test_syntax.rs
 647
 648            ### mod feature_module › pub mod nested_module › pub fn nested_function › L7-14
 649            ```
 650                    pub fn nested_function(
 651                        first_arg: String,
 652                        second_arg: i32,
 653                    ) {
 654                        println!("Function in nested module");
 655                        println!("{first_arg}");
 656                        println!("{second_arg}");
 657                    }
 658            ```
 659            "#
 660        .unindent();
 661        assert_eq!(result, expected);
 662    }
 663
 664    #[gpui::test]
 665    async fn test_grep_if_block(cx: &mut TestAppContext) {
 666        use unindent::Unindent;
 667        let project = setup_syntax_test(cx).await;
 668
 669        // Test: Line inside an if block
 670        let input = GrepToolInput {
 671            regex: "Inside if block".to_string(),
 672            include_pattern: Some("**/*.rs".to_string()),
 673            offset: 0,
 674            case_sensitive: false,
 675        };
 676
 677        let result = run_grep_tool(input, project.clone(), cx).await;
 678        let expected = r#"
 679            Found 1 matches:
 680
 681            ## Matches in root/test_syntax.rs
 682
 683            ### impl MyStruct › fn method_with_block › L26-28
 684            ```
 685                    if condition {
 686                        println!("Inside if block");
 687                    }
 688            ```
 689            "#
 690        .unindent();
 691        assert_eq!(result, expected);
 692    }
 693
 694    #[gpui::test]
 695    async fn test_grep_long_function_top(cx: &mut TestAppContext) {
 696        use unindent::Unindent;
 697        let project = setup_syntax_test(cx).await;
 698
 699        // Test: Line in the middle of a long function - should show message about remaining lines
 700        let input = GrepToolInput {
 701            regex: "Line 5".to_string(),
 702            include_pattern: Some("**/*.rs".to_string()),
 703            offset: 0,
 704            case_sensitive: false,
 705        };
 706
 707        let result = run_grep_tool(input, project.clone(), cx).await;
 708        let expected = r#"
 709            Found 1 matches:
 710
 711            ## Matches in root/test_syntax.rs
 712
 713            ### impl MyStruct › fn long_function › L31-41
 714            ```
 715                fn long_function() {
 716                    println!("Line 1");
 717                    println!("Line 2");
 718                    println!("Line 3");
 719                    println!("Line 4");
 720                    println!("Line 5");
 721                    println!("Line 6");
 722                    println!("Line 7");
 723                    println!("Line 8");
 724                    println!("Line 9");
 725                    println!("Line 10");
 726            ```
 727
 728            3 lines remaining in ancestor node. Read the file to see all.
 729            "#
 730        .unindent();
 731        assert_eq!(result, expected);
 732    }
 733
 734    #[gpui::test]
 735    async fn test_grep_long_function_bottom(cx: &mut TestAppContext) {
 736        use unindent::Unindent;
 737        let project = setup_syntax_test(cx).await;
 738
 739        // Test: Line in the long function
 740        let input = GrepToolInput {
 741            regex: "Line 12".to_string(),
 742            include_pattern: Some("**/*.rs".to_string()),
 743            offset: 0,
 744            case_sensitive: false,
 745        };
 746
 747        let result = run_grep_tool(input, project.clone(), cx).await;
 748        let expected = r#"
 749            Found 1 matches:
 750
 751            ## Matches in root/test_syntax.rs
 752
 753            ### impl MyStruct › fn long_function › L41-45
 754            ```
 755                    println!("Line 10");
 756                    println!("Line 11");
 757                    println!("Line 12");
 758                }
 759            }
 760            ```
 761            "#
 762        .unindent();
 763        assert_eq!(result, expected);
 764    }
 765
 766    async fn run_grep_tool(
 767        input: GrepToolInput,
 768        project: Entity<Project>,
 769        cx: &mut TestAppContext,
 770    ) -> String {
 771        let tool = Arc::new(GrepTool { project });
 772        let task = cx.update(|cx| tool.run(input, ToolCallEventStream::test().0, cx));
 773
 774        match task.await {
 775            Ok(result) => {
 776                if cfg!(windows) {
 777                    result.replace("root\\", "root/")
 778                } else {
 779                    result.to_string()
 780                }
 781            }
 782            Err(e) => panic!("Failed to run grep tool: {}", e),
 783        }
 784    }
 785
 786    fn init_test(cx: &mut TestAppContext) {
 787        cx.update(|cx| {
 788            let settings_store = SettingsStore::test(cx);
 789            cx.set_global(settings_store);
 790            language::init(cx);
 791            Project::init_settings(cx);
 792        });
 793    }
 794
 795    fn rust_lang() -> Language {
 796        Language::new(
 797            LanguageConfig {
 798                name: "Rust".into(),
 799                matcher: LanguageMatcher {
 800                    path_suffixes: vec!["rs".to_string()],
 801                    ..Default::default()
 802                },
 803                ..Default::default()
 804            },
 805            Some(tree_sitter_rust::LANGUAGE.into()),
 806        )
 807        .with_outline_query(include_str!("../../../languages/src/rust/outline.scm"))
 808        .unwrap()
 809    }
 810
 811    #[gpui::test]
 812    async fn test_grep_security_boundaries(cx: &mut TestAppContext) {
 813        init_test(cx);
 814
 815        let fs = FakeFs::new(cx.executor());
 816
 817        fs.insert_tree(
 818            path!("/"),
 819            json!({
 820                "project_root": {
 821                    "allowed_file.rs": "fn main() { println!(\"This file is in the project\"); }",
 822                    ".mysecrets": "SECRET_KEY=abc123\nfn secret() { /* private */ }",
 823                    ".secretdir": {
 824                        "config": "fn special_configuration() { /* excluded */ }"
 825                    },
 826                    ".mymetadata": "fn custom_metadata() { /* excluded */ }",
 827                    "subdir": {
 828                        "normal_file.rs": "fn normal_file_content() { /* Normal */ }",
 829                        "special.privatekey": "fn private_key_content() { /* private */ }",
 830                        "data.mysensitive": "fn sensitive_data() { /* private */ }"
 831                    }
 832                },
 833                "outside_project": {
 834                    "sensitive_file.rs": "fn outside_function() { /* This file is outside the project */ }"
 835                }
 836            }),
 837        )
 838        .await;
 839
 840        cx.update(|cx| {
 841            use gpui::UpdateGlobal;
 842            use project::WorktreeSettings;
 843            use settings::SettingsStore;
 844            SettingsStore::update_global(cx, |store, cx| {
 845                store.update_user_settings::<WorktreeSettings>(cx, |settings| {
 846                    settings.file_scan_exclusions = Some(vec![
 847                        "**/.secretdir".to_string(),
 848                        "**/.mymetadata".to_string(),
 849                    ]);
 850                    settings.private_files = Some(vec![
 851                        "**/.mysecrets".to_string(),
 852                        "**/*.privatekey".to_string(),
 853                        "**/*.mysensitive".to_string(),
 854                    ]);
 855                });
 856            });
 857        });
 858
 859        let project = Project::test(fs.clone(), [path!("/project_root").as_ref()], cx).await;
 860
 861        // Searching for files outside the project worktree should return no results
 862        let result = run_grep_tool(
 863            GrepToolInput {
 864                regex: "outside_function".to_string(),
 865                include_pattern: None,
 866                offset: 0,
 867                case_sensitive: false,
 868            },
 869            project.clone(),
 870            cx,
 871        )
 872        .await;
 873        let paths = extract_paths_from_results(&result);
 874        assert!(
 875            paths.is_empty(),
 876            "grep_tool should not find files outside the project worktree"
 877        );
 878
 879        // Searching within the project should succeed
 880        let result = run_grep_tool(
 881            GrepToolInput {
 882                regex: "main".to_string(),
 883                include_pattern: None,
 884                offset: 0,
 885                case_sensitive: false,
 886            },
 887            project.clone(),
 888            cx,
 889        )
 890        .await;
 891        let paths = extract_paths_from_results(&result);
 892        assert!(
 893            paths.iter().any(|p| p.contains("allowed_file.rs")),
 894            "grep_tool should be able to search files inside worktrees"
 895        );
 896
 897        // Searching files that match file_scan_exclusions should return no results
 898        let result = run_grep_tool(
 899            GrepToolInput {
 900                regex: "special_configuration".to_string(),
 901                include_pattern: None,
 902                offset: 0,
 903                case_sensitive: false,
 904            },
 905            project.clone(),
 906            cx,
 907        )
 908        .await;
 909        let paths = extract_paths_from_results(&result);
 910        assert!(
 911            paths.is_empty(),
 912            "grep_tool should not search files in .secretdir (file_scan_exclusions)"
 913        );
 914
 915        let result = run_grep_tool(
 916            GrepToolInput {
 917                regex: "custom_metadata".to_string(),
 918                include_pattern: None,
 919                offset: 0,
 920                case_sensitive: false,
 921            },
 922            project.clone(),
 923            cx,
 924        )
 925        .await;
 926        let paths = extract_paths_from_results(&result);
 927        assert!(
 928            paths.is_empty(),
 929            "grep_tool should not search .mymetadata files (file_scan_exclusions)"
 930        );
 931
 932        // Searching private files should return no results
 933        let result = run_grep_tool(
 934            GrepToolInput {
 935                regex: "SECRET_KEY".to_string(),
 936                include_pattern: None,
 937                offset: 0,
 938                case_sensitive: false,
 939            },
 940            project.clone(),
 941            cx,
 942        )
 943        .await;
 944        let paths = extract_paths_from_results(&result);
 945        assert!(
 946            paths.is_empty(),
 947            "grep_tool should not search .mysecrets (private_files)"
 948        );
 949
 950        let result = run_grep_tool(
 951            GrepToolInput {
 952                regex: "private_key_content".to_string(),
 953                include_pattern: None,
 954                offset: 0,
 955                case_sensitive: false,
 956            },
 957            project.clone(),
 958            cx,
 959        )
 960        .await;
 961        let paths = extract_paths_from_results(&result);
 962
 963        assert!(
 964            paths.is_empty(),
 965            "grep_tool should not search .privatekey files (private_files)"
 966        );
 967
 968        let result = run_grep_tool(
 969            GrepToolInput {
 970                regex: "sensitive_data".to_string(),
 971                include_pattern: None,
 972                offset: 0,
 973                case_sensitive: false,
 974            },
 975            project.clone(),
 976            cx,
 977        )
 978        .await;
 979        let paths = extract_paths_from_results(&result);
 980        assert!(
 981            paths.is_empty(),
 982            "grep_tool should not search .mysensitive files (private_files)"
 983        );
 984
 985        // Searching a normal file should still work, even with private_files configured
 986        let result = run_grep_tool(
 987            GrepToolInput {
 988                regex: "normal_file_content".to_string(),
 989                include_pattern: None,
 990                offset: 0,
 991                case_sensitive: false,
 992            },
 993            project.clone(),
 994            cx,
 995        )
 996        .await;
 997        let paths = extract_paths_from_results(&result);
 998        assert!(
 999            paths.iter().any(|p| p.contains("normal_file.rs")),
1000            "Should be able to search normal files"
1001        );
1002
1003        // Path traversal attempts with .. in include_pattern should not escape project
1004        let result = run_grep_tool(
1005            GrepToolInput {
1006                regex: "outside_function".to_string(),
1007                include_pattern: Some("../outside_project/**/*.rs".to_string()),
1008                offset: 0,
1009                case_sensitive: false,
1010            },
1011            project.clone(),
1012            cx,
1013        )
1014        .await;
1015        let paths = extract_paths_from_results(&result);
1016        assert!(
1017            paths.is_empty(),
1018            "grep_tool should not allow escaping project boundaries with relative paths"
1019        );
1020    }
1021
1022    #[gpui::test]
1023    async fn test_grep_with_multiple_worktree_settings(cx: &mut TestAppContext) {
1024        init_test(cx);
1025
1026        let fs = FakeFs::new(cx.executor());
1027
1028        // Create first worktree with its own private files
1029        fs.insert_tree(
1030            path!("/worktree1"),
1031            json!({
1032                ".zed": {
1033                    "settings.json": r#"{
1034                        "file_scan_exclusions": ["**/fixture.*"],
1035                        "private_files": ["**/secret.rs"]
1036                    }"#
1037                },
1038                "src": {
1039                    "main.rs": "fn main() { let secret_key = \"hidden\"; }",
1040                    "secret.rs": "const API_KEY: &str = \"secret_value\";",
1041                    "utils.rs": "pub fn get_config() -> String { \"config\".to_string() }"
1042                },
1043                "tests": {
1044                    "test.rs": "fn test_secret() { assert!(true); }",
1045                    "fixture.sql": "SELECT * FROM secret_table;"
1046                }
1047            }),
1048        )
1049        .await;
1050
1051        // Create second worktree with different private files
1052        fs.insert_tree(
1053            path!("/worktree2"),
1054            json!({
1055                ".zed": {
1056                    "settings.json": r#"{
1057                        "file_scan_exclusions": ["**/internal.*"],
1058                        "private_files": ["**/private.js", "**/data.json"]
1059                    }"#
1060                },
1061                "lib": {
1062                    "public.js": "export function getSecret() { return 'public'; }",
1063                    "private.js": "const SECRET_KEY = \"private_value\";",
1064                    "data.json": "{\"secret_data\": \"hidden\"}"
1065                },
1066                "docs": {
1067                    "README.md": "# Documentation with secret info",
1068                    "internal.md": "Internal secret documentation"
1069                }
1070            }),
1071        )
1072        .await;
1073
1074        // Set global settings
1075        cx.update(|cx| {
1076            SettingsStore::update_global(cx, |store, cx| {
1077                store.update_user_settings::<WorktreeSettings>(cx, |settings| {
1078                    settings.file_scan_exclusions =
1079                        Some(vec!["**/.git".to_string(), "**/node_modules".to_string()]);
1080                    settings.private_files = Some(vec!["**/.env".to_string()]);
1081                });
1082            });
1083        });
1084
1085        let project = Project::test(
1086            fs.clone(),
1087            [path!("/worktree1").as_ref(), path!("/worktree2").as_ref()],
1088            cx,
1089        )
1090        .await;
1091
1092        // Wait for worktrees to be fully scanned
1093        cx.executor().run_until_parked();
1094
1095        // Search for "secret" - should exclude files based on worktree-specific settings
1096        let result = run_grep_tool(
1097            GrepToolInput {
1098                regex: "secret".to_string(),
1099                include_pattern: None,
1100                offset: 0,
1101                case_sensitive: false,
1102            },
1103            project.clone(),
1104            cx,
1105        )
1106        .await;
1107        let paths = extract_paths_from_results(&result);
1108
1109        // Should find matches in non-private files
1110        assert!(
1111            paths.iter().any(|p| p.contains("main.rs")),
1112            "Should find 'secret' in worktree1/src/main.rs"
1113        );
1114        assert!(
1115            paths.iter().any(|p| p.contains("test.rs")),
1116            "Should find 'secret' in worktree1/tests/test.rs"
1117        );
1118        assert!(
1119            paths.iter().any(|p| p.contains("public.js")),
1120            "Should find 'secret' in worktree2/lib/public.js"
1121        );
1122        assert!(
1123            paths.iter().any(|p| p.contains("README.md")),
1124            "Should find 'secret' in worktree2/docs/README.md"
1125        );
1126
1127        // Should NOT find matches in private/excluded files based on worktree settings
1128        assert!(
1129            !paths.iter().any(|p| p.contains("secret.rs")),
1130            "Should not search in worktree1/src/secret.rs (local private_files)"
1131        );
1132        assert!(
1133            !paths.iter().any(|p| p.contains("fixture.sql")),
1134            "Should not search in worktree1/tests/fixture.sql (local file_scan_exclusions)"
1135        );
1136        assert!(
1137            !paths.iter().any(|p| p.contains("private.js")),
1138            "Should not search in worktree2/lib/private.js (local private_files)"
1139        );
1140        assert!(
1141            !paths.iter().any(|p| p.contains("data.json")),
1142            "Should not search in worktree2/lib/data.json (local private_files)"
1143        );
1144        assert!(
1145            !paths.iter().any(|p| p.contains("internal.md")),
1146            "Should not search in worktree2/docs/internal.md (local file_scan_exclusions)"
1147        );
1148
1149        // Test with `include_pattern` specific to one worktree
1150        let result = run_grep_tool(
1151            GrepToolInput {
1152                regex: "secret".to_string(),
1153                include_pattern: Some("worktree1/**/*.rs".to_string()),
1154                offset: 0,
1155                case_sensitive: false,
1156            },
1157            project.clone(),
1158            cx,
1159        )
1160        .await;
1161
1162        let paths = extract_paths_from_results(&result);
1163
1164        // Should only find matches in worktree1 *.rs files (excluding private ones)
1165        assert!(
1166            paths.iter().any(|p| p.contains("main.rs")),
1167            "Should find match in worktree1/src/main.rs"
1168        );
1169        assert!(
1170            paths.iter().any(|p| p.contains("test.rs")),
1171            "Should find match in worktree1/tests/test.rs"
1172        );
1173        assert!(
1174            !paths.iter().any(|p| p.contains("secret.rs")),
1175            "Should not find match in excluded worktree1/src/secret.rs"
1176        );
1177        assert!(
1178            paths.iter().all(|p| !p.contains("worktree2")),
1179            "Should not find any matches in worktree2"
1180        );
1181    }
1182
1183    // Helper function to extract file paths from grep results
1184    fn extract_paths_from_results(results: &str) -> Vec<String> {
1185        results
1186            .lines()
1187            .filter(|line| line.starts_with("## Matches in "))
1188            .map(|line| {
1189                line.strip_prefix("## Matches in ")
1190                    .unwrap()
1191                    .trim()
1192                    .to_string()
1193            })
1194            .collect()
1195    }
1196}