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 : /* textDocument/semanticTokens/full (and the range/delta variants below)
19 : * generated from the lexer token stream. Legend indexes must match
20 : * lsp_semantic_token_legend() in this file. */
21 :
22 : #define LSP_SEM_NAMESPACE 0
23 : #define LSP_SEM_CLASS 1
24 : #define LSP_SEM_INTERFACE 2
25 : #define LSP_SEM_ENUM 3
26 : #define LSP_SEM_FUNCTION 4
27 : #define LSP_SEM_METHOD 5
28 : #define LSP_SEM_PROPERTY 6
29 : #define LSP_SEM_VARIABLE 7
30 : #define LSP_SEM_PARAMETER 8
31 : #define LSP_SEM_KEYWORD 9
32 : #define LSP_SEM_COMMENT 10
33 : #define LSP_SEM_STRING 11
34 : #define LSP_SEM_NUMBER 12
35 : #define LSP_SEM_OPERATOR 13
36 : #define LSP_SEM_ENUM_MEMBER 14
37 : #define LSP_SEM_NONE (-1)
38 :
39 : /* Legend order: static, readonly, deprecated, declaration. Bit positions
40 : * must stay in sync with lsp_semantic_token_legend()'s tokenModifiers. */
41 : #define LSP_SEM_MOD_STATIC (1 << 0)
42 : #define LSP_SEM_MOD_READONLY (1 << 1)
43 : #define LSP_SEM_MOD_DEPRECATED (1 << 2)
44 : #define LSP_SEM_MOD_DECLARATION (1 << 3)
45 :
46 : typedef struct _lsp_semantic_emitter {
47 : zval *data;
48 : zend_long previous_line;
49 : zend_long previous_column;
50 : } lsp_semantic_emitter;
51 :
52 : typedef struct _lsp_semantic_decl_info {
53 : bool is_static;
54 : bool is_readonly;
55 : bool is_deprecated;
56 : bool has_modifier;
57 : } lsp_semantic_decl_info;
58 :
59 548 : extern void lsp_semantic_token_legend(zval *legend)
60 : {
61 : zval token_types, token_modifiers;
62 :
63 548 : array_init(legend);
64 548 : array_init(&token_types);
65 548 : add_next_index_string(&token_types, "namespace");
66 548 : add_next_index_string(&token_types, "class");
67 548 : add_next_index_string(&token_types, "interface");
68 548 : add_next_index_string(&token_types, "enum");
69 548 : add_next_index_string(&token_types, "function");
70 548 : add_next_index_string(&token_types, "method");
71 548 : add_next_index_string(&token_types, "property");
72 548 : add_next_index_string(&token_types, "variable");
73 548 : add_next_index_string(&token_types, "parameter");
74 548 : add_next_index_string(&token_types, "keyword");
75 548 : add_next_index_string(&token_types, "comment");
76 548 : add_next_index_string(&token_types, "string");
77 548 : add_next_index_string(&token_types, "number");
78 548 : add_next_index_string(&token_types, "operator");
79 548 : add_next_index_string(&token_types, "enumMember");
80 548 : add_assoc_zval(legend, "tokenTypes", &token_types);
81 548 : array_init(&token_modifiers);
82 548 : add_next_index_string(&token_modifiers, "static");
83 548 : add_next_index_string(&token_modifiers, "readonly");
84 548 : add_next_index_string(&token_modifiers, "deprecated");
85 548 : add_next_index_string(&token_modifiers, "declaration");
86 548 : add_assoc_zval(legend, "tokenModifiers", &token_modifiers);
87 548 : }
88 :
89 308 : static inline bool lsp_semantic_id_is_keyword(zend_long id)
90 : {
91 308 : switch (id) {
92 76 : case T_ABSTRACT:
93 : case T_ARRAY:
94 : case T_AS:
95 : case T_BREAK:
96 : case T_CALLABLE:
97 : case T_CASE:
98 : case T_CATCH:
99 : case T_CLASS:
100 : case T_CLASS_C:
101 : case T_CLONE:
102 : case T_CONST:
103 : case T_CONTINUE:
104 : case T_DECLARE:
105 : case T_DEFAULT:
106 : case T_DIR:
107 : case T_DO:
108 : case T_ECHO:
109 : case T_ELSE:
110 : case T_ELSEIF:
111 : case T_EMPTY:
112 : case T_ENDDECLARE:
113 : case T_ENDFOR:
114 : case T_ENDFOREACH:
115 : case T_ENDIF:
116 : case T_ENDSWITCH:
117 : case T_ENDWHILE:
118 : case T_ENUM:
119 : case T_EXIT:
120 : case T_EXTENDS:
121 : case T_FILE:
122 : case T_FINAL:
123 : case T_FINALLY:
124 : case T_FN:
125 : case T_FOR:
126 : case T_FOREACH:
127 : case T_FUNC_C:
128 : case T_FUNCTION:
129 : case T_GLOBAL:
130 : case T_GOTO:
131 : case T_IF:
132 : case T_IMPLEMENTS:
133 : case T_INCLUDE:
134 : case T_INCLUDE_ONCE:
135 : case T_INSTANCEOF:
136 : case T_INSTEADOF:
137 : case T_INTERFACE:
138 : case T_ISSET:
139 : case T_LINE:
140 : case T_LIST:
141 : case T_MATCH:
142 : case T_METHOD_C:
143 : case T_NAMESPACE:
144 : case T_NEW:
145 : case T_NS_C:
146 : case T_PRINT:
147 : case T_PRIVATE:
148 : case T_PROTECTED:
149 : case T_PUBLIC:
150 : case T_READONLY:
151 : case T_REQUIRE:
152 : case T_REQUIRE_ONCE:
153 : case T_RETURN:
154 : case T_STATIC:
155 : case T_SWITCH:
156 : case T_THROW:
157 : case T_TRAIT:
158 : case T_TRY:
159 : case T_UNSET:
160 : case T_USE:
161 : case T_VAR:
162 : case T_WHILE:
163 : case T_YIELD:
164 : case T_YIELD_FROM:
165 76 : return true;
166 : }
167 :
168 232 : return false;
169 : }
170 :
171 612 : static inline bool lsp_semantic_id_is_string(zend_long id)
172 : {
173 612 : return id == T_CONSTANT_ENCAPSED_STRING ||
174 612 : id == T_ENCAPSED_AND_WHITESPACE ||
175 1224 : id == T_START_HEREDOC ||
176 : id == T_END_HEREDOC
177 : ;
178 : }
179 :
180 72 : static inline bool lsp_semantic_text_is_keyword_literal(zend_string *text)
181 : {
182 32 : return zend_string_equals_literal_ci(text, "true") ||
183 72 : zend_string_equals_literal_ci(text, "false") ||
184 72 : zend_string_equals_literal_ci(text, "null") ||
185 196 : zend_string_equals_literal_ci(text, "self") ||
186 52 : zend_string_equals_literal_ci(text, "parent")
187 : ;
188 : }
189 :
190 544 : static inline bool lsp_semantic_id_is_significant(zend_long id)
191 : {
192 460 : return id != T_WHITESPACE && id != T_COMMENT && id != T_DOC_COMMENT &&
193 1004 : id != T_OPEN_TAG && id != T_CLOSE_TAG && id != T_OPEN_TAG_WITH_ECHO
194 : ;
195 : }
196 :
197 100 : static inline zend_long lsp_semantic_next_significant_id(HashTable *tokens, uint32_t start, uint32_t count)
198 : {
199 : zend_long id;
200 : zval *token;
201 : uint32_t i;
202 :
203 184 : for (i = start; i < count; i++) {
204 184 : token = zend_hash_index_find(tokens, i);
205 184 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
206 0 : continue;
207 : }
208 :
209 184 : id = lsp_token_long(token, "id", 0);
210 184 : if (lsp_semantic_id_is_significant(id)) {
211 100 : return id;
212 : }
213 : }
214 :
215 0 : return 0;
216 : }
217 :
218 28 : static inline bool lsp_semantic_next_is_open_paren(HashTable *tokens, uint32_t start, uint32_t count)
219 : {
220 28 : return lsp_semantic_next_significant_id(tokens, start, count) == (zend_long) '(';
221 : }
222 :
223 8 : static inline zend_long lsp_semantic_class_like_type(zend_long keyword_id)
224 : {
225 8 : if (keyword_id == T_INTERFACE) {
226 0 : return LSP_SEM_INTERFACE;
227 : }
228 :
229 8 : if (keyword_id == T_ENUM) {
230 0 : return LSP_SEM_ENUM;
231 : }
232 :
233 8 : return LSP_SEM_CLASS;
234 : }
235 :
236 8 : static inline bool lsp_semantic_doc_comment_is_deprecated(zend_string *text)
237 : {
238 8 : if (!text) {
239 0 : return false;
240 : }
241 :
242 8 : return zend_memnstr(ZSTR_VAL(text), "@deprecated", sizeof("@deprecated") - 1, ZSTR_VAL(text) + ZSTR_LEN(text)) != NULL;
243 : }
244 :
245 : /* Walks backward from (but excluding) start_index collecting the declaration
246 : * modifiers (static/readonly/visibility/var) and the nearest preceding doc
247 : * comment, stopping at the previous statement boundary. In param_context
248 : * (promoted constructor properties) '(' and ',' also terminate the scan so
249 : * sibling parameters/the function's own modifiers are never crossed. */
250 44 : static void lsp_semantic_scan_declaration_modifiers(HashTable *tokens, uint32_t start_index, bool param_context, lsp_semantic_decl_info *info)
251 : {
252 : zend_long i, id;
253 : zval *token;
254 : zend_string *text;
255 :
256 44 : memset(info, 0, sizeof(*info));
257 :
258 208 : for (i = (zend_long) start_index - 1; i >= 0; i--) {
259 200 : token = zend_hash_index_find(tokens, (zend_ulong) i);
260 200 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
261 0 : continue;
262 : }
263 :
264 200 : id = lsp_token_long(token, "id", 0);
265 :
266 200 : if (param_context && (id == (zend_long) '(' || id == (zend_long) ',')) {
267 : break;
268 : }
269 :
270 188 : if (id == (zend_long) ';' || id == (zend_long) '{' || id == (zend_long) '}') {
271 : break;
272 : }
273 :
274 164 : if (id == T_STATIC) {
275 8 : info->is_static = true;
276 8 : info->has_modifier = true;
277 156 : } else if (id == T_READONLY) {
278 4 : info->is_readonly = true;
279 4 : info->has_modifier = true;
280 152 : } else if (id == T_PUBLIC || id == T_PROTECTED || id == T_PRIVATE || id == T_VAR) {
281 24 : info->has_modifier = true;
282 128 : } else if (id == T_DOC_COMMENT) {
283 8 : text = lsp_token_string(token, "text");
284 8 : if (lsp_semantic_doc_comment_is_deprecated(text)) {
285 4 : info->is_deprecated = true;
286 : }
287 : }
288 : }
289 44 : }
290 :
291 196 : static inline void lsp_semantic_emit(lsp_semantic_emitter *emitter, zend_long line, zend_long column, zend_long length, zend_long type, zend_long modifiers)
292 : {
293 : zend_long delta_line, delta_column;
294 :
295 196 : if (length <= 0 || type == LSP_SEM_NONE) {
296 0 : return;
297 : }
298 :
299 196 : delta_line = line - emitter->previous_line;
300 196 : delta_column = delta_line == 0 ? column - emitter->previous_column : column;
301 196 : if (delta_line < 0 || delta_column < 0) {
302 0 : return;
303 : }
304 :
305 196 : add_next_index_long(emitter->data, delta_line);
306 196 : add_next_index_long(emitter->data, delta_column);
307 196 : add_next_index_long(emitter->data, length);
308 196 : add_next_index_long(emitter->data, type);
309 196 : add_next_index_long(emitter->data, modifiers);
310 196 : emitter->previous_line = line;
311 196 : emitter->previous_column = column;
312 : }
313 :
314 624 : static inline zend_long lsp_semantic_token_column(zend_string *text, size_t offset)
315 : {
316 624 : const char *value = ZSTR_VAL(text);
317 624 : size_t line_start = offset;
318 :
319 624 : if (offset > ZSTR_LEN(text)) {
320 0 : return 0;
321 : }
322 :
323 12480 : while (line_start > 0 && value[line_start - 1] != '\n') {
324 11856 : line_start--;
325 : }
326 :
327 624 : return lsp_byte_offset_to_utf16_units(value, line_start, offset);
328 : }
329 :
330 : /* Shared core for full/range responses: classifies every token in the
331 : * document, but only emits (and therefore only affects the running delta
332 : * base) tokens whose line falls within [line_start, line_end] when
333 : * has_bounds is set. The first emitted token's deltaLine always stays
334 : * relative to absolute line 0, matching the LSP delta-encoding rules for
335 : * both full and ranged responses. */
336 8 : static void lsp_semantic_tokens_collect(lsp_document *document, zval *data, bool has_bounds, zend_long line_start, zend_long line_end)
337 : {
338 : lsp_semantic_emitter emitter;
339 : zend_long id, type, line, column, offset, length, depth, class_depth, previous_id, context_id, context_keyword_index, paren_depth, modifiers;
340 : zend_string *token_text;
341 : zval *tokens_zv, *token;
342 : HashTable *tokens;
343 : uint32_t i, count;
344 : bool pending_class_body;
345 :
346 8 : emitter.data = data;
347 8 : emitter.previous_line = 0;
348 8 : emitter.previous_column = 0;
349 :
350 8 : tokens_zv = Z_TYPE(document->lsparrot) == IS_ARRAY
351 8 : ? zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1)
352 8 : : NULL
353 : ;
354 8 : if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
355 0 : return;
356 : }
357 :
358 8 : tokens = Z_ARRVAL_P(tokens_zv);
359 8 : count = zend_hash_num_elements(tokens);
360 8 : depth = 0;
361 8 : class_depth = -1;
362 8 : previous_id = 0;
363 8 : context_id = 0;
364 8 : context_keyword_index = -1;
365 8 : paren_depth = 0;
366 8 : pending_class_body = false;
367 :
368 632 : for (i = 0; i < count; i++) {
369 624 : token = zend_hash_index_find(tokens, i);
370 624 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
371 0 : continue;
372 : }
373 :
374 624 : id = lsp_token_long(token, "id", 0);
375 624 : offset = lsp_token_long(token, "offset", 0);
376 624 : length = lsp_token_long(token, "length", 0);
377 624 : line = lsp_token_long(token, "line", 1) - 1;
378 624 : column = lsp_semantic_token_column(document->text, (size_t) offset);
379 624 : token_text = lsp_token_string(token, "text");
380 624 : type = LSP_SEM_NONE;
381 624 : modifiers = 0;
382 :
383 : /* Comments and string literals are deliberately NOT emitted: the
384 : * client-side grammar colors them with richer sub-decorations (the
385 : * VSCode extension injects a PHPDoc grammar into comment scopes,
386 : * and string scopes carry escape-sequence highlighting). Semantic
387 : * tokens take precedence over the grammar in VSCode, so emitting
388 : * a flat comment/string token here would erase those decorations. */
389 624 : if (id == T_COMMENT || id == T_DOC_COMMENT) {
390 12 : continue;
391 : }
392 :
393 612 : if (lsp_semantic_id_is_string(id)) {
394 0 : previous_id = id;
395 :
396 0 : continue;
397 : }
398 :
399 612 : if (id == T_WHITESPACE || id == T_OPEN_TAG || id == T_CLOSE_TAG || id == T_INLINE_HTML) {
400 252 : continue;
401 : }
402 :
403 360 : if (id == (zend_long) '(') {
404 24 : paren_depth++;
405 336 : } else if (id == (zend_long) ')') {
406 24 : if (paren_depth > 0) {
407 24 : paren_depth--;
408 : }
409 : }
410 :
411 360 : if (id == (zend_long) '{') {
412 24 : depth++;
413 24 : if (pending_class_body) {
414 8 : class_depth = depth;
415 8 : pending_class_body = false;
416 : }
417 336 : } else if (id == (zend_long) '}') {
418 24 : if (class_depth >= 0 && depth == class_depth) {
419 8 : class_depth = -1;
420 : }
421 24 : if (depth > 0) {
422 24 : depth--;
423 : }
424 : }
425 :
426 360 : if (id == T_LNUMBER || id == T_DNUMBER) {
427 8 : type = LSP_SEM_NUMBER;
428 352 : } else if (id == T_VARIABLE) {
429 44 : bool is_this = token_text && zend_string_equals_literal(token_text, "$this");
430 :
431 : /* $this is a variable, not a keyword: tagging it keyword paints
432 : * it like `public`/`return` in clients that trust semantic
433 : * tokens over the grammar. */
434 44 : type = previous_id == T_PAAMAYIM_NEKUDOTAYIM ? LSP_SEM_PROPERTY : LSP_SEM_VARIABLE;
435 :
436 : /* Property declarations (and promoted constructor properties)
437 : * live directly at the class body depth: plain field
438 : * declarations sit at class_depth itself, and constructor
439 : * parameter lists never bump the brace depth, so promoted
440 : * parameters are also observed at class_depth while
441 : * paren_depth > 0 distinguishes them from a field statement. */
442 44 : if (!is_this && class_depth >= 0 && depth == class_depth) {
443 : lsp_semantic_decl_info decl_info;
444 :
445 20 : lsp_semantic_scan_declaration_modifiers(tokens, i, paren_depth > 0, &decl_info);
446 20 : if (decl_info.has_modifier) {
447 8 : modifiers |= LSP_SEM_MOD_DECLARATION;
448 8 : if (decl_info.is_static) {
449 4 : modifiers |= LSP_SEM_MOD_STATIC;
450 : }
451 8 : if (decl_info.is_readonly) {
452 4 : modifiers |= LSP_SEM_MOD_READONLY;
453 : }
454 8 : if (decl_info.is_deprecated) {
455 0 : modifiers |= LSP_SEM_MOD_DEPRECATED;
456 : }
457 : }
458 : }
459 308 : } else if (lsp_semantic_id_is_keyword(id)) {
460 76 : type = LSP_SEM_KEYWORD;
461 76 : if (id == T_CLASS || id == T_INTERFACE || id == T_TRAIT || id == T_ENUM) {
462 8 : context_id = id;
463 8 : context_keyword_index = (zend_long) i;
464 68 : } else if (id == T_FUNCTION || id == T_CONST || id == T_NAMESPACE || id == T_USE ||
465 44 : id == T_NEW || id == T_EXTENDS || id == T_IMPLEMENTS || id == T_INSTANCEOF
466 : ) {
467 24 : context_id = id;
468 24 : if (id == T_FUNCTION) {
469 16 : context_keyword_index = (zend_long) i;
470 : }
471 : }
472 232 : } else if (id == T_STRING || id == T_NAME_QUALIFIED || id == T_NAME_FULLY_QUALIFIED || id == T_NAME_RELATIVE) {
473 72 : if (token_text && lsp_semantic_text_is_keyword_literal(token_text) &&
474 40 : previous_id != T_OBJECT_OPERATOR && previous_id != T_NULLSAFE_OBJECT_OPERATOR &&
475 20 : lsp_semantic_next_significant_id(tokens, i + 1, count) != T_PAAMAYIM_NEKUDOTAYIM
476 : ) {
477 16 : type = LSP_SEM_KEYWORD;
478 56 : } else if (context_id == T_NAMESPACE || context_id == T_USE) {
479 0 : type = context_id == T_NAMESPACE ? LSP_SEM_NAMESPACE : LSP_SEM_CLASS;
480 0 : if (id == T_STRING && context_id == T_USE && previous_id == T_AS) {
481 0 : type = LSP_SEM_CLASS;
482 : }
483 56 : } else if (context_id == T_CLASS || context_id == T_INTERFACE || context_id == T_TRAIT || context_id == T_ENUM) {
484 8 : type = lsp_semantic_class_like_type(context_id);
485 8 : if (previous_id != T_EXTENDS && previous_id != T_IMPLEMENTS && previous_id != (zend_long) ',') {
486 8 : pending_class_body = true;
487 8 : modifiers |= LSP_SEM_MOD_DECLARATION;
488 8 : if (context_keyword_index >= 0) {
489 : lsp_semantic_decl_info decl_info;
490 :
491 8 : lsp_semantic_scan_declaration_modifiers(tokens, (uint32_t) context_keyword_index, false, &decl_info);
492 8 : if (decl_info.is_deprecated) {
493 0 : modifiers |= LSP_SEM_MOD_DEPRECATED;
494 : }
495 : }
496 : }
497 48 : } else if (context_id == T_NEW || context_id == T_EXTENDS || context_id == T_IMPLEMENTS || context_id == T_INSTANCEOF) {
498 0 : type = LSP_SEM_CLASS;
499 48 : } else if (context_id == T_FUNCTION) {
500 16 : type = class_depth >= 0 ? LSP_SEM_METHOD : LSP_SEM_FUNCTION;
501 16 : modifiers |= LSP_SEM_MOD_DECLARATION;
502 16 : if (context_keyword_index >= 0) {
503 : lsp_semantic_decl_info decl_info;
504 :
505 16 : lsp_semantic_scan_declaration_modifiers(tokens, (uint32_t) context_keyword_index, false, &decl_info);
506 16 : if (decl_info.is_static) {
507 4 : modifiers |= LSP_SEM_MOD_STATIC;
508 : }
509 16 : if (decl_info.is_deprecated) {
510 4 : modifiers |= LSP_SEM_MOD_DEPRECATED;
511 : }
512 : }
513 32 : } else if (context_id == T_CONST) {
514 0 : type = LSP_SEM_ENUM_MEMBER;
515 32 : } else if (previous_id == T_OBJECT_OPERATOR || previous_id == T_NULLSAFE_OBJECT_OPERATOR) {
516 4 : type = lsp_semantic_next_is_open_paren(tokens, i + 1, count) ? LSP_SEM_METHOD : LSP_SEM_PROPERTY;
517 28 : } else if (previous_id == T_PAAMAYIM_NEKUDOTAYIM) {
518 0 : if (token_text && zend_string_equals_literal_ci(token_text, "class")) {
519 0 : type = LSP_SEM_KEYWORD;
520 : } else {
521 0 : type = lsp_semantic_next_is_open_paren(tokens, i + 1, count) ? LSP_SEM_METHOD : LSP_SEM_ENUM_MEMBER;
522 : }
523 28 : } else if (lsp_semantic_next_significant_id(tokens, i + 1, count) == T_PAAMAYIM_NEKUDOTAYIM) {
524 4 : type = LSP_SEM_CLASS;
525 24 : } else if (lsp_semantic_next_is_open_paren(tokens, i + 1, count)) {
526 0 : type = LSP_SEM_FUNCTION;
527 24 : } else if (id != T_STRING) {
528 0 : type = LSP_SEM_CLASS;
529 24 : } else if (lsp_semantic_next_significant_id(tokens, i + 1, count) == T_VARIABLE) {
530 : /* Bare identifier directly before a variable: a type hint. */
531 20 : type = LSP_SEM_CLASS;
532 : }
533 : }
534 :
535 : /* Single-statement contexts end at statement delimiters. */
536 360 : if (id == (zend_long) ';' || id == (zend_long) '{' || id == (zend_long) '(' ||
537 260 : id == (zend_long) ')' || id == (zend_long) '='
538 : ) {
539 112 : context_id = 0;
540 : }
541 :
542 360 : if (type != LSP_SEM_NONE && (!has_bounds || (line >= line_start && line <= line_end))) {
543 : /* Token lengths arrive in bytes; the wire format counts UTF-16
544 : * code units. */
545 196 : if ((size_t) (offset + length) <= ZSTR_LEN(document->text)) {
546 196 : length = lsp_byte_offset_to_utf16_units(ZSTR_VAL(document->text), (size_t) offset, (size_t) (offset + length));
547 : }
548 196 : lsp_semantic_emit(&emitter, line, column, length, type, modifiers);
549 : }
550 :
551 360 : if (lsp_semantic_id_is_significant(id)) {
552 360 : previous_id = id;
553 : }
554 : }
555 : }
556 :
557 : /* Self-contained resultId -> {resultId, data} cache keyed by document uri,
558 : * used to serve textDocument/semanticTokens/full/delta. Entries are
559 : * overwritten (never merged) on every full/delta computation, so the
560 : * hashtable never grows past one entry per distinct document uri seen. */
561 : static HashTable lsp_semantic_tokens_cache;
562 : static bool lsp_semantic_tokens_cache_ready = false;
563 :
564 8 : static inline HashTable *lsp_semantic_tokens_cache_ht(void)
565 : {
566 8 : if (!lsp_semantic_tokens_cache_ready) {
567 8 : zend_hash_init(&lsp_semantic_tokens_cache, 8, NULL, ZVAL_PTR_DTOR, 0);
568 8 : lsp_semantic_tokens_cache_ready = true;
569 : }
570 :
571 8 : return &lsp_semantic_tokens_cache;
572 : }
573 :
574 : /* Closed documents must not pin their token snapshot forever. */
575 16 : extern void lsp_semantic_tokens_cache_evict(zend_string *uri)
576 : {
577 16 : if (lsp_semantic_tokens_cache_ready) {
578 0 : zend_hash_del(&lsp_semantic_tokens_cache, uri);
579 : }
580 16 : }
581 :
582 548 : extern void lsp_semantic_tokens_cache_clear(void)
583 : {
584 548 : if (lsp_semantic_tokens_cache_ready) {
585 8 : zend_hash_destroy(&lsp_semantic_tokens_cache);
586 8 : lsp_semantic_tokens_cache_ready = false;
587 : }
588 548 : }
589 :
590 8 : static zend_string *lsp_semantic_tokens_next_result_id(void)
591 : {
592 : static zend_ulong lsp_semantic_tokens_result_counter = 0;
593 : char buf[32];
594 : int len;
595 :
596 8 : lsp_semantic_tokens_result_counter++;
597 8 : len = snprintf(buf, sizeof(buf), "%lu", (unsigned long) lsp_semantic_tokens_result_counter);
598 :
599 8 : return zend_string_init(buf, (size_t) (len > 0 ? len : 0), 0);
600 : }
601 :
602 0 : static bool lsp_semantic_tokens_group_equal(HashTable *a, uint32_t group_a, HashTable *b, uint32_t group_b)
603 : {
604 : uint32_t k;
605 : zval *va, *vb;
606 :
607 0 : for (k = 0; k < 5; k++) {
608 0 : va = zend_hash_index_find(a, (zend_ulong) (group_a * 5 + k));
609 0 : vb = zend_hash_index_find(b, (zend_ulong) (group_b * 5 + k));
610 0 : if (!va || !vb || Z_TYPE_P(va) != IS_LONG || Z_TYPE_P(vb) != IS_LONG || Z_LVAL_P(va) != Z_LVAL_P(vb)) {
611 0 : return false;
612 : }
613 : }
614 :
615 0 : return true;
616 : }
617 :
618 : /* Computes a minimal single-splice SemanticTokensEdit between the previously
619 : * served flat data array and the freshly computed one, comparing in
620 : * 5-element (line, col, length, type, modifiers) groups so a splice never
621 : * lands in the middle of a token. */
622 0 : static void lsp_semantic_tokens_build_edits(zval *return_value, HashTable *old_ht, HashTable *new_ht)
623 : {
624 : zval edits, edit, edit_data;
625 : uint32_t old_len, new_len, old_groups, new_groups, prefix, suffix, max_common, i, k;
626 : zval *v;
627 :
628 0 : old_len = zend_hash_num_elements(old_ht);
629 0 : new_len = zend_hash_num_elements(new_ht);
630 0 : old_groups = old_len / 5;
631 0 : new_groups = new_len / 5;
632 :
633 0 : prefix = 0;
634 0 : max_common = old_groups < new_groups ? old_groups : new_groups;
635 0 : while (prefix < max_common && lsp_semantic_tokens_group_equal(old_ht, prefix, new_ht, prefix)) {
636 0 : prefix++;
637 : }
638 :
639 0 : suffix = 0;
640 0 : max_common -= prefix;
641 0 : while (suffix < max_common &&
642 0 : lsp_semantic_tokens_group_equal(old_ht, old_groups - 1 - suffix, new_ht, new_groups - 1 - suffix)
643 : ) {
644 0 : suffix++;
645 : }
646 :
647 0 : array_init(&edits);
648 0 : array_init(&edit);
649 0 : add_assoc_long(&edit, "start", (zend_long) (prefix * 5));
650 0 : add_assoc_long(&edit, "deleteCount", (zend_long) ((old_groups - prefix - suffix) * 5));
651 :
652 0 : array_init(&edit_data);
653 0 : for (i = prefix; i < new_groups - suffix; i++) {
654 0 : for (k = 0; k < 5; k++) {
655 0 : v = zend_hash_index_find(new_ht, (zend_ulong) (i * 5 + k));
656 0 : add_next_index_long(&edit_data, v && Z_TYPE_P(v) == IS_LONG ? Z_LVAL_P(v) : 0);
657 : }
658 : }
659 0 : add_assoc_zval(&edit, "data", &edit_data);
660 :
661 0 : add_next_index_zval(&edits, &edit);
662 0 : add_assoc_zval(return_value, "edits", &edits);
663 0 : }
664 :
665 8 : extern void lsp_lsparrot_semantic_tokens(lsp_server *server, zval *return_value, lsp_document *document)
666 : {
667 : zval data, cache_data, entry;
668 : zend_string *result_id;
669 :
670 : (void) server;
671 8 : array_init(return_value);
672 8 : array_init(&data);
673 8 : lsp_semantic_tokens_collect(document, &data, false, 0, 0);
674 :
675 8 : result_id = lsp_semantic_tokens_next_result_id();
676 :
677 8 : ZVAL_COPY(&cache_data, &data);
678 8 : array_init(&entry);
679 8 : add_assoc_str(&entry, "resultId", zend_string_copy(result_id));
680 8 : add_assoc_zval(&entry, "data", &cache_data);
681 8 : zend_hash_update(lsp_semantic_tokens_cache_ht(), document->uri, &entry);
682 :
683 8 : add_assoc_str(return_value, "resultId", result_id);
684 8 : add_assoc_zval(return_value, "data", &data);
685 8 : }
686 :
687 : /* textDocument/semanticTokens/range: identical classification pass, but only
688 : * tokens whose (0-based) line falls within params["range"] are emitted. */
689 0 : extern void lsp_lsparrot_semantic_tokens_range(lsp_server *server, zval *return_value, lsp_document *document, zval *params)
690 : {
691 : zval data, *range_zv, *start_zv, *end_zv;
692 0 : zend_long line_start = 0, line_end = ZEND_LONG_MAX;
693 :
694 : (void) server;
695 0 : array_init(return_value);
696 0 : array_init(&data);
697 :
698 0 : range_zv = lsp_array_find(params, "range");
699 0 : if (range_zv) {
700 0 : start_zv = lsp_array_find(range_zv, "start");
701 0 : end_zv = lsp_array_find(range_zv, "end");
702 0 : if (start_zv) {
703 0 : line_start = lsp_array_long(start_zv, "line", 0);
704 : }
705 0 : if (end_zv) {
706 0 : line_end = lsp_array_long(end_zv, "line", ZEND_LONG_MAX);
707 : }
708 : }
709 :
710 0 : lsp_semantic_tokens_collect(document, &data, true, line_start, line_end);
711 0 : add_assoc_zval(return_value, "data", &data);
712 0 : }
713 :
714 : /* textDocument/semanticTokens/full/delta: serves a SemanticTokensDelta when
715 : * params["previousResultId"] matches what is cached for this document uri,
716 : * otherwise falls back to a full SemanticTokens response (both are valid
717 : * per the LSP spec). The cache is always refreshed with the newly computed
718 : * snapshot so the next delta request has a baseline to diff against. */
719 0 : extern void lsp_lsparrot_semantic_tokens_full_delta(lsp_server *server, zval *return_value, lsp_document *document, zval *params)
720 : {
721 : zend_string *previous_result_id, *new_result_id;
722 : zval *cache_entry, *cached_result_id_zv, *cached_data_zv;
723 : zval new_data, old_data_snapshot, cache_data, entry;
724 : HashTable *cache;
725 : bool matched, have_snapshot;
726 :
727 : (void) server;
728 :
729 0 : previous_result_id = lsp_array_string(params, "previousResultId");
730 0 : cache = lsp_semantic_tokens_cache_ht();
731 0 : cache_entry = zend_hash_find(cache, document->uri);
732 0 : matched = false;
733 0 : have_snapshot = false;
734 0 : ZVAL_UNDEF(&old_data_snapshot);
735 :
736 0 : if (previous_result_id && cache_entry && Z_TYPE_P(cache_entry) == IS_ARRAY) {
737 0 : cached_result_id_zv = lsp_array_find(cache_entry, "resultId");
738 0 : if (cached_result_id_zv && Z_TYPE_P(cached_result_id_zv) == IS_STRING &&
739 0 : zend_string_equals(Z_STR_P(cached_result_id_zv), previous_result_id)
740 : ) {
741 0 : cached_data_zv = lsp_array_find(cache_entry, "data");
742 0 : if (cached_data_zv && Z_TYPE_P(cached_data_zv) == IS_ARRAY) {
743 0 : ZVAL_COPY(&old_data_snapshot, cached_data_zv);
744 0 : have_snapshot = true;
745 0 : matched = true;
746 : }
747 : }
748 : }
749 :
750 0 : array_init(&new_data);
751 0 : lsp_semantic_tokens_collect(document, &new_data, false, 0, 0);
752 0 : new_result_id = lsp_semantic_tokens_next_result_id();
753 :
754 : /* Refresh the cache before consuming new_data into the response so the
755 : * cached snapshot and the response each hold an independent reference. */
756 0 : ZVAL_COPY(&cache_data, &new_data);
757 0 : array_init(&entry);
758 0 : add_assoc_str(&entry, "resultId", zend_string_copy(new_result_id));
759 0 : add_assoc_zval(&entry, "data", &cache_data);
760 0 : zend_hash_update(cache, document->uri, &entry);
761 :
762 0 : array_init(return_value);
763 :
764 0 : if (matched && have_snapshot) {
765 0 : lsp_semantic_tokens_build_edits(return_value, Z_ARRVAL(old_data_snapshot), Z_ARRVAL(new_data));
766 0 : add_assoc_str(return_value, "resultId", zend_string_copy(new_result_id));
767 0 : zval_ptr_dtor(&new_data);
768 : } else {
769 0 : add_assoc_str(return_value, "resultId", zend_string_copy(new_result_id));
770 0 : add_assoc_zval(return_value, "data", &new_data);
771 : }
772 :
773 0 : if (have_snapshot) {
774 0 : zval_ptr_dtor(&old_data_snapshot);
775 : }
776 :
777 0 : zend_string_release(new_result_id);
778 0 : }
|