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

Method maybe_capture_fn_refs

codegraph-kernel/src/python.rs:862–938  ·  view source on GitHub ↗
(&mut self, node: Node<'t>)

Source from the content-addressed store, hash-verified

860 // --- fn refs (PYTHON_SPEC) ------------------------------------------------------
861
862 fn maybe_capture_fn_refs(&mut self, node: Node<'t>) {
863 let (mode, field): (&str, &str) = match node.kind() {
864 "argument_list" => ("args", ""),
865 "assignment" => ("rhs", "right"),
866 "keyword_argument" => ("value", "value"),
867 "pair" => ("value", "value"),
868 "list" => ("list", ""),
869 // `return SomeClass` / `return handler` (#1478) — a single
870 // returned expression is a direct named child ('list' shape);
871 // tuple returns sit under expression_list and are not descended
872 // (mirrors PYTHON_SPEC).
873 "return_statement" => ("list", ""),
874 _ => return,
875 };
876 if self.stack.is_empty() {
877 return;
878 }
879 let from = self.top_row();
880
881 let mut values: Vec<Node> = Vec::new();
882 match mode {
883 "args" | "list" => {
884 for c in node.named_children(&mut node.walk()) {
885 values.push(c);
886 }
887 }
888 "rhs" => {
889 if let Some(rhs) = node.child_by_field_name(field) {
890 let lhs_text = node
891 .child_by_field_name("left")
892 .map(|l| self.text(l))
893 .unwrap_or("");
894 let lhs_last = util::lhs_last_name()
895 .captures(lhs_text)
896 .and_then(|c| c.get(1))
897 .map(|m| m.as_str());
898 if !(lhs_last.is_some() && lhs_last == Some(self.text(rhs).trim())) {
899 values.push(rhs);
900 }
901 }
902 }
903 _ => {
904 if let Some(v) = node.child_by_field_name(field) {
905 values.push(v);
906 }
907 }
908 }
909
910 for v in values {
911 let (name, anchor) = match v.kind() {
912 "identifier" => (self.text(v).to_string(), v),
913 // Preserve only statically named member chains, as on WASM.
914 "attribute" => {
915 let Some(attr) = v.child_by_field_name("attribute") else { continue };
916 let name = self.text(v);
917 if !name.split('.').all(|part| {
918 !part.is_empty() && part.chars().enumerate().all(|(i, c)| {
919 c == '_' || c.is_ascii_alphabetic() || (i > 0 && c.is_ascii_digit())

Callers 3

visit_nodeMethod · 0.45
scan_fn_ref_subtreeMethod · 0.45

Calls 8

pushMethod · 0.80
mapMethod · 0.80
is_stoplistedFunction · 0.70
textMethod · 0.65
getMethod · 0.65
allMethod · 0.65
top_rowMethod · 0.45
walkMethod · 0.45

Tested by

no test coverage detected