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