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,6 +1,6 @@
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use crate::{CodeElement, ModuleName, Relationship};
|
||||
use crate::{CodeElement, ModuleName, Relationship, RelationshipKind};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CodeGraph {
|
||||
@@ -53,6 +53,105 @@ impl CodeGraph {
|
||||
modules
|
||||
}
|
||||
|
||||
pub fn elements_by_module(&self) -> (HashMap<String, Vec<&CodeElement>>, Vec<&CodeElement>) {
|
||||
let mut grouped: HashMap<String, Vec<&CodeElement>> = HashMap::new();
|
||||
let mut ungrouped: Vec<&CodeElement> = Vec::new();
|
||||
|
||||
for element in &self.elements {
|
||||
if let Some(module) = element.module() {
|
||||
grouped
|
||||
.entry(module.as_str().to_string())
|
||||
.or_default()
|
||||
.push(element);
|
||||
} else {
|
||||
ungrouped.push(element);
|
||||
}
|
||||
}
|
||||
|
||||
(grouped, ungrouped)
|
||||
}
|
||||
|
||||
pub fn resolve_relationships(self) -> CodeGraph {
|
||||
let mut file_types: HashMap<String, HashSet<String>> = HashMap::new();
|
||||
let mut name_modules: HashMap<&str, HashSet<Option<&str>>> = HashMap::new();
|
||||
let all_type_names: HashSet<&str> = self.elements.iter().map(|e| e.name()).collect();
|
||||
|
||||
for element in &self.elements {
|
||||
file_types
|
||||
.entry(element.file_path().as_str().to_string())
|
||||
.or_default()
|
||||
.insert(element.name().to_string());
|
||||
name_modules
|
||||
.entry(element.name())
|
||||
.or_default()
|
||||
.insert(element.module().map(|m| m.as_str()));
|
||||
}
|
||||
|
||||
let mut resolved = CodeGraph::new();
|
||||
for element in &self.elements {
|
||||
resolved.add_element(element.clone());
|
||||
}
|
||||
for rel in &self.relationships {
|
||||
match rel.kind() {
|
||||
RelationshipKind::Import => {
|
||||
resolved.add_relationship(rel.clone());
|
||||
}
|
||||
_ => {
|
||||
if !all_type_names.contains(rel.source())
|
||||
|| !all_type_names.contains(rel.target())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(src_file) = rel.source_file() {
|
||||
let file_key = src_file.as_str().to_string();
|
||||
if let Some(types_in_file) = file_types.get(&file_key)
|
||||
&& types_in_file.contains(rel.target())
|
||||
{
|
||||
resolved.add_relationship(rel.clone());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let tgt_modules = &name_modules[rel.target()];
|
||||
if tgt_modules.len() == 1 {
|
||||
resolved.add_relationship(rel.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
resolved
|
||||
}
|
||||
|
||||
pub fn filter_external_imports(self, known_modules: &HashSet<String>) -> CodeGraph {
|
||||
let module_names: HashSet<String> = self
|
||||
.modules()
|
||||
.iter()
|
||||
.map(|m| m.as_str().to_lowercase())
|
||||
.collect();
|
||||
|
||||
let all_known: HashSet<&str> = known_modules
|
||||
.iter()
|
||||
.map(|s| s.as_str())
|
||||
.chain(module_names.iter().map(|s| s.as_str()))
|
||||
.collect();
|
||||
|
||||
let mut filtered = CodeGraph::new();
|
||||
for element in &self.elements {
|
||||
filtered.add_element(element.clone());
|
||||
}
|
||||
for rel in &self.relationships {
|
||||
if rel.kind() == RelationshipKind::Import {
|
||||
let target_top = rel.target().split('.').next().unwrap_or("").to_lowercase();
|
||||
if !all_known.contains(target_top.as_str()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
filtered.add_relationship(rel.clone());
|
||||
}
|
||||
filtered
|
||||
}
|
||||
|
||||
pub fn subgraph_by_module(&self, module: &ModuleName) -> CodeGraph {
|
||||
let filtered_elements: Vec<CodeElement> = self
|
||||
.elements
|
||||
|
||||
Reference in New Issue
Block a user