grep_tool.rs

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