Skip to content

Commit

Permalink
refactor: api file split and arg type change
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Nov 16, 2024
1 parent 6b78322 commit cded273
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 143 deletions.
6 changes: 3 additions & 3 deletions examples/mini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ fn main() {
let all_files = g.files();
for file in &all_files {
// related file search
let related_files = g.related_files(file);
let related_files = g.related_files(file.clone());
for each_related in &related_files {
println!("{} -> {}: {}", file, each_related.name, each_related.score);
}

// file details
if !related_files.is_empty() {
let random_file = related_files[0].name.clone();
let meta = g.file_metadata(&random_file);
let meta = g.file_metadata(random_file.clone());
println!("symbols in {}: {:?}", random_file, meta.symbols.len());

// search all the references of symbols from this file
Expand All @@ -26,7 +26,7 @@ fn main() {
continue;
}

for (each_related_symbol, each_score) in g.related_symbols(each_symbol) {
for (each_related_symbol, each_score) in g.related_symbols(each_symbol.clone()) {
if each_score == 0 {
continue;
}
Expand Down
127 changes: 127 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use std::cmp::Reverse;
use std::collections::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
use crate::graph::{Graph, RelatedSymbol};
use crate::symbol::{DefRefPair, Symbol, SymbolKind};

#[derive(Serialize, Deserialize, Clone)]
pub struct RelatedFileContext {
pub name: String,
pub score: usize,
pub defs: usize,
pub refs: usize,
pub related_symbols: Vec<RelatedSymbol>,
}

#[derive(Serialize, Deserialize)]
pub struct FileMetadata {
pub symbols: Vec<Symbol>,
}

// Read API v1
impl Graph {
pub fn files(&self) -> HashSet<String> {
self.file_contexts
.iter()
.map(|each| each.path.clone())
.collect()
}

/// All files which pointed to this file
pub fn related_files(&self, file_name: String) -> Vec<RelatedFileContext> {
if !self.symbol_graph.file_mapping.contains_key(&file_name) {
return Vec::new();
}

// find all the defs in this file
// and tracking all the references and theirs
let mut file_counter = HashMap::new();
let mut file_ref_mapping: HashMap<String, Vec<RelatedSymbol>> = HashMap::new();

// other files -> this file
let definitions_in_file = self.symbol_graph.list_definitions(&file_name);
let definition_count = definitions_in_file.len();

definitions_in_file.iter().for_each(|def| {
self.symbol_graph
.list_references_by_definition(&def.id())
.iter()
.for_each(|(each_ref, weight)| {
let real_weight = std::cmp::max(weight / definition_count, 1);

file_counter.entry(each_ref.file.clone()).or_insert(0);
file_counter
.entry(each_ref.file.clone())
.and_modify(|w| *w += real_weight)
.or_insert(real_weight);

file_ref_mapping
.entry(each_ref.file.clone())
.and_modify(|v| {
v.push(RelatedSymbol {
symbol: each_ref.clone(),
weight: real_weight,
})
})
.or_insert(vec![RelatedSymbol {
symbol: each_ref.clone(),
weight: real_weight,
}]);
});
});

// this file -> other files
// TODO: need it?

// remove itself
file_counter.remove(&file_name);

let mut contexts = file_counter
.iter()
.map(|(k, v)| {
let related_symbols = file_ref_mapping[k].clone();
return RelatedFileContext {
name: k.clone(),
score: *v,
defs: self.symbol_graph.list_definitions(k).len(),
refs: self.symbol_graph.list_references(k).len(),
related_symbols,
};
})
.collect::<Vec<_>>();
contexts.sort_by_key(|context| Reverse(context.score));
contexts
}

pub fn related_symbols(&self, symbol: Symbol) -> HashMap<Symbol, usize> {
match symbol.kind {
SymbolKind::DEF => self
.symbol_graph
.list_references_by_definition(&symbol.id())
.into_iter()
.collect(),
SymbolKind::REF => self
.symbol_graph
.list_definitions_by_reference(&symbol.id())
.into_iter()
.collect(),
}
}

pub fn file_metadata(&self, file_name: String) -> FileMetadata {
let symbols = self
.symbol_graph
.list_symbols(&file_name)
.iter()
.cloned()
.collect();
FileMetadata { symbols }
}

pub fn pairs_between_files(&self, src_file: String, dst_file: String) -> Vec<DefRefPair> {
if !self.files().contains(&src_file) || !self.files().contains(&dst_file) {
return Vec::new();
}
self.symbol_graph.pairs_between_files(&src_file, &dst_file)
}
}
134 changes: 6 additions & 128 deletions src/graph.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::extractor::Extractor;
use crate::symbol::{DefRefPair, Symbol, SymbolGraph, SymbolKind};
use crate::symbol::{Symbol, SymbolGraph, SymbolKind};
use cupido::collector::config::Collect;
use cupido::collector::config::{get_collector, Config};
use cupido::relation::graph::RelationGraph as CupidoRelationGraph;
Expand All @@ -8,7 +8,6 @@ use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::cmp::Reverse;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -423,131 +422,10 @@ impl Graph {

#[derive(Serialize, Deserialize, Clone)]
pub struct RelatedSymbol {
symbol: Symbol,
weight: usize,
pub(crate) symbol: Symbol,
pub(crate) weight: usize,
}

// Read API
impl Graph {
pub fn files(&self) -> HashSet<String> {
self.file_contexts
.iter()
.map(|each| each.path.clone())
.collect()
}

/// All files which pointed to this file
pub fn related_files(&self, file_name: &String) -> Vec<RelatedFileContext> {
if !self.symbol_graph.file_mapping.contains_key(file_name) {
return Vec::new();
}

// find all the defs in this file
// and tracking all the references and theirs
let mut file_counter = HashMap::new();
let mut file_ref_mapping: HashMap<String, Vec<RelatedSymbol>> = HashMap::new();

// other files -> this file
let definitions_in_file = self.symbol_graph.list_definitions(file_name);
let definition_count = definitions_in_file.len();

definitions_in_file.iter().for_each(|def| {
self.symbol_graph
.list_references_by_definition(&def.id())
.iter()
.for_each(|(each_ref, weight)| {
let real_weight = std::cmp::max(weight / definition_count, 1);

file_counter.entry(each_ref.file.clone()).or_insert(0);
file_counter
.entry(each_ref.file.clone())
.and_modify(|w| *w += real_weight)
.or_insert(real_weight);

file_ref_mapping
.entry(each_ref.file.clone())
.and_modify(|v| {
v.push(RelatedSymbol {
symbol: each_ref.clone(),
weight: real_weight,
})
})
.or_insert(vec![RelatedSymbol {
symbol: each_ref.clone(),
weight: real_weight,
}]);
});
});

// this file -> other files
// TODO: need it?

// remove itself
file_counter.remove(file_name);

let mut contexts = file_counter
.iter()
.map(|(k, v)| {
let related_symbols = file_ref_mapping[k].clone();
return RelatedFileContext {
name: k.clone(),
score: *v,
defs: self.symbol_graph.list_definitions(k).len(),
refs: self.symbol_graph.list_references(k).len(),
related_symbols,
};
})
.collect::<Vec<_>>();
contexts.sort_by_key(|context| Reverse(context.score));
contexts
}

pub fn related_symbols(&self, symbol: &Symbol) -> HashMap<Symbol, usize> {
match symbol.kind {
SymbolKind::DEF => self
.symbol_graph
.list_references_by_definition(&symbol.id())
.into_iter()
.collect(),
SymbolKind::REF => self
.symbol_graph
.list_definitions_by_reference(&symbol.id())
.into_iter()
.collect(),
}
}

pub fn file_metadata(&self, file_name: &String) -> FileMetadata {
let symbols = self
.symbol_graph
.list_symbols(file_name)
.iter()
.cloned()
.collect();
FileMetadata { symbols }
}

pub fn pairs_between_files(&self, src_file: &String, dst_file: &String) -> Vec<DefRefPair> {
if !self.files().contains(src_file) || !self.files().contains(dst_file) {
return Vec::new();
}
self.symbol_graph.pairs_between_files(src_file, dst_file)
}
}

#[derive(Serialize, Deserialize, Clone)]
pub struct RelatedFileContext {
pub name: String,
pub score: usize,
pub defs: usize,
pub refs: usize,
pub related_symbols: Vec<RelatedSymbol>,
}

#[derive(Serialize, Deserialize)]
pub struct FileMetadata {
pub symbols: Vec<Symbol>,
}

fn create_cupido_graph(
project_path: &String,
Expand Down Expand Up @@ -682,7 +560,7 @@ mod tests {
let mut config = GraphConfig::default();
config.project_path = String::from("../stack-graphs");
let g = Graph::from(config);
let files = g.related_files(&String::from(
let files = g.related_files(String::from(
"tree-sitter-stack-graphs/src/cli/util/reporter.rs",
));
files.iter().for_each(|item| {
Expand All @@ -697,8 +575,8 @@ mod tests {
config.project_path = String::from(".");
let g = Graph::from(config);
let symbols: Vec<DefRefPair> = g.pairs_between_files(
&String::from("src/extractor.rs"),
&String::from("src/graph.rs"),
String::from("src/extractor.rs"),
String::from("src/graph.rs"),
);
symbols.iter().for_each(|pair| {
info!(
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub(crate) mod extractor;
pub mod api;
pub mod graph;
mod rule;
pub mod server;
Expand Down
Loading

0 comments on commit cded273

Please sign in to comment.