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