LCOV - code coverage report
Current view: top level - ext - lsp_server_feature_utils.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 89.2 % 555 495
Test Date: 2026-07-13 00:58:38 Functions: 100.0 % 31 31

            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          476 : static inline zend_string *lsp_normalized_signature_slice(const char *start, size_t length)
      17              : {
      18          476 :         smart_str signature = {0};
      19              :         size_t i;
      20          476 :         bool pending_space = false;
      21              :         char c;
      22              : 
      23        11080 :         for (i = 0; i < length; i++) {
      24        10604 :                 c = start[i];
      25        10604 :                 if (isspace((unsigned char) c)) {
      26          996 :                         pending_space = true;
      27          996 :                         continue;
      28              :                 }
      29              : 
      30         9608 :                 if (pending_space && signature.s && ZSTR_LEN(signature.s) > 0 &&
      31          832 :                         c != ')' && c != ',' && c != ':' && c != '|' && c != '&' &&
      32          832 :                         ZSTR_VAL(signature.s)[ZSTR_LEN(signature.s) - 1] != '(' &&
      33          828 :                         ZSTR_VAL(signature.s)[ZSTR_LEN(signature.s) - 1] != '|'
      34              :                 ) {
      35          828 :                         smart_str_appendc(&signature, ' ');
      36              :                 }
      37              : 
      38         9608 :                 smart_str_appendc(&signature, c);
      39         9608 :                 pending_space = false;
      40              :         }
      41              : 
      42          476 :         smart_str_0(&signature);
      43              : 
      44          476 :         return signature.s ? signature.s : zend_string_init("", 0, 0);
      45              : }
      46              : 
      47          476 : static inline zend_string *lsp_signature_phpdoc_return_type(HashTable *tokens, uint32_t name_index)
      48              : {
      49              :         zend_long i;
      50              :         zend_string *comment;
      51              :         zval *token;
      52              : 
      53         3072 :         for (i = (zend_long) name_index - 1; i >= 0; i--) {
      54         3060 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
      55         3060 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
      56            0 :                         continue;
      57              :                 }
      58              : 
      59         3060 :                 if (lsp_token_name_equals(token, "T_DOC_COMMENT")) {
      60           60 :                         comment = lsp_token_string(token, "text");
      61              : 
      62           60 :                         return comment ? lsp_phpdoc_return_type_from_comment(comment) : NULL;
      63              :                 }
      64              : 
      65         3000 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
      66              :                         break;
      67              :                 }
      68              :         }
      69              : 
      70          416 :         return NULL;
      71              : }
      72              : 
      73          476 : static inline bool lsp_signature_phpdoc_type_has_detail(zend_string *type)
      74              : {
      75              :         const char *value;
      76              :         size_t i;
      77              : 
      78          476 :         if (!type || ZSTR_LEN(type) == 0) {
      79          456 :                 return false;
      80              :         }
      81              : 
      82           20 :         value = ZSTR_VAL(type);
      83          208 :         for (i = 0; i < ZSTR_LEN(type); i++) {
      84          200 :                 if (
      85          200 :                         value[i] == '<' ||
      86          188 :                         value[i] == '{' ||
      87          188 :                         value[i] == '|' ||
      88          188 :                         value[i] == '&' ||
      89          188 :                         value[i] == '[' ||
      90          188 :                         value[i] == ']'
      91              :                 ) {
      92           12 :                         return true;
      93              :                 }
      94              :         }
      95              : 
      96           16 :         return strstr(value, "non-empty") != NULL ||
      97           16 :                 strstr(value, "positive-") != NULL ||
      98            8 :                 strstr(value, "class-string") != NULL
      99              :         ;
     100              : }
     101              : 
     102           12 : static inline zend_string *lsp_signature_with_phpdoc_return(zend_string *signature, zend_string *phpdoc_return)
     103              : {
     104              :         const char *value;
     105           12 :         smart_str detail = {0};
     106              :         size_t i, close_paren, colon;
     107              :         bool found_close_paren, found_colon;
     108              : 
     109           12 :         value = ZSTR_VAL(signature);
     110           12 :         close_paren = 0;
     111           12 :         colon = 0;
     112           12 :         found_close_paren = false;
     113           12 :         found_colon = false;
     114              : 
     115          136 :         for (i = ZSTR_LEN(signature); i > 0; i--) {
     116          136 :                 if (value[i - 1] == ')') {
     117           12 :                         close_paren = i - 1;
     118           12 :                         found_close_paren = true;
     119           12 :                         break;
     120              :                 }
     121              :         }
     122              : 
     123           12 :         if (!found_close_paren) {
     124            0 :                 return NULL;
     125              :         }
     126              : 
     127           12 :         for (i = close_paren + 1; i < ZSTR_LEN(signature); i++) {
     128           12 :                 if (isspace((unsigned char) value[i])) {
     129            0 :                         continue;
     130              :                 }
     131              : 
     132           12 :                 if (value[i] == ':') {
     133           12 :                         colon = i;
     134           12 :                         found_colon = true;
     135              :                 }
     136              : 
     137           12 :                 break;
     138              :         }
     139              : 
     140           12 :         if (found_colon) {
     141           12 :                 smart_str_appendl(&detail, value, colon + 1);
     142           12 :                 smart_str_appendc(&detail, ' ');
     143           12 :                 smart_str_append(&detail, phpdoc_return);
     144              :         } else {
     145            0 :                 smart_str_append(&detail, signature);
     146            0 :                 smart_str_appendl(&detail, ": ", sizeof(": ") - 1);
     147            0 :                 smart_str_append(&detail, phpdoc_return);
     148              :         }
     149              : 
     150           12 :         smart_str_0(&detail);
     151              : 
     152           12 :         return detail.s;
     153              : }
     154              : 
     155          836 : static inline bool lsp_find_matching_close_paren(zend_string *text, size_t open_offset, size_t *close_offset)
     156              : {
     157          836 :         const char *value = ZSTR_VAL(text);
     158              :         size_t i, depth;
     159              : 
     160          836 :         if (open_offset >= ZSTR_LEN(text) || value[open_offset] != '(') {
     161            0 :                 return false;
     162              :         }
     163              : 
     164          836 :         depth = 0;
     165         8368 :         for (i = open_offset; i < ZSTR_LEN(text); i++) {
     166         8368 :                 if (value[i] == '(') {
     167          836 :                         depth++;
     168         7532 :                 } else if (value[i] == ')') {
     169          836 :                         if (depth == 0) {
     170            0 :                                 return false;
     171              :                         }
     172          836 :                         depth--;
     173          836 :                         if (depth == 0) {
     174          836 :                                 *close_offset = i;
     175          836 :                                 return true;
     176              :                         }
     177              :                 }
     178              :         }
     179              : 
     180            0 :         return false;
     181              : }
     182              : 
     183          600 : extern bool lsp_find_function_scope_at(HashTable *tokens, zend_string *text, size_t offset, size_t *param_start, size_t *param_end, size_t *body_start, size_t *body_end, zend_long *body_depth)
     184              : {
     185          600 :         const char *value = ZSTR_VAL(text);
     186              :         zval *token;
     187              :         uint32_t i, count;
     188              :         size_t token_offset, p, close_paren, open_brace, close_brace, length, best_body_start;
     189              :         bool found;
     190              : 
     191          600 :         count = zend_hash_num_elements(tokens);
     192          600 :         length = ZSTR_LEN(text);
     193          600 :         found = false;
     194          600 :         best_body_start = 0;
     195          600 :         *param_start = 0;
     196          600 :         *param_end = 0;
     197          600 :         *body_start = 0;
     198          600 :         *body_end = length;
     199          600 :         *body_depth = 0;
     200              : 
     201        44424 :         for (i = 0; i < count; i++) {
     202        43824 :                 token = zend_hash_index_find(tokens, i);
     203        43824 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
     204        42932 :                         continue;
     205              :                 }
     206              : 
     207          892 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
     208          892 :                 if (token_offset >= offset || token_offset >= length) {
     209           88 :                         continue;
     210              :                 }
     211              : 
     212          804 :                 p = token_offset;
     213        14120 :                 while (p < length && value[p] != '(' && value[p] != '{' && value[p] != ';') {
     214        13316 :                         p++;
     215              :                 }
     216              : 
     217          804 :                 if (p >= length || value[p] != '(' || !lsp_find_matching_close_paren(text, p, &close_paren)) {
     218            0 :                         continue;
     219              :                 }
     220              : 
     221          804 :                 open_brace = close_paren + 1;
     222         9160 :                 while (open_brace < length && value[open_brace] != '{' && value[open_brace] != ';') {
     223         8356 :                         open_brace++;
     224              :                 }
     225              : 
     226          804 :                 if (open_brace >= length || value[open_brace] != '{' || offset <= open_brace) {
     227            8 :                         continue;
     228              :                 }
     229              : 
     230          796 :                 if (!lsp_find_matching_brace(text, open_brace, &close_brace)) {
     231            0 :                         close_brace = length;
     232              :                 }
     233              : 
     234          796 :                 if (offset > close_brace || open_brace + 1 <= best_body_start) {
     235          292 :                         continue;
     236              :                 }
     237              : 
     238          504 :                 *param_start = p;
     239          504 :                 *param_end = close_paren;
     240          504 :                 *body_start = open_brace + 1;
     241          504 :                 *body_end = close_brace;
     242          504 :                 *body_depth = lsp_brace_depth_at(text, open_brace + 1);
     243          504 :                 best_body_start = open_brace + 1;
     244          504 :                 found = true;
     245              :         }
     246              : 
     247          600 :         return found;
     248              : }
     249              : 
     250          472 : static inline bool lsp_find_function_index_by_param_start(HashTable *tokens, zend_string *text, size_t target_param_start, uint32_t *function_index)
     251              : {
     252          472 :         const char *value = ZSTR_VAL(text);
     253              :         zval *token;
     254              :         uint32_t i, count;
     255              :         size_t token_offset, p, length;
     256              : 
     257          472 :         count = zend_hash_num_elements(tokens);
     258          472 :         length = ZSTR_LEN(text);
     259        17784 :         for (i = 0; i < count; i++) {
     260        17784 :                 token = zend_hash_index_find(tokens, i);
     261        17784 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
     262        17044 :                         continue;
     263              :                 }
     264              : 
     265          740 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
     266          740 :                 p = token_offset;
     267        13076 :                 while (p < length && value[p] != '(' && value[p] != '{' && value[p] != ';') {
     268        12336 :                         p++;
     269              :                 }
     270              : 
     271          740 :                 if (p == target_param_start) {
     272          472 :                         *function_index = i;
     273          472 :                         return true;
     274              :                 }
     275              :         }
     276              : 
     277            0 :         return false;
     278              : }
     279              : 
     280           92 : static inline bool lsp_find_function_params_at(HashTable *tokens, zend_string *text, size_t offset, size_t *param_start, size_t *param_end, uint32_t *function_index)
     281              : {
     282           92 :         const char *value = ZSTR_VAL(text);
     283              :         zval *token;
     284              :         uint32_t i, count;
     285              :         size_t token_offset, p, close_paren, length, best_param_start;
     286              :         bool found;
     287              : 
     288           92 :         count = zend_hash_num_elements(tokens);
     289           92 :         length = ZSTR_LEN(text);
     290           92 :         found = false;
     291           92 :         best_param_start = 0;
     292           92 :         *param_start = 0;
     293           92 :         *param_end = 0;
     294           92 :         *function_index = 0;
     295              : 
     296         5204 :         for (i = 0; i < count; i++) {
     297         5112 :                 token = zend_hash_index_find(tokens, i);
     298         5112 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
     299         5028 :                         continue;
     300              :                 }
     301              : 
     302           84 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
     303           84 :                 if (token_offset >= offset || token_offset >= length) {
     304           52 :                         continue;
     305              :                 }
     306              : 
     307           32 :                 p = token_offset;
     308          568 :                 while (p < length && value[p] != '(' && value[p] != '{' && value[p] != ';') {
     309          536 :                         p++;
     310              :                 }
     311              : 
     312           32 :                 if (p >= length || value[p] != '(' || !lsp_find_matching_close_paren(text, p, &close_paren)) {
     313            0 :                         continue;
     314              :                 }
     315              : 
     316           32 :                 if (offset <= p || offset >= close_paren || p <= best_param_start) {
     317           24 :                         continue;
     318              :                 }
     319              : 
     320            8 :                 *param_start = p;
     321            8 :                 *param_end = close_paren;
     322            8 :                 *function_index = i;
     323            8 :                 best_param_start = p;
     324            8 :                 found = true;
     325              :         }
     326              : 
     327           92 :         return found;
     328              : }
     329              : 
     330          104 : static inline bool lsp_function_phpdoc_range(HashTable *tokens, uint32_t function_index, size_t *doc_start, size_t *doc_end)
     331              : {
     332              :         zend_long i;
     333              :         zend_string *comment;
     334              :         zval *token;
     335              :         size_t offset, length;
     336              : 
     337          104 :         *doc_start = 0;
     338          104 :         *doc_end = 0;
     339          408 :         for (i = (zend_long) function_index - 1; i >= 0; i--) {
     340          408 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
     341          408 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     342            0 :                         continue;
     343              :                 }
     344              : 
     345          408 :                 if (lsp_token_name_equals(token, "T_DOC_COMMENT")) {
     346           20 :                         comment = lsp_token_string(token, "text");
     347           20 :                         if (!comment) {
     348            0 :                                 return false;
     349              :                         }
     350              : 
     351           20 :                         offset = (size_t) lsp_token_long(token, "offset", 0);
     352           20 :                         length = (size_t) lsp_token_long(token, "length", (zend_long) ZSTR_LEN(comment));
     353           20 :                         *doc_start = offset;
     354           20 :                         *doc_end = offset + length;
     355              : 
     356           20 :                         return true;
     357              :                 }
     358              : 
     359          388 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     360              :                         break;
     361              :                 }
     362              :         }
     363              : 
     364           84 :         return false;
     365              : }
     366              : 
     367          104 : static inline zend_string *lsp_parameter_phpdoc_type_for_function(HashTable *tokens, zend_string *text, uint32_t function_index, zend_string *variable)
     368              : {
     369              :         size_t doc_start, doc_end;
     370              : 
     371          104 :         if (!lsp_function_phpdoc_range(tokens, function_index, &doc_start, &doc_end)) {
     372           84 :                 return NULL;
     373              :         }
     374              : 
     375           20 :         return lsp_phpdoc_type_for_word_range(text, doc_start, doc_end, variable);
     376              : }
     377              : 
     378         1492 : static inline bool lsp_parameter_modifier_at(const char *value, size_t start, size_t end, const char *modifier, size_t *next)
     379              : {
     380              :         size_t length;
     381              : 
     382         1492 :         length = strlen(modifier);
     383         1492 :         if (start + length > end || strncasecmp(value + start, modifier, length) != 0) {
     384         1376 :                 return false;
     385              :         }
     386              : 
     387          116 :         if (start + length < end && lsp_doc_is_identifier_char(value[start + length])) {
     388            0 :                 return false;
     389              :         }
     390              : 
     391          116 :         *next = start + length;
     392              : 
     393          116 :         return true;
     394              : }
     395              : 
     396          116 : static inline zend_string *lsp_parameter_type_before_variable(zend_string *text, size_t variable_offset, size_t param_start)
     397              : {
     398          116 :         const char *modifiers[6] = {"public", "protected", "private", "readonly", "static", "final"}, *value = ZSTR_VAL(text);
     399              :         size_t segment_start, type_start, type_end, i, next;
     400              :         uint32_t modifier_index;
     401              :         bool stripped;
     402              : 
     403          116 :         if (variable_offset > ZSTR_LEN(text) || param_start >= variable_offset) {
     404            0 :                 return NULL;
     405              :         }
     406              : 
     407          116 :         segment_start = param_start + 1;
     408         2000 :         for (i = param_start + 1; i < variable_offset; i++) {
     409         1884 :                 if (value[i] == ',') {
     410           40 :                         segment_start = i + 1;
     411              :                 }
     412              :         }
     413              : 
     414          116 :         type_start = segment_start;
     415          116 :         type_end = variable_offset;
     416          216 :         while (type_start < type_end && isspace((unsigned char) value[type_start])) {
     417          100 :                 type_start++;
     418              :         }
     419              : 
     420          232 :         while (type_end > type_start && isspace((unsigned char) value[type_end - 1])) {
     421          116 :                 type_end--;
     422              :         }
     423              : 
     424          116 :         while (type_end > type_start && (value[type_end - 1] == '&' || value[type_end - 1] == '.')) {
     425            0 :                 type_end--;
     426            0 :                 while (type_end > type_start && isspace((unsigned char) value[type_end - 1])) {
     427            0 :                         type_end--;
     428              :                 }
     429              :         }
     430              : 
     431              :         do {
     432          140 :                 stripped = false;
     433          880 :                 for (modifier_index = 0; modifier_index < sizeof(modifiers) / sizeof(modifiers[0]); modifier_index++) {
     434          764 :                         if (!lsp_parameter_modifier_at(value, type_start, type_end, modifiers[modifier_index], &next)) {
     435          740 :                                 continue;
     436              :                         }
     437              : 
     438           24 :                         type_start = next;
     439           48 :                         while (type_start < type_end && isspace((unsigned char) value[type_start])) {
     440           24 :                                 type_start++;
     441              :                         }
     442              : 
     443           24 :                         stripped = true;
     444           24 :                         break;
     445              :                 }
     446          140 :         } while (stripped);
     447              : 
     448          116 :         if (type_end <= type_start) {
     449            0 :                 return NULL;
     450              :         }
     451              : 
     452          116 :         return zend_string_init(value + type_start, type_end - type_start, 0);
     453              : }
     454              : 
     455           72 : static inline zend_string *lsp_property_type_before_variable(zend_string *text, size_t variable_offset)
     456              : {
     457           72 :         const char *modifiers[7] = {"public", "protected", "private", "readonly", "static", "final", "var"}, *value = ZSTR_VAL(text);
     458              :         size_t declaration_start, type_start, type_end, modifier_index, next, scan;
     459              :         bool stripped;
     460              : 
     461           72 :         if (variable_offset > ZSTR_LEN(text)) {
     462            0 :                 return NULL;
     463              :         }
     464              : 
     465           72 :         declaration_start = variable_offset;
     466           72 :         while (declaration_start > 0 &&
     467         1732 :                 value[declaration_start - 1] != ';' &&
     468         3440 :                 value[declaration_start - 1] != '{' &&
     469         1660 :                 value[declaration_start - 1] != '}'
     470              :         ) {
     471         1660 :                 declaration_start--;
     472              :         }
     473              : 
     474           72 :         type_start = declaration_start;
     475           72 :         type_end = variable_offset;
     476          440 :         while (type_start < type_end && isspace((unsigned char) value[type_start])) {
     477          368 :                 type_start++;
     478              :         }
     479              : 
     480          144 :         while (type_end > type_start && isspace((unsigned char) value[type_end - 1])) {
     481           72 :                 type_end--;
     482              :         }
     483              : 
     484         1220 :         for (scan = type_start; scan + 1 < type_end; scan++) {
     485         1148 :                 if (value[scan] == '*' && value[scan + 1] == '/') {
     486            4 :                         type_start = scan + 2;
     487              :                 }
     488              :         }
     489              : 
     490           92 :         while (type_start < type_end && isspace((unsigned char) value[type_start])) {
     491           20 :                 type_start++;
     492              :         }
     493              : 
     494              :         do {
     495          164 :                 stripped = false;
     496          800 :                 for (modifier_index = 0; modifier_index < sizeof(modifiers) / sizeof(modifiers[0]); modifier_index++) {
     497          728 :                         if (!lsp_parameter_modifier_at(value, type_start, type_end, modifiers[modifier_index], &next)) {
     498          636 :                                 continue;
     499              :                         }
     500              : 
     501           92 :                         type_start = next;
     502          184 :                         while (type_start < type_end && isspace((unsigned char) value[type_start])) {
     503           92 :                                 type_start++;
     504              :                         }
     505              : 
     506           92 :                         stripped = true;
     507           92 :                         break;
     508              :                 }
     509          164 :         } while (stripped);
     510              : 
     511           72 :         if (type_end <= type_start || (type_end - type_start == 1 && value[type_start] == '?')) {
     512            0 :                 return NULL;
     513              :         }
     514              : 
     515           72 :         return zend_string_init(value + type_start, type_end - type_start, 0);
     516              : }
     517              : 
     518          564 : extern zend_string *lsp_parameter_declared_type_for_variable(lsp_document *document, zend_string *variable, size_t offset)
     519              : {
     520              :         zend_long body_depth;
     521              :         zend_string *label, *type;
     522              :         zval *tokens_zv, *token;
     523              :         HashTable *tokens;
     524              :         uint32_t i, count, function_index;
     525              :         size_t param_start, param_end, body_start, body_end, token_offset;
     526              :         bool has_parameter_context;
     527              : 
     528          564 :         if (Z_TYPE(document->lsparrot) != IS_ARRAY) {
     529            0 :                 return NULL;
     530              :         }
     531              : 
     532          564 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
     533          564 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
     534            0 :                 return NULL;
     535              :         }
     536              : 
     537          564 :         tokens = Z_ARRVAL_P(tokens_zv);
     538          564 :         has_parameter_context = false;
     539          564 :         if (lsp_find_function_scope_at(tokens, document->text, offset, &param_start, &param_end, &body_start, &body_end, &body_depth)) {
     540          472 :                 has_parameter_context = lsp_find_function_index_by_param_start(tokens, document->text, param_start, &function_index);
     541           92 :         } else if (lsp_find_function_params_at(tokens, document->text, offset, &param_start, &param_end, &function_index)) {
     542            8 :                 has_parameter_context = true;
     543              :         }
     544              : 
     545          564 :         if (!has_parameter_context) {
     546           84 :                 return NULL;
     547              :         }
     548              : 
     549          480 :         count = zend_hash_num_elements(tokens);
     550        33824 :         for (i = 0; i < count; i++) {
     551        33448 :                 token = zend_hash_index_find(tokens, i);
     552        33448 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_VARIABLE")) {
     553        31408 :                         continue;
     554              :                 }
     555              : 
     556         2040 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
     557         2040 :                 if (token_offset <= param_start || token_offset >= param_end) {
     558         1800 :                         continue;
     559              :                 }
     560              : 
     561          240 :                 label = lsp_token_string(token, "text");
     562          240 :                 if (!label || !zend_string_equals(label, variable)) {
     563          136 :                         continue;
     564              :                 }
     565              : 
     566          104 :                 type = lsp_parameter_phpdoc_type_for_function(tokens, document->text, function_index, variable);
     567          104 :                 if (type) {
     568           16 :                         return type;
     569              :                 }
     570              : 
     571           88 :                 return lsp_parameter_type_before_variable(document->text, token_offset, param_start);
     572              :         }
     573              : 
     574          376 :         return NULL;
     575              : }
     576              : 
     577           36 : static inline zend_string *lsp_scope_variable_detail(lsp_server *server, lsp_document *document, zend_string *label, size_t offset, zend_string *declared_type, bool parameter)
     578              : {
     579              :         zend_string *type, *detail;
     580              : 
     581           36 :         type = parameter ? lsp_infer_variable_type(server, document, label, offset) : NULL;
     582           36 :         if (!type) {
     583           20 :                 type = declared_type ? zend_string_copy(declared_type) : lsp_infer_variable_type(server, document, label, offset);
     584              :         }
     585           36 :         if (!type) {
     586            0 :                 type = lsp_infer_variable_declared_type(server, document, label, offset);
     587              :         }
     588              : 
     589           36 :         if (type && ZSTR_LEN(type) > 0) {
     590           36 :                 detail = strpprintf(0, "%s %s: %s", parameter ? "parameter" : "variable", ZSTR_VAL(label), ZSTR_VAL(type));
     591           36 :                 zend_string_release(type);
     592              : 
     593           36 :                 return detail;
     594              :         }
     595              : 
     596            0 :         if (type) {
     597            0 :                 zend_string_release(type);
     598              :         }
     599              : 
     600            0 :         return strpprintf(0, "%s %s", parameter ? "parameter" : "variable", ZSTR_VAL(label));
     601              : }
     602              : 
     603           16 : static inline void lsp_add_scope_this_completion(zval *items, lsp_document *document, size_t offset, zend_string *prefix, size_t prefix_start)
     604              : {
     605              :         zend_long body_depth;
     606              :         zend_string *label, *class_name, *detail;
     607              :         size_t class_start, body_start, body_end;
     608              : 
     609           16 :         if (!lsp_matches_prefix_literal("$this", prefix)) {
     610           12 :                 return;
     611              :         }
     612              : 
     613            4 :         if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
     614            0 :                 return;
     615              :         }
     616              : 
     617            4 :         label = zend_string_init("$this", sizeof("$this") - 1, 0);
     618            4 :         class_name = lsp_class_declared_name(document->text, class_start, body_start);
     619            4 :         detail = class_name ? strpprintf(0, "variable $this: %s", ZSTR_VAL(class_name)) : zend_string_init("variable $this", sizeof("variable $this") - 1, 0);
     620            4 :         lsp_add_variable_completion_item_ex(items, label, detail, "lsparrot", document->text, prefix_start, offset);
     621            4 :         zend_string_release(label);
     622            4 :         zend_string_release(detail);
     623              : 
     624            4 :         if (class_name) {
     625            4 :                 zend_string_release(class_name);
     626              :         }
     627              : }
     628              : 
     629          476 : static inline bool lsp_string_equals_ci(zend_string *left, zend_string *right)
     630              : {
     631          476 :         return ZSTR_LEN(left) == ZSTR_LEN(right) && strncasecmp(ZSTR_VAL(left), ZSTR_VAL(right), ZSTR_LEN(left)) == 0;
     632              : }
     633              : 
     634          552 : extern bool lsp_find_class_header_from(zend_string *text, size_t search_start, size_t *class_start, size_t *body_start, size_t *body_end, zend_long *body_depth)
     635              : {
     636          552 :         const char *keywords[4] = {"class", "interface", "trait", "enum"}, *value = ZSTR_VAL(text);
     637              :         size_t i, p, close_offset, keyword_index, keyword_length;
     638              : 
     639        28512 :         for (i = search_start; i < ZSTR_LEN(text); i++) {
     640       140360 :                 for (keyword_index = 0; keyword_index < sizeof(keywords) / sizeof(keywords[0]); keyword_index++) {
     641       112400 :                         keyword_length = strlen(keywords[keyword_index]);
     642       112400 :                         if (i + keyword_length >= ZSTR_LEN(text) || strncasecmp(value + i, keywords[keyword_index], keyword_length) != 0) {
     643       111848 :                                 continue;
     644              :                         }
     645              : 
     646          552 :                         if (i > 0 && (lsp_doc_is_identifier_char(value[i - 1]) || value[i - 1] == '$')) {
     647           40 :                                 continue;
     648              :                         }
     649              : 
     650          512 :                         if (!lsp_text_is_word_boundary(text, i + keyword_length)) {
     651            0 :                                 continue;
     652              :                         }
     653              : 
     654          512 :                         p = i + keyword_length;
     655         8888 :                         while (p < ZSTR_LEN(text) && value[p] != '{') {
     656         8376 :                                 p++;
     657              :                         }
     658              : 
     659          512 :                         if (p >= ZSTR_LEN(text) || !lsp_find_matching_brace(text, p, &close_offset)) {
     660            0 :                                 return false;
     661              :                         }
     662              : 
     663          512 :                         *class_start = i;
     664          512 :                         *body_start = p + 1;
     665          512 :                         *body_end = close_offset;
     666          512 :                         *body_depth = lsp_brace_depth_at(text, p + 1);
     667              : 
     668          512 :                         return true;
     669              :                 }
     670              :         }
     671              : 
     672           40 :         return false;
     673              : }
     674              : 
     675          480 : extern size_t lsp_method_signature_end(HashTable *tokens, uint32_t name_index, zend_string *text, zend_long body_depth)
     676              : {
     677              :         zval *token;
     678          480 :         uint32_t i, count = zend_hash_num_elements(tokens);
     679          480 :         size_t fallback = (size_t) lsp_token_long(zend_hash_index_find(tokens, name_index), "offset", 0);
     680              : 
     681         4768 :         for (i = name_index; i < count; i++) {
     682         4768 :                 token = zend_hash_index_find(tokens, i);
     683         4768 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     684            0 :                         continue;
     685              :                 }
     686              : 
     687         4768 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
     688            0 :                         continue;
     689              :                 }
     690              : 
     691         4768 :                 if (lsp_token_is_char(token, '{') || lsp_token_is_char(token, ';')) {
     692          480 :                         return (size_t) lsp_token_long(token, "offset", (zend_long) fallback);
     693              :                 }
     694              : 
     695         4288 :                 fallback = (size_t) lsp_token_long(token, "offset", (zend_long) fallback) + (size_t) lsp_token_long(token, "length", 0);
     696              :         }
     697              : 
     698            0 :         return fallback;
     699              : }
     700              : 
     701          476 : extern zend_string *lsp_function_signature_detail(zend_string *text, zval *name_token, HashTable *tokens, uint32_t name_index, zend_long body_depth, const char *prefix)
     702              : {
     703          476 :         const char *value = ZSTR_VAL(text);
     704              :         zend_string *signature, *detail, *phpdoc_return, *phpdoc_signature;
     705              :         size_t start, end;
     706              : 
     707          476 :         if (!name_token || Z_TYPE_P(name_token) != IS_ARRAY) {
     708            0 :                 return NULL;
     709              :         }
     710              : 
     711          476 :         start = (size_t) lsp_token_long(name_token, "offset", 0);
     712          476 :         end = lsp_method_signature_end(tokens, name_index, text, body_depth);
     713              : 
     714          476 :         if (start >= ZSTR_LEN(text) || end <= start) {
     715            0 :                 return NULL;
     716              :         }
     717              : 
     718          476 :         if (end > ZSTR_LEN(text)) {
     719            0 :                 end = ZSTR_LEN(text);
     720              :         }
     721              : 
     722         2440 :         while (end > start && isspace((unsigned char) value[end - 1])) {
     723         1964 :                 end--;
     724              :         }
     725              : 
     726          476 :         signature = lsp_normalized_signature_slice(value + start, end - start);
     727          476 :         if (!signature || ZSTR_LEN(signature) == 0) {
     728            0 :                 if (signature) {
     729            0 :                         zend_string_release(signature);
     730              :                 }
     731              : 
     732            0 :                 return NULL;
     733              :         }
     734              : 
     735          476 :         phpdoc_return = lsp_signature_phpdoc_return_type(tokens, name_index);
     736          476 :         if (lsp_signature_phpdoc_type_has_detail(phpdoc_return)) {
     737           12 :                 phpdoc_signature = lsp_signature_with_phpdoc_return(signature, phpdoc_return);
     738           12 :                 if (phpdoc_signature && ZSTR_LEN(phpdoc_signature) > 0) {
     739           12 :                         zend_string_release(signature);
     740           12 :                         signature = phpdoc_signature;
     741            0 :                 } else if (phpdoc_signature) {
     742            0 :                         zend_string_release(phpdoc_signature);
     743              :                 }
     744              :         }
     745              : 
     746          476 :         if (phpdoc_return) {
     747           20 :                 zend_string_release(phpdoc_return);
     748              :         }
     749              : 
     750          476 :         if (!prefix) {
     751          316 :                 return signature;
     752              :         }
     753              : 
     754          160 :         detail = strpprintf(0, "%s %s", prefix, ZSTR_VAL(signature));
     755          160 :         zend_string_release(signature);
     756              : 
     757          160 :         return detail;
     758              : }
     759              : 
     760          584 : extern bool lsp_find_enclosing_class_header(zend_string *text, size_t offset, size_t *class_start, size_t *body_start, size_t *body_end, zend_long *body_depth)
     761              : {
     762          584 :         const char *keywords[4] = {"class", "interface", "trait", "enum"}, *value = ZSTR_VAL(text);
     763              :         size_t i, length, p, close_offset, keyword_index, keyword_length;
     764          584 :         bool found = false;
     765              : 
     766          584 :         length = offset > ZSTR_LEN(text) ? ZSTR_LEN(text) : offset;
     767              : 
     768       115344 :         for (i = 0; i < length; i++) {
     769       573800 :                 for (keyword_index = 0; keyword_index < sizeof(keywords) / sizeof(keywords[0]); keyword_index++) {
     770       459040 :                         keyword_length = strlen(keywords[keyword_index]);
     771       459040 :                         if (i + keyword_length >= length || memcmp(value + i, keywords[keyword_index], keyword_length) != 0) {
     772       458492 :                                 continue;
     773              :                         }
     774              : 
     775          548 :                         if (i > 0 && (lsp_doc_is_identifier_char(value[i - 1]) || value[i - 1] == '$')) {
     776            0 :                                 continue;
     777              :                         }
     778              : 
     779          548 :                         if (!lsp_text_is_word_boundary(text, i + keyword_length)) {
     780            0 :                                 continue;
     781              :                         }
     782              : 
     783          548 :                         p = i + keyword_length;
     784         8088 :                         while (p < ZSTR_LEN(text) && value[p] != '{') {
     785         7540 :                                 p++;
     786              :                         }
     787              : 
     788          548 :                         if (p >= ZSTR_LEN(text) || p >= offset) {
     789            4 :                                 continue;
     790              :                         }
     791              : 
     792          544 :                         if (!lsp_find_matching_brace(text, p, &close_offset) || offset > close_offset) {
     793           76 :                                 continue;
     794              :                         }
     795              : 
     796          468 :                         *class_start = i;
     797          468 :                         *body_start = p + 1;
     798          468 :                         *body_end = close_offset;
     799          468 :                         *body_depth = lsp_brace_depth_at(text, p + 1);
     800          468 :                         found = true;
     801              :                 }
     802              :         }
     803              : 
     804          584 :         return found;
     805              : }
     806              : 
     807            4 : extern bool lsp_find_first_class_header(zend_string *text, size_t *class_start, size_t *body_start, size_t *body_end, zend_long *body_depth)
     808              : {
     809            4 :         return lsp_find_class_header_from(text, 0, class_start, body_start, body_end, body_depth);
     810              : }
     811              : 
     812          140 : extern bool lsp_word_at_slice_equals(const char *start, const char *end, const char *word)
     813              : {
     814          140 :         size_t length = strlen(word);
     815              : 
     816          140 :         return (size_t) (end - start) == length && strncasecmp(start, word, length) == 0;
     817              : }
     818              : 
     819          368 : extern zend_string *lsp_class_extends_name(zend_string *text, size_t class_start, size_t body_start)
     820              : {
     821          368 :         const char *value = ZSTR_VAL(text), *name_start, *name_end;
     822              :         zend_string *raw, *resolved;
     823              :         size_t p, header_end, keyword_end;
     824              : 
     825          368 :         header_end = body_start > 0 ? body_start - 1 : body_start;
     826              : 
     827         5816 :         for (p = class_start; p < header_end; p++) {
     828         5604 :                 if (!lsp_keyword_at_slice(value, p, header_end, "extends", &keyword_end)) {
     829         5448 :                         continue;
     830              :                 }
     831              : 
     832          156 :                 p = keyword_end;
     833          312 :                 while (p < header_end && isspace((unsigned char) value[p])) {
     834          156 :                         p++;
     835              :                 }
     836              : 
     837          156 :                 name_start = value + p;
     838         1512 :                 while (p < header_end && (lsp_doc_is_identifier_char(value[p]) || value[p] == '\\')) {
     839         1356 :                         p++;
     840              :                 }
     841              : 
     842          156 :                 name_end = value + p;
     843          156 :                 if (name_end <= name_start) {
     844            0 :                         return NULL;
     845              :                 }
     846              : 
     847          156 :                 raw = zend_string_init(name_start, name_end - name_start, 0);
     848          156 :                 resolved = lsp_resolve_class_name_at(text, raw, (size_t) (name_start - value));
     849          156 :                 zend_string_release(raw);
     850              : 
     851          156 :                 return resolved;
     852              :         }
     853              : 
     854          212 :         return NULL;
     855              : }
     856              : 
     857          552 : extern zend_string *lsp_class_declared_name(zend_string *text, size_t class_start, size_t body_start)
     858              : {
     859          552 :         const char *keywords[4] = {"class", "interface", "trait", "enum"}, *value = ZSTR_VAL(text), *name_start, *name_end;
     860              :         zend_string *raw, *resolved;
     861              :         size_t p, header_end, keyword_end, i;
     862              : 
     863          552 :         header_end = body_start > 0 ? body_start - 1 : body_start;
     864          552 :         for (p = class_start; p < header_end; p++) {
     865          600 :                 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
     866          600 :                         if (!lsp_keyword_at_slice(value, p, header_end, keywords[i], &keyword_end)) {
     867           48 :                                 continue;
     868              :                         }
     869              : 
     870          552 :                         p = keyword_end;
     871         1104 :                         while (p < header_end && isspace((unsigned char) value[p])) {
     872          552 :                                 p++;
     873              :                         }
     874              : 
     875          552 :                         name_start = value + p;
     876         4792 :                         while (p < header_end && (lsp_doc_is_identifier_char(value[p]) || value[p] == '\\')) {
     877         4240 :                                 p++;
     878              :                         }
     879              : 
     880          552 :                         name_end = value + p;
     881          552 :                         if (name_end <= name_start) {
     882            0 :                                 return NULL;
     883              :                         }
     884              : 
     885          552 :                         raw = zend_string_init(name_start, name_end - name_start, 0);
     886          552 :                         resolved = lsp_resolve_class_name_at(text, raw, (size_t) (name_start - value));
     887          552 :                         zend_string_release(raw);
     888              : 
     889          552 :                         return resolved;
     890              :                 }
     891              :         }
     892              : 
     893            0 :         return NULL;
     894              : }
     895              : 
     896            4 : extern zend_string *lsp_parameter_declared_type_before_variable(zend_string *text, size_t variable_offset, size_t param_start)
     897              : {
     898            4 :         return lsp_parameter_type_before_variable(text, variable_offset, param_start);
     899              : }
     900              : 
     901           72 : extern zend_string *lsp_property_completion_detail(zend_string *text, zval *variable_token, bool is_static)
     902              : {
     903              :         zend_string *variable, *type, *detail;
     904              :         size_t variable_offset;
     905              : 
     906           72 :         variable = lsp_token_string(variable_token, "text");
     907           72 :         if (!variable) {
     908            0 :                 return NULL;
     909              :         }
     910              : 
     911           72 :         variable_offset = (size_t) lsp_token_long(variable_token, "offset", 0);
     912           72 :         type = lsp_property_type_before_variable(text, variable_offset);
     913           72 :         if (type && ZSTR_LEN(type) > 0) {
     914           72 :                 detail = is_static
     915           20 :                         ? strpprintf(0, "static property %s %s", ZSTR_VAL(type), ZSTR_VAL(variable))
     916           72 :                         : strpprintf(0, "property %s %s", ZSTR_VAL(type), ZSTR_VAL(variable))
     917              :                 ;
     918           72 :                 zend_string_release(type);
     919              : 
     920           72 :                 return detail;
     921              :         }
     922              : 
     923            0 :         if (type) {
     924            0 :                 zend_string_release(type);
     925              :         }
     926              : 
     927              :         return is_static
     928            0 :                 ? strpprintf(0, "static property %s", ZSTR_VAL(variable))
     929            0 :                 : strpprintf(0, "property %s", ZSTR_VAL(variable))
     930              :         ;
     931              : }
     932              : 
     933            8 : extern zend_string *lsp_promoted_property_completion_detail(zend_string *text, zval *variable_token, size_t param_start)
     934              : {
     935              :         zend_string *variable, *type, *detail;
     936              :         size_t variable_offset;
     937              : 
     938            8 :         variable = lsp_token_string(variable_token, "text");
     939            8 :         if (!variable) {
     940            0 :                 return NULL;
     941              :         }
     942              : 
     943            8 :         variable_offset = (size_t) lsp_token_long(variable_token, "offset", 0);
     944            8 :         type = lsp_parameter_type_before_variable(text, variable_offset, param_start);
     945            8 :         if (type && ZSTR_LEN(type) > 0) {
     946            8 :                 detail = strpprintf(0, "property %s %s", ZSTR_VAL(type), ZSTR_VAL(variable));
     947            8 :                 zend_string_release(type);
     948              : 
     949            8 :                 return detail;
     950              :         }
     951              : 
     952            0 :         if (type) {
     953            0 :                 zend_string_release(type);
     954              :         }
     955              : 
     956            0 :         return strpprintf(0, "property %s", ZSTR_VAL(variable));
     957              : }
     958              : 
     959           16 : extern void lsp_add_scope_variable_completions(lsp_server *server, zval *items, lsp_document *document, HashTable *tokens, size_t offset, zend_string *prefix, size_t prefix_start)
     960              : {
     961              :         zend_long body_depth;
     962              :         zend_string *label, *detail, *declared_type;
     963              :         zval *token;
     964              :         uint32_t i, count;
     965              :         size_t param_start, param_end, body_start, body_end, token_offset;
     966              :         bool has_scope;
     967              : 
     968           16 :         has_scope = lsp_find_function_scope_at(tokens, document->text, offset, &param_start, &param_end, &body_start, &body_end, &body_depth);
     969           16 :         if (!has_scope) {
     970            4 :                 param_start = 0;
     971            4 :                 param_end = 0;
     972            4 :                 body_start = 0;
     973            4 :                 body_end = ZSTR_LEN(document->text);
     974            4 :                 body_depth = 0;
     975              :         }
     976              : 
     977           16 :         lsp_add_scope_this_completion(items, document, offset, prefix, prefix_start);
     978              : 
     979           16 :         count = zend_hash_num_elements(tokens);
     980          912 :         for (i = 0; i < count; i++) {
     981          896 :                 token = zend_hash_index_find(tokens, i);
     982          896 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_VARIABLE")) {
     983          824 :                         continue;
     984              :                 }
     985              : 
     986           72 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
     987           72 :                 if (token_offset >= prefix_start) {
     988           16 :                         continue;
     989              :                 }
     990              : 
     991           56 :                 if (has_scope && ((token_offset < param_start || token_offset > param_end) && (token_offset < body_start || token_offset > body_end))) {
     992            4 :                         continue;
     993              :                 }
     994              : 
     995           52 :                 if (!has_scope && token_offset > body_end) {
     996            0 :                         continue;
     997              :                 }
     998              : 
     999           52 :                 label = lsp_token_string(token, "text");
    1000           52 :                 if (!label || !lsp_matches_prefix_string(label, prefix)) {
    1001           16 :                         continue;
    1002              :                 }
    1003              : 
    1004           36 :                 declared_type = NULL;
    1005           36 :                 if (has_scope && token_offset > param_start && token_offset < param_end) {
    1006           16 :                         declared_type = lsp_parameter_type_before_variable(document->text, token_offset, param_start);
    1007              :                 }
    1008              : 
    1009           36 :                 detail = lsp_scope_variable_detail(server, document, label, prefix_start, declared_type, declared_type != NULL);
    1010           36 :                 lsp_add_variable_completion_item_ex(items, label, detail, "lsparrot", document->text, prefix_start, offset);
    1011           36 :                 zend_string_release(detail);
    1012              : 
    1013           36 :                 if (declared_type) {
    1014           16 :                         zend_string_release(declared_type);
    1015              :                 }
    1016              :         }
    1017           16 : }
    1018              : 
    1019          444 : extern bool lsp_find_class_header_for_name(zend_string *text, zend_string *class_name, size_t *class_start, size_t *body_start, size_t *body_end, zend_long *body_depth)
    1020              : {
    1021              :         zend_long found_body_depth;
    1022              :         zend_string *declared;
    1023              :         size_t search_start, found_class_start, found_body_start, found_body_end;
    1024              : 
    1025          444 :         search_start = 0;
    1026          476 :         while (lsp_find_class_header_from(text, search_start, &found_class_start, &found_body_start, &found_body_end, &found_body_depth)) {
    1027          476 :                 declared = lsp_class_declared_name(text, found_class_start, found_body_start);
    1028          476 :                 if (declared && lsp_string_equals_ci(declared, class_name)) {
    1029          444 :                         zend_string_release(declared);
    1030          444 :                         *class_start = found_class_start;
    1031          444 :                         *body_start = found_body_start;
    1032          444 :                         *body_end = found_body_end;
    1033          444 :                         *body_depth = found_body_depth;
    1034              : 
    1035          444 :                         return true;
    1036              :                 }
    1037              : 
    1038           32 :                 if (declared) {
    1039           32 :                         zend_string_release(declared);
    1040              :                 }
    1041              : 
    1042           32 :                 search_start = found_body_end + 1;
    1043              :         }
    1044              : 
    1045            0 :         return false;
    1046              : }
    1047              : 
    1048          588 : extern lsp_document *lsp_document_for_path(lsp_server *server, zend_string *path)
    1049              : {
    1050          588 :         lsp_document *document = NULL, *candidate;
    1051              :         zval *value;
    1052              : 
    1053         1072 :         ZEND_HASH_FOREACH_VAL(&server->documents, value) {
    1054          564 :                 if (Z_TYPE_P(value) != IS_PTR) {
    1055            0 :                         continue;
    1056              :                 }
    1057              : 
    1058          564 :                 candidate = (lsp_document *) Z_PTR_P(value);
    1059          564 :                 if (candidate && candidate->path && zend_string_equals(candidate->path, path)) {
    1060           80 :                         document = candidate;
    1061           80 :                         break;
    1062              :                 }
    1063              :         } ZEND_HASH_FOREACH_END();
    1064              : 
    1065          588 :         return document;
    1066              : }
        

Generated by: LCOV version 2.0-1