typst_analyzer_analysis/completion/
resources.rs1
2use std::path::PathBuf;
3use walkdir::{WalkDir, DirEntry};
4
5use crate::typ_logger;
6
7pub fn get_images() -> Result<Vec<PathBuf>, anyhow::Error> {
8 let mut images = Vec::new();
9 let c_dir = std::env::current_dir()?; typ_logger!("c_dir: {:?}", c_dir);
11
12 if !c_dir.exists() || !c_dir.is_dir() {
13 typ_logger!("Directory does not exist or is not a valid directory: {:?}", c_dir);
14 return Ok(Vec::new());
15 }
16
17 let walker = WalkDir::new(&c_dir).into_iter(); for entry in walker.filter_entry(is_hidden) {
20 match entry {
21 Ok(entry) => {
22 typ_logger!("Found entry: {:?}", entry.path());
23
24 if entry.path().is_dir() {
25 continue; } else if entry.path().is_file() {
27 let filename = entry.path().file_name();
28 if let Some(ext) = filename {
29 typ_logger!("File name: {:?}", ext);
30 if let Some(ext_str) = ext.to_str() {
32 typ_logger!("File extension: {:?}", ext_str);
33 let ext_str_lower = ext_str.to_lowercase();
34
35 if ext_str_lower.ends_with("png") || ext_str_lower.ends_with("jpg") || ext_str_lower.ends_with("jpeg") || ext_str_lower.ends_with("gif") || ext_str_lower.ends_with("svg") {
37 let r_path =entry.path().strip_prefix(&c_dir)?;
39 typ_logger!("Absolute path found: {:?}", r_path);
40
41 images.push(r_path.to_path_buf()); }
43 }
44 }
45 }
46 }
47 Err(e) => {
48 typ_logger!("Error processing entry: {:?}", e);
49 }
50 }
51 }
52
53 Ok(images)
54}
55
56fn is_hidden(entry: &DirEntry) -> bool {
58 entry
59 .file_name()
60 .to_str()
61 .map(|s| s.starts_with("."))
62 .unwrap_or(false)
63}
64
65pub fn find_project_root() -> Option<PathBuf> {
67 let mut current_dir = std::env::current_dir().ok()?;
68 typ_logger!("Starting search for project root from: {:?}", current_dir);
69
70 while current_dir.parent().is_some() {
71 let marker = current_dir.join(".git"); if marker.exists() {
73 typ_logger!("Found project root: {:?}", current_dir);
74 return Some(current_dir);
75 }
76
77 current_dir = current_dir.parent()?.to_path_buf();
79 }
80
81 typ_logger!("No project root found");
82 None
83}