Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 66 additions & 16 deletions crates/codegraph-resolve/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ use crate::workspace_packages::{WorkspacePackages, load_workspace_packages};
use crate::{import_resolver, pathutil};
use codegraph_core::types::{EdgeKind, Language, Node, NodeKind};
use codegraph_store::Store;
use std::borrow::Borrow;
use std::cell::RefCell;
use std::collections::BTreeSet;
use std::path::Path;
use std::sync::Arc;

/// `DEFAULT_CACHE_LIMIT` (`index.ts:54`). Override via env in the upstream; the
/// Rust port keeps the fixed default for one resolution run.
Expand All @@ -33,13 +35,13 @@ pub struct StoreResolutionContext<'a> {
}

struct Caches {
node_cache: LruCache<String, Vec<Node>>,
node_cache: LruCache<String, Vec<Arc<Node>>>,
file_cache: LruCache<String, Option<String>>,
import_mapping_cache: LruCache<String, Vec<ImportMapping>>,
re_export_cache: LruCache<String, Vec<ReExport>>,
name_cache: LruCache<String, Vec<Node>>,
lower_name_cache: LruCache<String, Vec<Node>>,
qualified_name_cache: LruCache<String, Vec<Node>>,
name_cache: LruCache<String, Vec<Arc<Node>>>,
lower_name_cache: LruCache<String, Vec<Arc<Node>>>,
qualified_name_cache: LruCache<String, Vec<Arc<Node>>>,
project_aliases: Option<Option<AliasMap>>,
go_module: Option<Option<GoModule>>,
go_modules: Option<Vec<GoModule>>,
Expand Down Expand Up @@ -93,27 +95,55 @@ impl<'a> StoreResolutionContext<'a> {

impl ResolutionContext for StoreResolutionContext<'_> {
fn get_nodes_in_file(&self, file_path: &str) -> Vec<Node> {
self.get_nodes_in_file_shared(file_path)
.into_iter()
.map(|node| node.as_ref().clone())
.collect()
}

fn get_nodes_in_file_shared(&self, file_path: &str) -> Vec<Arc<Node>> {
let mut c = self.caches.borrow_mut();
if let Some(cached) = c.node_cache.get(&file_path.to_string()) {
return cached;
}
let result = self.store.nodes_by_file_path(file_path).unwrap_or_default();
let result: Vec<Arc<Node>> = self
.store
.nodes_by_file_path(file_path)
.unwrap_or_default()
.into_iter()
.map(Arc::new)
.collect();
c.node_cache.set(file_path.to_string(), result.clone());
result
}

fn get_nodes_by_name(&self, name: &str) -> Vec<Node> {
self.get_nodes_by_name_shared(name)
.into_iter()
.map(|node| node.as_ref().clone())
.collect()
}

fn get_nodes_by_name_shared(&self, name: &str) -> Vec<Arc<Node>> {
let mut c = self.caches.borrow_mut();
if let Some(cached) = c.name_cache.get(&name.to_string()) {
return cached;
}
let mut result = self.store.nodes_by_name(name).unwrap_or_default();
order_candidates(&mut result);
let result: Vec<Arc<Node>> = result.into_iter().map(Arc::new).collect();
c.name_cache.set(name.to_string(), result.clone());
result
}

fn get_nodes_by_qualified_name(&self, qualified_name: &str) -> Vec<Node> {
self.get_nodes_by_qualified_name_shared(qualified_name)
.into_iter()
.map(|node| node.as_ref().clone())
.collect()
}

fn get_nodes_by_qualified_name_shared(&self, qualified_name: &str) -> Vec<Arc<Node>> {
let mut c = self.caches.borrow_mut();
if let Some(cached) = c.qualified_name_cache.get(&qualified_name.to_string()) {
return cached;
Expand All @@ -123,6 +153,7 @@ impl ResolutionContext for StoreResolutionContext<'_> {
.nodes_by_qualified_name(qualified_name)
.unwrap_or_default();
order_candidates(&mut result);
let result: Vec<Arc<Node>> = result.into_iter().map(Arc::new).collect();
c.qualified_name_cache
.set(qualified_name.to_string(), result.clone());
result
Expand All @@ -132,6 +163,15 @@ impl ResolutionContext for StoreResolutionContext<'_> {
self.store.nodes_by_kind(kind).unwrap_or_default()
}

fn get_nodes_by_kind_shared(&self, kind: NodeKind) -> Vec<Arc<Node>> {
self.store
.nodes_by_kind(kind)
.unwrap_or_default()
.into_iter()
.map(Arc::new)
.collect()
}

fn known_node_names(&self) -> Vec<String> {
self.store.all_node_names().unwrap_or_default()
}
Expand Down Expand Up @@ -180,6 +220,13 @@ impl ResolutionContext for StoreResolutionContext<'_> {
}

fn get_nodes_by_lower_name(&self, lower_name: &str) -> Vec<Node> {
self.get_nodes_by_lower_name_shared(lower_name)
.into_iter()
.map(|node| node.as_ref().clone())
.collect()
}

fn get_nodes_by_lower_name_shared(&self, lower_name: &str) -> Vec<Arc<Node>> {
let mut c = self.caches.borrow_mut();
if let Some(cached) = c.lower_name_cache.get(&lower_name.to_string()) {
return cached;
Expand All @@ -189,6 +236,7 @@ impl ResolutionContext for StoreResolutionContext<'_> {
.nodes_by_lower_name(lower_name)
.unwrap_or_default();
order_candidates(&mut result);
let result: Vec<Arc<Node>> = result.into_iter().map(Arc::new).collect();
c.lower_name_cache
.set(lower_name.to_string(), result.clone());
result
Expand All @@ -210,11 +258,11 @@ impl ResolutionContext for StoreResolutionContext<'_> {
NodeKind::Protocol,
NodeKind::Enum,
];
let type_nodes: Vec<Node> = self
.get_nodes_by_name(type_name)
let type_nodes = self
.get_nodes_by_name_shared(type_name)
.into_iter()
.filter(|n| SUPERTYPE_BEARING.contains(&n.kind) && n.language == language)
.collect();
.collect::<Vec<_>>();
if type_nodes.is_empty() {
return Vec::new();
}
Expand Down Expand Up @@ -318,12 +366,6 @@ impl ResolutionContext for StoreResolutionContext<'_> {
}
}

/// Shares the byte-equivalence-critical [`order_candidates`] tie-break with the
/// [`SnapshotResolutionContext`](crate::snapshot_context::SnapshotResolutionContext).
pub(crate) fn order_candidates_pub(nodes: &mut [Node]) {
order_candidates(nodes);
}

/// Shares [`is_js_family_path`] with the snapshot context.
pub(crate) fn is_js_family_path_pub(file_path: &str) -> bool {
is_js_family_path(file_path)
Expand All @@ -350,8 +392,10 @@ pub(crate) fn load_go_modules_pub(project_root: &str, files: &[String]) -> Vec<G
/// such ties; sorting every candidate list by this stable key reproduces the
/// full-index order regardless of when a node was inserted, keeping incremental
/// resolution byte-equal to `index --force`.
fn order_candidates(nodes: &mut [Node]) {
pub(crate) fn order_candidates<T: Borrow<Node>>(nodes: &mut [T]) {
nodes.sort_by(|a, b| {
let a = a.borrow();
let b = b.borrow();
a.file_path
.cmp(&b.file_path)
.then(a.start_line.cmp(&b.start_line))
Expand Down Expand Up @@ -652,6 +696,12 @@ mod tests {
assert_eq!(ctx.get_nodes_by_lower_name("foo").len(), 1);
assert!(ctx.get_node_by_id("f1").is_some());
assert!(ctx.get_node_by_id("missing").is_none());
let by_name = ctx.get_nodes_by_name_shared("foo");
let by_name_cached = ctx.get_nodes_by_name_shared("foo");
assert!(Arc::ptr_eq(&by_name[0], &by_name_cached[0]));
let by_file = ctx.get_nodes_in_file_shared("a.ts");
let by_file_cached = ctx.get_nodes_in_file_shared("a.ts");
assert!(Arc::ptr_eq(&by_file[0], &by_file_cached[0]));
assert!(ctx.known_node_names().contains(&"foo".to_string()));
assert_eq!(ctx.get_project_root(), root.to_str().unwrap());
std::fs::remove_dir_all(&root).ok();
Expand Down Expand Up @@ -798,7 +848,7 @@ mod tests {
node("b", "n", NodeKind::Function, "b.ts", 1, 0),
node("a", "n", NodeKind::Function, "a.ts", 1, 0),
];
order_candidates_pub(&mut nodes);
order_candidates(&mut nodes);
assert_eq!(nodes[0].id, "a");
assert!(is_js_family_path_pub("x.ts"));
assert!(!is_js_family_path_pub("x.py"));
Expand Down
29 changes: 17 additions & 12 deletions crates/codegraph-resolve/src/frameworks/godot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,12 @@ impl FrameworkResolver for GodotResolver {
/// `[autoload]` entries that have a backing script path.
fn autoload_singletons(context: &dyn ResolutionContext) -> Vec<Node> {
context
.get_nodes_by_kind(NodeKind::Constant)
.get_nodes_by_kind_shared(NodeKind::Constant)
.into_iter()
.filter(|n| {
n.language == Language::GodotProject && godot_project::is_project_godot(&n.file_path)
})
.map(|node| node.as_ref().clone())
.collect()
}

Expand All @@ -360,10 +361,10 @@ fn resolve_class_member(
member: &str,
) -> Option<Node> {
let mut class_files: Vec<String> = context
.get_nodes_by_name(receiver)
.get_nodes_by_name_shared(receiver)
.into_iter()
.filter(|n| n.kind == NodeKind::Class && n.language == Language::Gdscript)
.map(|n| n.file_path)
.map(|n| n.file_path.clone())
.collect();
if class_files.is_empty() {
return None;
Expand All @@ -373,7 +374,7 @@ fn resolve_class_member(
let class_file = &class_files[0];

let mut matches = context
.get_nodes_in_file(class_file)
.get_nodes_in_file_shared(class_file)
.into_iter()
.filter(|n| {
n.kind == NodeKind::Function && n.language == Language::Gdscript && n.name == member
Expand All @@ -382,7 +383,7 @@ fn resolve_class_member(
if matches.next().is_some() {
return None;
}
Some(first)
Some(first.as_ref().clone())
}

/// Resolve an autoload method call `access` (`Receiver.member`) to the UNIQUE
Expand Down Expand Up @@ -410,7 +411,7 @@ fn resolve_autoload_func(context: &dyn ResolutionContext, access: &str) -> Optio
let script_path = bindings.get(receiver)?;

let mut matches = context
.get_nodes_in_file(script_path)
.get_nodes_in_file_shared(script_path)
.into_iter()
.filter(|n| {
n.kind == NodeKind::Function && n.language == Language::Gdscript && n.name == member
Expand All @@ -419,7 +420,7 @@ fn resolve_autoload_func(context: &dyn ResolutionContext, access: &str) -> Optio
if matches.next().is_some() {
return None;
}
Some(first)
Some(first.as_ref().clone())
}

/// Find the autoload singleton node whose name EXACTLY equals `name`.
Expand All @@ -428,11 +429,15 @@ fn resolve_autoload_func(context: &dyn ResolutionContext, access: &str) -> Optio
/// resolves when it names a real `[autoload]` singleton. Scans the `Constant`
/// nodes restricted to `project.godot` so it cannot match an unrelated constant.
fn find_autoload_singleton(context: &dyn ResolutionContext, name: &str) -> Option<Node> {
context.get_nodes_by_name(name).into_iter().find(|n| {
n.kind == NodeKind::Constant
&& n.language == Language::GodotProject
&& godot_project::is_project_godot(&n.file_path)
})
context
.get_nodes_by_name_shared(name)
.into_iter()
.find(|n| {
n.kind == NodeKind::Constant
&& n.language == Language::GodotProject
&& godot_project::is_project_godot(&n.file_path)
})
.map(|node| node.as_ref().clone())
}

/// Map of `autoload name -> repo-relative script path`, recovered by RE-READING
Expand Down
18 changes: 9 additions & 9 deletions crates/codegraph-resolve/src/frameworks/nestjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ impl FrameworkResolver for NestjsResolver {
if !suffix.is_match(&reference.reference_name) {
continue;
}
let candidates: Vec<Node> = context
.get_nodes_by_name(&reference.reference_name)
let candidates = context
.get_nodes_by_name_shared(&reference.reference_name)
.into_iter()
.filter(|n| n.kind == NodeKind::Class)
.collect();
.collect::<Vec<_>>();
if candidates.is_empty() {
return None;
}
Expand Down Expand Up @@ -270,17 +270,17 @@ impl FrameworkResolver for NestjsResolver {

let mut updates: Vec<Node> = Vec::new();
for (controller_name, prefix) in &controller_to_prefix {
let classes: Vec<Node> = context
.get_nodes_by_name(controller_name)
let classes = context
.get_nodes_by_name_shared(controller_name)
.into_iter()
.filter(|n| n.kind == NodeKind::Class)
.collect();
.collect::<Vec<_>>();
for cls in classes {
let routes: Vec<Node> = context
.get_nodes_in_file(&cls.file_path)
let routes = context
.get_nodes_in_file_shared(&cls.file_path)
.into_iter()
.filter(|n| n.kind == NodeKind::Route)
.collect();
.collect::<Vec<_>>();
for route in routes {
if route.start_line < cls.start_line || route.start_line > cls.end_line {
continue;
Expand Down
8 changes: 4 additions & 4 deletions crates/codegraph-resolve/src/frameworks/react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ fn resolve_component(
from_file: &str,
context: &dyn ResolutionContext,
) -> Option<String> {
let candidates = context.get_nodes_by_name(name);
let candidates = context.get_nodes_by_name_shared(name);
if candidates.is_empty() {
return None;
}
Expand Down Expand Up @@ -392,7 +392,7 @@ fn resolve_component(

/// `resolveHook` (react.ts:328-343).
fn resolve_hook(name: &str, context: &dyn ResolutionContext) -> Option<String> {
let candidates = context.get_nodes_by_name(name);
let candidates = context.get_nodes_by_name_shared(name);
if candidates.is_empty() {
return None;
}
Expand All @@ -415,12 +415,12 @@ fn resolve_hook(name: &str, context: &dyn ResolutionContext) -> Option<String> {

/// `resolveContext` (react.ts:348-368).
fn resolve_context(name: &str, context: &dyn ResolutionContext) -> Option<String> {
let candidates = context.get_nodes_by_name(name);
let candidates = context.get_nodes_by_name_shared(name);
if candidates.is_empty() {
// Strip Context/Provider suffix fallback (react.ts:351-357).
let base_name = strip_context_suffix(name);
if base_name != name {
let base_candidates = context.get_nodes_by_name(base_name);
let base_candidates = context.get_nodes_by_name_shared(base_name);
if !base_candidates.is_empty() {
return Some(base_candidates[0].id.clone());
}
Expand Down
6 changes: 3 additions & 3 deletions crates/codegraph-resolve/src/frameworks/tauri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ fn build_roster(context: &dyn ResolutionContext) -> Roster {
/// cannot say which function the attribute belongs to, so the key is poisoned
/// rather than guessed.
fn admit_command(roster: &mut Roster, context: &dyn ResolutionContext, file: &str, name: &str) {
let mut candidates: Vec<Node> = context
.get_nodes_in_file(file)
let mut candidates = context
.get_nodes_in_file_shared(file)
.into_iter()
.filter(|n| n.name == name && matches!(n.kind, NodeKind::Function | NodeKind::Method))
.collect();
.collect::<Vec<_>>();
let camel = snake_to_camel(name);
match candidates.len() {
1 => {
Expand Down
6 changes: 3 additions & 3 deletions crates/codegraph-resolve/src/frameworks/vue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ fn resolve_alias(alias_path: &str, context: &dyn ResolutionContext) -> Option<St
for ext in EXTS {
let full_path = format!("{alias_path}{ext}");
if context.file_exists(&full_path) {
let nodes = context.get_nodes_in_file(&full_path);
let nodes = context.get_nodes_in_file_shared(&full_path);
if !nodes.is_empty() {
return Some(nodes[0].id.clone());
}
Expand Down Expand Up @@ -299,10 +299,10 @@ fn resolve_component(

let component_in = |file: &str| -> Option<String> {
context
.get_nodes_in_file(file)
.get_nodes_in_file_shared(file)
.into_iter()
.find(|n| n.kind == NodeKind::Component && n.name == name)
.map(|n| n.id)
.map(|n| n.id.clone())
};

let from_dir = dir_of(from_file);
Expand Down
Loading
Loading