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 540 : extern void lsp_document_destroy(zval *value)
17 : {
18 540 : lsp_document *document = (lsp_document *) Z_PTR_P(value);
19 :
20 540 : if (!document) {
21 0 : return;
22 : }
23 :
24 540 : zend_string_release(document->uri);
25 540 : zend_string_release(document->path);
26 540 : zend_string_release(document->text);
27 :
28 540 : if (!Z_ISUNDEF(document->lsparrot)) {
29 540 : zval_ptr_dtor(&document->lsparrot);
30 : }
31 :
32 540 : if (document->outline_cache_text) {
33 12 : zend_string_release(document->outline_cache_text);
34 12 : zval_ptr_dtor(&document->outline_cache);
35 : }
36 :
37 540 : lsp_document_derived_invalidate(document);
38 540 : efree(document);
39 : }
40 :
41 1356 : extern void lsp_analyzer_job_clear(lsp_analyzer_job *job)
42 : {
43 1356 : if (lsp_process_id_valid(job->pid)) {
44 96 : lsp_process_close(job->pid);
45 : }
46 :
47 1356 : if (job->uri) {
48 0 : zend_string_release(job->uri);
49 0 : job->uri = NULL;
50 : }
51 :
52 1356 : if (job->cache_key) {
53 0 : zend_string_release(job->cache_key);
54 0 : job->cache_key = NULL;
55 : }
56 :
57 1356 : if (job->cache_file) {
58 96 : zend_string_release(job->cache_file);
59 96 : job->cache_file = NULL;
60 : }
61 :
62 1356 : if (job->project_root) {
63 96 : zend_string_release(job->project_root);
64 96 : job->project_root = NULL;
65 : }
66 :
67 1356 : job->pid = LSP_INVALID_PROCESS_ID;
68 1356 : job->version = 0;
69 1356 : job->running = false;
70 1356 : }
71 :
72 1192 : extern void lsp_analyzer_job_destroy(lsp_analyzer_job *job)
73 : {
74 : int status;
75 :
76 1192 : status = 0;
77 1192 : if (job->running && lsp_process_id_valid(job->pid)) {
78 28 : lsp_process_terminate(job->pid);
79 28 : if (!lsp_process_wait_timeout(job->pid, &status, 2.0)) {
80 0 : lsp_process_terminate_force(job->pid);
81 0 : lsp_process_wait_timeout(job->pid, &status, 2.0);
82 : }
83 : }
84 :
85 1192 : if (job->cache_file) {
86 28 : VCWD_UNLINK(ZSTR_VAL(job->cache_file));
87 : }
88 :
89 1192 : lsp_analyzer_job_clear(job);
90 1192 : }
91 :
92 1012 : static inline bool lsp_analyzer_job_matches_document(lsp_analyzer_job *job, lsp_document *document)
93 : {
94 1012 : return job->running &&
95 0 : job->uri &&
96 1012 : zend_string_equals(job->uri, document->uri) &&
97 0 : job->version == document->version
98 : ;
99 : }
100 :
101 1008 : static inline bool lsp_analyzer_job_table_matches_document(HashTable *jobs, lsp_document *document)
102 : {
103 : lsp_analyzer_job *job;
104 : zval *value;
105 :
106 1012 : ZEND_HASH_FOREACH_VAL(jobs, value) {
107 4 : if (Z_TYPE_P(value) != IS_PTR) {
108 0 : continue;
109 : }
110 :
111 4 : job = (lsp_analyzer_job *) Z_PTR_P(value);
112 4 : if (lsp_analyzer_job_matches_document(job, document)) {
113 0 : return true;
114 : }
115 : } ZEND_HASH_FOREACH_END();
116 :
117 1008 : return false;
118 : }
119 :
120 504 : extern bool lsp_analyzer_jobs_running_for_document(lsp_server *server, lsp_document *document)
121 : {
122 1008 : return lsp_analyzer_job_table_matches_document(&server->phpstan_jobs, document) ||
123 1008 : lsp_analyzer_job_table_matches_document(&server->psalm_jobs, document) ||
124 1008 : lsp_analyzer_job_matches_document(&server->phpstan_completion_job, document) ||
125 1512 : lsp_analyzer_job_matches_document(&server->psalm_completion_job, document) ||
126 504 : lsp_runner_has_pending_for_uri(server, document->uri)
127 : ;
128 : }
129 :
130 116 : extern uint32_t lsp_active_process_count(lsp_server *server)
131 : {
132 116 : uint32_t count = 0;
133 :
134 116 : count += lsp_analyzer_running_project_jobs(server);
135 :
136 116 : if (server->phpstan_completion_job.running && lsp_process_id_valid(server->phpstan_completion_job.pid)) {
137 0 : count++;
138 : }
139 :
140 116 : if (server->psalm_completion_job.running && lsp_process_id_valid(server->psalm_completion_job.pid)) {
141 0 : count++;
142 : }
143 :
144 116 : return count;
145 : }
146 :
147 56 : static inline int lsp_hex_value(char c)
148 : {
149 56 : if (c >= '0' && c <= '9') {
150 52 : return c - '0';
151 : }
152 :
153 4 : if (c >= 'a' && c <= 'f') {
154 0 : return c - 'a' + 10;
155 : }
156 :
157 4 : if (c >= 'A' && c <= 'F') {
158 4 : return c - 'A' + 10;
159 : }
160 :
161 0 : return -1;
162 : }
163 :
164 1128 : static inline zend_string *lsp_raw_url_decode(const char *value, size_t length)
165 : {
166 1128 : zend_string *decoded = zend_string_alloc(length, 0);
167 1128 : size_t i, out_length = 0;
168 1128 : char *out = ZSTR_VAL(decoded);
169 : int hi, lo;
170 :
171 43152 : for (i = 0; i < length; i++) {
172 42024 : if (value[i] == '%' && i + 2 < length) {
173 28 : hi = lsp_hex_value(value[i + 1]);
174 28 : lo = lsp_hex_value(value[i + 2]);
175 28 : if (hi >= 0 && lo >= 0) {
176 28 : out[out_length++] = (char) ((hi << 4) | lo);
177 28 : i += 2;
178 28 : continue;
179 : }
180 : }
181 41996 : out[out_length++] = value[i];
182 : }
183 :
184 1128 : out[out_length] = '\0';
185 1128 : ZSTR_LEN(decoded) = out_length;
186 :
187 1128 : return decoded;
188 : }
189 :
190 1128 : extern zend_string *lsp_uri_to_path(zend_string *uri)
191 : {
192 : const char *value;
193 : zend_string *decoded;
194 : size_t length;
195 : #if defined(_WIN32)
196 : const char *path_start;
197 : zend_string *path;
198 : #endif
199 :
200 1128 : value = ZSTR_VAL(uri);
201 1128 : length = ZSTR_LEN(uri);
202 1128 : if (length >= sizeof("file://") - 1 && memcmp(value, "file://", sizeof("file://") - 1) == 0) {
203 1128 : value += sizeof("file://") - 1;
204 1128 : length -= sizeof("file://") - 1;
205 :
206 1128 : decoded = lsp_raw_url_decode(value, length);
207 : #if defined(_WIN32)
208 : if (ZSTR_LEN(decoded) >= sizeof("localhost") - 1 &&
209 : strncasecmp(ZSTR_VAL(decoded), "localhost", sizeof("localhost") - 1) == 0 &&
210 : (ZSTR_LEN(decoded) == sizeof("localhost") - 1 || lsp_is_path_separator(ZSTR_VAL(decoded)[sizeof("localhost") - 1]))
211 : ) {
212 : path_start = ZSTR_VAL(decoded) + sizeof("localhost") - 1;
213 : path = zend_string_init(path_start, ZSTR_LEN(decoded) - (sizeof("localhost") - 1), 0);
214 : zend_string_release(decoded);
215 : decoded = path;
216 : }
217 :
218 : if (ZSTR_LEN(decoded) >= 3 &&
219 : lsp_is_path_separator(ZSTR_VAL(decoded)[0]) &&
220 : isalpha((unsigned char) ZSTR_VAL(decoded)[1]) &&
221 : ZSTR_VAL(decoded)[2] == ':'
222 : ) {
223 : path = zend_string_init(ZSTR_VAL(decoded) + 1, ZSTR_LEN(decoded) - 1, 0);
224 : zend_string_release(decoded);
225 :
226 : return path;
227 : }
228 :
229 : if (!lsp_path_is_absolute(decoded) && strchr(ZSTR_VAL(decoded), '/') != NULL) {
230 : path = strpprintf(0, "//%s", ZSTR_VAL(decoded));
231 : zend_string_release(decoded);
232 :
233 : return path;
234 : }
235 : #endif
236 :
237 1128 : return decoded;
238 : }
239 :
240 0 : return zend_string_copy(uri);
241 : }
242 :
243 : /* Percent-encode every path byte a file URI cannot carry verbatim. Without
244 : * this, paths containing spaces, '#' or non-ASCII produce invalid URIs that
245 : * clients refuse to open or fail to match against their own buffers. */
246 1048 : static inline zend_string *lsp_uri_encode_path(const char *value, size_t length)
247 : {
248 : static const char hex[] = "0123456789ABCDEF";
249 1048 : smart_str encoded = {0};
250 : size_t i;
251 : unsigned char c;
252 :
253 45160 : for (i = 0; i < length; i++) {
254 44112 : c = (unsigned char) value[i];
255 44112 : if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~' || c == '/') {
256 44100 : smart_str_appendc(&encoded, (char) c);
257 : } else {
258 12 : smart_str_appendc(&encoded, '%');
259 12 : smart_str_appendc(&encoded, hex[c >> 4]);
260 12 : smart_str_appendc(&encoded, hex[c & 0x0F]);
261 : }
262 : }
263 :
264 1048 : smart_str_0(&encoded);
265 :
266 1048 : return encoded.s ? encoded.s : ZSTR_EMPTY_ALLOC();
267 : }
268 :
269 1048 : extern zend_string *lsp_uri_from_path(zend_string *path)
270 : {
271 : zend_string *encoded, *uri;
272 : #if defined(_WIN32)
273 : zend_string *uri_path;
274 : size_t i;
275 :
276 : uri_path = zend_string_init(ZSTR_VAL(path), ZSTR_LEN(path), 0);
277 : for (i = 0; i < ZSTR_LEN(uri_path); i++) {
278 : if (ZSTR_VAL(uri_path)[i] == '\\') {
279 : ZSTR_VAL(uri_path)[i] = '/';
280 : }
281 : }
282 :
283 : if (ZSTR_LEN(uri_path) >= 2 && isalpha((unsigned char) ZSTR_VAL(uri_path)[0]) && ZSTR_VAL(uri_path)[1] == ':') {
284 : encoded = lsp_uri_encode_path(ZSTR_VAL(uri_path) + 2, ZSTR_LEN(uri_path) - 2);
285 : uri = strpprintf(0, "file:///%.2s%s", ZSTR_VAL(uri_path), ZSTR_VAL(encoded));
286 : } else {
287 : encoded = lsp_uri_encode_path(ZSTR_VAL(uri_path), ZSTR_LEN(uri_path));
288 : if (ZSTR_LEN(uri_path) >= 2 && lsp_is_path_separator(ZSTR_VAL(uri_path)[0]) && lsp_is_path_separator(ZSTR_VAL(uri_path)[1])) {
289 : uri = strpprintf(0, "file:%s", ZSTR_VAL(encoded));
290 : } else {
291 : uri = strpprintf(0, "file://%s", ZSTR_VAL(encoded));
292 : }
293 : }
294 :
295 : zend_string_release(encoded);
296 : zend_string_release(uri_path);
297 :
298 : return uri;
299 : #else
300 1048 : encoded = lsp_uri_encode_path(ZSTR_VAL(path), ZSTR_LEN(path));
301 1048 : uri = strpprintf(0, "file://%s", ZSTR_VAL(encoded));
302 1048 : zend_string_release(encoded);
303 :
304 1048 : return uri;
305 : #endif
306 : }
307 :
308 3900 : extern zend_string *lsp_read_file(zend_string *path)
309 : {
310 : zend_string *contents;
311 : php_stream *stream;
312 :
313 3900 : stream = php_stream_open_wrapper(ZSTR_VAL(path), "rb", IGNORE_PATH, NULL);
314 3900 : if (!stream) {
315 4 : return zend_empty_string;
316 : }
317 :
318 3896 : contents = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
319 3896 : php_stream_close(stream);
320 3896 : if (!contents) {
321 0 : return zend_empty_string;
322 : }
323 :
324 3896 : return contents;
325 : }
326 :
327 752 : static inline double lsp_array_double(zval *array, const char *key, double fallback)
328 : {
329 752 : zval *value = lsp_array_find(array, key);
330 :
331 752 : if (!value) {
332 592 : return fallback;
333 : }
334 :
335 160 : if (Z_TYPE_P(value) == IS_DOUBLE) {
336 4 : return Z_DVAL_P(value);
337 : }
338 :
339 156 : if (Z_TYPE_P(value) == IS_LONG) {
340 156 : return (double) Z_LVAL_P(value);
341 : }
342 :
343 0 : return fallback;
344 : }
345 :
346 444 : static inline bool lsp_array_bool(zval *array, const char *key, bool fallback)
347 : {
348 444 : zval *value = lsp_array_find(array, key);
349 :
350 444 : return value && Z_TYPE_P(value) == IS_TRUE ? true : (value && Z_TYPE_P(value) == IS_FALSE ? false : fallback);
351 : }
352 :
353 452 : static inline size_t lsp_parse_size_value(zval *value, size_t fallback)
354 : {
355 : const char *raw, *end;
356 : zend_string *string;
357 452 : size_t multiplier = 1;
358 : double number;
359 :
360 452 : if (!value) {
361 0 : return fallback;
362 : }
363 :
364 452 : if (Z_TYPE_P(value) == IS_LONG && Z_LVAL_P(value) > 0) {
365 0 : return (size_t) Z_LVAL_P(value);
366 : }
367 :
368 452 : if (Z_TYPE_P(value) != IS_STRING) {
369 0 : return fallback;
370 : }
371 :
372 452 : string = Z_STR_P(value);
373 452 : raw = ZSTR_VAL(string);
374 452 : errno = 0;
375 452 : number = strtod(raw, (char **) &end);
376 452 : if (errno != 0 || number <= 0) {
377 0 : return fallback;
378 : }
379 :
380 452 : while (*end && isspace((unsigned char) *end)) {
381 0 : end++;
382 : }
383 :
384 452 : if (*end == 'k' || *end == 'K') {
385 0 : multiplier = 1024u;
386 452 : } else if (*end == 'm' || *end == 'M') {
387 452 : multiplier = 1024u * 1024u;
388 0 : } else if (*end == 'g' || *end == 'G') {
389 0 : multiplier = 1024u * 1024u * 1024u;
390 : }
391 :
392 452 : return (size_t) (number * (double) multiplier);
393 : }
394 :
395 548 : static inline void lsp_options_parse_analyzer(lsp_options *options, zval *value)
396 : {
397 : zval *entry;
398 :
399 548 : options->analyzer_auto = true;
400 548 : options->analyzer_phpstan = false;
401 548 : options->analyzer_psalm = false;
402 548 : options->analyzer_psalm_ls = false;
403 :
404 548 : if (!value || lsp_zval_string_equals_literal(value, "auto")) {
405 16 : return;
406 : }
407 :
408 532 : if (lsp_zval_string_equals_literal(value, "lsparrot")) {
409 360 : options->analyzer_auto = false;
410 :
411 360 : return;
412 : }
413 :
414 172 : if (lsp_zval_string_equals_literal(value, "phpstan")) {
415 92 : options->analyzer_auto = false;
416 92 : options->analyzer_phpstan = true;
417 :
418 92 : return;
419 : }
420 :
421 80 : if (lsp_zval_string_equals_literal(value, "psalm")) {
422 28 : options->analyzer_auto = false;
423 28 : options->analyzer_psalm = true;
424 :
425 28 : return;
426 : }
427 :
428 52 : if (lsp_zval_string_equals_literal(value, "psalm-ls")) {
429 32 : options->analyzer_auto = false;
430 32 : options->analyzer_psalm_ls = true;
431 :
432 32 : return;
433 : }
434 :
435 20 : if (Z_TYPE_P(value) != IS_ARRAY) {
436 0 : return;
437 : }
438 :
439 20 : options->analyzer_auto = false;
440 60 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(value), entry) {
441 40 : if (lsp_zval_string_equals_literal(entry, "phpstan")) {
442 20 : options->analyzer_phpstan = true;
443 20 : } else if (lsp_zval_string_equals_literal(entry, "psalm")) {
444 20 : options->analyzer_psalm = true;
445 0 : } else if (lsp_zval_string_equals_literal(entry, "psalm-ls")) {
446 0 : options->analyzer_psalm_ls = true;
447 : }
448 : } ZEND_HASH_FOREACH_END();
449 : }
450 :
451 548 : static inline void lsp_options_parse_worker_php_args(lsp_options *options, zval *value)
452 : {
453 : zval *entry;
454 548 : uint32_t count = 0, index = 0;
455 :
456 548 : options->worker_php_args = NULL;
457 548 : options->worker_php_arg_count = 0;
458 548 : if (!value || Z_TYPE_P(value) != IS_ARRAY) {
459 544 : return;
460 : }
461 :
462 8 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(value), entry) {
463 4 : if (Z_TYPE_P(entry) == IS_STRING && Z_STRLEN_P(entry) > 0) {
464 4 : count++;
465 : }
466 : } ZEND_HASH_FOREACH_END();
467 :
468 4 : if (count == 0) {
469 0 : return;
470 : }
471 :
472 4 : options->worker_php_args = ecalloc(count, sizeof(zend_string *));
473 4 : options->worker_php_arg_count = count;
474 8 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(value), entry) {
475 4 : if (Z_TYPE_P(entry) == IS_STRING && Z_STRLEN_P(entry) > 0) {
476 4 : options->worker_php_args[index++] = zend_string_copy(Z_STR_P(entry));
477 : }
478 : } ZEND_HASH_FOREACH_END();
479 : }
480 :
481 548 : static inline void lsp_options_parse_phpstan(lsp_options *options, zval *value)
482 : {
483 : zend_long level;
484 :
485 548 : if (!value || Z_TYPE_P(value) != IS_ARRAY) {
486 536 : return;
487 : }
488 :
489 12 : level = lsp_array_long(value, "level", options->phpstan_level);
490 12 : options->phpstan_level = level >= 0 ? level : options->phpstan_level;
491 : }
492 :
493 548 : static inline void lsp_options_parse_psalm_cli(lsp_options *options, zval *value)
494 : {
495 : zend_long level;
496 :
497 548 : if (!value || Z_TYPE_P(value) != IS_ARRAY) {
498 504 : return;
499 : }
500 :
501 44 : level = lsp_array_long(value, "level", options->psalm_level);
502 44 : options->psalm_level = level >= 0 ? level : options->psalm_level;
503 : }
504 :
505 44 : static inline lsp_psalm_transport lsp_parse_psalm_transport(zval *value, lsp_psalm_transport fallback)
506 : {
507 44 : if (!value || Z_TYPE_P(value) != IS_STRING) {
508 12 : return fallback;
509 : }
510 :
511 32 : if (zend_string_equals_literal(Z_STR_P(value), "cli")) {
512 0 : return LSP_PSALM_TRANSPORT_CLI;
513 : }
514 :
515 36 : if (zend_string_equals_literal(Z_STR_P(value), "languageServer") ||
516 4 : zend_string_equals_literal(Z_STR_P(value), "language-server") ||
517 0 : zend_string_equals_literal(Z_STR_P(value), "lsp")
518 : ) {
519 32 : return LSP_PSALM_TRANSPORT_LANGUAGE_SERVER;
520 : }
521 :
522 0 : return LSP_PSALM_TRANSPORT_AUTO;
523 : }
524 :
525 548 : static inline void lsp_options_parse_psalm(lsp_options *options, zval *value)
526 : {
527 : zend_long debounce, wait_ms;
528 :
529 548 : if (!value || Z_TYPE_P(value) != IS_ARRAY) {
530 504 : return;
531 : }
532 :
533 44 : options->psalm_transport = lsp_parse_psalm_transport(lsp_array_find(value, "transport"), options->psalm_transport);
534 44 : options->psalm_on_change = lsp_array_bool(value, "onChange", options->psalm_on_change);
535 44 : options->psalm_enable_autocomplete = lsp_array_bool(value, "enableAutocomplete", options->psalm_enable_autocomplete);
536 44 : options->psalm_enable_diagnostics = lsp_array_bool(value, "enableDiagnostics", options->psalm_enable_diagnostics);
537 44 : options->psalm_enable_hover = lsp_array_bool(value, "enableHover", options->psalm_enable_hover);
538 44 : options->psalm_enable_definition = lsp_array_bool(value, "enableDefinition", options->psalm_enable_definition);
539 44 : options->psalm_enable_signature_help = lsp_array_bool(value, "enableSignatureHelp", options->psalm_enable_signature_help);
540 44 : options->psalm_show_info = lsp_array_bool(value, "showInfo", options->psalm_show_info);
541 44 : options->psalm_live_dead_code_diagnostics = lsp_array_bool(value, "liveDeadCodeDiagnostics", options->psalm_live_dead_code_diagnostics);
542 44 : options->psalm_in_memory = lsp_array_bool(value, "inMemory", options->psalm_in_memory);
543 44 : debounce = lsp_array_long(value, "onChangeDebounceMs", options->psalm_on_change_debounce_ms);
544 44 : wait_ms = lsp_array_long(value, "maxResponseWaitMs", options->psalm_max_response_wait_ms);
545 44 : options->psalm_on_change_debounce_ms = debounce >= 0 ? debounce : options->psalm_on_change_debounce_ms;
546 44 : options->psalm_max_response_wait_ms = wait_ms >= 0 ? wait_ms : options->psalm_max_response_wait_ms;
547 : }
548 :
549 552 : static inline void lsp_options_parse_formatting(lsp_options *options, zval *value)
550 : {
551 : zend_string *style;
552 : zend_long indent_size;
553 :
554 552 : if (!value || Z_TYPE_P(value) != IS_ARRAY) {
555 540 : return;
556 : }
557 :
558 12 : options->formatting_enabled = lsp_array_bool(value, "enabled", options->formatting_enabled);
559 12 : options->formatting_reindent = lsp_array_bool(value, "reindent", options->formatting_reindent);
560 12 : options->formatting_trim_trailing_whitespace = lsp_array_bool(value, "trimTrailingWhitespace", options->formatting_trim_trailing_whitespace);
561 12 : options->formatting_insert_final_newline = lsp_array_bool(value, "insertFinalNewline", options->formatting_insert_final_newline);
562 :
563 : /* 0 keeps following the client's tabSize per request. */
564 12 : indent_size = lsp_array_long(value, "indentSize", options->formatting_indent_size);
565 12 : if (indent_size >= 0 && indent_size <= 16) {
566 12 : options->formatting_indent_size = indent_size;
567 : }
568 :
569 12 : style = lsp_array_string(value, "indentStyle");
570 12 : if (style) {
571 8 : if (zend_string_equals_literal_ci(style, "space") || zend_string_equals_literal_ci(style, "spaces")) {
572 0 : options->formatting_indent_style = LSP_INDENT_STYLE_SPACE;
573 8 : } else if (zend_string_equals_literal_ci(style, "tab") || zend_string_equals_literal_ci(style, "tabs")) {
574 8 : options->formatting_indent_style = LSP_INDENT_STYLE_TAB;
575 0 : } else if (zend_string_equals_literal_ci(style, "client")) {
576 0 : options->formatting_indent_style = LSP_INDENT_STYLE_CLIENT;
577 : }
578 : }
579 : }
580 :
581 548 : extern void lsp_options_from_zval(lsp_options *options, zval *value)
582 : {
583 : zend_long phpstan_level, psalm_level;
584 : zend_string *memory_limit;
585 : zval *symbol_index, *workers, *phpstan, *psalm;
586 :
587 548 : memset(options, 0, sizeof(*options));
588 548 : options->symbol_index_size = 64u * 1024u * 1024u;
589 548 : options->worker_count = 0;
590 548 : options->phpstan_level = 6;
591 548 : options->psalm_level = 6;
592 548 : options->formatting_enabled = true;
593 548 : options->formatting_reindent = true;
594 548 : options->formatting_trim_trailing_whitespace = true;
595 548 : options->formatting_insert_final_newline = true;
596 548 : options->formatting_indent_style = LSP_INDENT_STYLE_CLIENT;
597 548 : options->formatting_indent_size = 0;
598 548 : options->memory_limit = zend_string_init("-1", sizeof("-1") - 1, 0);
599 548 : options->analyzer_diagnostics_timeout = 60.0;
600 548 : options->analyzer_type_query_timeout = 5.0;
601 548 : options->analyzer_auto = true;
602 548 : options->psalm_transport = LSP_PSALM_TRANSPORT_AUTO;
603 548 : options->psalm_on_change = true;
604 548 : options->psalm_enable_autocomplete = true;
605 548 : options->psalm_enable_diagnostics = true;
606 548 : options->psalm_enable_hover = true;
607 548 : options->psalm_enable_definition = true;
608 548 : options->psalm_enable_signature_help = true;
609 548 : options->psalm_show_info = false;
610 548 : options->psalm_live_dead_code_diagnostics = false;
611 548 : options->psalm_in_memory = false;
612 548 : options->psalm_on_change_debounce_ms = 500;
613 548 : options->psalm_max_response_wait_ms = 200;
614 :
615 548 : if (!value || Z_TYPE_P(value) != IS_ARRAY) {
616 0 : return;
617 : }
618 :
619 548 : lsp_options_parse_analyzer(options, lsp_array_find(value, "analyzer"));
620 548 : lsp_options_parse_worker_php_args(options, lsp_array_find(value, "workerPhpArgs"));
621 :
622 548 : memory_limit = lsp_array_string(value, "memoryLimit");
623 548 : if (memory_limit && ZSTR_LEN(memory_limit) > 0) {
624 0 : zend_string_release(options->memory_limit);
625 0 : options->memory_limit = zend_string_copy(memory_limit);
626 : }
627 :
628 548 : phpstan_level = lsp_array_long(value, "phpstanLevel", options->phpstan_level);
629 548 : options->phpstan_level = phpstan_level >= 0 ? phpstan_level : options->phpstan_level;
630 548 : psalm_level = lsp_array_long(value, "psalmLevel", options->psalm_level);
631 548 : options->psalm_level = psalm_level >= 0 ? psalm_level : options->psalm_level;
632 :
633 548 : symbol_index = lsp_array_find(value, "symbolIndex");
634 548 : if (symbol_index && Z_TYPE_P(symbol_index) == IS_ARRAY) {
635 452 : options->symbol_index_size = lsp_parse_size_value(lsp_array_find(symbol_index, "size"), options->symbol_index_size);
636 : }
637 :
638 548 : workers = lsp_array_find(value, "workers");
639 548 : if (workers && Z_TYPE_P(workers) == IS_ARRAY) {
640 372 : options->worker_count = lsp_array_long(workers, "count", 0);
641 372 : if (options->worker_count < 0) {
642 0 : options->worker_count = 0;
643 : }
644 :
645 372 : options->analyzer_diagnostics_timeout = lsp_array_double(workers, "analyzerDiagnosticsTimeout", 60.0);
646 372 : if (options->analyzer_diagnostics_timeout < 0.0) {
647 0 : options->analyzer_diagnostics_timeout = 60.0;
648 : }
649 :
650 : /* Interactive (completion/hover) type queries block the request
651 : * loop, so they get a much shorter budget than diagnostics; when
652 : * the resident runner is in use, a timed-out query keeps running
653 : * asynchronously and lands in the type cache. */
654 372 : options->analyzer_type_query_timeout = lsp_array_double(workers, "analyzerTypeQueryTimeout", 5.0);
655 372 : if (options->analyzer_type_query_timeout < 0.0) {
656 0 : options->analyzer_type_query_timeout = 5.0;
657 : }
658 : }
659 :
660 548 : lsp_options_parse_formatting(options, lsp_array_find(value, "formatting"));
661 :
662 548 : phpstan = lsp_array_find(value, "phpstan");
663 548 : lsp_options_parse_phpstan(options, phpstan);
664 :
665 548 : psalm = lsp_array_find(value, "psalm");
666 548 : lsp_options_parse_psalm_cli(options, psalm);
667 548 : lsp_options_parse_psalm(options, psalm);
668 : }
669 :
670 : /* workspace/didChangeConfiguration: apply the live-safe option subset without
671 : * a server restart. Structural options (symbol index size, worker fan-out,
672 : * analyzer selection) stay fixed for the process lifetime. Accepts either
673 : * the options at the top level of `settings` or nested under `lsparrot`. */
674 4 : extern void lsp_options_apply_runtime(lsp_options *options, zval *params)
675 : {
676 : zval *settings, *scoped;
677 : zend_string *memory_limit;
678 : zend_long level;
679 : double timeout;
680 :
681 4 : settings = params ? lsp_array_find(params, "settings") : NULL;
682 4 : if (!settings || Z_TYPE_P(settings) != IS_ARRAY) {
683 0 : return;
684 : }
685 :
686 4 : scoped = lsp_array_find(settings, "lsparrot");
687 4 : if (scoped && Z_TYPE_P(scoped) == IS_ARRAY) {
688 4 : settings = scoped;
689 : }
690 :
691 4 : lsp_options_parse_formatting(options, lsp_array_find(settings, "formatting"));
692 :
693 4 : level = lsp_array_long(settings, "phpstanLevel", options->phpstan_level);
694 4 : if (level >= 0) {
695 4 : options->phpstan_level = level;
696 : }
697 :
698 4 : level = lsp_array_long(settings, "psalmLevel", options->psalm_level);
699 4 : if (level >= 0) {
700 4 : options->psalm_level = level;
701 : }
702 :
703 4 : memory_limit = lsp_array_string(settings, "memoryLimit");
704 4 : if (memory_limit && ZSTR_LEN(memory_limit) > 0) {
705 0 : zend_string_release(options->memory_limit);
706 0 : options->memory_limit = zend_string_copy(memory_limit);
707 : }
708 :
709 4 : timeout = lsp_array_double(settings, "analyzerDiagnosticsTimeout", options->analyzer_diagnostics_timeout);
710 4 : if (timeout >= 0.0) {
711 4 : options->analyzer_diagnostics_timeout = timeout;
712 : }
713 :
714 4 : timeout = lsp_array_double(settings, "analyzerTypeQueryTimeout", options->analyzer_type_query_timeout);
715 4 : if (timeout >= 0.0) {
716 4 : options->analyzer_type_query_timeout = timeout;
717 : }
718 : }
719 :
720 548 : extern void lsp_options_destroy(lsp_options *options)
721 : {
722 : uint32_t i;
723 :
724 552 : for (i = 0; i < options->worker_php_arg_count; i++) {
725 4 : if (options->worker_php_args[i]) {
726 4 : zend_string_release(options->worker_php_args[i]);
727 : }
728 : }
729 :
730 548 : if (options->worker_php_args) {
731 4 : efree(options->worker_php_args);
732 : }
733 :
734 548 : if (options->memory_limit) {
735 548 : zend_string_release(options->memory_limit);
736 : }
737 :
738 548 : memset(options, 0, sizeof(*options));
739 548 : }
740 :
741 824 : extern void lsp_position_from_zval(zval *position, zend_long *line, zend_long *character)
742 : {
743 824 : *line = 0;
744 824 : *character = 0;
745 :
746 824 : if (!position || Z_TYPE_P(position) != IS_ARRAY) {
747 0 : return;
748 : }
749 :
750 824 : *line = lsp_array_long(position, "line", 0);
751 824 : *character = lsp_array_long(position, "character", 0);
752 :
753 824 : if (*line < 0) {
754 0 : *line = 0;
755 : }
756 :
757 824 : if (*character < 0) {
758 0 : *character = 0;
759 : }
760 : }
761 :
762 : /* LSP positions are UTF-16 code units (the protocol default encoding), while
763 : * documents are stored as UTF-8 bytes. Incoming character offsets have to be
764 : * widened to byte offsets and outgoing byte offsets narrowed back to UTF-16
765 : * units, or every feature drifts on lines containing multibyte text. */
766 836 : extern size_t lsp_utf16_units_to_byte_offset(const char *value, size_t line_start, size_t length, zend_long character)
767 : {
768 836 : size_t offset = line_start, step;
769 836 : zend_long units = 0;
770 : unsigned char lead;
771 :
772 15676 : while (offset < length && units < character) {
773 14840 : lead = (unsigned char) value[offset];
774 14840 : if (lead == '\n' || lead == '\r') {
775 : break;
776 : }
777 :
778 14840 : if (lead < 0x80) {
779 14792 : step = 1;
780 14792 : units += 1;
781 48 : } else if ((lead & 0xE0) == 0xC0) {
782 0 : step = 2;
783 0 : units += 1;
784 48 : } else if ((lead & 0xF0) == 0xE0) {
785 48 : step = 3;
786 48 : units += 1;
787 0 : } else if ((lead & 0xF8) == 0xF0) {
788 : /* Astral plane characters use a surrogate pair in UTF-16. */
789 0 : step = 4;
790 0 : units += 2;
791 : } else {
792 0 : step = 1;
793 0 : units += 1;
794 : }
795 :
796 14840 : offset += step;
797 14840 : if (offset > length) {
798 0 : offset = length;
799 : }
800 : }
801 :
802 836 : return offset;
803 : }
804 :
805 8452 : extern zend_long lsp_byte_offset_to_utf16_units(const char *value, size_t line_start, size_t end_offset)
806 : {
807 8452 : size_t offset = line_start;
808 8452 : zend_long units = 0;
809 : unsigned char lead;
810 :
811 112652 : while (offset < end_offset) {
812 104200 : lead = (unsigned char) value[offset];
813 104200 : if (lead < 0x80) {
814 104152 : offset += 1;
815 104152 : units += 1;
816 48 : } else if ((lead & 0xE0) == 0xC0) {
817 0 : offset += 2;
818 0 : units += 1;
819 48 : } else if ((lead & 0xF0) == 0xE0) {
820 48 : offset += 3;
821 48 : units += 1;
822 0 : } else if ((lead & 0xF8) == 0xF0) {
823 0 : offset += 4;
824 0 : units += 2;
825 : } else {
826 0 : offset += 1;
827 0 : units += 1;
828 : }
829 : }
830 :
831 8452 : return units;
832 : }
833 :
834 840 : extern size_t lsp_offset_at(zend_string *text, zend_long line, zend_long character)
835 : {
836 840 : const char *value = ZSTR_VAL(text), *next;
837 : zend_long current_line;
838 840 : size_t length = ZSTR_LEN(text), offset = 0;
839 :
840 9680 : for (current_line = 0; current_line < line; current_line++) {
841 8844 : next = memchr(value + offset, '\n', length - offset);
842 8844 : if (!next) {
843 4 : return length;
844 : }
845 :
846 8840 : offset = (size_t) (next - value) + 1;
847 : }
848 :
849 : /* Also clamps an overshooting character to the end of its line instead
850 : * of walking into later lines. */
851 836 : return lsp_utf16_units_to_byte_offset(value, offset, length, character);
852 : }
853 :
854 5368 : static inline bool lsp_is_word_char(char c)
855 : {
856 5368 : unsigned char ch = (unsigned char) c;
857 :
858 : /* Bytes >= 0x80 are legal in PHP identifiers ([\x80-\xff] in the lexer
859 : * grammar), which is how non-ASCII variable and class names appear. */
860 5368 : return isalnum(ch) || ch == '_' || ch == '$' || ch == '\\' || ch >= 0x80;
861 : }
862 :
863 572 : extern zend_string *lsp_prefix_at(zend_string *text, size_t offset)
864 : {
865 572 : const char *value = ZSTR_VAL(text);
866 : size_t start;
867 :
868 572 : if (offset > ZSTR_LEN(text)) {
869 0 : offset = ZSTR_LEN(text);
870 : }
871 :
872 572 : start = offset;
873 2716 : while (start > 0 && lsp_is_word_char(value[start - 1])) {
874 2144 : start--;
875 : }
876 :
877 572 : return zend_string_init(value + start, offset - start, 0);
878 : }
879 :
880 292 : extern zend_string *lsp_word_at(zend_string *text, size_t offset)
881 : {
882 292 : const char *value = ZSTR_VAL(text);
883 292 : size_t start, end, length = ZSTR_LEN(text);
884 :
885 292 : if (offset > length) {
886 0 : offset = length;
887 : }
888 :
889 292 : start = offset;
890 :
891 1216 : while (start > 0 && lsp_is_word_char(value[start - 1])) {
892 924 : start--;
893 : }
894 :
895 292 : end = offset;
896 :
897 1440 : while (end < length && lsp_is_word_char(value[end])) {
898 1148 : end++;
899 : }
900 :
901 292 : return zend_string_init(value + start, end - start, 0);
902 : }
903 :
904 532960 : extern bool lsp_matches_prefix_string(zend_string *label, zend_string *prefix)
905 : {
906 532960 : const char *label_value = ZSTR_VAL(label), *prefix_value = ZSTR_VAL(prefix);
907 532960 : size_t label_length = ZSTR_LEN(label), prefix_length = ZSTR_LEN(prefix), i;
908 :
909 532960 : prefix_value = lsp_skip_global_namespace_prefix(prefix_value, &prefix_length);
910 532960 : label_value = lsp_skip_global_namespace_prefix(label_value, &label_length);
911 :
912 532960 : if (prefix_length > 0 && prefix_value[0] == '$') {
913 132 : prefix_value++;
914 132 : prefix_length--;
915 : }
916 :
917 532960 : if (label_length > 0 && label_value[0] == '$') {
918 196 : label_value++;
919 196 : label_length--;
920 : }
921 :
922 532960 : if (prefix_length == 0) {
923 1604 : return true;
924 : }
925 :
926 531356 : if (prefix_length > label_length) {
927 43052 : return false;
928 : }
929 :
930 517580 : for (i = 0; i < prefix_length; i++) {
931 516144 : if (tolower((unsigned char) label_value[i]) != tolower((unsigned char) prefix_value[i])) {
932 486868 : return false;
933 : }
934 : }
935 :
936 1436 : return true;
937 : }
938 :
939 1724 : extern bool lsp_matches_prefix_literal(const char *label, zend_string *prefix)
940 : {
941 1724 : zend_string *label_string = zend_string_init(label, strlen(label), 0);
942 1724 : bool result = lsp_matches_prefix_string(label_string, prefix);
943 :
944 1724 : zend_string_release(label_string);
945 :
946 1724 : return result;
947 : }
948 :
949 548 : extern bool lsp_is_member_access_completion(zend_string *text, size_t offset, zend_string *prefix)
950 : {
951 548 : const char *value = ZSTR_VAL(text);
952 : size_t i;
953 :
954 548 : if (offset > ZSTR_LEN(text)) {
955 0 : offset = ZSTR_LEN(text);
956 : }
957 :
958 548 : i = offset >= ZSTR_LEN(prefix) ? offset - ZSTR_LEN(prefix) : 0;
959 :
960 912 : while (i > 0 && isspace((unsigned char) value[i - 1])) {
961 364 : i--;
962 : }
963 :
964 860 : return (i >= 2 && value[i - 2] == '-' && value[i - 1] == '>') ||
965 312 : (i >= 3 && value[i - 3] == '?' && value[i - 2] == '-' && value[i - 1] == '>')
966 : ;
967 : }
|