grep_tool.rs

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