@@ -6624,11 +6624,16 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
66246624 * scope. Note the call-scope side (compute_func_qn in extract_unified.c) is
66256625 * NOT language-gated — it qualifies whenever a scope is pushed — so this gate
66266626 * and the scope-push rule must move together, or a call QN names a def QN that
6627- * was never minted and the edge is dropped at write. */
6627+ * was never minted and the edge is dropped at write.
6628+ *
6629+ * Python: the only enclosing scope that reaches here is the def a nested
6630+ * def sits in (py_push_def_body), so `def index()` inside create_app is
6631+ * proj.mod.create_app.index. */
66286632 if (ctx->enclosing_class_qn &&
66296633 (ctx->language == CBM_LANG_CPP || ctx->language == CBM_LANG_CUDA ||
66306634 ctx->language == CBM_LANG_TYPESCRIPT || ctx->language == CBM_LANG_TSX ||
6631- ctx->language == CBM_LANG_ARKTS || ctx->language == CBM_LANG_NIX)) {
6635+ ctx->language == CBM_LANG_ARKTS || ctx->language == CBM_LANG_NIX ||
6636+ ctx->language == CBM_LANG_PYTHON)) {
66326637 def.qualified_name = cbm_arena_sprintf(a, "%s.%s", ctx->enclosing_class_qn, qn_name);
66336638 }
66346639 def.label = "Function";
@@ -11667,6 +11672,51 @@ static void push_class_body_children(TSNode node, const CBMLangSpec *spec, wd_st
1166711672 }
1166811673}
1166911674
11675+ /* Python: a def's body is a scope of its own, and a `def` or `class` statement
11676+ * in it is a definition like any other -- closures, decorator wrappers, the
11677+ * route handlers of an app factory (`@app.route` inside create_app), local
11678+ * classes. Walk the body with the def's QN as the enclosing scope, so a nested
11679+ * def is qualified by the def it sits in: proj.mod.create_app.index. That is
11680+ * the QN the Python LSP already gives the calls inside it (py_process_function)
11681+ * and the one the call-scope walk computes (extract_unified.c
11682+ * py_nearest_scope_qn). Only the body is walked: parameters,
11683+ * annotations and decorators hold expressions, never statements. A lambda or
11684+ * a comprehension holds no statements either, so neither becomes a def. */
11685+ static void py_push_def_body(wd_stack_t *s, TSNode def_node, const char *def_qn) {
11686+ TSNode body = ts_node_child_by_field_name(def_node, TS_FIELD("body"));
11687+ if (def_qn && !ts_node_is_null(body)) {
11688+ wd_push(s, body, def_qn);
11689+ }
11690+ }
11691+
11692+ /* Python methods are extracted by extract_class_methods, not by the walk, so
11693+ * their bodies are pushed here, under the QN push_method_def gave the method
11694+ * (class_qn.name), in source order. */
11695+ static void py_push_method_bodies(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
11696+ wd_stack_t *s) {
11697+ TSNode body = find_class_member_body(class_node, ctx->language);
11698+ if (ts_node_is_null(body) || !class_qn) {
11699+ return;
11700+ }
11701+ for (int i = (int)ts_node_named_child_count(body) - SKIP_CHAR; i >= 0; i--) {
11702+ TSNode method = ts_node_named_child(body, (uint32_t)i);
11703+ if (strcmp(ts_node_type(method), "decorated_definition") == 0) {
11704+ method = ts_node_child_by_field_name(method, TS_FIELD("definition"));
11705+ }
11706+ if (ts_node_is_null(method) || strcmp(ts_node_type(method), "function_definition") != 0) {
11707+ continue;
11708+ }
11709+ TSNode name_node = resolve_method_name(method, ctx->language);
11710+ char *name =
11711+ ts_node_is_null(name_node)
11712+ ? NULL
11713+ : cbm_func_name_node_text(ctx->arena, name_node, ctx->source, ctx->language);
11714+ if (name && name[0]) {
11715+ py_push_def_body(s, method, cbm_arena_sprintf(ctx->arena, "%s.%s", class_qn, name));
11716+ }
11717+ }
11718+ }
11719+
1167011720// ASCII case-insensitive equality vs a lowercase literal (CFML attribute names
1167111721// are case-insensitive, e.g. NAME / Name / name).
1167211722static bool ascii_ci_equals(const char *s, const char *lower_lit) {
@@ -12211,6 +12261,16 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
1221112261 }
1221212262 if (cbm_kind_in_set(node, spec->function_node_types)) {
1221312263 if (!is_template_class_node(node, ctx->language)) {
12264+ if (ctx->language == CBM_LANG_PYTHON) {
12265+ int defs_before = ctx->result->defs.count;
12266+ extract_func_def(ctx, node, spec);
12267+ if (ctx->result->defs.count > defs_before) {
12268+ py_push_def_body(&s, node,
12269+ ctx->result->defs.items[ctx->result->defs.count - SKIP_ONE]
12270+ .qualified_name);
12271+ }
12272+ continue;
12273+ }
1221412274 extract_func_def(ctx, node, spec);
1221512275 // Most languages stop here. JS/TS (and Wolfram) descend into the
1221612276 // function body so NESTED named definitions are also captured —
@@ -12268,6 +12328,9 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
1226812328 extract_class_def(ctx, node, spec);
1226912329 const char *new_enclosing = compute_class_qn(ctx, node, frame.enclosing_class_qn);
1227012330 push_class_body_children(node, spec, &s, new_enclosing, ctx);
12331+ if (ctx->language == CBM_LANG_PYTHON && new_enclosing != frame.enclosing_class_qn) {
12332+ py_push_method_bodies(ctx, node, new_enclosing, &s);
12333+ }
1227112334 continue;
1227212335 }
1227312336
0 commit comments