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 76 : static inline bool lsp_static_receiver_is_current_class(zend_string *receiver)
17 : {
18 132 : return (ZSTR_LEN(receiver) == sizeof("self") - 1 && strncasecmp(ZSTR_VAL(receiver), "self", ZSTR_LEN(receiver)) == 0) ||
19 56 : (ZSTR_LEN(receiver) == sizeof("static") - 1 && strncasecmp(ZSTR_VAL(receiver), "static", ZSTR_LEN(receiver)) == 0)
20 : ;
21 : }
22 :
23 48 : static inline bool lsp_static_receiver_is_parent_class(zend_string *receiver)
24 : {
25 48 : return ZSTR_LEN(receiver) == sizeof("parent") - 1 && strncasecmp(ZSTR_VAL(receiver), "parent", ZSTR_LEN(receiver)) == 0;
26 : }
27 :
28 312 : static inline bool lsp_static_member_access_context(lsp_document *document, size_t offset, zend_string *prefix, zend_string **class_name, zend_string **member_prefix, bool *current_class_access, bool *parent_class_access, size_t *current_class_start, size_t *current_body_start, size_t *current_body_end, zend_long *current_body_depth)
29 : {
30 312 : const char *value = ZSTR_VAL(document->text);
31 : zend_string *raw, *resolved, *parent_class;
32 : size_t i, class_start, class_end;
33 :
34 312 : *class_name = NULL;
35 312 : *member_prefix = NULL;
36 312 : *current_class_access = false;
37 312 : *parent_class_access = false;
38 312 : *current_class_start = 0;
39 312 : *current_body_start = 0;
40 312 : *current_body_end = 0;
41 312 : *current_body_depth = 0;
42 :
43 312 : if (offset > ZSTR_LEN(document->text)) {
44 0 : offset = ZSTR_LEN(document->text);
45 : }
46 :
47 312 : i = offset >= ZSTR_LEN(prefix) ? offset - ZSTR_LEN(prefix) : 0;
48 :
49 676 : while (i > 0 && isspace((unsigned char) value[i - 1])) {
50 364 : i--;
51 : }
52 :
53 312 : if (i < 2 || value[i - 2] != ':' || value[i - 1] != ':') {
54 236 : return false;
55 : }
56 :
57 76 : i -= 2;
58 76 : while (i > 0 && isspace((unsigned char) value[i - 1])) {
59 0 : i--;
60 : }
61 :
62 76 : class_end = i;
63 560 : while (i > 0 && (lsp_doc_is_identifier_char(value[i - 1]) || value[i - 1] == '\\')) {
64 484 : i--;
65 : }
66 :
67 76 : class_start = i;
68 76 : if (class_start == class_end) {
69 0 : return false;
70 : }
71 :
72 76 : raw = zend_string_init(value + class_start, class_end - class_start, 0);
73 76 : if (lsp_static_receiver_is_current_class(raw)) {
74 28 : if (!lsp_find_enclosing_class_header(document->text, offset, current_class_start, current_body_start, current_body_end, current_body_depth)) {
75 0 : zend_string_release(raw);
76 :
77 0 : return false;
78 : }
79 :
80 28 : resolved = lsp_class_declared_name(document->text, *current_class_start, *current_body_start);
81 28 : zend_string_release(raw);
82 :
83 28 : if (!resolved) {
84 0 : return false;
85 : }
86 :
87 28 : *class_name = resolved;
88 28 : *member_prefix = zend_string_copy(prefix);
89 28 : *current_class_access = true;
90 :
91 28 : return true;
92 : }
93 :
94 48 : if (lsp_static_receiver_is_parent_class(raw)) {
95 12 : if (!lsp_find_enclosing_class_header(document->text, offset, current_class_start, current_body_start, current_body_end, current_body_depth)) {
96 0 : zend_string_release(raw);
97 :
98 0 : return false;
99 : }
100 :
101 12 : parent_class = lsp_class_extends_name(document->text, *current_class_start, *current_body_start);
102 12 : zend_string_release(raw);
103 12 : if (!parent_class) {
104 0 : return false;
105 : }
106 :
107 12 : *class_name = parent_class;
108 12 : *member_prefix = zend_string_copy(prefix);
109 12 : *parent_class_access = true;
110 :
111 12 : return true;
112 : }
113 :
114 36 : resolved = lsp_resolve_class_name_at(document->text, raw, offset);
115 36 : if (resolved) {
116 36 : zend_string_release(raw);
117 36 : *class_name = resolved;
118 : } else {
119 0 : *class_name = raw;
120 : }
121 :
122 36 : *member_prefix = zend_string_copy(prefix);
123 :
124 36 : return true;
125 : }
126 :
127 76 : static inline void lsp_add_class_name_resolution_completion(zval *items, zend_string *member_prefix)
128 : {
129 : zend_string *label, *detail;
130 :
131 76 : if (ZSTR_LEN(member_prefix) > 0 && ZSTR_VAL(member_prefix)[0] == '$') {
132 8 : return;
133 : }
134 :
135 68 : if (!lsp_matches_prefix_literal("class", member_prefix)) {
136 36 : return;
137 : }
138 :
139 32 : label = zend_string_init("class", sizeof("class") - 1, 0);
140 32 : detail = zend_string_init("class name resolution", sizeof("class name resolution") - 1, 0);
141 32 : lsp_add_completion_item_ex(items, label, 21, detail, "lsparrot");
142 32 : zend_string_release(detail);
143 32 : zend_string_release(label);
144 : }
145 :
146 312 : static inline bool lsp_add_static_member_completions(lsp_server *server, zval *items, lsp_document *document, size_t offset, zend_string *prefix)
147 : {
148 : zend_long current_body_depth;
149 : zend_string *class_name, *member_prefix, *parent_class;
150 : size_t current_class_start, current_body_start, current_body_end;
151 : bool current_class_access, parent_class_access;
152 :
153 312 : if (!lsp_static_member_access_context(document, offset, prefix, &class_name, &member_prefix, ¤t_class_access, &parent_class_access, ¤t_class_start, ¤t_body_start, ¤t_body_end, ¤t_body_depth)) {
154 236 : return false;
155 : }
156 :
157 76 : lsp_add_class_name_resolution_completion(items, member_prefix);
158 :
159 76 : if (current_class_access) {
160 28 : lsp_add_current_static_member_completions(items, document, current_body_start, current_body_end, current_body_depth, member_prefix);
161 28 : lsp_add_current_class_phpdoc_member_completions(server, items, document, current_class_start, member_prefix, true);
162 :
163 28 : parent_class = lsp_class_extends_name(document->text, current_class_start, current_body_start);
164 28 : if (parent_class) {
165 28 : lsp_add_static_project_class_member_completions(server, items, parent_class, member_prefix);
166 28 : zend_string_release(parent_class);
167 : }
168 : }
169 :
170 76 : if (parent_class_access) {
171 12 : lsp_add_inherited_static_project_class_member_completions(server, items, class_name, member_prefix);
172 12 : lsp_add_inherited_project_class_method_completions(server, items, class_name, member_prefix);
173 12 : lsp_add_parent_reflection_class_member_completions(server, items, class_name, member_prefix);
174 : } else {
175 64 : lsp_add_static_project_class_member_completions(server, items, class_name, member_prefix);
176 : }
177 76 : zend_string_release(class_name);
178 76 : zend_string_release(member_prefix);
179 :
180 76 : return true;
181 : }
182 :
183 164 : static inline void lsp_phpdoc_add_completions(lsp_server *server, zval *items, lsp_document *document, size_t offset, zend_string *prefix)
184 : {
185 164 : lsp_phpdoc_add_annotation_completions(items, document->text, offset);
186 164 : lsp_phpdoc_add_variable_type_completions(server, items, document->text, prefix);
187 164 : lsp_phpdoc_add_template_completions(server, items, document->text, prefix);
188 164 : lsp_phpdoc_add_array_shape_completions(server, items, document, offset);
189 164 : }
190 :
191 32 : static inline bool lsp_previous_identifier_before(zend_string *text, size_t end_offset, size_t *word_start, size_t *word_end)
192 : {
193 32 : const char *value = ZSTR_VAL(text);
194 : size_t i;
195 :
196 32 : i = end_offset > ZSTR_LEN(text) ? ZSTR_LEN(text) : end_offset;
197 32 : while (i > 0 && isspace((unsigned char) value[i - 1])) {
198 0 : i--;
199 : }
200 :
201 32 : *word_end = i;
202 252 : while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
203 220 : i--;
204 : }
205 :
206 32 : *word_start = i;
207 :
208 32 : return *word_end > *word_start;
209 : }
210 :
211 48 : static inline bool lsp_identifier_slice_equals_literal(zend_string *text, size_t start, size_t end, const char *literal)
212 : {
213 : size_t length;
214 :
215 48 : if (end < start) {
216 0 : return false;
217 : }
218 :
219 48 : length = strlen(literal);
220 :
221 48 : return end - start == length && strncasecmp(ZSTR_VAL(text) + start, literal, length) == 0;
222 : }
223 :
224 20 : static inline bool lsp_function_like_keyword_before_open_paren(zend_string *text, size_t open_paren)
225 : {
226 20 : const char *value = ZSTR_VAL(text);
227 : size_t word_start, word_end, cursor;
228 :
229 20 : if (!lsp_previous_identifier_before(text, open_paren, &word_start, &word_end)) {
230 0 : cursor = open_paren > ZSTR_LEN(text) ? ZSTR_LEN(text) : open_paren;
231 0 : while (cursor > 0 && isspace((unsigned char) value[cursor - 1])) {
232 0 : cursor--;
233 : }
234 0 : if (cursor == 0 || value[cursor - 1] != '&') {
235 0 : return false;
236 : }
237 :
238 0 : return lsp_previous_identifier_before(text, cursor - 1, &word_start, &word_end) &&
239 0 : lsp_identifier_slice_equals_literal(text, word_start, word_end, "function")
240 : ;
241 : }
242 :
243 36 : if (lsp_identifier_slice_equals_literal(text, word_start, word_end, "function") ||
244 16 : lsp_identifier_slice_equals_literal(text, word_start, word_end, "fn")
245 : ) {
246 8 : return true;
247 : }
248 :
249 12 : cursor = word_start;
250 24 : while (cursor > 0 && isspace((unsigned char) value[cursor - 1])) {
251 12 : cursor--;
252 : }
253 :
254 12 : if (cursor > 0 && value[cursor - 1] == '&') {
255 0 : cursor--;
256 0 : while (cursor > 0 && isspace((unsigned char) value[cursor - 1])) {
257 0 : cursor--;
258 : }
259 : }
260 :
261 24 : return lsp_previous_identifier_before(text, cursor, &word_start, &word_end) &&
262 12 : lsp_identifier_slice_equals_literal(text, word_start, word_end, "function")
263 : ;
264 : }
265 :
266 184 : static inline bool lsp_return_type_scan_reaches_colon(zend_string *text, size_t offset, zend_string *prefix, size_t *colon_offset)
267 : {
268 184 : const char *value = ZSTR_VAL(text);
269 : size_t scan, prefix_start;
270 :
271 184 : prefix_start = offset >= ZSTR_LEN(prefix) ? offset - ZSTR_LEN(prefix) : 0;
272 184 : scan = prefix_start > ZSTR_LEN(text) ? ZSTR_LEN(text) : prefix_start;
273 500 : while (scan > 0) {
274 952 : while (scan > 0 && isspace((unsigned char) value[scan - 1])) {
275 452 : scan--;
276 : }
277 :
278 500 : if (scan == 0) {
279 0 : break;
280 : }
281 :
282 500 : if (value[scan - 1] == ':') {
283 20 : *colon_offset = scan - 1;
284 :
285 20 : return true;
286 : }
287 :
288 480 : if (lsp_doc_is_identifier_char(value[scan - 1]) ||
289 204 : value[scan - 1] == '\\' ||
290 200 : value[scan - 1] == '?' ||
291 164 : value[scan - 1] == '|' ||
292 164 : value[scan - 1] == '&'
293 : ) {
294 316 : scan--;
295 316 : continue;
296 : }
297 :
298 164 : break;
299 : }
300 :
301 164 : *colon_offset = 0;
302 :
303 164 : return false;
304 : }
305 :
306 184 : static inline bool lsp_is_return_type_completion_context(zend_string *text, size_t offset, zend_string *prefix)
307 : {
308 184 : const char *value = ZSTR_VAL(text);
309 : size_t colon_offset, scan, open_paren;
310 :
311 184 : if (!lsp_return_type_scan_reaches_colon(text, offset, prefix, &colon_offset)) {
312 164 : return false;
313 : }
314 :
315 20 : scan = colon_offset;
316 20 : while (scan > 0 && isspace((unsigned char) value[scan - 1])) {
317 0 : scan--;
318 : }
319 :
320 20 : if (scan == 0 || value[scan - 1] != ')') {
321 0 : return false;
322 : }
323 :
324 40 : return lsp_find_matching_open_paren(text, scan - 1, &open_paren) &&
325 20 : lsp_function_like_keyword_before_open_paren(text, open_paren)
326 : ;
327 : }
328 :
329 20 : static inline void lsp_add_return_type_builtin_completions(zval *items, zend_string *prefix)
330 : {
331 20 : const char *types[] = {
332 : "array",
333 : "bool",
334 : "callable",
335 : "false",
336 : "float",
337 : "int",
338 : "iterable",
339 : "mixed",
340 : "never",
341 : "null",
342 : "object",
343 : "parent",
344 : "self",
345 : "static",
346 : "string",
347 : "true",
348 : "void"
349 : };
350 : zend_string *label, *detail;
351 : uint32_t i;
352 :
353 360 : for (i = 0; i < sizeof(types) / sizeof(types[0]); i++) {
354 340 : label = zend_string_init(types[i], strlen(types[i]), 0);
355 340 : if (!lsp_matches_prefix_string(label, prefix)) {
356 272 : zend_string_release(label);
357 272 : continue;
358 : }
359 :
360 68 : detail = strpprintf(0, "type %s", types[i]);
361 68 : lsp_add_completion_item_ex(items, label, 25, detail, "lsparrot");
362 68 : zend_string_release(detail);
363 68 : zend_string_release(label);
364 : }
365 20 : }
366 :
367 20 : static inline void lsp_add_current_document_class_like_completions(zval *items, lsp_document *document, HashTable *tokens, zend_string *prefix)
368 : {
369 : zend_long kind;
370 : zend_string *label, *detail, *namespace_name;
371 : zval *token;
372 : uint32_t i, count;
373 :
374 20 : namespace_name = lsp_document_namespace_cached(document);
375 20 : count = zend_hash_num_elements(tokens);
376 1760 : for (i = 0; i < count; i++) {
377 1740 : token = zend_hash_index_find(tokens, i);
378 1740 : if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_is_class_like(token)) {
379 1724 : continue;
380 : }
381 :
382 16 : label = lsp_next_string_token(tokens, i + 1);
383 16 : if (!label || !lsp_matches_prefix_string(label, prefix)) {
384 12 : continue;
385 : }
386 :
387 4 : kind = lsp_token_name_equals(token, "T_INTERFACE") ? 8 : (lsp_token_name_equals(token, "T_TRAIT") ? 9 : (lsp_token_name_equals(token, "T_ENUM") ? 13 : 7));
388 8 : detail = strpprintf(0, "%s %s",
389 6 : lsp_token_name_equals(token, "T_INTERFACE") ? "interface" : (lsp_token_name_equals(token, "T_TRAIT") ? "trait" : (lsp_token_name_equals(token, "T_ENUM") ? "enum" : "class")),
390 4 : ZSTR_VAL(label)
391 : );
392 4 : lsp_add_completion_item_qualified(items, label, kind, detail, namespace_name);
393 4 : zend_string_release(detail);
394 : }
395 20 : }
396 :
397 1116 : static inline bool lsp_prefix_has_global_namespace(zend_string *prefix)
398 : {
399 1116 : return ZSTR_LEN(prefix) > 0 && ZSTR_VAL(prefix)[0] == '\\';
400 : }
401 :
402 1116 : static inline bool lsp_prefix_is_qualified_name(zend_string *prefix)
403 : {
404 1116 : return memchr(ZSTR_VAL(prefix), '\\', ZSTR_LEN(prefix)) != NULL;
405 : }
406 :
407 1116 : static inline zend_string *lsp_completion_insert_name(zend_string *label, zend_string *qualified_name, zend_string *prefix)
408 : {
409 : zend_string *name, *global_name;
410 :
411 1116 : name = lsp_prefix_is_qualified_name(prefix) ? zend_string_copy(qualified_name) : zend_string_copy(label);
412 1116 : if (lsp_prefix_has_global_namespace(prefix)) {
413 300 : global_name = strpprintf(0, "\\%s", ZSTR_VAL(name));
414 :
415 300 : zend_string_release(name);
416 :
417 300 : return global_name;
418 : }
419 :
420 816 : return name;
421 : }
422 :
423 1116 : static inline void lsp_add_text_edit_completion_item(zval *items, zend_string *label, zend_string *qualified_name, zend_long kind, zend_string *detail, const char *source, zend_string *text, size_t offset, zend_string *prefix, bool call_snippet)
424 : {
425 : zend_string *insert_name, *new_text;
426 : zval item, data, text_edit, range;
427 : size_t prefix_start;
428 :
429 1116 : prefix_start = offset >= ZSTR_LEN(prefix) ? offset - ZSTR_LEN(prefix) : 0;
430 1116 : insert_name = lsp_completion_insert_name(label, qualified_name, prefix);
431 1116 : new_text = call_snippet ? lsp_completion_call_snippet_for_detail(insert_name, detail) : zend_string_copy(insert_name);
432 :
433 1116 : array_init(&item);
434 1116 : add_assoc_str(&item, "label", zend_string_copy(label));
435 1116 : add_assoc_long(&item, "kind", kind);
436 1116 : add_assoc_str(&item, "detail", zend_string_copy(detail));
437 1116 : add_assoc_str(&item, "filterText", zend_string_copy(insert_name));
438 1116 : if (call_snippet) {
439 136 : add_assoc_long(&item, "insertTextFormat", 2);
440 : }
441 :
442 1116 : array_init(&text_edit);
443 1116 : lsp_range_from_offsets(text, prefix_start, offset, &range);
444 1116 : add_assoc_zval(&text_edit, "range", &range);
445 1116 : add_assoc_str(&text_edit, "newText", new_text);
446 1116 : add_assoc_zval(&item, "textEdit", &text_edit);
447 1116 : array_init(&data);
448 1116 : add_assoc_string(&data, "source", source);
449 1116 : add_assoc_str(&data, "qualifiedName", zend_string_copy(qualified_name));
450 1116 : add_assoc_zval(&item, "data", &data);
451 1116 : add_next_index_zval(items, &item);
452 1116 : zend_string_release(insert_name);
453 1116 : }
454 :
455 164 : static inline void lsp_add_current_document_constant_completions(zval *items, lsp_document *document, HashTable *tokens, zend_string *prefix)
456 : {
457 : zend_string *label, *detail;
458 : zval *token;
459 : uint32_t i, count;
460 : size_t token_offset;
461 :
462 164 : count = zend_hash_num_elements(tokens);
463 6048 : for (i = 0; i < count; i++) {
464 5884 : token = zend_hash_index_find(tokens, i);
465 5884 : if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_CONST")) {
466 5864 : continue;
467 : }
468 :
469 20 : token_offset = (size_t) lsp_token_long(token, "offset", 0);
470 20 : if (lsp_offset_is_inside_class_body(document->text, token_offset)) {
471 0 : continue;
472 : }
473 :
474 20 : label = lsp_next_string_token(tokens, i + 1);
475 20 : if (!label || !lsp_matches_prefix_string(label, prefix)) {
476 16 : continue;
477 : }
478 :
479 4 : detail = strpprintf(0, "constant %s", ZSTR_VAL(label));
480 4 : lsp_add_completion_item_ex(items, label, 21, detail, "lsparrot");
481 4 : zend_string_release(detail);
482 : }
483 164 : }
484 :
485 164 : static inline void lsp_add_builtin_constant_completions(zval *items, zend_string *prefix, zend_string *text, size_t offset)
486 : {
487 : zend_constant *constant;
488 : zend_string *label, *detail;
489 : const char *label_value;
490 : size_t i, label_length;
491 :
492 164 : if (!EG(zend_constants) || lsp_effective_name_prefix_length(prefix) == 0) {
493 8 : return;
494 : }
495 :
496 149916 : ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
497 149760 : if (!constant || !constant->name) {
498 0 : continue;
499 : }
500 :
501 149760 : label_value = ZSTR_VAL(constant->name);
502 149760 : label_length = ZSTR_LEN(constant->name);
503 2403804 : for (i = ZSTR_LEN(constant->name); i > 0; i--) {
504 2256540 : if (ZSTR_VAL(constant->name)[i - 1] == '\\') {
505 2496 : label_value = ZSTR_VAL(constant->name) + i;
506 2496 : label_length = ZSTR_LEN(constant->name) - i;
507 2496 : break;
508 : }
509 : }
510 :
511 149760 : label = zend_string_init(label_value, label_length, 0);
512 149760 : if (!lsp_matches_prefix_string(label, prefix) && !lsp_matches_prefix_string(constant->name, prefix)) {
513 148832 : zend_string_release(label);
514 148832 : continue;
515 : }
516 :
517 928 : detail = strpprintf(0, "constant %s", ZSTR_VAL(constant->name));
518 928 : lsp_add_text_edit_completion_item(items, label, constant->name, 21, detail, "lsparrot", text, offset, prefix, false);
519 928 : zend_string_release(detail);
520 928 : zend_string_release(label);
521 : } ZEND_HASH_FOREACH_END();
522 : }
523 :
524 52 : static inline zend_long lsp_builtin_class_completion_kind(zend_class_entry *ce)
525 : {
526 52 : if (ce->ce_flags & ZEND_ACC_INTERFACE) {
527 8 : return 8;
528 : }
529 :
530 44 : if (ce->ce_flags & ZEND_ACC_TRAIT) {
531 0 : return 9;
532 : }
533 :
534 : #ifdef ZEND_ACC_ENUM
535 44 : if (ce->ce_flags & ZEND_ACC_ENUM) {
536 0 : return 13;
537 : }
538 : #endif
539 :
540 44 : return 7;
541 : }
542 :
543 980 : static inline char lsp_builtin_class_symbol_kind(zend_class_entry *ce)
544 : {
545 980 : if (ce->ce_flags & ZEND_ACC_INTERFACE) {
546 108 : return LSP_SYMBOL_INTERFACE;
547 : }
548 :
549 872 : if (ce->ce_flags & ZEND_ACC_TRAIT) {
550 0 : return LSP_SYMBOL_TRAIT;
551 : }
552 :
553 : #ifdef ZEND_ACC_ENUM
554 872 : if (ce->ce_flags & ZEND_ACC_ENUM) {
555 24 : return LSP_SYMBOL_ENUM;
556 : }
557 : #endif
558 :
559 848 : return LSP_SYMBOL_CLASS;
560 : }
561 :
562 164 : static inline void lsp_add_builtin_class_completions_ex(zval *items, zend_string *prefix, zend_string *text, size_t offset, bool allow_empty_prefix)
563 : {
564 : const char *label_value;
565 : zend_string *label, *detail;
566 : zend_class_entry *ce;
567 : size_t label_length;
568 :
569 164 : if (!CG(class_table) || (!allow_empty_prefix && lsp_effective_name_prefix_length(prefix) == 0)) {
570 8 : return;
571 : }
572 :
573 38220 : ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
574 38064 : if (!ce || !ce->name) {
575 0 : continue;
576 : }
577 :
578 38064 : label_value = lsp_basename_from_fqcn(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), &label_length);
579 38064 : label = zend_string_init(label_value, label_length, 0);
580 38064 : if (!lsp_matches_prefix_string(label, prefix) && !lsp_matches_prefix_string(ce->name, prefix)) {
581 38012 : zend_string_release(label);
582 38012 : continue;
583 : }
584 :
585 52 : detail = strpprintf(0, "%s%s", lsp_builtin_class_detail_prefix(ce), ZSTR_VAL(ce->name));
586 52 : lsp_add_text_edit_completion_item(items, label, ce->name, lsp_builtin_class_completion_kind(ce), detail, "lsparrot", text, offset, prefix, false);
587 52 : zend_string_release(detail);
588 52 : zend_string_release(label);
589 : } ZEND_HASH_FOREACH_END();
590 : }
591 :
592 164 : static inline void lsp_add_builtin_class_completions(zval *items, zend_string *prefix, zend_string *text, size_t offset)
593 : {
594 164 : lsp_add_builtin_class_completions_ex(items, prefix, text, offset, false);
595 164 : }
596 :
597 20 : static inline void lsp_add_return_type_builtin_class_completions(zval *items, lsp_document *document, zend_string *prefix, size_t offset)
598 : {
599 : const char *label_value;
600 : zend_string *label;
601 : zend_class_entry *ce;
602 : size_t label_length;
603 :
604 20 : if (!CG(class_table)) {
605 0 : return;
606 : }
607 :
608 4900 : ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
609 4880 : if (!ce || !ce->name) {
610 0 : continue;
611 : }
612 :
613 4880 : label_value = lsp_basename_from_fqcn(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), &label_length);
614 4880 : label = zend_string_init(label_value, label_length, 0);
615 4880 : if (!lsp_matches_prefix_string(label, prefix) && !lsp_matches_prefix_string(ce->name, prefix)) {
616 3900 : zend_string_release(label);
617 3900 : continue;
618 : }
619 :
620 980 : lsp_add_class_like_symbol_completion_item(items, document, offset, prefix, lsp_builtin_class_symbol_kind(ce), ce->name);
621 980 : zend_string_release(label);
622 : } ZEND_HASH_FOREACH_END();
623 : }
624 :
625 164 : static inline void lsp_add_builtin_function_completions(zval *items, zend_string *prefix, zend_string *text, size_t offset)
626 : {
627 : zend_function *function;
628 : zend_string *name, *detail;
629 :
630 164 : if (!CG(function_table) || lsp_effective_name_prefix_length(prefix) == 0) {
631 8 : return;
632 : }
633 :
634 146172 : ZEND_HASH_FOREACH_PTR(CG(function_table), function) {
635 146016 : if (!function || !function->common.function_name) {
636 0 : continue;
637 : }
638 :
639 146016 : name = function->common.function_name;
640 146016 : if (!lsp_matches_prefix_string(name, prefix)) {
641 145880 : continue;
642 : }
643 :
644 136 : detail = strpprintf(0, "function %s(...)", ZSTR_VAL(name));
645 136 : lsp_add_text_edit_completion_item(items, name, name, 3, detail, "lsparrot", text, offset, prefix, true);
646 136 : zend_string_release(detail);
647 : } ZEND_HASH_FOREACH_END();
648 : }
649 :
650 28 : static inline zend_string *lsp_analyzer_completion_cache_key(const char *analyzer, lsp_document *document, size_t offset, zend_string *prefix)
651 : {
652 28 : return strpprintf(0, "%s:%s:" ZEND_LONG_FMT ":%zu:%s", analyzer, ZSTR_VAL(document->uri), document->version, offset, ZSTR_VAL(prefix));
653 : }
654 :
655 : static inline lsp_analyzer_job *lsp_analyzer_completion_job(lsp_server *server, const char *analyzer)
656 : {
657 : return strcmp(analyzer, "phpstan") == 0 ? &server->phpstan_completion_job : &server->psalm_completion_job;
658 : }
659 :
660 520 : static inline bool lsp_analyzer_completion_enabled(lsp_server *server, const char *analyzer)
661 : {
662 520 : return strcmp(analyzer, "phpstan") == 0 ? server->phpstan_enabled : server->psalm_enabled;
663 : }
664 :
665 124 : static inline void lsp_completion_item_set_source(zval *item, const char *source)
666 : {
667 : zval *data, new_data;
668 :
669 124 : if (Z_TYPE_P(item) != IS_ARRAY) {
670 0 : return;
671 : }
672 :
673 124 : SEPARATE_ARRAY(item);
674 124 : data = zend_hash_str_find(Z_ARRVAL_P(item), "data", sizeof("data") - 1);
675 124 : if (!data || Z_TYPE_P(data) != IS_ARRAY) {
676 0 : array_init(&new_data);
677 0 : add_assoc_string(&new_data, "source", source);
678 0 : add_assoc_zval(item, "data", &new_data);
679 :
680 0 : return;
681 : }
682 :
683 124 : SEPARATE_ARRAY(data);
684 124 : zend_hash_str_del(Z_ARRVAL_P(data), "source", sizeof("source") - 1);
685 124 : add_assoc_string(data, "source", source);
686 : }
687 :
688 28 : static inline void lsp_completion_items_set_source(zval *items, const char *source)
689 : {
690 : zval *item;
691 :
692 28 : if (Z_TYPE_P(items) != IS_ARRAY) {
693 0 : return;
694 : }
695 :
696 152 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(items), item) {
697 124 : lsp_completion_item_set_source(item, source);
698 : } ZEND_HASH_FOREACH_END();
699 : }
700 :
701 4 : static inline bool lsp_phpstan_type_token_is_builtin(zend_string *name)
702 : {
703 4 : const char *builtins[] = {
704 : "array",
705 : "bool",
706 : "callable",
707 : "false",
708 : "float",
709 : "int",
710 : "iterable",
711 : "mixed",
712 : "never",
713 : "non-empty-array",
714 : "non-empty-list",
715 : "non-empty-string",
716 : "null",
717 : "object",
718 : "resource",
719 : "self",
720 : "static",
721 : "string",
722 : "true",
723 : "void"
724 : };
725 : uint32_t i;
726 :
727 84 : for (i = 0; i < sizeof(builtins) / sizeof(builtins[0]); i++) {
728 80 : if (ZSTR_LEN(name) == strlen(builtins[i]) && strncasecmp(ZSTR_VAL(name), builtins[i], ZSTR_LEN(name)) == 0) {
729 0 : return true;
730 : }
731 : }
732 :
733 4 : return false;
734 : }
735 :
736 4 : static inline zend_string *lsp_phpstan_type_first_name(zend_string *type)
737 : {
738 : const char *p, *end, *start;
739 :
740 4 : p = ZSTR_VAL(type);
741 4 : end = p + ZSTR_LEN(type);
742 4 : while (p < end && isspace((unsigned char) *p)) {
743 0 : p++;
744 : }
745 :
746 4 : while (p < end && (*p == '?' || *p == '(')) {
747 0 : p++;
748 : }
749 :
750 4 : while (p < end && isspace((unsigned char) *p)) {
751 0 : p++;
752 : }
753 :
754 4 : if (p < end && *p == '\\') {
755 0 : p++;
756 : }
757 :
758 4 : start = p;
759 140 : while (p < end && (lsp_doc_is_identifier_char(*p) || *p == '\\' || *p == '-')) {
760 136 : p++;
761 : }
762 :
763 4 : if (p == start) {
764 0 : return NULL;
765 : }
766 :
767 4 : return zend_string_init(start, p - start, 0);
768 : }
769 :
770 4 : static inline zend_string *lsp_phpstan_completion_class_from_type(lsp_document *document, zend_string *type)
771 : {
772 : const char *slash;
773 : zend_string *raw, *resolved;
774 :
775 4 : raw = lsp_phpstan_type_first_name(type);
776 4 : if (!raw) {
777 0 : return NULL;
778 : }
779 :
780 4 : if (lsp_phpstan_type_token_is_builtin(raw)) {
781 0 : zend_string_release(raw);
782 :
783 0 : return NULL;
784 : }
785 :
786 4 : slash = memchr(ZSTR_VAL(raw), '\\', ZSTR_LEN(raw));
787 4 : if (slash) {
788 4 : return raw;
789 : }
790 :
791 0 : resolved = lsp_resolve_class_name(document->text, raw);
792 0 : zend_string_release(raw);
793 :
794 0 : return resolved;
795 : }
796 :
797 48 : static inline zend_string *lsp_phpstan_member_receiver_expression(zend_string *text, size_t offset, zend_string *prefix, zend_string **member_prefix, size_t *receiver_start_offset)
798 : {
799 : const char *value;
800 : size_t receiver_end, start, i, depth;
801 :
802 48 : *member_prefix = NULL;
803 48 : if (receiver_start_offset) {
804 24 : *receiver_start_offset = 0;
805 : }
806 48 : if (!lsp_member_access_arrow(text, offset, prefix, NULL, &receiver_end)) {
807 0 : return NULL;
808 : }
809 :
810 48 : value = ZSTR_VAL(text);
811 48 : start = receiver_end;
812 48 : depth = 0;
813 816 : for (i = receiver_end; i > 0; i--) {
814 816 : if (value[i - 1] == ')' || value[i - 1] == ']' || value[i - 1] == '}') {
815 16 : depth++;
816 16 : continue;
817 : }
818 :
819 800 : if (value[i - 1] == '(' || value[i - 1] == '[' || value[i - 1] == '{') {
820 16 : if (depth > 0) {
821 16 : depth--;
822 16 : continue;
823 : }
824 :
825 0 : break;
826 : }
827 :
828 784 : if (depth == 0 && (value[i - 1] == '\n' || value[i - 1] == '\r' || value[i - 1] == ';' || value[i - 1] == '=' || value[i - 1] == ',')) {
829 : break;
830 : }
831 : }
832 :
833 48 : start = i;
834 400 : while (start < receiver_end && isspace((unsigned char) value[start])) {
835 352 : start++;
836 : }
837 :
838 48 : if (receiver_end >= start + sizeof("return") - 1 &&
839 8 : memcmp(value + start, "return", sizeof("return") - 1) == 0 &&
840 0 : !lsp_doc_is_identifier_char(value[start + sizeof("return") - 1])
841 : ) {
842 0 : start += sizeof("return") - 1;
843 0 : while (start < receiver_end && isspace((unsigned char) value[start])) {
844 0 : start++;
845 : }
846 : }
847 :
848 48 : if (receiver_end <= start) {
849 0 : return NULL;
850 : }
851 :
852 48 : *member_prefix = zend_string_copy(prefix);
853 48 : if (receiver_start_offset) {
854 24 : *receiver_start_offset = start;
855 : }
856 :
857 48 : return zend_string_init(value + start, receiver_end - start, 0);
858 : }
859 :
860 24 : static inline void lsp_add_phpstan_member_completions(lsp_server *server, zval *items, lsp_document *document, size_t offset, zend_string *prefix)
861 : {
862 : zend_string *receiver, *member_prefix, *type, *class_name;
863 :
864 24 : receiver = lsp_phpstan_member_receiver_expression(document->text, offset, prefix, &member_prefix, NULL);
865 24 : if (!receiver) {
866 20 : return;
867 : }
868 :
869 24 : type = lsp_phpstan_type_for_expression(server, document, receiver, offset);
870 24 : if (!type) {
871 20 : zend_string_release(receiver);
872 20 : zend_string_release(member_prefix);
873 :
874 20 : return;
875 : }
876 :
877 4 : class_name = lsp_phpstan_completion_class_from_type(document, type);
878 4 : if (class_name) {
879 4 : lsp_add_inherited_public_project_class_member_completions(server, items, class_name, member_prefix);
880 4 : zend_string_release(class_name);
881 : }
882 :
883 4 : zend_string_release(type);
884 4 : zend_string_release(receiver);
885 4 : zend_string_release(member_prefix);
886 : }
887 :
888 0 : static inline void lsp_add_psalm_member_completions(lsp_server *server, zval *items, lsp_document *document, size_t offset, zend_string *prefix)
889 : {
890 : zend_string *receiver, *member_prefix, *type, *class_name;
891 :
892 0 : receiver = lsp_phpstan_member_receiver_expression(document->text, offset, prefix, &member_prefix, NULL);
893 0 : if (!receiver) {
894 0 : return;
895 : }
896 :
897 0 : type = lsp_psalm_type_for_expression(server, document, receiver, offset);
898 0 : if (!type) {
899 0 : zend_string_release(receiver);
900 0 : zend_string_release(member_prefix);
901 :
902 0 : return;
903 : }
904 :
905 0 : class_name = lsp_phpstan_completion_class_from_type(document, type);
906 0 : if (class_name) {
907 0 : lsp_add_inherited_public_project_class_member_completions(server, items, class_name, member_prefix);
908 0 : zend_string_release(class_name);
909 : }
910 :
911 0 : zend_string_release(type);
912 0 : zend_string_release(receiver);
913 0 : zend_string_release(member_prefix);
914 : }
915 :
916 24 : static inline void lsp_position_zval_at_offset(zval *position, zend_string *text, size_t offset)
917 : {
918 : const char *value;
919 : size_t i, length, line_start;
920 : zend_long line, character;
921 :
922 24 : value = ZSTR_VAL(text);
923 24 : length = ZSTR_LEN(text);
924 24 : if (offset > length) {
925 0 : offset = length;
926 : }
927 :
928 24 : line = 0;
929 24 : line_start = 0;
930 6072 : for (i = 0; i < offset; i++) {
931 6048 : if (value[i] == '\n') {
932 332 : line++;
933 332 : line_start = i + 1;
934 : }
935 : }
936 :
937 24 : character = (zend_long) (offset - line_start);
938 24 : array_init(position);
939 24 : add_assoc_long(position, "line", line);
940 24 : add_assoc_long(position, "character", character);
941 24 : }
942 :
943 12 : static inline size_t lsp_psalm_ls_rhs_hover_probe_offset(zend_string *text, size_t rhs_start, size_t rhs_end)
944 : {
945 : const char *value;
946 : size_t i, best_arrow, best_static, first_identifier, p, length;
947 : int32_t paren_depth, bracket_depth, brace_depth;
948 : bool found_arrow, found_static, found_identifier;
949 :
950 12 : value = ZSTR_VAL(text);
951 12 : length = ZSTR_LEN(text);
952 12 : if (rhs_start >= length) {
953 0 : return length;
954 : }
955 12 : if (rhs_end > length) {
956 0 : rhs_end = length;
957 : }
958 12 : if (rhs_end <= rhs_start) {
959 0 : return rhs_start;
960 : }
961 :
962 12 : best_arrow = rhs_start;
963 12 : best_static = rhs_start;
964 12 : first_identifier = rhs_start;
965 12 : paren_depth = 0;
966 12 : bracket_depth = 0;
967 12 : brace_depth = 0;
968 12 : found_arrow = false;
969 12 : found_static = false;
970 12 : found_identifier = false;
971 :
972 388 : for (i = rhs_start; i < rhs_end; i++) {
973 376 : if (!found_identifier && (lsp_doc_is_identifier_char(value[i]) || value[i] == '$' || value[i] == '\\')) {
974 12 : first_identifier = value[i] == '$' && i + 1 < rhs_end ? i + 1 : i;
975 12 : found_identifier = true;
976 : }
977 :
978 376 : if (value[i] == '(') {
979 12 : paren_depth++;
980 12 : continue;
981 : }
982 364 : if (value[i] == ')' && paren_depth > 0) {
983 12 : paren_depth--;
984 12 : continue;
985 : }
986 352 : if (value[i] == '[') {
987 0 : bracket_depth++;
988 0 : continue;
989 : }
990 352 : if (value[i] == ']' && bracket_depth > 0) {
991 0 : bracket_depth--;
992 0 : continue;
993 : }
994 352 : if (value[i] == '{') {
995 0 : brace_depth++;
996 0 : continue;
997 : }
998 352 : if (value[i] == '}' && brace_depth > 0) {
999 0 : brace_depth--;
1000 0 : continue;
1001 : }
1002 :
1003 352 : if (paren_depth == 0 && bracket_depth == 0 && brace_depth == 0) {
1004 288 : if (i + 1 < rhs_end && value[i] == '-' && value[i + 1] == '>') {
1005 12 : best_arrow = i + 1;
1006 12 : found_arrow = true;
1007 276 : } else if (i + 2 < rhs_end && value[i] == '?' && value[i + 1] == '-' && value[i + 2] == '>') {
1008 0 : best_arrow = i + 2;
1009 0 : found_arrow = true;
1010 276 : } else if (i + 1 < rhs_end && value[i] == ':' && value[i + 1] == ':') {
1011 0 : best_static = i + 1;
1012 0 : found_static = true;
1013 : }
1014 : }
1015 : }
1016 :
1017 12 : if (found_arrow) {
1018 12 : return best_arrow;
1019 : }
1020 0 : if (found_static) {
1021 0 : return best_static;
1022 : }
1023 :
1024 0 : p = rhs_start;
1025 0 : while (p < rhs_end && isspace((unsigned char) value[p])) {
1026 0 : p++;
1027 : }
1028 0 : if (p + sizeof("new") - 1 < rhs_end &&
1029 0 : strncasecmp(value + p, "new", sizeof("new") - 1) == 0 &&
1030 0 : !lsp_doc_is_identifier_char(value[p + sizeof("new") - 1])
1031 : ) {
1032 0 : p += sizeof("new") - 1;
1033 0 : while (p < rhs_end && isspace((unsigned char) value[p])) {
1034 0 : p++;
1035 : }
1036 0 : if (p < rhs_end && value[p] == '\\') {
1037 0 : p++;
1038 : }
1039 0 : if (p < rhs_end) {
1040 0 : return p;
1041 : }
1042 : }
1043 :
1044 0 : return found_identifier ? first_identifier : rhs_start;
1045 : }
1046 :
1047 24 : static inline bool lsp_previous_variable_assignment_probe_offset(zend_string *text, zend_string *variable, size_t before_offset, size_t *probe_offset)
1048 : {
1049 : const char *value;
1050 : size_t length, variable_length, i, candidate, after, rhs_start, rhs_end;
1051 : int32_t paren_depth, bracket_depth, brace_depth;
1052 :
1053 24 : if (!text || !variable || ZSTR_LEN(variable) == 0) {
1054 0 : return false;
1055 : }
1056 :
1057 24 : value = ZSTR_VAL(text);
1058 24 : length = ZSTR_LEN(text);
1059 24 : variable_length = ZSTR_LEN(variable);
1060 24 : if (before_offset > length) {
1061 0 : before_offset = length;
1062 : }
1063 24 : if (before_offset < variable_length) {
1064 0 : return false;
1065 : }
1066 :
1067 24 : i = before_offset;
1068 1816 : while (i >= variable_length) {
1069 1816 : candidate = i - variable_length;
1070 1816 : if (memcmp(value + candidate, ZSTR_VAL(variable), variable_length) == 0 &&
1071 12 : (candidate == 0 || !lsp_doc_is_identifier_char(value[candidate - 1])) &&
1072 12 : (candidate + variable_length >= length || !lsp_doc_is_identifier_char(value[candidate + variable_length]))
1073 : ) {
1074 12 : after = candidate + variable_length;
1075 24 : while (after < length && isspace((unsigned char) value[after])) {
1076 12 : after++;
1077 : }
1078 12 : if (after < length && value[after] == '=' &&
1079 12 : (after + 1 >= length || (value[after + 1] != '=' && value[after + 1] != '>')) &&
1080 12 : (after == 0 || (value[after - 1] != '!' && value[after - 1] != '<' && value[after - 1] != '>'))
1081 : ) {
1082 12 : rhs_start = after + 1;
1083 24 : while (rhs_start < length && isspace((unsigned char) value[rhs_start])) {
1084 12 : rhs_start++;
1085 : }
1086 :
1087 12 : rhs_end = rhs_start;
1088 12 : paren_depth = 0;
1089 12 : bracket_depth = 0;
1090 12 : brace_depth = 0;
1091 388 : while (rhs_end < length) {
1092 388 : if (value[rhs_end] == '(') {
1093 12 : paren_depth++;
1094 376 : } else if (value[rhs_end] == ')' && paren_depth > 0) {
1095 12 : paren_depth--;
1096 364 : } else if (value[rhs_end] == '[') {
1097 0 : bracket_depth++;
1098 364 : } else if (value[rhs_end] == ']' && bracket_depth > 0) {
1099 0 : bracket_depth--;
1100 364 : } else if (value[rhs_end] == '{') {
1101 0 : brace_depth++;
1102 364 : } else if (value[rhs_end] == '}' && brace_depth > 0) {
1103 0 : brace_depth--;
1104 364 : } else if (paren_depth == 0 && bracket_depth == 0 && brace_depth == 0 && (value[rhs_end] == ';' || value[rhs_end] == '\n' || value[rhs_end] == '\r')) {
1105 : break;
1106 : }
1107 376 : rhs_end++;
1108 : }
1109 :
1110 12 : while (rhs_end > rhs_start && isspace((unsigned char) value[rhs_end - 1])) {
1111 0 : rhs_end--;
1112 : }
1113 :
1114 12 : *probe_offset = rhs_end > rhs_start ? lsp_psalm_ls_rhs_hover_probe_offset(text, rhs_start, rhs_end) : candidate + 1;
1115 :
1116 12 : return true;
1117 : }
1118 : }
1119 :
1120 1804 : if (i == variable_length) {
1121 12 : break;
1122 : }
1123 1792 : i--;
1124 : }
1125 :
1126 12 : return false;
1127 : }
1128 :
1129 24 : static inline void lsp_add_psalm_ls_hover_member_completions(lsp_server *server, zval *items, lsp_document *document, size_t offset, zend_string *prefix)
1130 : {
1131 : zend_string *receiver, *member_prefix, *type, *class_name;
1132 : zval receiver_position;
1133 : size_t receiver_start, receiver_probe_offset, assignment_probe_offset;
1134 :
1135 24 : receiver = lsp_phpstan_member_receiver_expression(document->text, offset, prefix, &member_prefix, &receiver_start);
1136 24 : if (!receiver) {
1137 24 : return;
1138 : }
1139 :
1140 24 : receiver_probe_offset = receiver_start;
1141 24 : if (ZSTR_LEN(receiver) > 1 && ZSTR_VAL(receiver)[0] == '$') {
1142 24 : if (lsp_previous_variable_assignment_probe_offset(document->text, receiver, receiver_start, &assignment_probe_offset)) {
1143 12 : receiver_probe_offset = assignment_probe_offset;
1144 : } else {
1145 12 : receiver_probe_offset++;
1146 : }
1147 : }
1148 24 : lsp_position_zval_at_offset(&receiver_position, document->text, receiver_probe_offset);
1149 24 : type = lsp_psalm_ls_type_for_position_async(server, document, &receiver_position, receiver_probe_offset, receiver);
1150 24 : zval_ptr_dtor(&receiver_position);
1151 24 : if (!type) {
1152 24 : zend_string_release(receiver);
1153 24 : zend_string_release(member_prefix);
1154 :
1155 24 : return;
1156 : }
1157 :
1158 0 : class_name = lsp_phpstan_completion_class_from_type(document, type);
1159 0 : if (class_name) {
1160 0 : lsp_add_inherited_public_project_class_member_completions(server, items, class_name, member_prefix);
1161 0 : zend_string_release(class_name);
1162 : }
1163 :
1164 0 : zend_string_release(type);
1165 0 : zend_string_release(receiver);
1166 0 : zend_string_release(member_prefix);
1167 : }
1168 :
1169 28 : static inline bool lsp_build_analyzer_completion_items(lsp_server *server, zval *items, lsp_document *document, size_t offset, zend_string *prefix, const char *analyzer)
1170 : {
1171 28 : array_init(items);
1172 28 : if (lsp_is_member_access_completion(document->text, offset, prefix)) {
1173 24 : if (strcmp(analyzer, "phpstan") == 0) {
1174 24 : lsp_add_phpstan_member_completions(server, items, document, offset, prefix);
1175 0 : } else if (strcmp(analyzer, "psalm") == 0) {
1176 0 : lsp_add_psalm_member_completions(server, items, document, offset, prefix);
1177 : }
1178 24 : lsp_add_this_member_completions(server, items, document, offset, prefix);
1179 24 : lsp_phpdoc_add_object_shape_member_completions(server, items, document, offset, prefix);
1180 24 : lsp_add_inferred_member_completions(server, items, document, offset, prefix);
1181 4 : } else if (!lsp_add_static_member_completions(server, items, document, offset, prefix)) {
1182 0 : return false;
1183 : }
1184 :
1185 28 : if (zend_hash_num_elements(Z_ARRVAL_P(items)) == 0) {
1186 0 : return false;
1187 : }
1188 :
1189 28 : lsp_completion_items_set_source(items, analyzer);
1190 28 : lsp_deduplicate_completion_items(items);
1191 :
1192 28 : return zend_hash_num_elements(Z_ARRVAL_P(items)) > 0;
1193 : }
1194 :
1195 28 : static inline void lsp_append_cached_analyzer_completion_items(lsp_server *server, zval *items, zend_string *key)
1196 : {
1197 : zval *cached, *item, copy;
1198 :
1199 28 : cached = zend_hash_find(&server->completion_cache, key);
1200 28 : if (!cached || Z_TYPE_P(cached) != IS_ARRAY) {
1201 0 : return;
1202 : }
1203 :
1204 148 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(cached), item) {
1205 120 : ZVAL_COPY(©, item);
1206 120 : add_next_index_zval(items, ©);
1207 : } ZEND_HASH_FOREACH_END();
1208 : }
1209 :
1210 520 : static inline bool lsp_analyzer_completion_cache_or_schedule(lsp_server *server, zval *items, lsp_document *document, size_t offset, zend_string *prefix, const char *analyzer)
1211 : {
1212 : zend_string *key;
1213 : zval built_items;
1214 : bool built;
1215 :
1216 520 : if (!lsp_analyzer_completion_enabled(server, analyzer)) {
1217 492 : return false;
1218 : }
1219 :
1220 28 : key = lsp_analyzer_completion_cache_key(analyzer, document, offset, prefix);
1221 28 : if (zend_hash_exists(&server->completion_cache, key)) {
1222 0 : lsp_append_cached_analyzer_completion_items(server, items, key);
1223 0 : zend_string_release(key);
1224 :
1225 0 : return false;
1226 : }
1227 :
1228 28 : built = false;
1229 28 : if (lsp_build_analyzer_completion_items(server, &built_items, document, offset, prefix, analyzer)) {
1230 28 : zend_hash_update(&server->completion_cache, key, &built_items);
1231 28 : lsp_append_cached_analyzer_completion_items(server, items, key);
1232 28 : built = true;
1233 : } else {
1234 : /* The builder array_inits its output unconditionally; discard it on
1235 : * the not-built paths or every member completion leaks a hashtable. */
1236 0 : zval_ptr_dtor(&built_items);
1237 : }
1238 :
1239 28 : zend_string_release(key);
1240 :
1241 28 : return built;
1242 : }
1243 :
1244 260 : static inline bool lsp_add_analyzer_completion_items(lsp_server *server, zval *items, lsp_document *document, zval *position, size_t offset, zend_string *prefix)
1245 : {
1246 260 : bool pending = false;
1247 :
1248 260 : pending = lsp_analyzer_completion_cache_or_schedule(server, items, document, offset, prefix, "phpstan") || pending;
1249 260 : pending = lsp_analyzer_completion_cache_or_schedule(server, items, document, offset, prefix, "psalm") || pending;
1250 260 : if (lsp_psalm_ls_enabled(server)) {
1251 24 : if (lsp_is_member_access_completion(document->text, offset, prefix)) {
1252 24 : lsp_add_psalm_ls_hover_member_completions(server, items, document, offset, prefix);
1253 : }
1254 24 : pending = lsp_psalm_ls_completion_cache_or_schedule(server, items, document, position, offset, prefix) || pending;
1255 : }
1256 :
1257 260 : return pending;
1258 : }
1259 :
1260 36 : extern bool lsp_offset_is_inside_class_body(zend_string *text, size_t offset)
1261 : {
1262 : zend_long body_depth;
1263 : size_t search_start, class_start, body_start, body_end;
1264 :
1265 36 : search_start = 0;
1266 60 : while (lsp_find_class_header_from(text, search_start, &class_start, &body_start, &body_end, &body_depth)) {
1267 24 : if (offset >= body_start && offset < body_end) {
1268 0 : return true;
1269 : }
1270 :
1271 24 : search_start = body_end + 1;
1272 : }
1273 :
1274 36 : return false;
1275 : }
1276 :
1277 56 : extern const char *lsp_builtin_class_detail_prefix(zend_class_entry *ce)
1278 : {
1279 56 : if (ce->ce_flags & ZEND_ACC_INTERFACE) {
1280 8 : return "interface ";
1281 : }
1282 :
1283 48 : if (ce->ce_flags & ZEND_ACC_TRAIT) {
1284 0 : return "trait ";
1285 : }
1286 :
1287 : #ifdef ZEND_ACC_ENUM
1288 48 : if (ce->ce_flags & ZEND_ACC_ENUM) {
1289 0 : return "enum ";
1290 : }
1291 : #endif
1292 :
1293 48 : return "class ";
1294 : }
1295 :
1296 3608 : extern void lsp_reap_analyzer_completion_jobs(void)
1297 : {
1298 3608 : }
1299 :
1300 504 : extern void lsp_lsparrot_completion(lsp_server *server, zval *return_value, lsp_document *document, zval *position)
1301 : {
1302 : zend_long line, character, kind;
1303 : zend_string *prefix, *label, *detail;
1304 : zval items, *tokens_zv, *token, *name_token;
1305 : HashTable *tokens;
1306 : uint32_t i, count, name_index;
1307 : size_t offset, prefix_start;
1308 : char constraint_kind;
1309 : bool incomplete;
1310 :
1311 504 : lsp_position_from_zval(position, &line, &character);
1312 504 : offset = lsp_offset_at(document->text, line, character);
1313 504 : prefix = lsp_prefix_at(document->text, offset);
1314 504 : lsp_reap_analyzer_completion_jobs();
1315 :
1316 504 : array_init(return_value);
1317 504 : incomplete = lsp_analyzer_jobs_running_for_document(server, document);
1318 504 : add_assoc_bool(return_value, "isIncomplete", incomplete);
1319 504 : array_init(&items);
1320 :
1321 504 : constraint_kind = lsp_type_constraint_completion_kind(document->text, offset, prefix);
1322 504 : if (constraint_kind != '\0') {
1323 8 : lsp_add_project_symbol_kind_completions(server, &items, document, offset, prefix, constraint_kind);
1324 8 : lsp_deduplicate_completion_items(&items);
1325 8 : lsp_completion_attach_items(return_value, &items);
1326 8 : zend_string_release(prefix);
1327 :
1328 340 : return;
1329 : }
1330 :
1331 496 : if (lsp_is_member_access_completion(document->text, offset, prefix)) {
1332 188 : lsp_add_this_member_completions(server, &items, document, offset, prefix);
1333 188 : lsp_phpdoc_add_object_shape_member_completions(server, &items, document, offset, prefix);
1334 188 : lsp_add_inferred_member_completions(server, &items, document, offset, prefix);
1335 188 : incomplete = lsp_add_analyzer_completion_items(server, &items, document, position, offset, prefix) || incomplete;
1336 188 : lsp_deduplicate_completion_items(&items);
1337 188 : add_assoc_bool(return_value, "isIncomplete", incomplete);
1338 188 : lsp_completion_attach_items(return_value, &items);
1339 188 : zend_string_release(prefix);
1340 :
1341 188 : return;
1342 : }
1343 :
1344 308 : if (lsp_add_static_member_completions(server, &items, document, offset, prefix)) {
1345 72 : incomplete = lsp_add_analyzer_completion_items(server, &items, document, position, offset, prefix) || incomplete;
1346 72 : lsp_deduplicate_completion_items(&items);
1347 72 : add_assoc_bool(return_value, "isIncomplete", incomplete);
1348 72 : lsp_completion_attach_items(return_value, &items);
1349 72 : zend_string_release(prefix);
1350 :
1351 72 : return;
1352 : }
1353 :
1354 236 : if (lsp_phpdoc_add_array_shape_completions(server, &items, document, offset)) {
1355 32 : lsp_deduplicate_completion_items(&items);
1356 32 : lsp_completion_attach_items(return_value, &items);
1357 32 : zend_string_release(prefix);
1358 :
1359 32 : return;
1360 : }
1361 :
1362 204 : tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
1363 204 : if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
1364 0 : lsp_completion_attach_items(return_value, &items);
1365 0 : zend_string_release(prefix);
1366 :
1367 0 : return;
1368 : }
1369 :
1370 204 : tokens = Z_ARRVAL_P(tokens_zv);
1371 204 : count = zend_hash_num_elements(tokens);
1372 204 : prefix_start = offset >= ZSTR_LEN(prefix) ? offset - ZSTR_LEN(prefix) : 0;
1373 204 : if (ZSTR_LEN(prefix) > 0 && ZSTR_VAL(prefix)[0] == '$') {
1374 16 : lsp_add_scope_variable_completions(server, &items, document, tokens, offset, prefix, prefix_start);
1375 16 : lsp_phpdoc_add_variable_type_completion_edits(server, &items, document->text, prefix, prefix_start, offset);
1376 16 : lsp_deduplicate_completion_items(&items);
1377 16 : lsp_completion_attach_items(return_value, &items);
1378 16 : zend_string_release(prefix);
1379 :
1380 16 : return;
1381 : }
1382 :
1383 188 : if (lsp_add_override_method_completions(server, &items, document, tokens, offset, prefix)) {
1384 4 : lsp_deduplicate_completion_items(&items);
1385 4 : lsp_completion_attach_items(return_value, &items);
1386 4 : zend_string_release(prefix);
1387 :
1388 4 : return;
1389 : }
1390 :
1391 184 : if (lsp_is_return_type_completion_context(document->text, offset, prefix)) {
1392 20 : lsp_add_return_type_builtin_completions(&items, prefix);
1393 20 : lsp_add_current_document_class_like_completions(&items, document, tokens, prefix);
1394 20 : lsp_add_project_class_like_completions(server, &items, document, offset, prefix);
1395 20 : lsp_add_return_type_builtin_class_completions(&items, document, prefix, offset);
1396 20 : lsp_deduplicate_completion_items(&items);
1397 20 : lsp_completion_attach_items(return_value, &items);
1398 20 : zend_string_release(prefix);
1399 :
1400 20 : return;
1401 : }
1402 :
1403 164 : lsp_add_current_document_constant_completions(&items, document, tokens, prefix);
1404 :
1405 6048 : for (i = 0; i < count; i++) {
1406 5884 : token = zend_hash_index_find(tokens, i);
1407 5884 : label = NULL;
1408 5884 : detail = NULL;
1409 5884 : kind = 6;
1410 :
1411 5884 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
1412 0 : continue;
1413 : }
1414 :
1415 5884 : if (lsp_token_name_equals(token, "T_FUNCTION")) {
1416 160 : name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
1417 160 : label = lsp_token_string(name_token, "text");
1418 160 : kind = 3;
1419 160 : if (label) {
1420 160 : detail = lsp_function_signature_detail(document->text, name_token, tokens, name_index, lsp_brace_depth_at(document->text, (size_t) lsp_token_long(token, "offset", 0)), "function");
1421 160 : if (!detail) {
1422 0 : detail = strpprintf(0, "function %s", ZSTR_VAL(label));
1423 : }
1424 : }
1425 5724 : } else if (lsp_token_is_class_like(token)) {
1426 92 : label = lsp_next_string_token(tokens, i + 1);
1427 92 : kind = lsp_token_name_equals(token, "T_INTERFACE") ? 8 : (lsp_token_name_equals(token, "T_TRAIT") ? 9 : (lsp_token_name_equals(token, "T_ENUM") ? 13 : 7));
1428 92 : if (label) {
1429 88 : detail = strpprintf(0, "%s %s", lsp_token_name_equals(token, "T_INTERFACE") ? "interface" : (lsp_token_name_equals(token, "T_TRAIT") ? "trait" : (lsp_token_name_equals(token, "T_ENUM") ? "enum" : "class")), ZSTR_VAL(label));
1430 : }
1431 : } else {
1432 5632 : continue;
1433 : }
1434 :
1435 252 : if (!label || !detail) {
1436 4 : continue;
1437 : }
1438 :
1439 248 : if (!lsp_matches_prefix_string(label, prefix)) {
1440 224 : zend_string_release(detail);
1441 :
1442 224 : continue;
1443 : }
1444 :
1445 24 : if (lsp_token_is_class_like(token)) {
1446 0 : lsp_add_completion_item_qualified(&items, label, kind, detail, lsp_document_namespace_cached(document));
1447 : } else {
1448 24 : lsp_add_completion_item(&items, label, kind, detail);
1449 : }
1450 24 : zend_string_release(detail);
1451 : }
1452 :
1453 164 : lsp_add_project_symbol_completions(server, &items, document, offset, prefix);
1454 164 : lsp_add_builtin_constant_completions(&items, prefix, document->text, offset);
1455 164 : lsp_add_builtin_class_completions(&items, prefix, document->text, offset);
1456 164 : lsp_add_builtin_function_completions(&items, prefix, document->text, offset);
1457 164 : lsp_phpdoc_add_completions(server, &items, document, offset, prefix);
1458 :
1459 164 : lsp_add_keyword_completion(&items, "class", prefix);
1460 164 : lsp_add_keyword_completion(&items, "function", prefix);
1461 164 : lsp_add_keyword_completion(&items, "interface", prefix);
1462 164 : lsp_add_keyword_completion(&items, "trait", prefix);
1463 164 : lsp_add_keyword_completion(&items, "enum", prefix);
1464 164 : lsp_add_keyword_completion(&items, "public", prefix);
1465 164 : lsp_add_keyword_completion(&items, "private", prefix);
1466 164 : lsp_add_keyword_completion(&items, "protected", prefix);
1467 164 : lsp_add_keyword_completion(&items, "static", prefix);
1468 164 : lsp_add_keyword_completion(&items, "readonly", prefix);
1469 :
1470 164 : lsp_deduplicate_completion_items(&items);
1471 164 : lsp_completion_attach_items(return_value, &items);
1472 164 : zend_string_release(prefix);
1473 : }
|