Resolve a selector_expression that may chain through call_expressions. * Go pattern: pb.NewFooClient(conn).GetBar → "pb.NewFooClient.GetBar" * Without this, cbm_node_text returns full text including args/parens. * Iteratively walks the chain: selector → operand(call) → function(selector) → ... */
| 155 | * Without this, cbm_node_text returns full text including args/parens. |
| 156 | * Iteratively walks the chain: selector → operand(call) → function(selector) → ... */ |
| 157 | static char *resolve_chained_selector(CBMArena *a, TSNode sel, const char *source) { |
| 158 | TSNode operand = ts_node_child_by_field_name(sel, TS_FIELD("operand")); |
| 159 | TSNode field = ts_node_child_by_field_name(sel, TS_FIELD("field")); |
| 160 | if (ts_node_is_null(operand) || ts_node_is_null(field) || |
| 161 | strcmp(ts_node_type(operand), "call_expression") != 0) { |
| 162 | return cbm_node_text(a, sel, source); |
| 163 | } |
| 164 | |
| 165 | /* Operand is a call_expression — extract its callee iteratively. |
| 166 | * Walk: call_expression → function field → if selector_expression, repeat. */ |
| 167 | char *method = cbm_node_text(a, field, source); |
| 168 | TSNode inner = operand; |
| 169 | enum { MAX_CHAIN_DEPTH = 4 }; |
| 170 | for (int depth = 0; depth < MAX_CHAIN_DEPTH; depth++) { |
| 171 | TSNode fn = ts_node_child_by_field_name(inner, TS_FIELD("function")); |
| 172 | if (ts_node_is_null(fn)) { |
| 173 | break; |
| 174 | } |
| 175 | const char *fnk = ts_node_type(fn); |
| 176 | if (strcmp(fnk, "selector_expression") == 0) { |
| 177 | /* Check if this selector also chains through a call */ |
| 178 | TSNode inner_op = ts_node_child_by_field_name(fn, TS_FIELD("operand")); |
| 179 | if (!ts_node_is_null(inner_op) && |
| 180 | strcmp(ts_node_type(inner_op), "call_expression") == 0) { |
| 181 | inner = inner_op; |
| 182 | continue; |
| 183 | } |
| 184 | } |
| 185 | /* Reached a non-chained callee — extract its text */ |
| 186 | char *base = cbm_node_text(a, fn, source); |
| 187 | if (base && method) { |
| 188 | return cbm_arena_sprintf(a, "%s.%s", base, method); |
| 189 | } |
| 190 | return method; |
| 191 | } |
| 192 | |
| 193 | /* Fallback: just return the method name */ |
| 194 | return method; |
| 195 | } |
| 196 | |
| 197 | // Strip a trailing generic argument list ("<...>" / "[...]") from a type name, |
| 198 | // returning the bare type identifier. Mutates an arena-owned copy in place. |
no test coverage detected