| 2886 | } |
| 2887 | |
| 2888 | void php_lsp_process_file(PHPLSPContext *ctx, TSNode root) { |
| 2889 | if (ts_node_is_null(root)) |
| 2890 | return; |
| 2891 | |
| 2892 | /* Pass 1: namespace + use declarations. */ |
| 2893 | // Collect top-level children once (O(n)); ts_node_child(root,i) is O(i) → O(n²). |
| 2894 | uint32_t kn = 0; |
| 2895 | TSNode *kids = cbm_lsp_collect_children(ctx->arena, root, &kn); |
| 2896 | for (uint32_t i = 0; i < kn; i++) { |
| 2897 | TSNode c = kids[i]; |
| 2898 | const char *k = ts_node_type(c); |
| 2899 | if (strcmp(k, "namespace_definition") == 0) { |
| 2900 | set_namespace_from_decl(ctx, c); |
| 2901 | } else if (strcmp(k, "namespace_use_declaration") == 0) { |
| 2902 | collect_use_declaration(ctx, c); |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | /* Pass 2: process classes and top-level functions. */ |
| 2907 | for (uint32_t i = 0; i < kn; i++) { |
| 2908 | TSNode c = kids[i]; |
| 2909 | const char *k = ts_node_type(c); |
| 2910 | if (strcmp(k, "class_declaration") == 0 || strcmp(k, "trait_declaration") == 0 || |
| 2911 | strcmp(k, "interface_declaration") == 0 || strcmp(k, "enum_declaration") == 0) { |
| 2912 | process_class_decl(ctx, c); |
| 2913 | } else if (strcmp(k, "function_definition") == 0 || |
| 2914 | strcmp(k, "function_static_declaration") == 0) { |
| 2915 | process_function_like(ctx, c); |
| 2916 | } else if (strcmp(k, "namespace_definition") == 0) { |
| 2917 | /* Body-style: namespace App { ... }. Set namespace + collect |
| 2918 | * use clauses inside the block, then walk children. */ |
| 2919 | set_namespace_from_decl(ctx, c); |
| 2920 | /* Collect any namespace_use_declaration inside the block. */ |
| 2921 | uint32_t nc2 = ts_node_child_count(c); |
| 2922 | for (uint32_t i2 = 0; i2 < nc2; i2++) { |
| 2923 | TSNode ch = ts_node_child(c, i2); |
| 2924 | if (ts_node_is_null(ch) || !ts_node_is_named(ch)) |
| 2925 | continue; |
| 2926 | if (strcmp(ts_node_type(ch), "namespace_use_declaration") == 0) { |
| 2927 | collect_use_declaration(ctx, ch); |
| 2928 | } else if (strcmp(ts_node_type(ch), "declaration_list") == 0) { |
| 2929 | /* use clauses can also appear inside the declaration_list. */ |
| 2930 | uint32_t ncc = ts_node_child_count(ch); |
| 2931 | for (uint32_t k3 = 0; k3 < ncc; k3++) { |
| 2932 | TSNode cch = ts_node_child(ch, k3); |
| 2933 | if (ts_node_is_null(cch) || !ts_node_is_named(cch)) |
| 2934 | continue; |
| 2935 | if (strcmp(ts_node_type(cch), "namespace_use_declaration") == 0) { |
| 2936 | collect_use_declaration(ctx, cch); |
| 2937 | } |
| 2938 | } |
| 2939 | } |
| 2940 | } |
| 2941 | /* The body of `namespace App { ... }` may be a `declaration_list` |
| 2942 | * or a `compound_statement` depending on tree-sitter-php |
| 2943 | * version. Iterate ALL named children of the namespace and |
| 2944 | * dispatch any class/function/etc. seen at any depth. */ |
| 2945 | for (uint32_t j = 0; j < nc2; j++) { |
no test coverage detected