Mountain/RPC/CocoonService/FileSystem/
FindFiles.rs1#![allow(non_snake_case)]
2
3use globset::Glob;
7use tonic::{Response, Status};
8
9use crate::{
10 RPC::CocoonService::CocoonServiceImpl,
11 Vine::Generated::{FindFilesRequest, FindFilesResponse},
12 dev_log,
13};
14
15pub async fn Fn(Service:&CocoonServiceImpl, Request:FindFilesRequest) -> Result<Response<FindFilesResponse>, Status> {
16 dev_log!("cocoon", "[CocoonService] Finding files with pattern: {}", Request.pattern);
17
18 let Matcher = Glob::new(&Request.pattern)
19 .map_err(|Error| {
20 Status::invalid_argument(format!("find_files: invalid pattern '{}': {}", Request.pattern, Error))
21 })?
22 .compile_matcher();
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
31 let SearchRoots = if Roots.is_empty() {
32 vec![std::env::current_dir().unwrap_or_default()]
33 } else {
34 Roots
35 };
36
37 let mut URIs = Vec::new();
38
39 fn WalkAndCollect(
40 Directory:&std::path::Path,
41 Root:&std::path::Path,
42 Matcher:&globset::GlobMatcher,
43 Results:&mut Vec<String>,
44 ) {
45 if let Ok(Entries) = std::fs::read_dir(Directory) {
46 for Entry in Entries.flatten() {
47 let EntryPath = Entry.path();
48 if EntryPath.is_dir() {
49 WalkAndCollect(&EntryPath, Root, Matcher, Results);
50 } else if let Ok(Relative) = EntryPath.strip_prefix(Root) {
51 if Matcher.is_match(Relative) {
52 Results.push(format!("file://{}", EntryPath.display()));
53 }
54 }
55 }
56 }
57 }
58
59 for Root in &SearchRoots {
60 WalkAndCollect(Root, Root, &Matcher, &mut URIs);
61 }
62
63 dev_log!(
64 "cocoon",
65 "[CocoonService] find_files: {} results for pattern '{}'",
66 URIs.len(),
67 Request.pattern
68 );
69 Ok(Response::new(FindFilesResponse { uris:URIs }))
70}