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                    matches_found += 1;
 286                }
 287            }
 288
 289            if matches_found == 0 {
 290                Ok("No matches found".into())
 291            } else if has_more_matches {
 292                Ok(format!(
 293                    "Showing matches {}-{} (there were more matches found; use offset: {} to see next page):\n{output}",
 294                    input.offset + 1,
 295                    input.offset + matches_found,
 296                    input.offset + RESULTS_PER_PAGE,
 297                ))
 298            } else {
 299                Ok(format!("Found {matches_found} matches:\n{output}"))
 300            }
 301        })
 302    }
 303}
 304
 305#[cfg(test)]
 306mod tests {
 307    use crate::ToolCallEventStream;
 308
 309    use super::*;
 310    use gpui::{TestAppContext, UpdateGlobal};
 311    use language::{Language, LanguageConfig, LanguageMatcher};
 312    use project::{FakeFs, Project, WorktreeSettings};
 313    use serde_json::json;
 314    use settings::SettingsStore;
 315    use unindent::Unindent;
 316    use util::path;
 317
 318    #[gpui::test]
 319    async fn test_grep_tool_with_include_pattern(cx: &mut TestAppContext) {
 320        init_test(cx);
 321        cx.executor().allow_parking();
 322
 323        let fs = FakeFs::new(cx.executor().clone());
 324        fs.insert_tree(
 325            path!("/root"),
 326            serde_json::json!({
 327                "src": {
 328                    "main.rs": "fn main() {\n    println!(\"Hello, world!\");\n}",
 329                    "utils": {
 330                        "helper.rs": "fn helper() {\n    println!(\"I'm a helper!\");\n}",
 331                    },
 332                },
 333                "tests": {
 334                    "test_main.rs": "fn test_main() {\n    assert!(true);\n}",
 335                }
 336            }),
 337        )
 338        .await;
 339
 340        let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
 341
 342        // Test with include pattern for Rust files inside the root of the project
 343        let input = GrepToolInput {
 344            regex: "println".to_string(),
 345            include_pattern: Some("root/**/*.rs".to_string()),
 346            offset: 0,
 347            case_sensitive: false,
 348        };
 349
 350        let result = run_grep_tool(input, project.clone(), cx).await;
 351        assert!(result.contains("main.rs"), "Should find matches in main.rs");
 352        assert!(
 353            result.contains("helper.rs"),
 354            "Should find matches in helper.rs"
 355        );
 356        assert!(
 357            !result.contains("test_main.rs"),
 358            "Should not include test_main.rs even though it's a .rs file (because it doesn't have the pattern)"
 359        );
 360
 361        // Test with include pattern for src directory only
 362        let input = GrepToolInput {
 363            regex: "fn".to_string(),
 364            include_pattern: Some("root/**/src/**".to_string()),
 365            offset: 0,
 366            case_sensitive: false,
 367        };
 368
 369        let result = run_grep_tool(input, project.clone(), cx).await;
 370        assert!(
 371            result.contains("main.rs"),
 372            "Should find matches in src/main.rs"
 373        );
 374        assert!(
 375            result.contains("helper.rs"),
 376            "Should find matches in src/utils/helper.rs"
 377        );
 378        assert!(
 379            !result.contains("test_main.rs"),
 380            "Should not include test_main.rs as it's not in src directory"
 381        );
 382
 383        // Test with empty include pattern (should default to all files)
 384        let input = GrepToolInput {
 385            regex: "fn".to_string(),
 386            include_pattern: None,
 387            offset: 0,
 388            case_sensitive: false,
 389        };
 390
 391        let result = run_grep_tool(input, project.clone(), cx).await;
 392        assert!(result.contains("main.rs"), "Should find matches in main.rs");
 393        assert!(
 394            result.contains("helper.rs"),
 395            "Should find matches in helper.rs"
 396        );
 397        assert!(
 398            result.contains("test_main.rs"),
 399            "Should include test_main.rs"
 400        );
 401    }
 402
 403    #[gpui::test]
 404    async fn test_grep_tool_with_case_sensitivity(cx: &mut TestAppContext) {
 405        init_test(cx);
 406        cx.executor().allow_parking();
 407
 408        let fs = FakeFs::new(cx.executor().clone());
 409        fs.insert_tree(
 410            path!("/root"),
 411            serde_json::json!({
 412                "case_test.txt": "This file has UPPERCASE and lowercase text.\nUPPERCASE patterns should match only with case_sensitive: true",
 413            }),
 414        )
 415        .await;
 416
 417        let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
 418
 419        // Test case-insensitive search (default)
 420        let input = GrepToolInput {
 421            regex: "uppercase".to_string(),
 422            include_pattern: Some("**/*.txt".to_string()),
 423            offset: 0,
 424            case_sensitive: false,
 425        };
 426
 427        let result = run_grep_tool(input, project.clone(), cx).await;
 428        assert!(
 429            result.contains("UPPERCASE"),
 430            "Case-insensitive search should match uppercase"
 431        );
 432
 433        // Test case-sensitive search
 434        let input = GrepToolInput {
 435            regex: "uppercase".to_string(),
 436            include_pattern: Some("**/*.txt".to_string()),
 437            offset: 0,
 438            case_sensitive: true,
 439        };
 440
 441        let result = run_grep_tool(input, project.clone(), cx).await;
 442        assert!(
 443            !result.contains("UPPERCASE"),
 444            "Case-sensitive search should not match uppercase"
 445        );
 446
 447        // Test case-sensitive search
 448        let input = GrepToolInput {
 449            regex: "LOWERCASE".to_string(),
 450            include_pattern: Some("**/*.txt".to_string()),
 451            offset: 0,
 452            case_sensitive: true,
 453        };
 454
 455        let result = run_grep_tool(input, project.clone(), cx).await;
 456
 457        assert!(
 458            !result.contains("lowercase"),
 459            "Case-sensitive search should match lowercase"
 460        );
 461
 462        // Test case-sensitive search for lowercase pattern
 463        let input = GrepToolInput {
 464            regex: "lowercase".to_string(),
 465            include_pattern: Some("**/*.txt".to_string()),
 466            offset: 0,
 467            case_sensitive: true,
 468        };
 469
 470        let result = run_grep_tool(input, project.clone(), cx).await;
 471        assert!(
 472            result.contains("lowercase"),
 473            "Case-sensitive search should match lowercase text"
 474        );
 475    }
 476
 477    /// Helper function to set up a syntax test environment
 478    async fn setup_syntax_test(cx: &mut TestAppContext) -> Entity<Project> {
 479        use unindent::Unindent;
 480        init_test(cx);
 481        cx.executor().allow_parking();
 482
 483        let fs = FakeFs::new(cx.executor().clone());
 484
 485        // Create test file with syntax structures
 486        fs.insert_tree(
 487            path!("/root"),
 488            serde_json::json!({
 489                "test_syntax.rs": r#"
 490                    fn top_level_function() {
 491                        println!("This is at the top level");
 492                    }
 493
 494                    mod feature_module {
 495                        pub mod nested_module {
 496                            pub fn nested_function(
 497                                first_arg: String,
 498                                second_arg: i32,
 499                            ) {
 500                                println!("Function in nested module");
 501                                println!("{first_arg}");
 502                                println!("{second_arg}");
 503                            }
 504                        }
 505                    }
 506
 507                    struct MyStruct {
 508                        field1: String,
 509                        field2: i32,
 510                    }
 511
 512                    impl MyStruct {
 513                        fn method_with_block() {
 514                            let condition = true;
 515                            if condition {
 516                                println!("Inside if block");
 517                            }
 518                        }
 519
 520                        fn long_function() {
 521                            println!("Line 1");
 522                            println!("Line 2");
 523                            println!("Line 3");
 524                            println!("Line 4");
 525                            println!("Line 5");
 526                            println!("Line 6");
 527                            println!("Line 7");
 528                            println!("Line 8");
 529                            println!("Line 9");
 530                            println!("Line 10");
 531                            println!("Line 11");
 532                            println!("Line 12");
 533                        }
 534                    }
 535
 536                    trait Processor {
 537                        fn process(&self, input: &str) -> String;
 538                    }
 539
 540                    impl Processor for MyStruct {
 541                        fn process(&self, input: &str) -> String {
 542                            format!("Processed: {}", input)
 543                        }
 544                    }
 545                "#.unindent().trim(),
 546            }),
 547        )
 548        .await;
 549
 550        let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
 551
 552        project.update(cx, |project, _cx| {
 553            project.languages().add(rust_lang().into())
 554        });
 555
 556        project
 557    }
 558
 559    #[gpui::test]
 560    async fn test_grep_top_level_function(cx: &mut TestAppContext) {
 561        let project = setup_syntax_test(cx).await;
 562
 563        // Test: Line at the top level of the file
 564        let input = GrepToolInput {
 565            regex: "This is at the top level".to_string(),
 566            include_pattern: Some("**/*.rs".to_string()),
 567            offset: 0,
 568            case_sensitive: false,
 569        };
 570
 571        let result = run_grep_tool(input, project.clone(), cx).await;
 572        let expected = r#"
 573            Found 1 matches:
 574
 575            ## Matches in root/test_syntax.rs
 576
 577            ### fn top_level_function › L1-3
 578            ```
 579            fn top_level_function() {
 580                println!("This is at the top level");
 581            }
 582            ```
 583            "#
 584        .unindent();
 585        assert_eq!(result, expected);
 586    }
 587
 588    #[gpui::test]
 589    async fn test_grep_function_body(cx: &mut TestAppContext) {
 590        let project = setup_syntax_test(cx).await;
 591
 592        // Test: Line inside a function body
 593        let input = GrepToolInput {
 594            regex: "Function in nested module".to_string(),
 595            include_pattern: Some("**/*.rs".to_string()),
 596            offset: 0,
 597            case_sensitive: false,
 598        };
 599
 600        let result = run_grep_tool(input, project.clone(), cx).await;
 601        let expected = r#"
 602            Found 1 matches:
 603
 604            ## Matches in root/test_syntax.rs
 605
 606            ### mod feature_module › pub mod nested_module › pub fn nested_function › L10-14
 607            ```
 608                    ) {
 609                        println!("Function in nested module");
 610                        println!("{first_arg}");
 611                        println!("{second_arg}");
 612                    }
 613            ```
 614            "#
 615        .unindent();
 616        assert_eq!(result, expected);
 617    }
 618
 619    #[gpui::test]
 620    async fn test_grep_function_args_and_body(cx: &mut TestAppContext) {
 621        let project = setup_syntax_test(cx).await;
 622
 623        // Test: Line with a function argument
 624        let input = GrepToolInput {
 625            regex: "second_arg".to_string(),
 626            include_pattern: Some("**/*.rs".to_string()),
 627            offset: 0,
 628            case_sensitive: false,
 629        };
 630
 631        let result = run_grep_tool(input, project.clone(), cx).await;
 632        let expected = r#"
 633            Found 1 matches:
 634
 635            ## Matches in root/test_syntax.rs
 636
 637            ### mod feature_module › pub mod nested_module › pub fn nested_function › L7-14
 638            ```
 639                    pub fn nested_function(
 640                        first_arg: String,
 641                        second_arg: i32,
 642                    ) {
 643                        println!("Function in nested module");
 644                        println!("{first_arg}");
 645                        println!("{second_arg}");
 646                    }
 647            ```
 648            "#
 649        .unindent();
 650        assert_eq!(result, expected);
 651    }
 652
 653    #[gpui::test]
 654    async fn test_grep_if_block(cx: &mut TestAppContext) {
 655        use unindent::Unindent;
 656        let project = setup_syntax_test(cx).await;
 657
 658        // Test: Line inside an if block
 659        let input = GrepToolInput {
 660            regex: "Inside if block".to_string(),
 661            include_pattern: Some("**/*.rs".to_string()),
 662            offset: 0,
 663            case_sensitive: false,
 664        };
 665
 666        let result = run_grep_tool(input, project.clone(), cx).await;
 667        let expected = r#"
 668            Found 1 matches:
 669
 670            ## Matches in root/test_syntax.rs
 671
 672            ### impl MyStruct › fn method_with_block › L26-28
 673            ```
 674                    if condition {
 675                        println!("Inside if block");
 676                    }
 677            ```
 678            "#
 679        .unindent();
 680        assert_eq!(result, expected);
 681    }
 682
 683    #[gpui::test]
 684    async fn test_grep_long_function_top(cx: &mut TestAppContext) {
 685        use unindent::Unindent;
 686        let project = setup_syntax_test(cx).await;
 687
 688        // Test: Line in the middle of a long function - should show message about remaining lines
 689        let input = GrepToolInput {
 690            regex: "Line 5".to_string(),
 691            include_pattern: Some("**/*.rs".to_string()),
 692            offset: 0,
 693            case_sensitive: false,
 694        };
 695
 696        let result = run_grep_tool(input, project.clone(), cx).await;
 697        let expected = r#"
 698            Found 1 matches:
 699
 700            ## Matches in root/test_syntax.rs
 701
 702            ### impl MyStruct › fn long_function › L31-41
 703            ```
 704                fn long_function() {
 705                    println!("Line 1");
 706                    println!("Line 2");
 707                    println!("Line 3");
 708                    println!("Line 4");
 709                    println!("Line 5");
 710                    println!("Line 6");
 711                    println!("Line 7");
 712                    println!("Line 8");
 713                    println!("Line 9");
 714                    println!("Line 10");
 715            ```
 716
 717            3 lines remaining in ancestor node. Read the file to see all.
 718            "#
 719        .unindent();
 720        assert_eq!(result, expected);
 721    }
 722
 723    #[gpui::test]
 724    async fn test_grep_long_function_bottom(cx: &mut TestAppContext) {
 725        use unindent::Unindent;
 726        let project = setup_syntax_test(cx).await;
 727
 728        // Test: Line in the long function
 729        let input = GrepToolInput {
 730            regex: "Line 12".to_string(),
 731            include_pattern: Some("**/*.rs".to_string()),
 732            offset: 0,
 733            case_sensitive: false,
 734        };
 735
 736        let result = run_grep_tool(input, project.clone(), cx).await;
 737        let expected = r#"
 738            Found 1 matches:
 739
 740            ## Matches in root/test_syntax.rs
 741
 742            ### impl MyStruct › fn long_function › L41-45
 743            ```
 744                    println!("Line 10");
 745                    println!("Line 11");
 746                    println!("Line 12");
 747                }
 748            }
 749            ```
 750            "#
 751        .unindent();
 752        assert_eq!(result, expected);
 753    }
 754
 755    async fn run_grep_tool(
 756        input: GrepToolInput,
 757        project: Entity<Project>,
 758        cx: &mut TestAppContext,
 759    ) -> String {
 760        let tool = Arc::new(GrepTool { project });
 761        let task = cx.update(|cx| tool.run(input, ToolCallEventStream::test().0, cx));
 762
 763        match task.await {
 764            Ok(result) => {
 765                if cfg!(windows) {
 766                    result.replace("root\\", "root/")
 767                } else {
 768                    result.to_string()
 769                }
 770            }
 771            Err(e) => panic!("Failed to run grep tool: {}", e),
 772        }
 773    }
 774
 775    fn init_test(cx: &mut TestAppContext) {
 776        cx.update(|cx| {
 777            let settings_store = SettingsStore::test(cx);
 778            cx.set_global(settings_store);
 779            language::init(cx);
 780            Project::init_settings(cx);
 781        });
 782    }
 783
 784    fn rust_lang() -> Language {
 785        Language::new(
 786            LanguageConfig {
 787                name: "Rust".into(),
 788                matcher: LanguageMatcher {
 789                    path_suffixes: vec!["rs".to_string()],
 790                    ..Default::default()
 791                },
 792                ..Default::default()
 793            },
 794            Some(tree_sitter_rust::LANGUAGE.into()),
 795        )
 796        .with_outline_query(include_str!("../../../languages/src/rust/outline.scm"))
 797        .unwrap()
 798    }
 799
 800    #[gpui::test]
 801    async fn test_grep_security_boundaries(cx: &mut TestAppContext) {
 802        init_test(cx);
 803
 804        let fs = FakeFs::new(cx.executor());
 805
 806        fs.insert_tree(
 807            path!("/"),
 808            json!({
 809                "project_root": {
 810                    "allowed_file.rs": "fn main() { println!(\"This file is in the project\"); }",
 811                    ".mysecrets": "SECRET_KEY=abc123\nfn secret() { /* private */ }",
 812                    ".secretdir": {
 813                        "config": "fn special_configuration() { /* excluded */ }"
 814                    },
 815                    ".mymetadata": "fn custom_metadata() { /* excluded */ }",
 816                    "subdir": {
 817                        "normal_file.rs": "fn normal_file_content() { /* Normal */ }",
 818                        "special.privatekey": "fn private_key_content() { /* private */ }",
 819                        "data.mysensitive": "fn sensitive_data() { /* private */ }"
 820                    }
 821                },
 822                "outside_project": {
 823                    "sensitive_file.rs": "fn outside_function() { /* This file is outside the project */ }"
 824                }
 825            }),
 826        )
 827        .await;
 828
 829        cx.update(|cx| {
 830            use gpui::UpdateGlobal;
 831            use project::WorktreeSettings;
 832            use settings::SettingsStore;
 833            SettingsStore::update_global(cx, |store, cx| {
 834                store.update_user_settings::<WorktreeSettings>(cx, |settings| {
 835                    settings.file_scan_exclusions = Some(vec![
 836                        "**/.secretdir".to_string(),
 837                        "**/.mymetadata".to_string(),
 838                    ]);
 839                    settings.private_files = Some(vec![
 840                        "**/.mysecrets".to_string(),
 841                        "**/*.privatekey".to_string(),
 842                        "**/*.mysensitive".to_string(),
 843                    ]);
 844                });
 845            });
 846        });
 847
 848        let project = Project::test(fs.clone(), [path!("/project_root").as_ref()], cx).await;
 849
 850        // Searching for files outside the project worktree should return no results
 851        let result = run_grep_tool(
 852            GrepToolInput {
 853                regex: "outside_function".to_string(),
 854                include_pattern: None,
 855                offset: 0,
 856                case_sensitive: false,
 857            },
 858            project.clone(),
 859            cx,
 860        )
 861        .await;
 862        let paths = extract_paths_from_results(&result);
 863        assert!(
 864            paths.is_empty(),
 865            "grep_tool should not find files outside the project worktree"
 866        );
 867
 868        // Searching within the project should succeed
 869        let result = run_grep_tool(
 870            GrepToolInput {
 871                regex: "main".to_string(),
 872                include_pattern: None,
 873                offset: 0,
 874                case_sensitive: false,
 875            },
 876            project.clone(),
 877            cx,
 878        )
 879        .await;
 880        let paths = extract_paths_from_results(&result);
 881        assert!(
 882            paths.iter().any(|p| p.contains("allowed_file.rs")),
 883            "grep_tool should be able to search files inside worktrees"
 884        );
 885
 886        // Searching files that match file_scan_exclusions should return no results
 887        let result = run_grep_tool(
 888            GrepToolInput {
 889                regex: "special_configuration".to_string(),
 890                include_pattern: None,
 891                offset: 0,
 892                case_sensitive: false,
 893            },
 894            project.clone(),
 895            cx,
 896        )
 897        .await;
 898        let paths = extract_paths_from_results(&result);
 899        assert!(
 900            paths.is_empty(),
 901            "grep_tool should not search files in .secretdir (file_scan_exclusions)"
 902        );
 903
 904        let result = run_grep_tool(
 905            GrepToolInput {
 906                regex: "custom_metadata".to_string(),
 907                include_pattern: None,
 908                offset: 0,
 909                case_sensitive: false,
 910            },
 911            project.clone(),
 912            cx,
 913        )
 914        .await;
 915        let paths = extract_paths_from_results(&result);
 916        assert!(
 917            paths.is_empty(),
 918            "grep_tool should not search .mymetadata files (file_scan_exclusions)"
 919        );
 920
 921        // Searching private files should return no results
 922        let result = run_grep_tool(
 923            GrepToolInput {
 924                regex: "SECRET_KEY".to_string(),
 925                include_pattern: None,
 926                offset: 0,
 927                case_sensitive: false,
 928            },
 929            project.clone(),
 930            cx,
 931        )
 932        .await;
 933        let paths = extract_paths_from_results(&result);
 934        assert!(
 935            paths.is_empty(),
 936            "grep_tool should not search .mysecrets (private_files)"
 937        );
 938
 939        let result = run_grep_tool(
 940            GrepToolInput {
 941                regex: "private_key_content".to_string(),
 942                include_pattern: None,
 943                offset: 0,
 944                case_sensitive: false,
 945            },
 946            project.clone(),
 947            cx,
 948        )
 949        .await;
 950        let paths = extract_paths_from_results(&result);
 951
 952        assert!(
 953            paths.is_empty(),
 954            "grep_tool should not search .privatekey files (private_files)"
 955        );
 956
 957        let result = run_grep_tool(
 958            GrepToolInput {
 959                regex: "sensitive_data".to_string(),
 960                include_pattern: None,
 961                offset: 0,
 962                case_sensitive: false,
 963            },
 964            project.clone(),
 965            cx,
 966        )
 967        .await;
 968        let paths = extract_paths_from_results(&result);
 969        assert!(
 970            paths.is_empty(),
 971            "grep_tool should not search .mysensitive files (private_files)"
 972        );
 973
 974        // Searching a normal file should still work, even with private_files configured
 975        let result = run_grep_tool(
 976            GrepToolInput {
 977                regex: "normal_file_content".to_string(),
 978                include_pattern: None,
 979                offset: 0,
 980                case_sensitive: false,
 981            },
 982            project.clone(),
 983            cx,
 984        )
 985        .await;
 986        let paths = extract_paths_from_results(&result);
 987        assert!(
 988            paths.iter().any(|p| p.contains("normal_file.rs")),
 989            "Should be able to search normal files"
 990        );
 991
 992        // Path traversal attempts with .. in include_pattern should not escape project
 993        let result = run_grep_tool(
 994            GrepToolInput {
 995                regex: "outside_function".to_string(),
 996                include_pattern: Some("../outside_project/**/*.rs".to_string()),
 997                offset: 0,
 998                case_sensitive: false,
 999            },
1000            project.clone(),
1001            cx,
1002        )
1003        .await;
1004        let paths = extract_paths_from_results(&result);
1005        assert!(
1006            paths.is_empty(),
1007            "grep_tool should not allow escaping project boundaries with relative paths"
1008        );
1009    }
1010
1011    #[gpui::test]
1012    async fn test_grep_with_multiple_worktree_settings(cx: &mut TestAppContext) {
1013        init_test(cx);
1014
1015        let fs = FakeFs::new(cx.executor());
1016
1017        // Create first worktree with its own private files
1018        fs.insert_tree(
1019            path!("/worktree1"),
1020            json!({
1021                ".zed": {
1022                    "settings.json": r#"{
1023                        "file_scan_exclusions": ["**/fixture.*"],
1024                        "private_files": ["**/secret.rs"]
1025                    }"#
1026                },
1027                "src": {
1028                    "main.rs": "fn main() { let secret_key = \"hidden\"; }",
1029                    "secret.rs": "const API_KEY: &str = \"secret_value\";",
1030                    "utils.rs": "pub fn get_config() -> String { \"config\".to_string() }"
1031                },
1032                "tests": {
1033                    "test.rs": "fn test_secret() { assert!(true); }",
1034                    "fixture.sql": "SELECT * FROM secret_table;"
1035                }
1036            }),
1037        )
1038        .await;
1039
1040        // Create second worktree with different private files
1041        fs.insert_tree(
1042            path!("/worktree2"),
1043            json!({
1044                ".zed": {
1045                    "settings.json": r#"{
1046                        "file_scan_exclusions": ["**/internal.*"],
1047                        "private_files": ["**/private.js", "**/data.json"]
1048                    }"#
1049                },
1050                "lib": {
1051                    "public.js": "export function getSecret() { return 'public'; }",
1052                    "private.js": "const SECRET_KEY = \"private_value\";",
1053                    "data.json": "{\"secret_data\": \"hidden\"}"
1054                },
1055                "docs": {
1056                    "README.md": "# Documentation with secret info",
1057                    "internal.md": "Internal secret documentation"
1058                }
1059            }),
1060        )
1061        .await;
1062
1063        // Set global settings
1064        cx.update(|cx| {
1065            SettingsStore::update_global(cx, |store, cx| {
1066                store.update_user_settings::<WorktreeSettings>(cx, |settings| {
1067                    settings.file_scan_exclusions =
1068                        Some(vec!["**/.git".to_string(), "**/node_modules".to_string()]);
1069                    settings.private_files = Some(vec!["**/.env".to_string()]);
1070                });
1071            });
1072        });
1073
1074        let project = Project::test(
1075            fs.clone(),
1076            [path!("/worktree1").as_ref(), path!("/worktree2").as_ref()],
1077            cx,
1078        )
1079        .await;
1080
1081        // Wait for worktrees to be fully scanned
1082        cx.executor().run_until_parked();
1083
1084        // Search for "secret" - should exclude files based on worktree-specific settings
1085        let result = run_grep_tool(
1086            GrepToolInput {
1087                regex: "secret".to_string(),
1088                include_pattern: None,
1089                offset: 0,
1090                case_sensitive: false,
1091            },
1092            project.clone(),
1093            cx,
1094        )
1095        .await;
1096        let paths = extract_paths_from_results(&result);
1097
1098        // Should find matches in non-private files
1099        assert!(
1100            paths.iter().any(|p| p.contains("main.rs")),
1101            "Should find 'secret' in worktree1/src/main.rs"
1102        );
1103        assert!(
1104            paths.iter().any(|p| p.contains("test.rs")),
1105            "Should find 'secret' in worktree1/tests/test.rs"
1106        );
1107        assert!(
1108            paths.iter().any(|p| p.contains("public.js")),
1109            "Should find 'secret' in worktree2/lib/public.js"
1110        );
1111        assert!(
1112            paths.iter().any(|p| p.contains("README.md")),
1113            "Should find 'secret' in worktree2/docs/README.md"
1114        );
1115
1116        // Should NOT find matches in private/excluded files based on worktree settings
1117        assert!(
1118            !paths.iter().any(|p| p.contains("secret.rs")),
1119            "Should not search in worktree1/src/secret.rs (local private_files)"
1120        );
1121        assert!(
1122            !paths.iter().any(|p| p.contains("fixture.sql")),
1123            "Should not search in worktree1/tests/fixture.sql (local file_scan_exclusions)"
1124        );
1125        assert!(
1126            !paths.iter().any(|p| p.contains("private.js")),
1127            "Should not search in worktree2/lib/private.js (local private_files)"
1128        );
1129        assert!(
1130            !paths.iter().any(|p| p.contains("data.json")),
1131            "Should not search in worktree2/lib/data.json (local private_files)"
1132        );
1133        assert!(
1134            !paths.iter().any(|p| p.contains("internal.md")),
1135            "Should not search in worktree2/docs/internal.md (local file_scan_exclusions)"
1136        );
1137
1138        // Test with `include_pattern` specific to one worktree
1139        let result = run_grep_tool(
1140            GrepToolInput {
1141                regex: "secret".to_string(),
1142                include_pattern: Some("worktree1/**/*.rs".to_string()),
1143                offset: 0,
1144                case_sensitive: false,
1145            },
1146            project.clone(),
1147            cx,
1148        )
1149        .await;
1150
1151        let paths = extract_paths_from_results(&result);
1152
1153        // Should only find matches in worktree1 *.rs files (excluding private ones)
1154        assert!(
1155            paths.iter().any(|p| p.contains("main.rs")),
1156            "Should find match in worktree1/src/main.rs"
1157        );
1158        assert!(
1159            paths.iter().any(|p| p.contains("test.rs")),
1160            "Should find match in worktree1/tests/test.rs"
1161        );
1162        assert!(
1163            !paths.iter().any(|p| p.contains("secret.rs")),
1164            "Should not find match in excluded worktree1/src/secret.rs"
1165        );
1166        assert!(
1167            paths.iter().all(|p| !p.contains("worktree2")),
1168            "Should not find any matches in worktree2"
1169        );
1170    }
1171
1172    // Helper function to extract file paths from grep results
1173    fn extract_paths_from_results(results: &str) -> Vec<String> {
1174        results
1175            .lines()
1176            .filter(|line| line.starts_with("## Matches in "))
1177            .map(|line| {
1178                line.strip_prefix("## Matches in ")
1179                    .unwrap()
1180                    .trim()
1181                    .to_string()
1182            })
1183            .collect()
1184    }
1185}