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 : #include <Zend/zend_language_parser.h>
17 :
18 548 : static inline void lsp_initialize(lsp_server *server, zval *params, zval *return_value)
19 : {
20 548 : zend_string *root_uri = lsp_array_string(params, "rootUri"), *root_path = lsp_array_string(params, "rootPath");
21 : zval capabilities, sync, save, completion, triggers, code_lens, signature, signature_triggers, code_action, code_action_kinds, rename, server_info, semantic_tokens, legend;
22 : zval workspace, workspace_folders, file_operations, will_rename, filters, filter, pattern;
23 :
24 548 : if (root_uri) {
25 548 : zend_string_release(server->root);
26 548 : server->root = lsp_uri_to_path(root_uri);
27 0 : } else if (root_path) {
28 0 : zend_string_release(server->root);
29 0 : server->root = zend_string_copy(root_path);
30 : }
31 :
32 : /* Multi-root: every workspace folder is indexed; the first folder (or
33 : * rootUri) anchors the index cache location. */
34 548 : lsp_workspace_parse_folders(server, params);
35 :
36 548 : lsp_resolve_analyzers(server);
37 548 : lsp_build_project_index(server);
38 548 : lsp_schedule_workspace_analyzers(server);
39 :
40 548 : array_init(return_value);
41 548 : array_init(&capabilities);
42 :
43 548 : array_init(&sync);
44 548 : add_assoc_bool(&sync, "openClose", true);
45 548 : add_assoc_long(&sync, "change", 1);
46 548 : array_init(&save);
47 548 : add_assoc_bool(&save, "includeText", true);
48 548 : add_assoc_zval(&sync, "save", &save);
49 548 : add_assoc_zval(&capabilities, "textDocumentSync", &sync);
50 :
51 548 : array_init(&completion);
52 548 : add_assoc_bool(&completion, "resolveProvider", false);
53 548 : array_init(&triggers);
54 548 : add_next_index_string(&triggers, "$");
55 548 : add_next_index_string(&triggers, ">");
56 548 : add_next_index_string(&triggers, ":");
57 548 : add_next_index_string(&triggers, "[");
58 548 : add_next_index_string(&triggers, "(");
59 548 : add_next_index_string(&triggers, ",");
60 548 : add_next_index_string(&triggers, " ");
61 548 : add_next_index_string(&triggers, "@");
62 548 : add_next_index_string(&triggers, "-");
63 548 : add_next_index_string(&triggers, "<");
64 548 : add_next_index_string(&triggers, "{");
65 548 : add_next_index_string(&triggers, "\\");
66 548 : add_assoc_zval(&completion, "triggerCharacters", &triggers);
67 548 : add_assoc_zval(&capabilities, "completionProvider", &completion);
68 :
69 548 : add_assoc_string(&capabilities, "positionEncoding", "utf-16");
70 548 : add_assoc_bool(&capabilities, "hoverProvider", true);
71 548 : add_assoc_bool(&capabilities, "definitionProvider", true);
72 548 : add_assoc_bool(&capabilities, "declarationProvider", true);
73 548 : add_assoc_bool(&capabilities, "typeDefinitionProvider", true);
74 548 : add_assoc_bool(&capabilities, "referencesProvider", true);
75 548 : add_assoc_bool(&capabilities, "documentHighlightProvider", true);
76 548 : add_assoc_bool(&capabilities, "implementationProvider", true);
77 548 : add_assoc_bool(&capabilities, "foldingRangeProvider", true);
78 548 : add_assoc_bool(&capabilities, "callHierarchyProvider", true);
79 548 : add_assoc_bool(&capabilities, "typeHierarchyProvider", true);
80 548 : add_assoc_bool(&capabilities, "selectionRangeProvider", true);
81 : {
82 : zval document_link;
83 :
84 548 : array_init(&document_link);
85 548 : add_assoc_bool(&document_link, "resolveProvider", false);
86 548 : add_assoc_zval(&capabilities, "documentLinkProvider", &document_link);
87 : }
88 548 : array_init(&code_lens);
89 548 : add_assoc_bool(&code_lens, "resolveProvider", false);
90 548 : add_assoc_zval(&capabilities, "codeLensProvider", &code_lens);
91 548 : array_init(&code_action);
92 548 : array_init(&code_action_kinds);
93 548 : add_next_index_string(&code_action_kinds, "quickfix");
94 548 : add_next_index_string(&code_action_kinds, "source.organizeImports");
95 548 : add_assoc_zval(&code_action, "codeActionKinds", &code_action_kinds);
96 548 : add_assoc_zval(&capabilities, "codeActionProvider", &code_action);
97 548 : array_init(&rename);
98 548 : add_assoc_bool(&rename, "prepareProvider", true);
99 548 : add_assoc_zval(&capabilities, "renameProvider", &rename);
100 548 : add_assoc_bool(&capabilities, "documentFormattingProvider", server->options.formatting_enabled);
101 548 : add_assoc_bool(&capabilities, "documentRangeFormattingProvider", server->options.formatting_enabled);
102 548 : add_assoc_bool(&capabilities, "inlayHintProvider", true);
103 548 : array_init(&semantic_tokens);
104 548 : lsp_semantic_token_legend(&legend);
105 548 : add_assoc_zval(&semantic_tokens, "legend", &legend);
106 : {
107 : zval semantic_full;
108 :
109 548 : array_init(&semantic_full);
110 548 : add_assoc_bool(&semantic_full, "delta", true);
111 548 : add_assoc_zval(&semantic_tokens, "full", &semantic_full);
112 : }
113 548 : add_assoc_bool(&semantic_tokens, "range", true);
114 548 : add_assoc_zval(&capabilities, "semanticTokensProvider", &semantic_tokens);
115 548 : array_init(&signature);
116 548 : array_init(&signature_triggers);
117 548 : add_next_index_string(&signature_triggers, "(");
118 548 : add_next_index_string(&signature_triggers, ",");
119 548 : add_assoc_zval(&signature, "triggerCharacters", &signature_triggers);
120 548 : add_assoc_zval(&capabilities, "signatureHelpProvider", &signature);
121 548 : add_assoc_bool(&capabilities, "documentSymbolProvider", true);
122 548 : add_assoc_bool(&capabilities, "workspaceSymbolProvider", true);
123 :
124 548 : array_init(&workspace);
125 548 : array_init(&workspace_folders);
126 548 : add_assoc_bool(&workspace_folders, "supported", true);
127 548 : add_assoc_bool(&workspace_folders, "changeNotifications", true);
128 548 : add_assoc_zval(&workspace, "workspaceFolders", &workspace_folders);
129 548 : array_init(&file_operations);
130 548 : array_init(&will_rename);
131 548 : array_init(&filters);
132 548 : array_init(&filter);
133 548 : array_init(&pattern);
134 548 : add_assoc_string(&pattern, "glob", "**/*.php");
135 548 : add_assoc_string(&pattern, "matches", "file");
136 548 : add_assoc_zval(&filter, "pattern", &pattern);
137 548 : add_next_index_zval(&filters, &filter);
138 548 : add_assoc_zval(&will_rename, "filters", &filters);
139 548 : add_assoc_zval(&file_operations, "willRename", &will_rename);
140 548 : add_assoc_zval(&workspace, "fileOperations", &file_operations);
141 548 : add_assoc_zval(&capabilities, "workspace", &workspace);
142 :
143 548 : add_assoc_zval(return_value, "capabilities", &capabilities);
144 :
145 548 : array_init(&server_info);
146 548 : add_assoc_string(&server_info, "name", "PHP CLI LSP Extension");
147 548 : add_assoc_string(&server_info, "version", PHP_LSPARROT_VERSION);
148 548 : add_assoc_zval(return_value, "serverInfo", &server_info);
149 548 : }
150 :
151 536 : static inline void lsp_did_open(lsp_server *server, zval *params)
152 : {
153 : lsp_document *document;
154 : zend_long version;
155 : zend_string *uri, *text;
156 : zval *td;
157 :
158 536 : td = lsp_array_find(params, "textDocument");
159 536 : version = lsp_array_long(td, "version", 0);
160 536 : uri = lsp_array_string(td, "uri");
161 536 : text = lsp_array_string(td, "text");
162 :
163 536 : if (!uri || !text) {
164 0 : return;
165 : }
166 :
167 536 : document = lsp_document_open_or_change(server, uri, version, text);
168 536 : lsp_psalm_ls_document_open(server, document);
169 536 : lsp_publish_document_diagnostics(server, document);
170 : }
171 :
172 24 : static inline void lsp_did_change(lsp_server *server, zval *params)
173 : {
174 : lsp_document *document;
175 : zend_long version;
176 : zend_string *uri, *text;
177 24 : zval *td , *changes = lsp_array_find(params, "contentChanges"), *change = NULL;
178 :
179 24 : td = lsp_array_find(params, "textDocument");
180 24 : version = lsp_array_long(td, "version", 0);
181 24 : uri = lsp_array_string(td, "uri");
182 :
183 : /* Full-document sync is advertised, so every change event carries the
184 : * whole text and the LAST event in the batch is authoritative. A change
185 : * carrying a range would be an incremental event from a non-conforming
186 : * client; treating its fragment as the full document would corrupt the
187 : * buffer, so such events are ignored. */
188 24 : if (changes && Z_TYPE_P(changes) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(changes)) > 0) {
189 24 : change = zend_hash_index_find(Z_ARRVAL_P(changes), zend_hash_num_elements(Z_ARRVAL_P(changes)) - 1);
190 : }
191 :
192 24 : if (change && lsp_array_find(change, "range")) {
193 0 : return;
194 : }
195 :
196 24 : text = lsp_array_string(change, "text");
197 24 : if (!uri || !text) {
198 0 : return;
199 : }
200 :
201 24 : document = lsp_document_open_or_change(server, uri, version, text);
202 24 : lsp_psalm_ls_document_change(server, document);
203 :
204 : /* Coalesce typing bursts: when a newer change for this document is
205 : * already queued (with no request in between), publishing diagnostics
206 : * for this revision would be wasted work. */
207 24 : if (!lsp_protocol_queue_supersedes_change(uri)) {
208 20 : lsp_publish_document_diagnostics(server, document);
209 : }
210 : }
211 :
212 12 : static inline void lsp_did_save(lsp_server *server, zval *params)
213 : {
214 : lsp_document *document;
215 12 : zend_string *uri, *text = lsp_array_string(params, "text");
216 : zval *td, *existing;
217 :
218 12 : td = lsp_array_find(params, "textDocument");
219 12 : uri = lsp_array_string(td, "uri");
220 :
221 12 : if (!uri) {
222 0 : return;
223 : }
224 :
225 12 : existing = zend_hash_find(&server->documents, uri);
226 12 : if (!existing) {
227 0 : return;
228 : }
229 :
230 12 : document = (lsp_document *) Z_PTR_P(existing);
231 12 : if (text) {
232 12 : zend_string_release(document->text);
233 12 : document->text = zend_string_copy(text);
234 : }
235 :
236 12 : lsp_document_analyze(document);
237 : /* Saving is the natural refresh point for the version-less analyzer type
238 : * cache entries of this document. */
239 12 : lsp_server_evict_document_caches(server, uri, true);
240 : /* Drop only the member-cache entries built from the saved file; ancestor
241 : * entries re-validate themselves through version/mtime freshness checks. */
242 12 : lsp_member_cache_invalidate_path(server, document->path);
243 12 : lsp_index_refresh_file(server, document->path);
244 12 : lsp_psalm_ls_document_save(server, document);
245 12 : lsp_reschedule_project_analyzers(server, document);
246 12 : lsp_publish_document_diagnostics(server, document);
247 : }
248 :
249 4 : static inline void lsp_did_change_watched_files(lsp_server *server, zval *params)
250 : {
251 : zend_string *uri, *path;
252 4 : zval *changes = lsp_array_find(params, "changes"), *change;
253 :
254 4 : if (!changes || Z_TYPE_P(changes) != IS_ARRAY) {
255 0 : zend_hash_clean(&server->member_cache);
256 :
257 0 : return;
258 : }
259 :
260 8 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(changes), change) {
261 4 : uri = lsp_array_string(change, "uri");
262 4 : if (!uri) {
263 0 : continue;
264 : }
265 :
266 4 : path = lsp_uri_to_path(uri);
267 4 : lsp_member_cache_invalidate_path(server, path);
268 4 : lsp_index_refresh_file(server, path);
269 4 : zend_string_release(path);
270 : } ZEND_HASH_FOREACH_END();
271 : }
272 :
273 4 : static inline void lsp_did_close(lsp_server *server, zval *params)
274 : {
275 : zval *td;
276 : zend_string *uri;
277 :
278 4 : td = lsp_array_find(params, "textDocument");
279 4 : uri = lsp_array_string(td, "uri");
280 :
281 4 : if (!uri) {
282 0 : return;
283 : }
284 :
285 4 : lsp_psalm_ls_document_close(server, uri);
286 4 : lsp_server_evict_document_caches(server, uri, true);
287 4 : zend_hash_del(&server->documents, uri);
288 4 : lsp_publish_empty_diagnostics(uri);
289 : }
290 :
291 796 : static inline void lsp_document_request(lsp_server *server, zval *params, void (*handler)(lsp_server *, zval *, lsp_document *, zval *), zval *return_value)
292 : {
293 : lsp_document *document;
294 : zend_string *uri;
295 796 : zval *td, *position = lsp_array_find(params, "position");
296 :
297 796 : td = lsp_array_find(params, "textDocument");
298 796 : uri = lsp_array_string(td, "uri");
299 :
300 796 : if (!uri) {
301 0 : ZVAL_NULL(return_value);
302 :
303 0 : return;
304 : }
305 :
306 796 : document = lsp_document_from_uri(server, uri);
307 796 : handler(server, return_value, document, position);
308 : }
309 :
310 : static inline void lsp_document_request_no_server(lsp_server *server, zval *params, void (*handler)(zval *, lsp_document *, zval *), zval *return_value)
311 : {
312 : lsp_document *document;
313 : zend_string *uri;
314 : zval *td, *position = lsp_array_find(params, "position");
315 :
316 : td = lsp_array_find(params, "textDocument");
317 : uri = lsp_array_string(td, "uri");
318 :
319 : if (!uri) {
320 : ZVAL_NULL(return_value);
321 :
322 : return;
323 : }
324 :
325 : document = lsp_document_from_uri(server, uri);
326 : handler(return_value, document, position);
327 : }
328 :
329 20 : static inline void lsp_document_request_no_position(lsp_server *server, zval *params, void (*handler)(lsp_server *, zval *, lsp_document *), zval *return_value)
330 : {
331 : lsp_document *document;
332 : zend_string *uri;
333 : zval *td;
334 :
335 20 : td = lsp_array_find(params, "textDocument");
336 20 : uri = lsp_array_string(td, "uri");
337 :
338 20 : if (!uri) {
339 0 : ZVAL_NULL(return_value);
340 :
341 0 : return;
342 : }
343 :
344 20 : document = lsp_document_from_uri(server, uri);
345 20 : handler(server, return_value, document);
346 : }
347 :
348 64 : static inline void lsp_document_request_params(lsp_server *server, zval *params, void (*handler)(lsp_server *, zval *, lsp_document *, zval *), zval *return_value)
349 : {
350 : lsp_document *document;
351 : zend_string *uri;
352 : zval *td;
353 :
354 64 : td = lsp_array_find(params, "textDocument");
355 64 : uri = lsp_array_string(td, "uri");
356 :
357 64 : if (!uri) {
358 0 : ZVAL_NULL(return_value);
359 :
360 0 : return;
361 : }
362 :
363 64 : document = lsp_document_from_uri(server, uri);
364 64 : handler(server, return_value, document, params);
365 : }
366 :
367 16 : static inline void lsp_document_request_no_server_no_position(lsp_server *server, zval *params, void (*handler)(zval *, lsp_document *), zval *return_value)
368 : {
369 : lsp_document *document;
370 : zend_string *uri;
371 : zval *td;
372 :
373 16 : td = lsp_array_find(params, "textDocument");
374 16 : uri = lsp_array_string(td, "uri");
375 :
376 16 : if (!uri) {
377 0 : ZVAL_NULL(return_value);
378 :
379 0 : return;
380 : }
381 :
382 16 : document = lsp_document_from_uri(server, uri);
383 16 : handler(return_value, document);
384 : }
385 :
386 864 : static inline zend_string *lsp_path_to_uri(zend_string *path)
387 : {
388 864 : return lsp_uri_from_path(path);
389 : }
390 :
391 864 : static inline void lsp_default_range(zval *range)
392 : {
393 : zval start, end;
394 :
395 864 : array_init(range);
396 864 : array_init(&start);
397 864 : add_assoc_long(&start, "line", 0);
398 864 : add_assoc_long(&start, "character", 0);
399 864 : array_init(&end);
400 864 : add_assoc_long(&end, "line", 0);
401 864 : add_assoc_long(&end, "character", 1);
402 864 : add_assoc_zval(range, "start", &start);
403 864 : add_assoc_zval(range, "end", &end);
404 864 : }
405 :
406 : #define LSP_SYMBOL_FRAME_MAX 16
407 :
408 : typedef struct _lsp_document_symbol_frame {
409 : zval item; /* DocumentSymbol under construction */
410 : zval children;
411 : zend_long body_depth;
412 : size_t start_offset;
413 : bool is_enum;
414 : bool is_class_like;
415 : } lsp_document_symbol_frame;
416 :
417 4 : static inline void lsp_document_symbol_leaf(zval *target, lsp_document *document, zend_string *name, zend_long kind, size_t start_offset, size_t end_offset, size_t name_offset, size_t name_length)
418 : {
419 : zval item, range, selection_range;
420 :
421 4 : array_init(&item);
422 4 : add_assoc_str(&item, "name", zend_string_copy(name));
423 4 : add_assoc_long(&item, "kind", kind);
424 4 : lsp_range_from_offsets(document->text, start_offset, end_offset, &range);
425 4 : add_assoc_zval(&item, "range", &range);
426 4 : lsp_range_from_offsets(document->text, name_offset, name_offset + name_length, &selection_range);
427 4 : add_assoc_zval(&item, "selectionRange", &selection_range);
428 4 : add_next_index_zval(target, &item);
429 4 : }
430 :
431 32 : static inline void lsp_document_symbol_frame_open(lsp_document_symbol_frame *frame, lsp_document *document, zend_string *name, zend_long kind, size_t start_offset, size_t name_offset, size_t name_length, zend_long body_depth, bool is_enum, bool is_class_like)
432 : {
433 : zval selection_range;
434 :
435 32 : array_init(&frame->item);
436 32 : add_assoc_str(&frame->item, "name", zend_string_copy(name));
437 32 : add_assoc_long(&frame->item, "kind", kind);
438 32 : lsp_range_from_offsets(document->text, name_offset, name_offset + name_length, &selection_range);
439 32 : add_assoc_zval(&frame->item, "selectionRange", &selection_range);
440 32 : array_init(&frame->children);
441 32 : frame->body_depth = body_depth;
442 32 : frame->start_offset = start_offset;
443 32 : frame->is_enum = is_enum;
444 32 : frame->is_class_like = is_class_like;
445 32 : }
446 :
447 32 : static inline void lsp_document_symbol_frame_close(lsp_document_symbol_frame *frame, lsp_document *document, zval *target, size_t end_offset)
448 : {
449 : zval range;
450 :
451 32 : lsp_range_from_offsets(document->text, frame->start_offset, end_offset, &range);
452 32 : add_assoc_zval(&frame->item, "range", &range);
453 :
454 32 : if (zend_hash_num_elements(Z_ARRVAL(frame->children)) > 0) {
455 12 : add_assoc_zval(&frame->item, "children", &frame->children);
456 : } else {
457 20 : zval_ptr_dtor(&frame->children);
458 : }
459 :
460 32 : add_next_index_zval(target, &frame->item);
461 32 : }
462 :
463 : /* Hierarchical DocumentSymbol tree from the token stream: classes own their
464 : * methods and enum cases as children, with real ranges (declaration through
465 : * closing brace) and name-based selection ranges. Token-driven so it also
466 : * works on documents that do not currently parse. */
467 : static inline void lsp_document_symbols_uncached(zval *return_value, lsp_document *document);
468 :
469 : /* Text pointer identity keys the cache the same way the token cache does:
470 : * didChange swaps document->text, naturally invalidating the memo. */
471 16 : static inline void lsp_document_symbols(zval *return_value, lsp_document *document)
472 : {
473 16 : if (document->outline_cache_text == document->text && Z_TYPE(document->outline_cache) == IS_ARRAY) {
474 4 : ZVAL_COPY(return_value, &document->outline_cache);
475 :
476 4 : return;
477 : }
478 :
479 12 : lsp_document_symbols_uncached(return_value, document);
480 :
481 12 : if (document->outline_cache_text) {
482 0 : zend_string_release(document->outline_cache_text);
483 0 : zval_ptr_dtor(&document->outline_cache);
484 : }
485 :
486 12 : document->outline_cache_text = zend_string_copy(document->text);
487 12 : ZVAL_COPY(&document->outline_cache, return_value);
488 : }
489 :
490 12 : static inline void lsp_document_symbols_uncached(zval *return_value, lsp_document *document)
491 : {
492 : lsp_document_symbol_frame frames[LSP_SYMBOL_FRAME_MAX];
493 12 : zend_long depth = 0, kind, pending_kind = 0;
494 12 : zend_string *label, *pending_name = NULL;
495 : zval *tokens_zv, *token, *name_token;
496 : zval *target;
497 : HashTable *tokens;
498 : uint32_t i, count, name_index;
499 12 : int frame_count = 0;
500 12 : size_t token_offset, pending_start = 0, pending_name_offset = 0, pending_name_length = 0;
501 12 : bool pending_is_class = false, pending_active = false, previous_is_new = false;
502 :
503 12 : array_init(return_value);
504 12 : tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
505 12 : if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
506 0 : return;
507 : }
508 :
509 12 : tokens = Z_ARRVAL_P(tokens_zv);
510 12 : count = zend_hash_num_elements(tokens);
511 808 : for (i = 0; i < count; i++) {
512 796 : token = zend_hash_index_find(tokens, i);
513 796 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
514 0 : continue;
515 : }
516 :
517 796 : if (lsp_token_name_equals(token, "T_WHITESPACE") || lsp_token_name_equals(token, "T_COMMENT") || lsp_token_name_equals(token, "T_DOC_COMMENT")) {
518 296 : continue;
519 : }
520 :
521 500 : token_offset = (size_t) lsp_token_long(token, "offset", 0);
522 :
523 500 : if (lsp_token_is_char(token, '{') || lsp_token_name_equals(token, "T_CURLY_OPEN") || lsp_token_name_equals(token, "T_DOLLAR_OPEN_CURLY_BRACES")) {
524 32 : depth++;
525 32 : if (pending_active && frame_count < LSP_SYMBOL_FRAME_MAX) {
526 32 : lsp_document_symbol_frame_open(&frames[frame_count], document, pending_name, pending_kind, pending_start, pending_name_offset, pending_name_length, depth, pending_is_class && pending_kind == 10, pending_is_class);
527 32 : frame_count++;
528 32 : pending_active = false;
529 32 : pending_name = NULL;
530 : }
531 32 : previous_is_new = false;
532 32 : continue;
533 : }
534 :
535 468 : if (lsp_token_is_char(token, '}')) {
536 32 : if (frame_count > 0 && frames[frame_count - 1].body_depth == depth) {
537 32 : frame_count--;
538 32 : target = frame_count > 0 ? &frames[frame_count - 1].children : return_value;
539 32 : lsp_document_symbol_frame_close(&frames[frame_count], document, target, token_offset + 1);
540 : }
541 32 : if (depth > 0) {
542 32 : depth--;
543 : }
544 32 : previous_is_new = false;
545 32 : continue;
546 : }
547 :
548 436 : if (pending_active && lsp_token_is_char(token, ';')) {
549 : /* Bodyless declaration (abstract/interface method). */
550 0 : target = frame_count > 0 ? &frames[frame_count - 1].children : return_value;
551 0 : lsp_document_symbol_leaf(target, document, pending_name, pending_kind, pending_start, token_offset + 1, pending_name_offset, pending_name_length);
552 0 : pending_active = false;
553 0 : pending_name = NULL;
554 0 : continue;
555 : }
556 :
557 436 : if (lsp_token_is_class_like(token)) {
558 16 : if (previous_is_new) {
559 : /* Anonymous class: no symbol, braces tracked by depth. */
560 0 : previous_is_new = false;
561 0 : continue;
562 : }
563 :
564 16 : label = lsp_next_string_token(tokens, i + 1);
565 16 : if (label) {
566 16 : kind = lsp_token_name_equals(token, "T_INTERFACE") ? 11 : (lsp_token_name_equals(token, "T_ENUM") ? 10 : 5);
567 16 : name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
568 16 : pending_active = true;
569 16 : pending_is_class = true;
570 16 : pending_kind = kind;
571 16 : pending_name = label;
572 16 : pending_start = token_offset;
573 16 : pending_name_offset = name_token ? (size_t) lsp_token_long(name_token, "offset", 0) : token_offset;
574 16 : pending_name_length = ZSTR_LEN(label);
575 : }
576 16 : previous_is_new = false;
577 16 : continue;
578 : }
579 :
580 420 : if (lsp_token_name_equals(token, "T_FUNCTION")) {
581 16 : name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
582 16 : label = name_token ? lsp_token_string(name_token, "text") : NULL;
583 16 : if (label) {
584 8 : kind = frame_count > 0 && frames[frame_count - 1].body_depth == depth && frames[frame_count - 1].is_class_like
585 8 : ? (zend_string_equals_literal(label, "__construct") ? 9 : 6)
586 24 : : 12
587 : ;
588 16 : pending_active = true;
589 16 : pending_is_class = false;
590 16 : pending_kind = kind;
591 16 : pending_name = label;
592 16 : pending_start = token_offset;
593 16 : pending_name_offset = (size_t) lsp_token_long(name_token, "offset", 0);
594 16 : pending_name_length = ZSTR_LEN(label);
595 : }
596 16 : previous_is_new = false;
597 16 : continue;
598 : }
599 :
600 404 : if (lsp_token_name_equals(token, "T_CASE") &&
601 4 : frame_count > 0 &&
602 4 : frames[frame_count - 1].is_enum &&
603 4 : frames[frame_count - 1].body_depth == depth
604 : ) {
605 4 : label = lsp_next_string_token(tokens, i + 1);
606 4 : if (label) {
607 4 : name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
608 6 : lsp_document_symbol_leaf(&frames[frame_count - 1].children, document, label, 22,
609 : token_offset,
610 4 : (name_token ? (size_t) lsp_token_long(name_token, "offset", 0) : token_offset) + ZSTR_LEN(label),
611 4 : name_token ? (size_t) lsp_token_long(name_token, "offset", 0) : token_offset,
612 : ZSTR_LEN(label));
613 : }
614 4 : previous_is_new = false;
615 4 : continue;
616 : }
617 :
618 400 : previous_is_new = lsp_token_name_equals(token, "T_NEW");
619 : }
620 :
621 : /* Close frames left open by an unfinished document. */
622 12 : while (frame_count > 0) {
623 0 : frame_count--;
624 0 : target = frame_count > 0 ? &frames[frame_count - 1].children : return_value;
625 0 : lsp_document_symbol_frame_close(&frames[frame_count], document, target, ZSTR_LEN(document->text));
626 : }
627 : }
628 :
629 1684 : static inline bool lsp_matches_query(const char *value, size_t value_length, zend_string *query)
630 : {
631 : /* Case-insensitive subsequence match (every substring match is also a
632 : * subsequence match, so one pass decides). The query arrives pre-lowered
633 : * by the caller so the per-entry cost is a single tolower per byte. */
634 : const char *query_value;
635 1684 : size_t i, query_length, query_offset = 0;
636 :
637 1684 : if (!query || ZSTR_LEN(query) == 0) {
638 816 : return true;
639 : }
640 :
641 868 : query_value = ZSTR_VAL(query);
642 868 : query_length = ZSTR_LEN(query);
643 868 : if (query_length > value_length) {
644 800 : return false;
645 : }
646 :
647 1236 : for (i = 0; i < value_length && query_offset < query_length; i++) {
648 1168 : if (tolower((unsigned char) value[i]) == (unsigned char) query_value[query_offset]) {
649 364 : query_offset++;
650 : }
651 : }
652 :
653 68 : return query_offset == query_length;
654 : }
655 :
656 864 : static inline void lsp_add_workspace_symbol(zval *items, zend_string *name, zend_long kind, zend_string *uri)
657 : {
658 : zval item, location, range;
659 :
660 864 : array_init(&item);
661 864 : add_assoc_str(&item, "name", zend_string_copy(name));
662 864 : add_assoc_long(&item, "kind", kind);
663 864 : array_init(&location);
664 864 : add_assoc_str(&location, "uri", zend_string_copy(uri));
665 864 : lsp_default_range(&range);
666 864 : add_assoc_zval(&location, "range", &range);
667 864 : add_assoc_zval(&item, "location", &location);
668 864 : add_next_index_zval(items, &item);
669 864 : }
670 :
671 : /* Editors re-issue workspace/symbol on every keystroke of the picker, so the
672 : * scan is capped and prioritized: one pass over the index emits project
673 : * symbols directly (stopping at the cap) while vendor matches queue in a side
674 : * list that only tops up remaining slots. */
675 : #define LSP_WORKSPACE_SYMBOL_LIMIT 256
676 :
677 864 : static inline void lsp_add_workspace_symbol_entry(lsp_symbol_index *region, const lsp_symbol_entry *entry, zval *items)
678 : {
679 : zend_string *name, *uri, *path_string;
680 : const char *fqcn, *path;
681 :
682 864 : fqcn = lsp_symbol_entry_fqcn(region, entry);
683 864 : path = lsp_symbol_entry_path(region, entry);
684 864 : name = zend_string_init(fqcn, entry->fqcn_length, 0);
685 864 : path_string = zend_string_init(path, entry->path_length, 0);
686 864 : uri = lsp_path_to_uri(path_string);
687 864 : lsp_add_workspace_symbol(items, name, lsp_symbol_workspace_kind((char) entry->kind), uri);
688 864 : zend_string_release(uri);
689 864 : zend_string_release(path_string);
690 864 : zend_string_release(name);
691 864 : }
692 :
693 48 : static inline void lsp_add_workspace_symbols_from_index(lsp_server *server, zval *items, zend_string *query)
694 : {
695 : const lsp_symbol_entry *entry;
696 48 : lsp_symbol_index *region = &server->symbol_index;
697 : const char *fqcn;
698 : uint32_t i, project_count, vendor_count, vendor_take;
699 : uint32_t *vendor_matches;
700 :
701 48 : lsp_symbol_index_table_ensure(region);
702 :
703 48 : vendor_matches = emalloc(sizeof(uint32_t) * LSP_WORKSPACE_SYMBOL_LIMIT);
704 48 : project_count = 0;
705 48 : vendor_count = 0;
706 :
707 1732 : for (i = 0; i < region->entry_count && project_count < LSP_WORKSPACE_SYMBOL_LIMIT; i++) {
708 1684 : entry = ®ion->entries[i];
709 1684 : if ((entry->flags & LSP_SYMBOL_ENTRY_DELETED) != 0) {
710 0 : continue;
711 : }
712 :
713 1684 : if ((entry->flags & LSP_SYMBOL_ENTRY_VENDOR) != 0) {
714 4 : if (vendor_count >= LSP_WORKSPACE_SYMBOL_LIMIT) {
715 0 : continue;
716 : }
717 : }
718 :
719 1684 : fqcn = lsp_symbol_entry_fqcn(region, entry);
720 1684 : if (!lsp_matches_query(fqcn, entry->fqcn_length, query)) {
721 820 : continue;
722 : }
723 :
724 864 : if ((entry->flags & LSP_SYMBOL_ENTRY_VENDOR) != 0) {
725 4 : vendor_matches[vendor_count++] = i;
726 : } else {
727 860 : lsp_add_workspace_symbol_entry(region, entry, items);
728 860 : project_count++;
729 : }
730 : }
731 :
732 48 : vendor_take = LSP_WORKSPACE_SYMBOL_LIMIT - project_count;
733 48 : if (vendor_take > vendor_count) {
734 48 : vendor_take = vendor_count;
735 : }
736 52 : for (i = 0; i < vendor_take; i++) {
737 4 : lsp_add_workspace_symbol_entry(region, ®ion->entries[vendor_matches[i]], items);
738 : }
739 :
740 48 : efree(vendor_matches);
741 48 : }
742 :
743 48 : static inline void lsp_workspace_symbols(lsp_server *server, zval *params, zval *return_value)
744 : {
745 : zend_string *query, *lowered;
746 :
747 48 : query = lsp_array_string(params, "query");
748 48 : lowered = query ? zend_string_tolower(query) : NULL;
749 :
750 48 : array_init(return_value);
751 :
752 48 : lsp_index_join_worker(server);
753 48 : lsp_add_workspace_symbols_from_index(server, return_value, lowered);
754 :
755 48 : if (lowered) {
756 48 : zend_string_release(lowered);
757 : }
758 48 : }
759 :
760 : /* ----------------------------------------------------------------------
761 : * textDocument/selectionRange: expand-selection chains built from the word
762 : * under the cursor and the enclosing bracket pairs (content first, then the
763 : * pair including its delimiters), ending at the whole document.
764 : * ---------------------------------------------------------------------- */
765 :
766 : #define LSP_SELECTION_STACK_MAX 128
767 :
768 28 : static inline void lsp_selection_push_level(zval *chain, zend_string *text, size_t start, size_t end, size_t *previous_start, size_t *previous_end)
769 : {
770 : zval level, range;
771 :
772 28 : if (start == *previous_start && end == *previous_end) {
773 0 : return;
774 : }
775 :
776 : /* Chains must strictly widen. */
777 28 : if (start > *previous_start || end < *previous_end) {
778 0 : return;
779 : }
780 :
781 28 : array_init(&level);
782 28 : lsp_range_from_offsets(text, start, end, &range);
783 28 : add_assoc_zval(&level, "range", &range);
784 28 : add_next_index_zval(chain, &level);
785 28 : *previous_start = start;
786 28 : *previous_end = end;
787 : }
788 :
789 4 : static inline void lsp_selection_range_for_offset(lsp_document *document, size_t offset, zval *result)
790 : {
791 : zval *tokens_zv, *token, chain, *level, *parent_target, parent_copy;
792 : zend_string *word;
793 : HashTable *tokens;
794 : size_t open_stack[LSP_SELECTION_STACK_MAX];
795 : size_t token_offset, open_offset, previous_start, previous_end, word_start, word_end;
796 : zend_long id;
797 4 : uint32_t i, count, depth = 0, chain_count;
798 :
799 4 : array_init(&chain);
800 4 : previous_start = SIZE_MAX;
801 4 : previous_end = 0;
802 :
803 4 : word = lsp_word_at(document->text, offset);
804 4 : if (ZSTR_LEN(word) > 0) {
805 4 : word_start = offset;
806 20 : while (word_start > 0 && (lsp_doc_is_identifier_char(ZSTR_VAL(document->text)[word_start - 1]) || ZSTR_VAL(document->text)[word_start - 1] == '$')) {
807 16 : word_start--;
808 : }
809 4 : word_end = word_start + ZSTR_LEN(word);
810 4 : previous_start = word_start;
811 4 : previous_end = word_end;
812 :
813 : {
814 : zval level_zv, range;
815 :
816 4 : array_init(&level_zv);
817 4 : lsp_range_from_offsets(document->text, word_start, word_end, &range);
818 4 : add_assoc_zval(&level_zv, "range", &range);
819 4 : add_next_index_zval(&chain, &level_zv);
820 : }
821 : }
822 4 : zend_string_release(word);
823 :
824 4 : tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
825 4 : if (tokens_zv && Z_TYPE_P(tokens_zv) == IS_ARRAY) {
826 4 : tokens = Z_ARRVAL_P(tokens_zv);
827 4 : count = zend_hash_num_elements(tokens);
828 228 : for (i = 0; i < count; i++) {
829 224 : token = zend_hash_index_find(tokens, i);
830 224 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
831 0 : continue;
832 : }
833 :
834 224 : id = lsp_token_long(token, "id", 0);
835 224 : token_offset = (size_t) lsp_token_long(token, "offset", 0);
836 :
837 224 : if (id == '(' || id == '[' || id == '{' || id == T_CURLY_OPEN || id == T_DOLLAR_OPEN_CURLY_BRACES || id == T_ATTRIBUTE) {
838 16 : if (depth < LSP_SELECTION_STACK_MAX) {
839 16 : open_stack[depth] = token_offset;
840 : }
841 16 : depth++;
842 16 : continue;
843 : }
844 :
845 208 : if (id == ')' || id == ']' || id == '}') {
846 16 : if (depth == 0) {
847 0 : continue;
848 : }
849 16 : depth--;
850 16 : if (depth >= LSP_SELECTION_STACK_MAX) {
851 0 : continue;
852 : }
853 :
854 16 : open_offset = open_stack[depth];
855 : /* Closing pairs surface innermost-first for offsets they
856 : * enclose, exactly the chain order needed. */
857 16 : if (open_offset < offset && offset <= token_offset) {
858 12 : lsp_selection_push_level(&chain, document->text, open_offset + 1, token_offset, &previous_start, &previous_end);
859 12 : lsp_selection_push_level(&chain, document->text, open_offset, token_offset + 1, &previous_start, &previous_end);
860 : }
861 : }
862 : }
863 : }
864 :
865 4 : lsp_selection_push_level(&chain, document->text, 0, ZSTR_LEN(document->text), &previous_start, &previous_end);
866 :
867 : /* Fold the flat chain into the nested {range, parent} shape. */
868 4 : chain_count = zend_hash_num_elements(Z_ARRVAL(chain));
869 4 : if (chain_count == 0) {
870 : zval range, level_zv;
871 :
872 0 : array_init(result);
873 0 : lsp_range_from_offsets(document->text, offset, offset, &range);
874 0 : add_assoc_zval(result, "range", &range);
875 0 : zval_ptr_dtor(&chain);
876 : (void) level_zv;
877 :
878 0 : return;
879 : }
880 :
881 : /* Build from the outermost inward: parent links point outward. */
882 4 : ZVAL_UNDEF(&parent_copy);
883 4 : parent_target = NULL;
884 36 : for (i = chain_count; i > 0; i--) {
885 32 : level = zend_hash_index_find(Z_ARRVAL(chain), i - 1);
886 32 : if (!level || Z_TYPE_P(level) != IS_ARRAY) {
887 0 : continue;
888 : }
889 :
890 32 : if (!Z_ISUNDEF(parent_copy)) {
891 28 : add_assoc_zval(level, "parent", &parent_copy);
892 : }
893 :
894 32 : ZVAL_COPY(&parent_copy, level);
895 : }
896 :
897 : (void) parent_target;
898 :
899 4 : if (!Z_ISUNDEF(parent_copy)) {
900 4 : ZVAL_COPY(result, &parent_copy);
901 4 : zval_ptr_dtor(&parent_copy);
902 : } else {
903 0 : array_init(result);
904 : }
905 :
906 4 : zval_ptr_dtor(&chain);
907 : }
908 :
909 4 : extern void lsp_lsparrot_selection_range(lsp_server *server, zval *return_value, lsp_document *document, zval *params)
910 : {
911 : zval *positions, *position, entry;
912 : zend_long line, character;
913 : size_t offset;
914 :
915 : (void) server;
916 :
917 4 : array_init(return_value);
918 :
919 4 : positions = lsp_array_find(params, "positions");
920 4 : if (!positions || Z_TYPE_P(positions) != IS_ARRAY) {
921 0 : return;
922 : }
923 :
924 8 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(positions), position) {
925 4 : lsp_position_from_zval(position, &line, &character);
926 4 : offset = lsp_offset_at(document->text, line, character);
927 4 : lsp_selection_range_for_offset(document, offset, &entry);
928 4 : add_next_index_zval(return_value, &entry);
929 : } ZEND_HASH_FOREACH_END();
930 : }
931 :
932 : /* ----------------------------------------------------------------------
933 : * textDocument/documentLink: clickable require/include string arguments.
934 : * `__DIR__ . '...'` and plain relative/absolute literals resolve against
935 : * the document's directory; links only surface for files that exist.
936 : * ---------------------------------------------------------------------- */
937 :
938 4 : static inline zend_string *lsp_document_link_dirname(zend_string *path)
939 : {
940 4 : const char *value = ZSTR_VAL(path), *slash = strrchr(value, '/');
941 :
942 4 : if (!slash || slash == value) {
943 0 : return zend_string_init("/", 1, 0);
944 : }
945 :
946 4 : return zend_string_init(value, (size_t) (slash - value), 0);
947 : }
948 :
949 4 : extern void lsp_lsparrot_document_link(lsp_server *server, zval *return_value, lsp_document *document)
950 : {
951 : zval *tokens_zv, *token, *string_token, link, range;
952 : HashTable *tokens;
953 : zend_string *literal, *base_dir, *resolved, *uri;
954 : const char *inner;
955 : size_t inner_length, string_offset;
956 : zend_long id;
957 : uint32_t i, j, count;
958 : bool has_dir_prefix;
959 :
960 : (void) server;
961 :
962 4 : array_init(return_value);
963 :
964 4 : tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
965 4 : if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY || !document->path) {
966 0 : return;
967 : }
968 :
969 4 : tokens = Z_ARRVAL_P(tokens_zv);
970 4 : count = zend_hash_num_elements(tokens);
971 4 : base_dir = lsp_document_link_dirname(document->path);
972 :
973 228 : for (i = 0; i < count; i++) {
974 224 : token = zend_hash_index_find(tokens, i);
975 224 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
976 0 : continue;
977 : }
978 :
979 224 : id = lsp_token_long(token, "id", 0);
980 224 : if (id != T_REQUIRE && id != T_REQUIRE_ONCE && id != T_INCLUDE && id != T_INCLUDE_ONCE) {
981 212 : continue;
982 : }
983 :
984 : /* Accept: [(] [__DIR__ .] 'literal' */
985 12 : string_token = NULL;
986 12 : has_dir_prefix = false;
987 40 : for (j = i + 1; j < count && j < i + 8; j++) {
988 40 : zval *candidate = zend_hash_index_find(tokens, j);
989 : zend_long candidate_id;
990 :
991 40 : if (!candidate || Z_TYPE_P(candidate) != IS_ARRAY) {
992 0 : continue;
993 : }
994 :
995 40 : candidate_id = lsp_token_long(candidate, "id", 0);
996 40 : if (candidate_id == T_WHITESPACE || candidate_id == (zend_long) '(' || candidate_id == (zend_long) '.') {
997 24 : continue;
998 : }
999 :
1000 16 : if (candidate_id == T_DIR) {
1001 4 : has_dir_prefix = true;
1002 4 : continue;
1003 : }
1004 :
1005 12 : if (candidate_id == T_CONSTANT_ENCAPSED_STRING) {
1006 12 : string_token = candidate;
1007 : }
1008 :
1009 12 : break;
1010 : }
1011 :
1012 12 : if (!string_token) {
1013 0 : continue;
1014 : }
1015 :
1016 12 : literal = lsp_token_string(string_token, "text");
1017 12 : if (!literal || ZSTR_LEN(literal) < 3) {
1018 0 : continue;
1019 : }
1020 :
1021 12 : inner = ZSTR_VAL(literal) + 1;
1022 12 : inner_length = ZSTR_LEN(literal) - 2;
1023 12 : if (inner_length == 0 || memchr(inner, '\\', inner_length) || memchr(inner, '$', inner_length)) {
1024 0 : continue;
1025 : }
1026 :
1027 12 : if (has_dir_prefix || inner[0] != '/') {
1028 12 : resolved = strpprintf(0, "%s%s%.*s", ZSTR_VAL(base_dir), inner[0] == '/' ? "" : "/", (int) inner_length, inner);
1029 : } else {
1030 0 : resolved = zend_string_init(inner, inner_length, 0);
1031 : }
1032 :
1033 12 : if (!lsp_is_regular_file(resolved)) {
1034 4 : zend_string_release(resolved);
1035 4 : continue;
1036 : }
1037 :
1038 8 : string_offset = (size_t) lsp_token_long(string_token, "offset", 0);
1039 8 : uri = lsp_uri_from_path(resolved);
1040 8 : array_init(&link);
1041 8 : lsp_range_from_offsets(document->text, string_offset, string_offset + ZSTR_LEN(literal), &range);
1042 8 : add_assoc_zval(&link, "range", &range);
1043 8 : add_assoc_str(&link, "target", uri);
1044 8 : add_next_index_zval(return_value, &link);
1045 8 : zend_string_release(resolved);
1046 : }
1047 :
1048 4 : zend_string_release(base_dir);
1049 : }
1050 :
1051 : #define LSP_FOLDING_STACK_MAX 128
1052 :
1053 24 : static inline void lsp_folding_add_range(zval *items, zend_long start_line, zend_long end_line, const char *kind)
1054 : {
1055 : zval range;
1056 :
1057 24 : if (end_line <= start_line) {
1058 12 : return;
1059 : }
1060 :
1061 12 : array_init(&range);
1062 12 : add_assoc_long(&range, "startLine", start_line);
1063 12 : add_assoc_long(&range, "endLine", end_line);
1064 12 : if (kind) {
1065 4 : add_assoc_string(&range, "kind", kind);
1066 : }
1067 12 : add_next_index_zval(items, &range);
1068 : }
1069 :
1070 4 : static inline zend_long lsp_folding_token_newlines(zend_string *text)
1071 : {
1072 4 : const char *p = ZSTR_VAL(text), *end = p + ZSTR_LEN(text);
1073 4 : zend_long count = 0;
1074 :
1075 16 : while (p < end) {
1076 16 : p = memchr(p, '\n', (size_t) (end - p));
1077 16 : if (!p) {
1078 4 : break;
1079 : }
1080 12 : count++;
1081 12 : p++;
1082 : }
1083 :
1084 4 : return count;
1085 : }
1086 :
1087 : /* Brace/bracket/paren blocks, multi-line doc comments, heredocs, and grouped
1088 : * use statements fold. The closing-delimiter line stays visible (endLine =
1089 : * closer line - 1), matching how VSCode folds braces; comments collapse onto
1090 : * their first line. */
1091 4 : extern void lsp_lsparrot_folding_range(lsp_server *server, zval *return_value, lsp_document *document)
1092 : {
1093 : zval *tokens_zv, *token;
1094 : zend_string *token_text;
1095 : HashTable *tokens;
1096 : zend_long open_lines[LSP_FOLDING_STACK_MAX];
1097 : zend_long token_line, newlines, heredoc_start_line;
1098 : uint32_t i, count, depth;
1099 : zend_long id;
1100 :
1101 : (void) server;
1102 :
1103 4 : array_init(return_value);
1104 :
1105 4 : tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
1106 4 : if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
1107 0 : return;
1108 : }
1109 :
1110 4 : tokens = Z_ARRVAL_P(tokens_zv);
1111 4 : count = zend_hash_num_elements(tokens);
1112 4 : depth = 0;
1113 4 : heredoc_start_line = -1;
1114 :
1115 260 : for (i = 0; i < count; i++) {
1116 256 : token = zend_hash_index_find(tokens, i);
1117 256 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
1118 0 : continue;
1119 : }
1120 :
1121 256 : id = lsp_token_long(token, "id", 0);
1122 256 : token_line = lsp_token_long(token, "line", 1) - 1;
1123 :
1124 256 : if (id == T_COMMENT || id == T_DOC_COMMENT) {
1125 4 : token_text = lsp_token_string(token, "text");
1126 4 : newlines = token_text ? lsp_folding_token_newlines(token_text) : 0;
1127 4 : if (newlines > 0) {
1128 4 : lsp_folding_add_range(return_value, token_line, token_line + newlines, "comment");
1129 : }
1130 4 : continue;
1131 : }
1132 :
1133 252 : if (id == T_START_HEREDOC) {
1134 0 : heredoc_start_line = token_line;
1135 0 : continue;
1136 : }
1137 :
1138 252 : if (id == T_END_HEREDOC) {
1139 0 : if (heredoc_start_line >= 0) {
1140 0 : lsp_folding_add_range(return_value, heredoc_start_line, token_line - 1, NULL);
1141 0 : heredoc_start_line = -1;
1142 : }
1143 0 : continue;
1144 : }
1145 :
1146 252 : if (id == '{' || id == '(' || id == '[' || id == T_ATTRIBUTE || id == T_CURLY_OPEN || id == T_DOLLAR_OPEN_CURLY_BRACES) {
1147 20 : if (depth < LSP_FOLDING_STACK_MAX) {
1148 20 : open_lines[depth] = token_line;
1149 : }
1150 20 : depth++;
1151 20 : continue;
1152 : }
1153 :
1154 232 : if (id == '}' || id == ')' || id == ']') {
1155 20 : if (depth > 0) {
1156 20 : depth--;
1157 20 : if (depth < LSP_FOLDING_STACK_MAX) {
1158 20 : lsp_folding_add_range(return_value, open_lines[depth], token_line - 1, NULL);
1159 : }
1160 : }
1161 20 : continue;
1162 : }
1163 : }
1164 : }
1165 :
1166 2844 : static inline bool lsp_server_handle(lsp_server *server, zend_string *method, zval *params, zval *return_value)
1167 : {
1168 2844 : if (zend_string_equals_literal(method, "initialize")) {
1169 548 : lsp_initialize(server, params, return_value);
1170 :
1171 548 : return true;
1172 : }
1173 :
1174 2296 : if (zend_string_equals_literal(method, "initialized")) {
1175 0 : ZVAL_NULL(return_value);
1176 :
1177 0 : return true;
1178 : }
1179 :
1180 2296 : if (zend_string_equals_literal(method, "lsparrot.php/status")) {
1181 116 : lsp_server_status(server, return_value);
1182 :
1183 116 : return true;
1184 : }
1185 :
1186 2180 : if (zend_string_equals_literal(method, "workspace/didChangeWatchedFiles")) {
1187 4 : lsp_did_change_watched_files(server, params);
1188 4 : ZVAL_NULL(return_value);
1189 :
1190 4 : return true;
1191 : }
1192 :
1193 2176 : if (zend_string_equals_literal(method, "shutdown")) {
1194 : /* Per the spec the server stays alive after shutdown and only the
1195 : * exit notification (or EOF) ends the loop. */
1196 548 : server->saw_shutdown = true;
1197 548 : ZVAL_NULL(return_value);
1198 :
1199 548 : return true;
1200 : }
1201 :
1202 1628 : if (zend_string_equals_literal(method, "exit")) {
1203 72 : server->shutdown = true;
1204 72 : ZVAL_NULL(return_value);
1205 :
1206 72 : return true;
1207 : }
1208 :
1209 1556 : if (zend_string_equals_literal(method, "textDocument/didOpen")) {
1210 536 : lsp_did_open(server, params);
1211 536 : ZVAL_NULL(return_value);
1212 :
1213 536 : return true;
1214 : }
1215 :
1216 1020 : if (zend_string_equals_literal(method, "textDocument/didChange")) {
1217 24 : lsp_did_change(server, params);
1218 24 : ZVAL_NULL(return_value);
1219 :
1220 24 : return true;
1221 : }
1222 :
1223 996 : if (zend_string_equals_literal(method, "textDocument/didSave")) {
1224 12 : lsp_did_save(server, params);
1225 12 : ZVAL_NULL(return_value);
1226 :
1227 12 : return true;
1228 : }
1229 :
1230 984 : if (zend_string_equals_literal(method, "textDocument/didClose")) {
1231 4 : lsp_did_close(server, params);
1232 4 : ZVAL_NULL(return_value);
1233 :
1234 4 : return true;
1235 : }
1236 :
1237 980 : if (zend_string_equals_literal(method, "textDocument/completion")) {
1238 504 : lsp_document_request(server, params, lsp_lsparrot_completion, return_value);
1239 :
1240 504 : return true;
1241 : }
1242 :
1243 476 : if (zend_string_equals_literal(method, "textDocument/hover")) {
1244 176 : lsp_document_request(server, params, lsp_lsparrot_hover, return_value);
1245 :
1246 176 : return true;
1247 : }
1248 :
1249 536 : if (zend_string_equals_literal(method, "textDocument/definition") ||
1250 236 : zend_string_equals_literal(method, "textDocument/declaration")
1251 : ) {
1252 : /* PHP has no declaration/definition split; declaration aliases the
1253 : * definition lookup. */
1254 68 : lsp_document_request(server, params, lsp_lsparrot_definition, return_value);
1255 :
1256 68 : return true;
1257 : }
1258 :
1259 232 : if (zend_string_equals_literal(method, "textDocument/typeDefinition")) {
1260 4 : lsp_document_request(server, params, lsp_lsparrot_type_definition, return_value);
1261 :
1262 4 : return true;
1263 : }
1264 :
1265 228 : if (zend_string_equals_literal(method, "textDocument/foldingRange")) {
1266 4 : lsp_document_request_no_position(server, params, lsp_lsparrot_folding_range, return_value);
1267 :
1268 4 : return true;
1269 : }
1270 :
1271 224 : if (zend_string_equals_literal(method, "textDocument/semanticTokens/range")) {
1272 0 : lsp_document_request_params(server, params, lsp_lsparrot_semantic_tokens_range, return_value);
1273 :
1274 0 : return true;
1275 : }
1276 :
1277 224 : if (zend_string_equals_literal(method, "textDocument/semanticTokens/full/delta")) {
1278 0 : lsp_document_request_params(server, params, lsp_lsparrot_semantic_tokens_full_delta, return_value);
1279 :
1280 0 : return true;
1281 : }
1282 :
1283 224 : if (zend_string_equals_literal(method, "textDocument/selectionRange")) {
1284 4 : lsp_document_request_params(server, params, lsp_lsparrot_selection_range, return_value);
1285 :
1286 4 : return true;
1287 : }
1288 :
1289 220 : if (zend_string_equals_literal(method, "textDocument/documentLink")) {
1290 4 : lsp_document_request_no_position(server, params, lsp_lsparrot_document_link, return_value);
1291 :
1292 4 : return true;
1293 : }
1294 :
1295 216 : if (zend_string_equals_literal(method, "textDocument/prepareCallHierarchy")) {
1296 4 : lsp_document_request(server, params, lsp_lsparrot_prepare_call_hierarchy, return_value);
1297 :
1298 4 : return true;
1299 : }
1300 :
1301 212 : if (zend_string_equals_literal(method, "callHierarchy/incomingCalls")) {
1302 4 : lsp_lsparrot_call_hierarchy_incoming(server, return_value, params);
1303 :
1304 4 : return true;
1305 : }
1306 :
1307 208 : if (zend_string_equals_literal(method, "callHierarchy/outgoingCalls")) {
1308 4 : lsp_lsparrot_call_hierarchy_outgoing(server, return_value, params);
1309 :
1310 4 : return true;
1311 : }
1312 :
1313 204 : if (zend_string_equals_literal(method, "textDocument/prepareTypeHierarchy")) {
1314 4 : lsp_document_request(server, params, lsp_lsparrot_prepare_type_hierarchy, return_value);
1315 :
1316 4 : return true;
1317 : }
1318 :
1319 200 : if (zend_string_equals_literal(method, "typeHierarchy/supertypes")) {
1320 4 : lsp_lsparrot_type_hierarchy_supertypes(server, return_value, params);
1321 :
1322 4 : return true;
1323 : }
1324 :
1325 196 : if (zend_string_equals_literal(method, "typeHierarchy/subtypes")) {
1326 4 : lsp_lsparrot_type_hierarchy_subtypes(server, return_value, params);
1327 :
1328 4 : return true;
1329 : }
1330 :
1331 192 : if (zend_string_equals_literal(method, "workspace/didChangeConfiguration")) {
1332 4 : lsp_options_apply_runtime(&server->options, params);
1333 4 : ZVAL_NULL(return_value);
1334 :
1335 4 : return true;
1336 : }
1337 :
1338 188 : if (zend_string_equals_literal(method, "workspace/didChangeWorkspaceFolders")) {
1339 0 : lsp_workspace_did_change_folders(server, params);
1340 0 : ZVAL_NULL(return_value);
1341 :
1342 0 : return true;
1343 : }
1344 :
1345 188 : if (zend_string_equals_literal(method, "workspace/willRenameFiles")) {
1346 8 : lsp_lsparrot_will_rename_files(server, return_value, params);
1347 :
1348 8 : return true;
1349 : }
1350 :
1351 180 : if (zend_string_equals_literal(method, "textDocument/references")) {
1352 4 : lsp_document_request_params(server, params, lsp_lsparrot_references, return_value);
1353 :
1354 4 : return true;
1355 : }
1356 :
1357 176 : if (zend_string_equals_literal(method, "textDocument/documentHighlight")) {
1358 12 : lsp_document_request(server, params, lsp_lsparrot_document_highlight, return_value);
1359 :
1360 12 : return true;
1361 : }
1362 :
1363 164 : if (zend_string_equals_literal(method, "textDocument/implementation")) {
1364 4 : lsp_document_request(server, params, lsp_lsparrot_implementation, return_value);
1365 :
1366 4 : return true;
1367 : }
1368 :
1369 160 : if (zend_string_equals_literal(method, "textDocument/codeAction")) {
1370 12 : lsp_document_request_params(server, params, lsp_lsparrot_code_action, return_value);
1371 :
1372 12 : return true;
1373 : }
1374 :
1375 148 : if (zend_string_equals_literal(method, "textDocument/prepareRename")) {
1376 4 : lsp_document_request(server, params, lsp_lsparrot_prepare_rename, return_value);
1377 :
1378 4 : return true;
1379 : }
1380 :
1381 144 : if (zend_string_equals_literal(method, "textDocument/rename")) {
1382 8 : lsp_document_request_params(server, params, lsp_lsparrot_rename, return_value);
1383 :
1384 8 : return true;
1385 : }
1386 :
1387 136 : if (zend_string_equals_literal(method, "textDocument/formatting")) {
1388 20 : lsp_document_request_params(server, params, lsp_lsparrot_formatting, return_value);
1389 :
1390 20 : return true;
1391 : }
1392 :
1393 116 : if (zend_string_equals_literal(method, "textDocument/rangeFormatting")) {
1394 8 : lsp_document_request_params(server, params, lsp_lsparrot_range_formatting, return_value);
1395 :
1396 8 : return true;
1397 : }
1398 :
1399 108 : if (zend_string_equals_literal(method, "textDocument/inlayHint")) {
1400 8 : lsp_document_request_params(server, params, lsp_lsparrot_inlay_hint, return_value);
1401 :
1402 8 : return true;
1403 : }
1404 :
1405 100 : if (zend_string_equals_literal(method, "textDocument/codeLens")) {
1406 4 : lsp_document_request_no_position(server, params, lsp_lsparrot_code_lens, return_value);
1407 :
1408 4 : return true;
1409 : }
1410 :
1411 96 : if (zend_string_equals_literal(method, "textDocument/semanticTokens/full")) {
1412 8 : lsp_document_request_no_position(server, params, lsp_lsparrot_semantic_tokens, return_value);
1413 :
1414 8 : return true;
1415 : }
1416 :
1417 88 : if (zend_string_equals_literal(method, "textDocument/signatureHelp")) {
1418 16 : lsp_document_request(server, params, lsp_lsparrot_signature_help, return_value);
1419 :
1420 16 : return true;
1421 : }
1422 :
1423 72 : if (zend_string_equals_literal(method, "textDocument/documentSymbol")) {
1424 16 : lsp_document_request_no_server_no_position(server, params, lsp_document_symbols, return_value);
1425 :
1426 16 : return true;
1427 : }
1428 :
1429 56 : if (zend_string_equals_literal(method, "workspace/symbol")) {
1430 48 : lsp_workspace_symbols(server, params, return_value);
1431 :
1432 48 : return true;
1433 : }
1434 :
1435 8 : ZVAL_NULL(return_value);
1436 :
1437 8 : return false;
1438 : }
1439 :
1440 548 : extern void lsp_server_loop(lsp_server *server)
1441 : {
1442 : double started_at;
1443 : zval message, *method_zv, *params, *id, result, empty_params;
1444 : bool has_id, handled;
1445 :
1446 3400 : while (!server->shutdown && lsp_protocol_next_message(server, &message)) {
1447 2852 : lsp_reap_analyzer_jobs(server);
1448 2852 : method_zv = lsp_array_find(&message, "method");
1449 2852 : params = lsp_array_find(&message, "params");
1450 2852 : id = zend_hash_str_find(Z_ARRVAL(message), "id", sizeof("id") - 1);
1451 2852 : has_id = zend_hash_str_exists(Z_ARRVAL(message), "id", sizeof("id") - 1);
1452 :
1453 2852 : ZVAL_UNDEF(&result);
1454 :
1455 2852 : if (!method_zv || Z_TYPE_P(method_zv) != IS_STRING) {
1456 0 : if (has_id) {
1457 0 : lsp_protocol_error(id, -32600, "Invalid Request");
1458 : }
1459 :
1460 0 : zval_ptr_dtor(&message);
1461 :
1462 0 : continue;
1463 : }
1464 :
1465 2852 : if (has_id && lsp_protocol_request_is_cancelled(id)) {
1466 4 : lsp_protocol_error(id, -32800, "Request cancelled");
1467 4 : zval_ptr_dtor(&message);
1468 :
1469 4 : continue;
1470 : }
1471 :
1472 : /* After shutdown only the exit notification is honored; requests get
1473 : * InvalidRequest and other notifications are dropped. */
1474 2848 : if (server->saw_shutdown && !zend_string_equals_literal(Z_STR_P(method_zv), "exit")) {
1475 4 : if (has_id) {
1476 4 : lsp_protocol_error(id, -32600, "Invalid Request");
1477 : }
1478 :
1479 4 : zval_ptr_dtor(&message);
1480 :
1481 4 : continue;
1482 : }
1483 :
1484 2844 : started_at = lsp_now_seconds();
1485 :
1486 2844 : if (!params || Z_TYPE_P(params) != IS_ARRAY) {
1487 0 : array_init(&empty_params);
1488 0 : handled = lsp_server_handle(server, Z_STR_P(method_zv), &empty_params, &result);
1489 0 : zval_ptr_dtor(&empty_params);
1490 : } else {
1491 2844 : handled = lsp_server_handle(server, Z_STR_P(method_zv), params, &result);
1492 : }
1493 :
1494 2844 : lsp_perf_stats_record(server, Z_STR_P(method_zv), lsp_now_seconds() - started_at);
1495 :
1496 2844 : if (EG(exception)) {
1497 0 : zend_clear_exception();
1498 0 : if (has_id) {
1499 0 : lsp_protocol_error(id, -32603, "Internal error");
1500 : }
1501 2844 : } else if (has_id) {
1502 2184 : if (handled) {
1503 2180 : lsp_protocol_respond(id, &result);
1504 : } else {
1505 4 : lsp_protocol_error(id, -32601, "Method not found");
1506 : }
1507 : }
1508 2844 : if (!Z_ISUNDEF(result)) {
1509 2844 : zval_ptr_dtor(&result);
1510 : }
1511 :
1512 2844 : zval_ptr_dtor(&message);
1513 : }
1514 548 : }
|