MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extract_object_literal

Method extract_object_literal

codegraph-kernel/src/kotlin.rs:1473–1555  ·  view source on GitHub ↗

extractKotlinObjectLiteral — `object : Api.Listener { … }`: an anonymous class. Headers (each delegation_specifier) walked first by the CURRENT walker in the enclosing scope; an `instantiates` ref from the stack top to the first supertype's last segment; a class ` `; `extends` refs (dotted path) from it, at each path's first type_identifier; its class_body children through visit_no

(&mut self, node: Node<'t>, body_walker: bool)

Source from the content-addressed store, hash-verified

1471 /// type_identifier; its class_body children through visit_node. False
1472 /// (nothing done) without a class_body.
1473 fn extract_object_literal(&mut self, node: Node<'t>, body_walker: bool) -> bool {
1474 stack_guard!();
1475 let Some(body) = (0..node.named_child_count())
1476 .filter_map(|i| node.named_child(i))
1477 .find(|c| c.kind() == "class_body")
1478 else {
1479 return false;
1480 };
1481 let specs: Vec<Node<'t>> = (0..node.named_child_count())
1482 .filter_map(|i| node.named_child(i))
1483 .filter(|c| c.kind() == "delegation_specifier")
1484 .collect();
1485 // (name, first type_identifier, built by a constructor_invocation)
1486 let mut supertypes: Vec<(String, Node<'t>, bool)> = Vec::new();
1487 for spec in &specs {
1488 let holder = (0..spec.named_child_count())
1489 .filter_map(|j| spec.named_child(j))
1490 .find(|c| c.kind() == "constructor_invocation" || c.kind() == "explicit_delegation");
1491 let from = holder.unwrap_or(*spec);
1492 let user_type = (0..from.named_child_count())
1493 .filter_map(|j| from.named_child(j))
1494 .find(|c| c.kind() == "user_type");
1495 let ids: Vec<Node<'t>> = match user_type {
1496 Some(t) => (0..t.named_child_count())
1497 .filter_map(|j| t.named_child(j))
1498 .filter(|c| c.kind() == "type_identifier")
1499 .collect(),
1500 None => Vec::new(),
1501 };
1502 if let Some(first) = ids.first() {
1503 let name = ids.iter().map(|c| self.text(*c)).collect::<Vec<_>>().join(".");
1504 let built = holder.map(|h| h.kind() == "constructor_invocation").unwrap_or(false);
1505 supertypes.push((name, *first, built));
1506 }
1507 }
1508 for spec in &specs {
1509 if body_walker {
1510 self.visit_for_calls_and_structure(*spec);
1511 } else {
1512 self.visit_node(*spec);
1513 }
1514 }
1515 // The class it builds, else its first supertype: the object's name and,
1516 // as written, what the code around instantiates.
1517 let primary = supertypes.iter().find(|s| s.2).or(supertypes.first());
1518 let short_name = match primary {
1519 Some((name, _, _)) => match name.rfind('.') {
1520 Some(dot) => name[dot + 1..].to_string(),
1521 None => name.clone(),
1522 },
1523 None => "Object".to_string(),
1524 };
1525 if let Some((written, at, _)) = primary {
1526 if !self.stack.is_empty() {
1527 let owner = self.top_row();
1528 let at = *at;
1529 let written = written.clone();
1530 self.push_ref_at(owner, &written, edge_kind_index("instantiates").unwrap(), at);

Callers 2

visit_nodeMethod · 0.80

Calls 14

edge_kind_indexFunction · 0.85
mapMethod · 0.80
pushMethod · 0.80
textMethod · 0.65
unwrapMethod · 0.60
collectMethod · 0.45
firstMethod · 0.45
joinMethod · 0.45
visit_nodeMethod · 0.45
top_rowMethod · 0.45
push_ref_atMethod · 0.45

Tested by

no test coverage detected