LCOV - code coverage report
Current view: top level - ext - lsp_server_override.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 84.1 % 207 174
Test Date: 2026-07-13 00:58:38 Functions: 100.0 % 9 9

            Line data    Source code
       1              : /*
       2              :   +----------------------------------------------------------------------+
       3              :   | LSParrot PHP LSP Extension                                           |
       4              :   +----------------------------------------------------------------------+
       5              :   | Copyright (c) LSParrot GitHub Organization                           |
       6              :   +----------------------------------------------------------------------+
       7              :   | This source file is subject to the 0BSD license that is              |
       8              :   | bundled with this package in the file LICENSE.                       |
       9              :   +----------------------------------------------------------------------+
      10              :   | Author: Go Kudo <zeriyoshi@gmail.com>                                |
      11              :   +----------------------------------------------------------------------+
      12              : */
      13              : 
      14              : #include "lsp_internal.h"
      15              : 
      16          188 : static inline bool lsp_override_declaration_context(zend_string *text, size_t offset, zend_string *prefix, size_t *class_start, size_t *body_start, size_t *body_end, zend_long *body_depth, lsp_method_visibility *visibility, bool *is_static)
      17              : {
      18              :         const char *value, *word_start, *word_end;
      19              :         size_t prefix_start, statement_start, p;
      20          188 :         bool saw_function = false;
      21              : 
      22          188 :         value = ZSTR_VAL(text);
      23              : 
      24          188 :         if (!lsp_find_enclosing_class_header(text, offset, class_start, body_start, body_end, body_depth)) {
      25          116 :                 return false;
      26              :         }
      27              : 
      28           72 :         prefix_start = offset >= ZSTR_LEN(prefix) ? offset - ZSTR_LEN(prefix) : 0;
      29           72 :         if (prefix_start < *body_start || prefix_start > *body_end || lsp_brace_depth_at(text, prefix_start) != *body_depth) {
      30           48 :                 return false;
      31              :         }
      32              : 
      33           24 :         statement_start = prefix_start;
      34           24 :         while (statement_start > *body_start &&
      35          552 :                 value[statement_start - 1] != '\n' &&
      36          528 :                 value[statement_start - 1] != ';' &&
      37         1080 :                 value[statement_start - 1] != '{' &&
      38          528 :                 value[statement_start - 1] != '}'
      39              :         ) {
      40          528 :                 statement_start--;
      41              :         }
      42              : 
      43           24 :         *visibility = LSP_METHOD_VISIBILITY_PUBLIC;
      44           24 :         *is_static = false;
      45              : 
      46           24 :         p = statement_start;
      47           48 :         while (p < prefix_start) {
      48          168 :                 while (p < prefix_start && isspace((unsigned char) value[p])) {
      49          120 :                         p++;
      50              :                 }
      51              : 
      52           48 :                 if (p >= prefix_start) {
      53            8 :                         break;
      54              :                 }
      55              : 
      56           40 :                 if (!lsp_doc_is_identifier_start(value[p])) {
      57            0 :                         return false;
      58              :                 }
      59              : 
      60           40 :                 word_start = value + p;
      61              : 
      62           40 :                 p++;
      63          284 :                 while (p < prefix_start && lsp_doc_is_identifier_char(value[p])) {
      64          244 :                         p++;
      65              :                 }
      66              : 
      67           40 :                 word_end = value + p;
      68              : 
      69           40 :                 if (lsp_word_at_slice_equals(word_start, word_end, "public")) {
      70           20 :                         *visibility = LSP_METHOD_VISIBILITY_PUBLIC;
      71           20 :                 } else if (lsp_word_at_slice_equals(word_start, word_end, "protected")) {
      72            4 :                         *visibility = LSP_METHOD_VISIBILITY_PROTECTED;
      73           16 :                 } else if (lsp_word_at_slice_equals(word_start, word_end, "private")) {
      74            0 :                         return false;
      75           16 :                 } else if (lsp_word_at_slice_equals(word_start, word_end, "static")) {
      76            0 :                         *is_static = true;
      77           32 :                 } else if (lsp_word_at_slice_equals(word_start, word_end, "abstract") ||
      78           16 :                         lsp_word_at_slice_equals(word_start, word_end, "final")
      79              :                 ) {
      80            0 :                         continue;
      81           16 :                 } else if (lsp_word_at_slice_equals(word_start, word_end, "function")) {
      82           16 :                         saw_function = true;
      83           16 :                         break;
      84              :                 } else {
      85            0 :                         return false;
      86              :                 }
      87              :         }
      88              : 
      89           24 :         if (!saw_function) {
      90            8 :                 return false;
      91              :         }
      92              : 
      93           32 :         while (p < prefix_start && isspace((unsigned char) value[p])) {
      94           16 :                 p++;
      95              :         }
      96              : 
      97           16 :         if (p < prefix_start && value[p] == '&') {
      98            0 :                 p++;
      99            0 :                 while (p < prefix_start && isspace((unsigned char) value[p])) {
     100            0 :                         p++;
     101              :                 }
     102              :         }
     103              : 
     104           16 :         return p == prefix_start;
     105              : }
     106              : 
     107            4 : static inline void lsp_method_modifiers(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth, lsp_method_visibility *visibility, bool *is_static, bool *is_final)
     108              : {
     109              :         zend_long i;
     110              :         zval *token;
     111              : 
     112            4 :         *visibility = LSP_METHOD_VISIBILITY_PUBLIC;
     113            4 :         *is_static = false;
     114            4 :         *is_final = false;
     115              : 
     116           60 :         for (i = (zend_long) index - 1; i >= 0; i--) {
     117           56 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
     118           56 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     119            0 :                         continue;
     120              :                 }
     121              : 
     122           56 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
     123           44 :                         continue;
     124              :                 }
     125              : 
     126           12 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     127              :                         break;
     128              :                 }
     129              : 
     130           12 :                 if (lsp_token_name_equals(token, "T_PRIVATE")) {
     131            0 :                         *visibility = LSP_METHOD_VISIBILITY_PRIVATE;
     132           12 :                 } else if (lsp_token_name_equals(token, "T_PROTECTED")) {
     133            0 :                         *visibility = LSP_METHOD_VISIBILITY_PROTECTED;
     134           12 :                 } else if (lsp_token_name_equals(token, "T_PUBLIC")) {
     135            4 :                         *visibility = LSP_METHOD_VISIBILITY_PUBLIC;
     136            8 :                 } else if (lsp_token_name_equals(token, "T_STATIC")) {
     137            0 :                         *is_static = true;
     138            8 :                 } else if (lsp_token_text_equals(token, "final")) {
     139            0 :                         *is_final = true;
     140              :                 }
     141              :         }
     142            4 : }
     143              : 
     144            4 : static inline bool lsp_class_declares_method(zend_string *text, HashTable *tokens, size_t body_start, size_t body_end, zend_long body_depth, zend_string *method_name)
     145              : {
     146              :         zend_string *label;
     147              :         zval *token;
     148            4 :         uint32_t i, count = zend_hash_num_elements(tokens);
     149              : 
     150          108 :         for (i = 0; i < count; i++) {
     151          104 :                 token = zend_hash_index_find(tokens, i);
     152          104 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
     153           76 :                         continue;
     154              :                 }
     155              : 
     156           28 :                 if (!lsp_token_name_equals(token, "T_FUNCTION") || !lsp_token_at_depth(text, token, body_depth)) {
     157           24 :                         continue;
     158              :                 }
     159              : 
     160            4 :                 label = lsp_next_string_token(tokens, i + 1);
     161            4 :                 if (label && zend_string_equals(label, method_name)) {
     162            0 :                         return true;
     163              :                 }
     164              :         }
     165              : 
     166            4 :         return false;
     167              : }
     168              : 
     169            4 : static inline bool lsp_completion_items_contain_method(zval *items, zend_string *label)
     170              : {
     171              :         zend_long kind;
     172              :         zend_string *item_label;
     173              :         zval *item;
     174              : 
     175            4 :         if (Z_TYPE_P(items) != IS_ARRAY) {
     176            0 :                 return false;
     177              :         }
     178              : 
     179            4 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(items), item) {
     180            0 :                 if (Z_TYPE_P(item) != IS_ARRAY) {
     181            0 :                         continue;
     182              :                 }
     183              : 
     184            0 :                 kind = lsp_array_long(item, "kind", 0);
     185            0 :                 item_label = lsp_array_string(item, "label");
     186              : 
     187            0 :                 if (kind == 2 && item_label && zend_string_equals(item_label, label)) {
     188            0 :                         return true;
     189              :                 }
     190              :         } ZEND_HASH_FOREACH_END();
     191              : 
     192            4 :         return false;
     193              : }
     194              : 
     195            4 : static inline void lsp_snippet_append_escaped(smart_str *snippet, const char *value, size_t length)
     196              : {
     197              :         size_t i;
     198              : 
     199          132 :         for (i = 0; i < length; i++) {
     200          128 :                 if (value[i] == '\\' || value[i] == '$' || value[i] == '}') {
     201            4 :                         smart_str_appendc(snippet, '\\');
     202              :                 }
     203              : 
     204          128 :                 smart_str_appendc(snippet, value[i]);
     205              :         }
     206            4 : }
     207              : 
     208            4 : static inline zend_string *lsp_override_method_snippet(zend_string *text, zval *name_token, HashTable *tokens, uint32_t name_index, zend_long body_depth)
     209              : {
     210            4 :         const char *value = ZSTR_VAL(text);
     211            4 :         smart_str snippet = {0};
     212              :         size_t start, end;
     213              : 
     214            4 :         start = (size_t) lsp_token_long(name_token, "offset", 0);
     215            4 :         end = lsp_method_signature_end(tokens, name_index, text, body_depth);
     216              : 
     217            4 :         if (end > ZSTR_LEN(text)) {
     218            0 :                 end = ZSTR_LEN(text);
     219              :         }
     220              : 
     221           24 :         while (end > start && isspace((unsigned char) value[end - 1])) {
     222           20 :                 end--;
     223              :         }
     224              : 
     225            4 :         lsp_snippet_append_escaped(&snippet, value + start, end - start);
     226            4 :         smart_str_appendc(&snippet, '\n');
     227            4 :         smart_str_appendc(&snippet, '{');
     228            4 :         smart_str_appendc(&snippet, '\n');
     229            4 :         smart_str_appendl(&snippet, "    $0", sizeof("    $0") - 1);
     230            4 :         smart_str_appendc(&snippet, '\n');
     231            4 :         smart_str_appendc(&snippet, '}');
     232            4 :         smart_str_0(&snippet);
     233              : 
     234            4 :         return snippet.s;
     235              : }
     236              : 
     237            4 : static inline void lsp_add_override_completion_item(zval *items, lsp_document *document, size_t prefix_start, size_t offset, zend_string *label, zend_string *parent_class, zend_string *snippet, zend_string *signature)
     238              : {
     239              :         zend_string *detail, *sort_text;
     240              :         zval item, data, text_edit, edit_range;
     241              : 
     242            4 :         detail = signature
     243            4 :                 ? strpprintf(0, "override %s::%s", ZSTR_VAL(parent_class), ZSTR_VAL(signature))
     244            4 :                 : strpprintf(0, "override %s::%s", ZSTR_VAL(parent_class), ZSTR_VAL(label))
     245              :         ;
     246              : 
     247            4 :         sort_text = strpprintf(0, "0000:%s", ZSTR_VAL(label));
     248              : 
     249            4 :         array_init(&item);
     250            4 :         add_assoc_str(&item, "label", zend_string_copy(label));
     251            4 :         add_assoc_long(&item, "kind", 2);
     252            4 :         add_assoc_str(&item, "detail", detail);
     253            4 :         add_assoc_str(&item, "filterText", zend_string_copy(label));
     254            4 :         add_assoc_str(&item, "sortText", sort_text);
     255            4 :         add_assoc_long(&item, "insertTextFormat", 2);
     256            4 :         array_init(&text_edit);
     257            4 :         lsp_range_from_offsets(document->text, prefix_start, offset, &edit_range);
     258            4 :         add_assoc_zval(&text_edit, "range", &edit_range);
     259            4 :         add_assoc_str(&text_edit, "newText", zend_string_copy(snippet));
     260            4 :         add_assoc_zval(&item, "textEdit", &text_edit);
     261            4 :         array_init(&data);
     262            4 :         add_assoc_string(&data, "source", "lsparrot");
     263            4 :         add_assoc_zval(&item, "data", &data);
     264            4 :         add_next_index_zval(items, &item);
     265            4 : }
     266              : 
     267            4 : static inline void lsp_add_override_method_completions_for_class(lsp_server *server, zval *items, lsp_document *document, size_t prefix_start, size_t offset, zend_string *prefix, zend_string *class_name, lsp_method_visibility requested_visibility, bool requested_static, HashTable *current_tokens, size_t current_body_start, size_t current_body_end, zend_long current_body_depth, uint32_t depth)
     268              : {
     269              :         lsp_method_visibility visibility;
     270            4 :         zend_long body_depth = 0;
     271              :         zend_string *path, *contents, *label, *snippet, *signature, *parent_class;
     272              :         zval tokens_zv, *token, *name_token;
     273              :         HashTable *tokens;
     274              :         uint32_t i, count, name_index;
     275            4 :         size_t class_start = 0, body_start = 0, body_end = 0;
     276              :         bool method_static, method_final;
     277              : 
     278            4 :         if (depth > 8) {
     279            0 :                 return;
     280              :         }
     281              : 
     282            4 :         path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, class_name);
     283            4 :         if (!path) {
     284            0 :                 return;
     285              :         }
     286              : 
     287            4 :         contents = lsp_read_file(path);
     288            4 :         zend_string_release(path);
     289            4 :         if (contents == zend_empty_string) {
     290            0 :                 return;
     291              :         }
     292              : 
     293            4 :         ZVAL_UNDEF(&tokens_zv);
     294            4 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
     295              : 
     296            4 :         if (Z_TYPE(tokens_zv) != IS_ARRAY || !lsp_find_first_class_header(contents, &class_start, &body_start, &body_end, &body_depth)) {
     297            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
     298            0 :                         zval_ptr_dtor(&tokens_zv);
     299              :                 }
     300              : 
     301            0 :                 zend_string_release(contents);
     302              : 
     303            0 :                 return;
     304              :         }
     305              : 
     306            4 :         tokens = Z_ARRVAL(tokens_zv);
     307            4 :         count = zend_hash_num_elements(tokens);
     308          288 :         for (i = 0; i < count; i++) {
     309          284 :                 token = zend_hash_index_find(tokens, i);
     310          284 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
     311           48 :                         continue;
     312              :                 }
     313              : 
     314          236 :                 if (!lsp_token_name_equals(token, "T_FUNCTION") || !lsp_token_at_depth(contents, token, body_depth)) {
     315          224 :                         continue;
     316              :                 }
     317              : 
     318           12 :                 name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
     319           12 :                 label = lsp_token_string(name_token, "text");
     320           12 :                 if (!label ||
     321           16 :                         !lsp_matches_prefix_string(label, prefix) ||
     322            8 :                         zend_string_equals_literal(label, "__construct") ||
     323            8 :                         lsp_class_declares_method(document->text, current_tokens, current_body_start, current_body_end, current_body_depth, label) ||
     324            4 :                         lsp_completion_items_contain_method(items, label)
     325              :                 ) {
     326            8 :                         continue;
     327              :                 }
     328              : 
     329            4 :                 lsp_method_modifiers(tokens, i, contents, body_depth, &visibility, &method_static, &method_final);
     330              : 
     331            4 :                 if (visibility == LSP_METHOD_VISIBILITY_PRIVATE || method_final || visibility != requested_visibility || method_static != requested_static) {
     332            0 :                         continue;
     333              :                 }
     334              : 
     335            4 :                 snippet = lsp_override_method_snippet(contents, name_token, tokens, name_index, body_depth);
     336            4 :                 signature = lsp_function_signature_detail(contents, name_token, tokens, name_index, body_depth, NULL);
     337            4 :                 lsp_add_override_completion_item(items, document, prefix_start, offset, label, class_name, snippet, signature);
     338              : 
     339            4 :                 if (signature) {
     340            4 :                         zend_string_release(signature);
     341              :                 }
     342              : 
     343            4 :                 zend_string_release(snippet);
     344              :         }
     345              : 
     346            4 :         parent_class = lsp_class_extends_name(contents, class_start, body_start);
     347            4 :         if (parent_class) {
     348            0 :                 lsp_add_override_method_completions_for_class(server, items, document, prefix_start, offset, prefix, parent_class, requested_visibility, requested_static, current_tokens, current_body_start, current_body_end, current_body_depth, depth + 1);
     349            0 :                 zend_string_release(parent_class);
     350              :         }
     351              : 
     352            4 :         zval_ptr_dtor(&tokens_zv);
     353            4 :         zend_string_release(contents);
     354              : }
     355              : 
     356          188 : extern bool lsp_add_override_method_completions(lsp_server *server, zval *items, lsp_document *document, HashTable *tokens, size_t offset, zend_string *prefix)
     357              : {
     358              :         lsp_method_visibility visibility;
     359          188 :         zend_long body_depth = 0;
     360              :         zend_string *parent_class;
     361          188 :         size_t class_start = 0, body_start = 0, body_end = 0, prefix_start;
     362              :         bool is_static;
     363              : 
     364          188 :         if (!lsp_override_declaration_context(document->text, offset, prefix, &class_start, &body_start, &body_end, &body_depth, &visibility, &is_static)) {
     365          184 :                 return false;
     366              :         }
     367              : 
     368            4 :         parent_class = lsp_class_extends_name(document->text, class_start, body_start);
     369            4 :         if (!parent_class) {
     370            0 :                 return true;
     371              :         }
     372              : 
     373            4 :         prefix_start = offset >= ZSTR_LEN(prefix) ? offset - ZSTR_LEN(prefix) : 0;
     374            4 :         lsp_add_override_method_completions_for_class(server, items, document, prefix_start, offset, prefix, parent_class, visibility, is_static, tokens, body_start, body_end, body_depth, 0);
     375            4 :         zend_string_release(parent_class);
     376              : 
     377            4 :         return true;
     378              : }
     379              : 
        

Generated by: LCOV version 2.0-1