Skip to content

Commit 5b661a7

Browse files
committed
refactor(dependencies): consolidate RefFinder structs
1 parent 88cf9a1 commit 5b661a7

1 file changed

Lines changed: 33 additions & 38 deletions

File tree

crates/plotnik-lib/src/query/dependencies.rs

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,19 @@ fn collect_refs<'a>(expr: &Expr, symbol_table: &'a SymbolTable) -> IndexSet<&'a
515515
refs
516516
}
517517

518+
/// Whether to search for any reference or only unguarded ones.
519+
#[derive(Clone, Copy, PartialEq, Eq)]
520+
enum RefSearchMode {
521+
/// Find any reference to the target.
522+
Any,
523+
/// Find only unguarded references (not inside a NamedNode/AnonymousNode).
524+
Unguarded,
525+
}
526+
518527
struct RefFinder<'a> {
519528
target: &'a str,
520529
found: Option<TextRange>,
530+
mode: RefSearchMode,
521531
}
522532

523533
impl Visitor for RefFinder<'_> {
@@ -528,49 +538,23 @@ impl Visitor for RefFinder<'_> {
528538
walk_expr(self, expr);
529539
}
530540

531-
fn visit_ref(&mut self, r: &Ref) {
532-
if self.found.is_some() {
533-
return;
534-
}
535-
if let Some(name) = r.name()
536-
&& name.text() == self.target
537-
{
538-
self.found = Some(name.text_range());
541+
fn visit_named_node(&mut self, node: &NamedNode) {
542+
if self.mode == RefSearchMode::Unguarded {
543+
return; // Guarded: stop recursion
539544
}
540-
}
541-
}
542-
543-
fn find_ref_range(expr: &Expr, target: &str) -> Option<TextRange> {
544-
let mut visitor = RefFinder {
545-
target,
546-
found: None,
547-
};
548-
visitor.visit_expr(expr);
549-
visitor.found
550-
}
551-
552-
struct UnguardedRefFinder<'a> {
553-
target: &'a str,
554-
found: Option<TextRange>,
555-
}
556-
557-
impl Visitor for UnguardedRefFinder<'_> {
558-
fn visit_expr(&mut self, expr: &Expr) {
559-
if self.found.is_some() {
560-
return;
561-
}
562-
walk_expr(self, expr);
563-
}
564-
565-
fn visit_named_node(&mut self, _node: &NamedNode) {
566-
// Guarded: stop recursion
545+
super::visitor::walk_named_node(self, node);
567546
}
568547

569548
fn visit_anonymous_node(&mut self, _node: &AnonymousNode) {
570-
// Guarded: stop recursion
549+
if self.mode == RefSearchMode::Unguarded {
550+
return; // Guarded: stop recursion
551+
}
571552
}
572553

573554
fn visit_ref(&mut self, r: &Ref) {
555+
if self.found.is_some() {
556+
return;
557+
}
574558
if let Some(name) = r.name()
575559
&& name.text() == self.target
576560
{
@@ -584,17 +568,28 @@ impl Visitor for UnguardedRefFinder<'_> {
584568
if self.found.is_some() {
585569
return;
586570
}
587-
if expr_guarantees_consumption(&child) {
571+
if self.mode == RefSearchMode::Unguarded && expr_guarantees_consumption(&child) {
588572
return;
589573
}
590574
}
591575
}
592576
}
593577

578+
fn find_ref_range(expr: &Expr, target: &str) -> Option<TextRange> {
579+
let mut visitor = RefFinder {
580+
target,
581+
found: None,
582+
mode: RefSearchMode::Any,
583+
};
584+
visitor.visit_expr(expr);
585+
visitor.found
586+
}
587+
594588
fn find_unguarded_ref_range(expr: &Expr, target: &str) -> Option<TextRange> {
595-
let mut visitor = UnguardedRefFinder {
589+
let mut visitor = RefFinder {
596590
target,
597591
found: None,
592+
mode: RefSearchMode::Unguarded,
598593
};
599594
visitor.visit_expr(expr);
600595
visitor.found

0 commit comments

Comments
 (0)