@@ -6943,11 +6943,16 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
69436943 * scope. Note the call-scope side (compute_func_qn in extract_unified.c) is
69446944 * NOT language-gated — it qualifies whenever a scope is pushed — so this gate
69456945 * and the scope-push rule must move together, or a call QN names a def QN that
6946- * was never minted and the edge is dropped at write. */
6946+ * was never minted and the edge is dropped at write.
6947+ *
6948+ * Python: the only enclosing scope that reaches here is the def a nested
6949+ * def sits in (py_push_def_body), so `def index()` inside create_app is
6950+ * proj.mod.create_app.index. */
69476951 if (ctx->enclosing_class_qn &&
69486952 (ctx->language == CBM_LANG_CPP || ctx->language == CBM_LANG_CUDA ||
69496953 ctx->language == CBM_LANG_TYPESCRIPT || ctx->language == CBM_LANG_TSX ||
6950- ctx->language == CBM_LANG_ARKTS || ctx->language == CBM_LANG_NIX)) {
6954+ ctx->language == CBM_LANG_ARKTS || ctx->language == CBM_LANG_NIX ||
6955+ ctx->language == CBM_LANG_PYTHON)) {
69516956 def.qualified_name = cbm_arena_sprintf(a, "%s.%s", ctx->enclosing_class_qn, qn_name);
69526957 }
69536958 def.label = "Function";
@@ -11990,6 +11995,51 @@ static void push_class_body_children(TSNode node, const CBMLangSpec *spec, wd_st
1199011995 }
1199111996}
1199211997
11998+ /* Python: a def's body is a scope of its own, and a `def` or `class` statement
11999+ * in it is a definition like any other -- closures, decorator wrappers, the
12000+ * route handlers of an app factory (`@app.route` inside create_app), local
12001+ * classes. Walk the body with the def's QN as the enclosing scope, so a nested
12002+ * def is qualified by the def it sits in: proj.mod.create_app.index. That is
12003+ * the QN the Python LSP already gives the calls inside it (py_process_function)
12004+ * and the one the call-scope walk computes (extract_unified.c
12005+ * py_nearest_scope_qn). Only the body is walked: parameters,
12006+ * annotations and decorators hold expressions, never statements. A lambda or
12007+ * a comprehension holds no statements either, so neither becomes a def. */
12008+ static void py_push_def_body(wd_stack_t *s, TSNode def_node, const char *def_qn) {
12009+ TSNode body = ts_node_child_by_field_name(def_node, TS_FIELD("body"));
12010+ if (def_qn && !ts_node_is_null(body)) {
12011+ wd_push(s, body, def_qn);
12012+ }
12013+ }
12014+
12015+ /* Python methods are extracted by extract_class_methods, not by the walk, so
12016+ * their bodies are pushed here, under the QN push_method_def gave the method
12017+ * (class_qn.name), in source order. */
12018+ static void py_push_method_bodies(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
12019+ wd_stack_t *s) {
12020+ TSNode body = find_class_member_body(class_node, ctx->language);
12021+ if (ts_node_is_null(body) || !class_qn) {
12022+ return;
12023+ }
12024+ for (int i = (int)ts_node_named_child_count(body) - SKIP_CHAR; i >= 0; i--) {
12025+ TSNode method = ts_node_named_child(body, (uint32_t)i);
12026+ if (strcmp(ts_node_type(method), "decorated_definition") == 0) {
12027+ method = ts_node_child_by_field_name(method, TS_FIELD("definition"));
12028+ }
12029+ if (ts_node_is_null(method) || strcmp(ts_node_type(method), "function_definition") != 0) {
12030+ continue;
12031+ }
12032+ TSNode name_node = resolve_method_name(method, ctx->language);
12033+ char *name =
12034+ ts_node_is_null(name_node)
12035+ ? NULL
12036+ : cbm_func_name_node_text(ctx->arena, name_node, ctx->source, ctx->language);
12037+ if (name && name[0]) {
12038+ py_push_def_body(s, method, cbm_arena_sprintf(ctx->arena, "%s.%s", class_qn, name));
12039+ }
12040+ }
12041+ }
12042+
1199312043// ASCII case-insensitive equality vs a lowercase literal (CFML attribute names
1199412044// are case-insensitive, e.g. NAME / Name / name).
1199512045static bool ascii_ci_equals(const char *s, const char *lower_lit) {
@@ -12534,6 +12584,16 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
1253412584 }
1253512585 if (cbm_kind_in_set(node, spec->function_node_types)) {
1253612586 if (!is_template_class_node(node, ctx->language)) {
12587+ if (ctx->language == CBM_LANG_PYTHON) {
12588+ int defs_before = ctx->result->defs.count;
12589+ extract_func_def(ctx, node, spec);
12590+ if (ctx->result->defs.count > defs_before) {
12591+ py_push_def_body(&s, node,
12592+ ctx->result->defs.items[ctx->result->defs.count - SKIP_ONE]
12593+ .qualified_name);
12594+ }
12595+ continue;
12596+ }
1253712597 extract_func_def(ctx, node, spec);
1253812598 // Most languages stop here. JS/TS (and Wolfram) descend into the
1253912599 // function body so NESTED named definitions are also captured —
@@ -12591,6 +12651,9 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
1259112651 extract_class_def(ctx, node, spec);
1259212652 const char *new_enclosing = compute_class_qn(ctx, node, frame.enclosing_class_qn);
1259312653 push_class_body_children(node, spec, &s, new_enclosing, ctx);
12654+ if (ctx->language == CBM_LANG_PYTHON && new_enclosing != frame.enclosing_class_qn) {
12655+ py_push_method_bodies(ctx, node, new_enclosing, &s);
12656+ }
1259412657 continue;
1259512658 }
1259612659
0 commit comments