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