| 1588 | // --- value references -------------------------------------------------------------- |
| 1589 | |
| 1590 | fn flush_value_refs(&mut self, root: Node<'t>) { |
| 1591 | let scopes = std::mem::take(&mut self.value_scopes); |
| 1592 | let mut targets = std::mem::take(&mut self.fs_values); |
| 1593 | let counts = std::mem::take(&mut self.fs_value_counts); |
| 1594 | if std::env::var("CODEGRAPH_VALUE_REFS").as_deref() == Ok("0") { |
| 1595 | return; |
| 1596 | } |
| 1597 | if targets.is_empty() || scopes.is_empty() || util::is_generated_file(self.file_path) { |
| 1598 | return; |
| 1599 | } |
| 1600 | |
| 1601 | // Shadow prune — kotlin cases: property_declaration (its |
| 1602 | // variable_declaration's first simple_identifier; destructuring bumps |
| 1603 | // nothing) AND the shared `assignment` case (the swift-sweep lesson — |
| 1604 | // directly_assignable_expression children bump). |
| 1605 | let mut decl_counts: HashMap<&str, u32> = HashMap::new(); |
| 1606 | let mut dstack: Vec<Node> = vec![root]; |
| 1607 | let mut dvisited = 0usize; |
| 1608 | while let Some(n) = dstack.pop() { |
| 1609 | if dvisited >= MAX_VALUE_REF_NODES { |
| 1610 | break; |
| 1611 | } |
| 1612 | dvisited += 1; |
| 1613 | if n.kind() == "assignment" { |
| 1614 | let left = n |
| 1615 | .child_by_field_name("left") |
| 1616 | .or_else(|| n.child_by_field_name("pattern")) |
| 1617 | .or_else(|| n.named_child(0)); |
| 1618 | if let Some(left) = left { |
| 1619 | if left.kind() == "identifier" { |
| 1620 | let nm = self.text(left); |
| 1621 | if targets.contains_key(nm) { |
| 1622 | *decl_counts.entry(nm).or_insert(0) += 1; |
| 1623 | } |
| 1624 | } else { |
| 1625 | for i in 0..left.named_child_count() { |
| 1626 | let Some(c) = left.named_child(i) else { continue }; |
| 1627 | if matches!(c.kind(), "identifier" | "simple_identifier") { |
| 1628 | let nm = self.text(c); |
| 1629 | if targets.contains_key(nm) { |
| 1630 | *decl_counts.entry(nm).or_insert(0) += 1; |
| 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | if n.kind() == "property_declaration" { |
| 1638 | let vd = (0..n.named_child_count()) |
| 1639 | .filter_map(|i| n.named_child(i)) |
| 1640 | .find(|c| c.kind() == "variable_declaration"); |
| 1641 | if let Some(vd) = vd { |
| 1642 | let id = (0..vd.named_child_count()) |
| 1643 | .filter_map(|i| vd.named_child(i)) |
| 1644 | .find(|c| c.kind() == "simple_identifier"); |
| 1645 | if let Some(id) = id { |
| 1646 | let nm = self.text(id); |
| 1647 | if targets.contains_key(nm) { |