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

Method extract_object_literal

codegraph-kernel/src/kotlin.rs:1423–1509  ·  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

1421 /// type_identifier; its class_body children through visit_node. False
1422 /// (nothing done) without a class_body.
1423 fn extract_object_literal(&mut self, node: Node<'t>, body_walker: bool) -> bool {
1424 stack_guard!();
1425 let Some(body) = (0..node.named_child_count())
1426 .filter_map(|i| node.named_child(i))
1427 .find(|c| c.kind() == "class_body")
1428 else {
1429 return false;
1430 };
1431 let specs: Vec<Node<'t>> = (0..node.named_child_count())
1432 .filter_map(|i| node.named_child(i))
1433 .filter(|c| c.kind() == "delegation_specifier")
1434 .collect();
1435 // (name, first type_identifier, built by a constructor_invocation)
1436 let mut supertypes: Vec<(String, Node<'t>, bool)> = Vec::new();
1437 for spec in &specs {
1438 let holder = (0..spec.named_child_count())
1439 .filter_map(|j| spec.named_child(j))
1440 .find(|c| c.kind() == "constructor_invocation" || c.kind() == "explicit_delegation");
1441 let from = holder.unwrap_or(*spec);
1442 let user_type = (0..from.named_child_count())
1443 .filter_map(|j| from.named_child(j))
1444 .find(|c| c.kind() == "user_type");
1445 let ids: Vec<Node<'t>> = match user_type {
1446 Some(t) => (0..t.named_child_count())
1447 .filter_map(|j| t.named_child(j))
1448 .filter(|c| c.kind() == "type_identifier")
1449 .collect(),
1450 None => Vec::new(),
1451 };
1452 if let Some(first) = ids.first() {
1453 let name = ids.iter().map(|c| self.text(*c)).collect::<Vec<_>>().join(".");
1454 let built = holder.map(|h| h.kind() == "constructor_invocation").unwrap_or(false);
1455 supertypes.push((name, *first, built));
1456 }
1457 }
1458 for spec in &specs {
1459 if body_walker {
1460 self.visit_for_calls_and_structure(*spec);
1461 } else {
1462 self.visit_node(*spec);
1463 }
1464 }
1465 // The class it builds, else its first supertype: the object's name and,
1466 // as written, what the code around instantiates.
1467 let primary = supertypes.iter().find(|s| s.2).or(supertypes.first());
1468 let short_name = match primary {
1469 Some((name, _, _)) => match name.rfind('.') {
1470 Some(dot) => name[dot + 1..].to_string(),
1471 None => name.clone(),
1472 },
1473 None => "Object".to_string(),
1474 };
1475 if let Some((written, at, _)) = primary {
1476 if !self.stack.is_empty() {
1477 let owner = self.top_row();
1478 let at = *at;
1479 let written = written.clone();
1480 self.push_ref_at(owner, &written, edge_kind_index("instantiates").unwrap(), at);

Callers 2

visit_nodeMethod · 0.80

Calls 13

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