grep_tool.rs

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