LCOV - code coverage report
Current view: top level - ext - lsp_server_signature.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 73.8 % 294 217
Test Date: 2026-07-13 00:58:38 Functions: 92.3 % 13 12

            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           16 : static inline bool lsp_signature_call_context(zend_string *text, size_t offset, size_t *paren, zend_string **name, size_t *active_parameter, zend_string **named_argument)
      17              : {
      18           16 :         const char *value = ZSTR_VAL(text);
      19           16 :         size_t i, name_start, name_end, depth = 0, argument_start, cursor;
      20              :         char c;
      21              : 
      22           16 :         *paren = (size_t) -1;
      23           16 :         *name = NULL;
      24           16 :         *active_parameter = 0;
      25           16 :         *named_argument = NULL;
      26              : 
      27           16 :         if (offset == 0) {
      28            0 :                 return false;
      29              :         }
      30              : 
      31           80 :         for (i = offset; i > 0; i--) {
      32           80 :                 c = value[i - 1];
      33           80 :                 if (c == ')') {
      34            0 :                         depth++;
      35           80 :                 } else if (c == '(') {
      36           16 :                         if (depth == 0) {
      37           16 :                                 *paren = i - 1;
      38           16 :                                 break;
      39              :                         }
      40            0 :                         depth--;
      41              :                 }
      42              :         }
      43              : 
      44           16 :         if (*paren == (size_t) -1 || *paren == 0) {
      45            0 :                 return false;
      46              :         }
      47              : 
      48           16 :         argument_start = *paren + 1;
      49           80 :         for (i = *paren + 1, depth = 0; i < offset; i++) {
      50           64 :                 if (value[i] == '(' || value[i] == '[' || value[i] == '{') {
      51            0 :                         depth++;
      52           64 :                 } else if ((value[i] == ')' || value[i] == ']' || value[i] == '}') && depth > 0) {
      53            0 :                         depth--;
      54           64 :                 } else if (value[i] == ',' && depth == 0) {
      55            4 :                         (*active_parameter)++;
      56            4 :                         argument_start = i + 1;
      57              :                 }
      58              :         }
      59              : 
      60              :         /* PHP 8 named argument in the slot being typed: `name:` binds a specific
      61              :          * parameter, so the positional comma count is not the active one. */
      62           16 :         cursor = argument_start;
      63           20 :         while (cursor < offset && isspace((unsigned char) value[cursor])) {
      64            4 :                 cursor++;
      65              :         }
      66           16 :         i = cursor;
      67           36 :         while (i < offset && (isalnum((unsigned char) value[i]) || value[i] == '_')) {
      68           20 :                 i++;
      69              :         }
      70           16 :         if (i > cursor && i < offset && value[i] == ':' && (i + 1 >= offset || value[i + 1] != ':')) {
      71            4 :                 *named_argument = zend_string_init(value + cursor, i - cursor, 0);
      72              :         }
      73              : 
      74           16 :         name_end = *paren;
      75           16 :         while (name_end > 0 && isspace((unsigned char) value[name_end - 1])) {
      76            0 :                 name_end--;
      77              :         }
      78              : 
      79           16 :         name_start = name_end;
      80          164 :         while (name_start > 0 && (isalnum((unsigned char) value[name_start - 1]) || value[name_start - 1] == '_' || value[name_start - 1] == '\\')) {
      81          148 :                 name_start--;
      82              :         }
      83              : 
      84           16 :         if (name_start == name_end) {
      85            0 :                 return false;
      86              :         }
      87              : 
      88           16 :         *name = zend_string_init(value + name_start, name_end - name_start, 0);
      89              : 
      90           16 :         return true;
      91              : }
      92              : 
      93           12 : static inline zend_string *lsp_cached_class_method_signature(zval *entry, zend_string *method_name)
      94              : {
      95              :         zend_string *label, *detail;
      96              :         zval *methods, *method;
      97              : 
      98           12 :         methods = zend_hash_str_find(Z_ARRVAL_P(entry), "methods", sizeof("methods") - 1);
      99           12 :         if (!methods || Z_TYPE_P(methods) != IS_ARRAY) {
     100            0 :                 return NULL;
     101              :         }
     102              : 
     103           12 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(methods), method) {
     104           12 :                 if (Z_TYPE_P(method) != IS_ARRAY) {
     105            0 :                         continue;
     106              :                 }
     107              : 
     108           12 :                 label = lsp_array_string(method, "label");
     109           12 :                 detail = lsp_array_string(method, "detail");
     110           12 :                 if (label && detail && zend_string_equals(label, method_name)) {
     111           12 :                         return zend_string_copy(detail);
     112              :                 }
     113              :         } ZEND_HASH_FOREACH_END();
     114              : 
     115            0 :         return NULL;
     116              : }
     117              : 
     118           12 : static inline zend_string *lsp_project_class_method_signature(lsp_server *server, zend_string *class_name, zend_string *method_name)
     119              : {
     120           12 :         zend_string *current, *next, *detail = NULL;
     121              :         zval *entry, *parent;
     122              :         HashTable visited;
     123              : 
     124           12 :         zend_hash_init(&visited, 8, NULL, NULL, 0);
     125           12 :         current = zend_string_copy(class_name);
     126           12 :         while (current) {
     127           12 :                 if (zend_hash_exists(&visited, current)) {
     128            0 :                         break;
     129              :                 }
     130              : 
     131           12 :                 zend_hash_add_empty_element(&visited, current);
     132           12 :                 entry = lsp_class_member_cache_entry(server, current);
     133           12 :                 if (!entry || Z_TYPE_P(entry) != IS_ARRAY) {
     134              :                         break;
     135              :                 }
     136              : 
     137           12 :                 detail = lsp_cached_class_method_signature(entry, method_name);
     138           12 :                 if (detail) {
     139           12 :                         break;
     140              :                 }
     141              : 
     142            0 :                 parent = zend_hash_str_find(Z_ARRVAL_P(entry), "parent", sizeof("parent") - 1);
     143            0 :                 next = parent && Z_TYPE_P(parent) == IS_STRING && Z_STRLEN_P(parent) > 0 ? zend_string_copy(Z_STR_P(parent)) : NULL;
     144            0 :                 zend_string_release(current);
     145            0 :                 current = next;
     146              :         }
     147              : 
     148           12 :         if (current) {
     149           12 :                 zend_string_release(current);
     150              :         }
     151              : 
     152           12 :         zend_hash_destroy(&visited);
     153              : 
     154           12 :         return detail;
     155              : }
     156              : 
     157           12 : static inline zend_string *lsp_document_class_method_signature(lsp_document *document, size_t offset, zend_string *method_name)
     158              : {
     159           12 :         zend_long body_depth = 0;
     160              :         zend_string *label, *detail;
     161              :         zval *tokens_zv, *token, *name_token;
     162              :         HashTable *tokens;
     163              :         uint32_t i, count, name_index;
     164           12 :         size_t class_start = 0, body_start = 0, body_end = 0;
     165              : 
     166           12 :         if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
     167            0 :                 return NULL;
     168              :         }
     169              : 
     170           12 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
     171           12 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
     172            0 :                 return NULL;
     173              :         }
     174              : 
     175           12 :         tokens = Z_ARRVAL_P(tokens_zv);
     176           12 :         count = zend_hash_num_elements(tokens);
     177          488 :         for (i = 0; i < count; i++) {
     178          480 :                 token = zend_hash_index_find(tokens, i);
     179          480 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
     180          240 :                         continue;
     181              :                 }
     182              : 
     183          240 :                 if (!lsp_token_name_equals(token, "T_FUNCTION") || !lsp_token_at_depth(document->text, token, body_depth)) {
     184          228 :                         continue;
     185              :                 }
     186              : 
     187           12 :                 name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
     188           12 :                 label = lsp_token_string(name_token, "text");
     189           12 :                 if (!label || !zend_string_equals(label, method_name)) {
     190            8 :                         continue;
     191              :                 }
     192              : 
     193            4 :                 detail = lsp_function_signature_detail(document->text, name_token, tokens, name_index, body_depth, NULL);
     194            4 :                 if (detail) {
     195            4 :                         return detail;
     196              :                 }
     197              : 
     198            0 :                 return strpprintf(0, "%s(...)", ZSTR_VAL(method_name));
     199              :         }
     200              : 
     201            8 :         return NULL;
     202              : }
     203              : 
     204           12 : static inline zend_string *lsp_document_parent_class_at(lsp_document *document, size_t offset)
     205              : {
     206           12 :         zend_long body_depth = 0;
     207           12 :         size_t class_start = 0, body_start = 0, body_end = 0;
     208              : 
     209           12 :         if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
     210            0 :                 return NULL;
     211              :         }
     212              : 
     213           12 :         return lsp_class_extends_name(document->text, class_start, body_start);
     214              : }
     215              : 
     216           12 : static inline zend_string *lsp_member_call_signature(lsp_server *server, lsp_document *document, size_t name_end, zend_string *method_name)
     217              : {
     218              :         zend_string *receiver, *member_prefix, *class_name, *parent_class, *detail;
     219              : 
     220           12 :         detail = NULL;
     221           12 :         if (lsp_member_access_context(document->text, name_end, method_name, &receiver, &member_prefix)) {
     222           12 :                 if (zend_string_equals_literal(receiver, "$this")) {
     223           12 :                         detail = lsp_document_class_method_signature(document, name_end, method_name);
     224           12 :                         if (!detail) {
     225            8 :                                 parent_class = lsp_document_parent_class_at(document, name_end);
     226            8 :                                 if (parent_class) {
     227            8 :                                         detail = lsp_project_class_method_signature(server, parent_class, method_name);
     228            8 :                                         zend_string_release(parent_class);
     229              :                                 }
     230              :                         }
     231              : 
     232           12 :                         zend_string_release(receiver);
     233           12 :                         zend_string_release(member_prefix);
     234              : 
     235           12 :                         return detail;
     236              :                 }
     237              : 
     238            0 :                 zend_string_release(receiver);
     239            0 :                 zend_string_release(member_prefix);
     240              :         }
     241              : 
     242            0 :         if (lsp_member_access_class_context(server, document, name_end, method_name, &class_name, &member_prefix)) {
     243            0 :                 detail = lsp_project_class_method_signature(server, class_name, method_name);
     244              : 
     245            0 :                 zend_string_release(class_name);
     246            0 :                 zend_string_release(member_prefix);
     247              :         }
     248              : 
     249            0 :         return detail;
     250              : }
     251              : 
     252           16 : static inline bool lsp_signature_function_declaration_context(zend_string *text, size_t paren, zend_string *method_name)
     253              : {
     254           16 :         const char *value = ZSTR_VAL(text);
     255           16 :         size_t name_end, name_start, keyword_start, p, keyword_length = sizeof("function") - 1;
     256              : 
     257           16 :         name_end = paren;
     258           16 :         while (name_end > 0 && isspace((unsigned char) value[name_end - 1])) {
     259            0 :                 name_end--;
     260              :         }
     261              : 
     262           16 :         if (name_end < ZSTR_LEN(method_name)) {
     263            0 :                 return false;
     264              :         }
     265              : 
     266           16 :         name_start = name_end - ZSTR_LEN(method_name);
     267           16 :         if (strncasecmp(value + name_start, ZSTR_VAL(method_name), ZSTR_LEN(method_name)) != 0) {
     268            0 :                 return false;
     269              :         }
     270              : 
     271           16 :         if (name_start > 0 && lsp_doc_is_identifier_char(value[name_start - 1])) {
     272            0 :                 return false;
     273              :         }
     274              : 
     275           16 :         p = name_start;
     276           20 :         while (p > 0 && isspace((unsigned char) value[p - 1])) {
     277            4 :                 p--;
     278              :         }
     279              : 
     280           16 :         if (p > 0 && value[p - 1] == '&') {
     281            0 :                 p--;
     282            0 :                 while (p > 0 && isspace((unsigned char) value[p - 1])) {
     283            0 :                         p--;
     284              :                 }
     285              :         }
     286              : 
     287           16 :         if (p < keyword_length) {
     288            0 :                 return false;
     289              :         }
     290              : 
     291           16 :         keyword_start = p - keyword_length;
     292           16 :         if (strncasecmp(value + keyword_start, "function", keyword_length) != 0) {
     293           12 :                 return false;
     294              :         }
     295              : 
     296            4 :         if (keyword_start > 0 && lsp_doc_is_identifier_char(value[keyword_start - 1])) {
     297            0 :                 return false;
     298              :         }
     299              : 
     300            4 :         return true;
     301              : }
     302              : 
     303           16 : static inline zend_string *lsp_override_declaration_signature(lsp_server *server, lsp_document *document, size_t paren, zend_string *method_name)
     304              : {
     305              :         zend_string *parent_class, *detail;
     306              : 
     307           16 :         if (!lsp_signature_function_declaration_context(document->text, paren, method_name)) {
     308           12 :                 return NULL;
     309              :         }
     310              : 
     311            4 :         parent_class = lsp_document_parent_class_at(document, paren);
     312            4 :         if (!parent_class) {
     313            0 :                 return NULL;
     314              :         }
     315              : 
     316            4 :         detail = lsp_project_class_method_signature(server, parent_class, method_name);
     317            4 :         zend_string_release(parent_class);
     318              : 
     319            4 :         return detail;
     320              : }
     321              : 
     322            0 : static inline zend_string *lsp_document_function_signature(lsp_document *document, zend_string *function_name)
     323              : {
     324              :         zend_long body_depth;
     325              :         zend_string *label, *detail;
     326              :         zval *tokens_zv, *token, *name_token;
     327              :         HashTable *tokens;
     328              :         uint32_t i, count, name_index;
     329              : 
     330            0 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
     331            0 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
     332            0 :                 return NULL;
     333              :         }
     334              : 
     335            0 :         tokens = Z_ARRVAL_P(tokens_zv);
     336            0 :         count = zend_hash_num_elements(tokens);
     337            0 :         for (i = 0; i < count; i++) {
     338            0 :                 token = zend_hash_index_find(tokens, i);
     339            0 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
     340            0 :                         continue;
     341              :                 }
     342              : 
     343            0 :                 name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
     344            0 :                 label = lsp_token_string(name_token, "text");
     345            0 :                 if (!label || !zend_string_equals(label, function_name)) {
     346            0 :                         continue;
     347              :                 }
     348              : 
     349            0 :                 body_depth = lsp_brace_depth_at(document->text, (size_t) lsp_token_long(token, "offset", 0));
     350            0 :                 detail = lsp_function_signature_detail(document->text, name_token, tokens, name_index, body_depth, "function");
     351            0 :                 if (detail) {
     352            0 :                         return detail;
     353              :                 }
     354              : 
     355            0 :                 return strpprintf(0, "%s(...)", ZSTR_VAL(function_name));
     356              :         }
     357              : 
     358            0 :         return NULL;
     359              : }
     360              : 
     361           16 : static inline void lsp_signature_add_parameters(zval *signature, zend_string *label)
     362              : {
     363           16 :         const char *value = ZSTR_VAL(label);
     364              :         zval parameters, parameter;
     365           16 :         size_t length = ZSTR_LEN(label), open = (size_t) -1, close = (size_t) -1, i, start, end, paren_depth = 0, bracket_depth = 0, brace_depth = 0, angle_depth = 0;
     366           16 :         bool has_parameter = false;
     367              :         char c;
     368              : 
     369          164 :         for (i = 0; i < length; i++) {
     370          164 :                 if (value[i] == '(') {
     371           16 :                         open = i;
     372           16 :                         break;
     373              :                 }
     374              :         }
     375              : 
     376           16 :         if (open == (size_t) -1) {
     377            0 :                 return;
     378              :         }
     379              : 
     380          500 :         for (i = open + 1; i < length; i++) {
     381          500 :                 c = value[i];
     382          500 :                 if (c == '(') {
     383            0 :                         paren_depth++;
     384          500 :                 } else if (c == ')' && paren_depth == 0) {
     385           16 :                         close = i;
     386           16 :                         break;
     387          484 :                 } else if (c == ')' && paren_depth > 0) {
     388            0 :                         paren_depth--;
     389              :                 }
     390              :         }
     391              : 
     392           16 :         if (close == (size_t) -1 || close <= open + 1) {
     393            0 :                 return;
     394              :         }
     395              : 
     396           16 :         array_init(&parameters);
     397           16 :         start = open + 1;
     398          516 :         for (i = start; i <= close; i++) {
     399          500 :                 c = i < close ? value[i] : ',';
     400          500 :                 if ((c == ',' && paren_depth == 0 && bracket_depth == 0 && brace_depth == 0 && angle_depth == 0) || i == close) {
     401           36 :                         end = i;
     402              : 
     403           56 :                         while (start < end && isspace((unsigned char) value[start])) {
     404           20 :                                 start++;
     405              :                         }
     406              : 
     407           36 :                         while (end > start && isspace((unsigned char) value[end - 1])) {
     408            0 :                                 end--;
     409              :                         }
     410              : 
     411           36 :                         if (end > start) {
     412           36 :                                 array_init(&parameter);
     413           36 :                                 add_assoc_stringl(&parameter, "label", value + start, end - start);
     414           36 :                                 add_next_index_zval(&parameters, &parameter);
     415           36 :                                 has_parameter = true;
     416              :                         }
     417           36 :                         start = i + 1;
     418              : 
     419           36 :                         continue;
     420              :                 }
     421              : 
     422          464 :                 if (c == '(') {
     423            0 :                         paren_depth++;
     424          464 :                 } else if (c == ')' && paren_depth > 0) {
     425            0 :                         paren_depth--;
     426          464 :                 } else if (c == '[') {
     427            0 :                         bracket_depth++;
     428          464 :                 } else if (c == ']' && bracket_depth > 0) {
     429            0 :                         bracket_depth--;
     430          464 :                 } else if (c == '{') {
     431            0 :                         brace_depth++;
     432          464 :                 } else if (c == '}' && brace_depth > 0) {
     433            0 :                         brace_depth--;
     434          464 :                 } else if (c == '<') {
     435            0 :                         angle_depth++;
     436          464 :                 } else if (c == '>' && angle_depth > 0) {
     437            0 :                         angle_depth--;
     438              :                 }
     439              :         }
     440              : 
     441           16 :         if (has_parameter) {
     442           16 :                 add_assoc_zval(signature, "parameters", &parameters);
     443              :         } else {
     444            0 :                 zval_ptr_dtor(&parameters);
     445              :         }
     446              : }
     447              : 
     448           16 : static inline void lsp_signature_result(zval *return_value, zend_string *label, size_t active_parameter)
     449              : {
     450              :         zval signatures, signature;
     451              : 
     452           16 :         array_init(return_value);
     453           16 :         array_init(&signatures);
     454           16 :         array_init(&signature);
     455           16 :         add_assoc_str(&signature, "label", label);
     456           16 :         add_assoc_string(&signature, "documentation", "lsparrot signature context");
     457           16 :         lsp_signature_add_parameters(&signature, label);
     458           16 :         add_next_index_zval(&signatures, &signature);
     459           16 :         add_assoc_zval(return_value, "signatures", &signatures);
     460           16 :         add_assoc_long(return_value, "activeSignature", 0);
     461           16 :         add_assoc_long(return_value, "activeParameter", (zend_long) active_parameter);
     462           16 : }
     463              : 
     464              : /* Index of parameter `$name` inside a rendered signature label such as
     465              :  * "function foo(int $a, string $b = '')", or -1 when absent. */
     466            4 : static inline zend_long lsp_signature_parameter_index(zend_string *label, zend_string *parameter_name)
     467              : {
     468            4 :         const char *value = ZSTR_VAL(label), *end = value + ZSTR_LEN(label), *open, *p;
     469            4 :         zend_long index = 0;
     470            4 :         size_t name_length = ZSTR_LEN(parameter_name);
     471              : 
     472            4 :         open = memchr(value, '(', ZSTR_LEN(label));
     473            4 :         if (!open) {
     474            0 :                 return -1;
     475              :         }
     476              : 
     477          112 :         for (p = open + 1; p < end; p++) {
     478          112 :                 if (*p != '$') {
     479          100 :                         continue;
     480              :                 }
     481              : 
     482           12 :                 if ((size_t) (end - p - 1) >= name_length &&
     483           12 :                         memcmp(p + 1, ZSTR_VAL(parameter_name), name_length) == 0 &&
     484            4 :                         (p + 1 + name_length >= end || !(isalnum((unsigned char) p[1 + name_length]) || p[1 + name_length] == '_'))
     485              :                 ) {
     486            4 :                         return index;
     487              :                 }
     488              : 
     489            8 :                 index++;
     490              :         }
     491              : 
     492            0 :         return -1;
     493              : }
     494              : 
     495           16 : extern void lsp_lsparrot_signature_help(lsp_server *server, zval *return_value, lsp_document *document, zval *position)
     496              : {
     497              :         zend_long line, character, named_index;
     498              :         zend_string *name, *label, *named_argument;
     499           16 :         size_t offset, paren = (size_t) -1, active_parameter = 0;
     500              : 
     501           16 :         lsp_position_from_zval(position, &line, &character);
     502           16 :         offset = lsp_offset_at(document->text, line, character);
     503           16 :         if (!lsp_signature_call_context(document->text, offset, &paren, &name, &active_parameter, &named_argument)) {
     504            0 :                 ZVAL_NULL(return_value);
     505              : 
     506            0 :                 return;
     507              :         }
     508              : 
     509           16 :         label = lsp_override_declaration_signature(server, document, paren, name);
     510           16 :         if (!label) {
     511           12 :                 label = lsp_member_call_signature(server, document, paren, name);
     512              :         }
     513              : 
     514           16 :         if (!label) {
     515            0 :                 label = lsp_document_function_signature(document, name);
     516              :         }
     517              : 
     518           16 :         if (!label) {
     519            0 :                 label = strpprintf(0, "%s(...)", ZSTR_VAL(name));
     520              :         }
     521              : 
     522           16 :         if (named_argument) {
     523            4 :                 named_index = lsp_signature_parameter_index(label, named_argument);
     524            4 :                 if (named_index >= 0) {
     525            4 :                         active_parameter = (size_t) named_index;
     526              :                 }
     527            4 :                 zend_string_release(named_argument);
     528              :         }
     529              : 
     530           16 :         lsp_signature_result(return_value, label, active_parameter);
     531              : 
     532           16 :         zend_string_release(name);
     533              : }
        

Generated by: LCOV version 2.0-1