Skip to main content

typst_analyzer_analysis/completion/
resources.rs

1
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()?; // Get the current directory
10    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(); // Initialize the iterator
18
19    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; // Skip directories
26                } 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                        // Normalize extension extraction and match against supported types
31                        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                            // Check if file has a valid image extension
36                            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                                // Convert to an absolute path
38                                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()); // Add to the images vector
42                            }
43                        }
44                    }
45                }
46            }
47            Err(e) => {
48                typ_logger!("Error processing entry: {:?}", e);
49            }
50        }
51    }
52
53    Ok(images)
54}
55
56// Helper function to filter out hidden files
57fn 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
65/// Finds the project root by searching upward for a marker file/directory (e.g., `.git`)
66pub 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"); // Change `.git` to another marker if needed
72        if marker.exists() {
73            typ_logger!("Found project root: {:?}", current_dir);
74            return Some(current_dir);
75        }
76
77        // Move one directory up
78        current_dir = current_dir.parent()?.to_path_buf();
79    }
80
81    typ_logger!("No project root found");
82    None
83}