refactor: deepen modules, consolidate inference, delete dead code
- Extract build_graph/load_config/create_renderer in presentation (393→~250 lines) - Move module inference into ModuleName::from_path(), delete 3 scattered copies - Move resolve_relationships/filter_external_imports into CodeGraph - Add LanguageExtractor trait in tree-sitter adapter - Add CodeGraph::elements_by_module(), replace 6 identical grouping loops - Delete dead RenderDiagrams query
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::DomainError;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
@@ -12,6 +15,60 @@ impl ModuleName {
|
||||
Ok(Self(trimmed.to_string()))
|
||||
}
|
||||
|
||||
pub fn from_path(
|
||||
file_path: &str,
|
||||
root: &Path,
|
||||
module_mappings: &HashMap<String, String>,
|
||||
) -> Option<Self> {
|
||||
let relative = file_path
|
||||
.strip_prefix(root.to_str().unwrap_or(""))
|
||||
.unwrap_or(file_path)
|
||||
.trim_start_matches('/');
|
||||
|
||||
for (pattern, module_name) in module_mappings {
|
||||
if relative.starts_with(pattern.as_str()) {
|
||||
return Self::new(module_name).ok();
|
||||
}
|
||||
}
|
||||
|
||||
let parts: Vec<&str> = relative.split('/').collect();
|
||||
if parts.len() <= 1 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let module_dir = if (parts[0] == "crates" || parts[0] == "src") && parts.len() > 2 {
|
||||
parts[1]
|
||||
} else if parts[0] != "src" && parts.len() > 1 {
|
||||
parts[0]
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
|
||||
Self::new(&Self::capitalize(module_dir)).ok()
|
||||
}
|
||||
|
||||
pub fn from_directory_group(member_path: &str) -> Option<Self> {
|
||||
let parts: Vec<&str> = member_path.split('/').collect();
|
||||
if parts.len() < 3 {
|
||||
return None;
|
||||
}
|
||||
let group = parts[parts.len() - 2];
|
||||
Self::new(&Self::capitalize(group)).ok()
|
||||
}
|
||||
|
||||
pub fn capitalize(s: &str) -> String {
|
||||
s.split('-')
|
||||
.map(|seg| {
|
||||
if seg.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
format!("{}{}", seg[..1].to_uppercase(), &seg[1..])
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("-")
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user