Skip to main content

Mountain/RPC/CocoonService/FileSystem/
FindTextInFiles.rs

1#![allow(non_snake_case)]
2
3//! Substring search across the workspace, capped at 1,000 matches. Skips
4//! hidden directories plus `node_modules` and `target`. Runs the walk in
5//! `tokio::task::spawn_blocking` so the event loop stays responsive.
6
7use tonic::{Response, Status};
8
9use crate::{
10	RPC::CocoonService::CocoonServiceImpl,
11	Vine::Generated::{FindTextInFilesRequest, FindTextInFilesResponse, Position, Range, TextMatch, Uri},
12	dev_log,
13};
14
15pub async fn Fn(
16	Service:&CocoonServiceImpl,
17	Request:FindTextInFilesRequest,
18) -> Result<Response<FindTextInFilesResponse>, Status> {
19	if Request.pattern.is_empty() {
20		return Ok(Response::new(FindTextInFilesResponse::default()));
21	}
22	dev_log!("cocoon", "[CocoonService] find_text_in_files: pattern='{}'", Request.pattern);
23
24	let Roots:Vec<std::path::PathBuf> = {
25		match Service.environment.ApplicationState.Workspace.WorkspaceFolders.lock() {
26			Ok(Guard) => Guard.iter().map(|F| std::path::PathBuf::from(F.URI.path())).collect(),
27			Err(_) => Vec::new(),
28		}
29	};
30	let SearchRoots = if Roots.is_empty() {
31		vec![std::env::current_dir().unwrap_or_default()]
32	} else {
33		Roots
34	};
35
36	let Pattern = Request.pattern.clone();
37	let Matches = tokio::task::spawn_blocking(move || {
38		let mut Results:Vec<TextMatch> = Vec::new();
39		const MAX_MATCHES:usize = 1000;
40
41		fn WalkAndSearch(Directory:&std::path::Path, Pattern:&str, Results:&mut Vec<TextMatch>) {
42			if Results.len() >= MAX_MATCHES {
43				return;
44			}
45			if let Ok(Entries) = std::fs::read_dir(Directory) {
46				for Entry in Entries.flatten() {
47					if Results.len() >= MAX_MATCHES {
48						break;
49					}
50					let Path = Entry.path();
51					if Path.is_dir() {
52						let Name = Path.file_name().and_then(|N| N.to_str()).unwrap_or("");
53						if Name.starts_with('.') || Name == "node_modules" || Name == "target" {
54							continue;
55						}
56						WalkAndSearch(&Path, Pattern, Results);
57					} else if Path.is_file() {
58						if let Ok(Content) = std::fs::read_to_string(&Path) {
59							for (LineIndex, Line) in Content.lines().enumerate() {
60								if Results.len() >= MAX_MATCHES {
61									break;
62								}
63								if let Some(ColumnIndex) = Line.find(Pattern) {
64									Results.push(TextMatch {
65										uri:Some(Uri { value:format!("file://{}", Path.display()) }),
66										range:Some(Range {
67											start:Some(Position {
68												line:LineIndex as u32,
69												character:ColumnIndex as u32,
70											}),
71											end:Some(Position {
72												line:LineIndex as u32,
73												character:(ColumnIndex + Pattern.len()) as u32,
74											}),
75										}),
76										preview:Line.to_string(),
77									});
78								}
79							}
80						}
81					}
82				}
83			}
84		}
85
86		for Root in &SearchRoots {
87			WalkAndSearch(Root, &Pattern, &mut Results);
88			if Results.len() >= MAX_MATCHES {
89				break;
90			}
91		}
92		Results
93	})
94	.await
95	.unwrap_or_default();
96
97	dev_log!(
98		"cocoon",
99		"[CocoonService] find_text_in_files: {} matches for '{}'",
100		Matches.len(),
101		Request.pattern
102	);
103	Ok(Response::new(FindTextInFilesResponse { matches:Matches }))
104}