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 : #define LSP_PSALM_LS_WRITE_TIMEOUT 2.0
17 : #define LSP_PSALM_LS_SHUTDOWN_TIMEOUT 0.5
18 : #define LSP_PSALM_LS_TERMINATE_TIMEOUT 1.0
19 : #define LSP_PSALM_LS_KILL_TIMEOUT 1.0
20 :
21 : typedef struct _lsp_psalm_ls_buffer {
22 : char *data;
23 : size_t length;
24 : size_t capacity;
25 : } lsp_psalm_ls_buffer;
26 :
27 : typedef struct _lsp_psalm_ls_session {
28 : zend_string *root;
29 : zend_string *config;
30 : lsp_process_id pid;
31 : lsp_pipe_handle input_pipe;
32 : lsp_pipe_handle output_pipe;
33 : lsp_pipe_handle error_pipe;
34 : zend_long next_id;
35 : zend_long initialize_id;
36 : bool running;
37 : bool initialized;
38 : bool failed;
39 : uint32_t restart_count;
40 : double started_at;
41 : HashTable diagnostics;
42 : HashTable pending;
43 : lsp_psalm_ls_buffer output_buffer;
44 : lsp_psalm_ls_buffer error_buffer;
45 : } lsp_psalm_ls_session;
46 :
47 3928 : static inline double lsp_psalm_ls_now(void)
48 : {
49 3928 : return lsp_now_seconds();
50 : }
51 :
52 64 : static inline void lsp_psalm_ls_buffer_destroy(lsp_psalm_ls_buffer *buffer)
53 : {
54 64 : if (buffer->data) {
55 28 : efree(buffer->data);
56 : }
57 :
58 64 : memset(buffer, 0, sizeof(*buffer));
59 64 : }
60 :
61 88 : static inline bool lsp_psalm_ls_buffer_append(lsp_psalm_ls_buffer *buffer, const char *data, size_t length)
62 : {
63 : size_t capacity;
64 :
65 88 : if (length == 0) {
66 0 : return true;
67 : }
68 :
69 88 : if (buffer->length + length > buffer->capacity) {
70 28 : capacity = buffer->capacity == 0 ? 8192 : buffer->capacity;
71 28 : while (capacity < buffer->length + length) {
72 0 : capacity *= 2;
73 : }
74 :
75 28 : buffer->data = erealloc(buffer->data, capacity);
76 28 : buffer->capacity = capacity;
77 : }
78 :
79 88 : memcpy(buffer->data + buffer->length, data, length);
80 88 : buffer->length += length;
81 :
82 88 : return true;
83 : }
84 :
85 88 : static inline void lsp_psalm_ls_buffer_consume(lsp_psalm_ls_buffer *buffer, size_t length)
86 : {
87 88 : if (length >= buffer->length) {
88 88 : buffer->length = 0;
89 :
90 88 : return;
91 : }
92 :
93 0 : memmove(buffer->data, buffer->data + length, buffer->length - length);
94 0 : buffer->length -= length;
95 : }
96 :
97 650 : static inline bool lsp_psalm_ls_find_header(lsp_psalm_ls_buffer *buffer, size_t *header_end, size_t *body_start)
98 : {
99 : size_t i;
100 :
101 2270 : for (i = 0; i + 3 < buffer->length; i++) {
102 1708 : if (buffer->data[i] == '\r' && buffer->data[i + 1] == '\n' && buffer->data[i + 2] == '\r' && buffer->data[i + 3] == '\n') {
103 88 : *header_end = i;
104 88 : *body_start = i + 4;
105 :
106 88 : return true;
107 : }
108 : }
109 :
110 562 : for (i = 0; i + 1 < buffer->length; i++) {
111 0 : if (buffer->data[i] == '\n' && buffer->data[i + 1] == '\n') {
112 0 : *header_end = i;
113 0 : *body_start = i + 2;
114 :
115 0 : return true;
116 : }
117 : }
118 :
119 562 : return false;
120 : }
121 :
122 88 : static inline bool lsp_psalm_ls_header_length(lsp_psalm_ls_buffer *buffer, size_t header_end, size_t *length)
123 : {
124 88 : const char *prefix = "Content-Length:";
125 88 : size_t prefix_length = strlen(prefix), line_start = 0, line_end, p;
126 :
127 88 : *length = 0;
128 88 : while (line_start < header_end) {
129 88 : line_end = line_start;
130 1708 : while (line_end < header_end && buffer->data[line_end] != '\n' && buffer->data[line_end] != '\r') {
131 1620 : line_end++;
132 : }
133 :
134 88 : if (line_end - line_start >= prefix_length &&
135 88 : strncasecmp(buffer->data + line_start, prefix, prefix_length) == 0
136 : ) {
137 88 : p = line_start + prefix_length;
138 176 : while (p < line_end && isspace((unsigned char) buffer->data[p])) {
139 88 : p++;
140 : }
141 88 : *length = (size_t) strtoull(buffer->data + p, NULL, 10);
142 :
143 88 : return *length > 0;
144 : }
145 :
146 0 : line_start = line_end;
147 0 : while (line_start < header_end && (buffer->data[line_start] == '\n' || buffer->data[line_start] == '\r')) {
148 0 : line_start++;
149 : }
150 : }
151 :
152 0 : return false;
153 : }
154 :
155 650 : static inline bool lsp_psalm_ls_next_body(lsp_psalm_ls_buffer *buffer, zend_string **body)
156 : {
157 : size_t header_end, body_start, length, frame_end;
158 :
159 650 : *body = NULL;
160 650 : if (!lsp_psalm_ls_find_header(buffer, &header_end, &body_start)) {
161 562 : return false;
162 : }
163 :
164 88 : if (!lsp_psalm_ls_header_length(buffer, header_end, &length)) {
165 0 : lsp_psalm_ls_buffer_consume(buffer, body_start);
166 :
167 0 : return false;
168 : }
169 :
170 88 : frame_end = body_start + length;
171 88 : if (buffer->length < frame_end) {
172 0 : return false;
173 : }
174 :
175 88 : *body = zend_string_init(buffer->data + body_start, length, 0);
176 88 : lsp_psalm_ls_buffer_consume(buffer, frame_end);
177 :
178 88 : return true;
179 : }
180 :
181 184 : static inline bool lsp_psalm_ls_send_zval(lsp_psalm_ls_session *session, zval *message)
182 : {
183 184 : smart_str json = {0};
184 : zend_string *header;
185 : bool ok;
186 :
187 184 : if (!session->running || !lsp_pipe_handle_valid(session->input_pipe)) {
188 0 : return false;
189 : }
190 :
191 184 : php_json_encode(&json, message, 0);
192 184 : if (!json.s) {
193 0 : return false;
194 : }
195 :
196 184 : smart_str_0(&json);
197 184 : header = strpprintf(0, "Content-Length: %zu\r\n\r\n", ZSTR_LEN(json.s));
198 368 : ok = lsp_pipe_write_all_timeout(session->input_pipe, ZSTR_VAL(header), ZSTR_LEN(header), LSP_PSALM_LS_WRITE_TIMEOUT) &&
199 184 : lsp_pipe_write_all_timeout(session->input_pipe, ZSTR_VAL(json.s), ZSTR_LEN(json.s), LSP_PSALM_LS_WRITE_TIMEOUT)
200 : ;
201 184 : zend_string_release(header);
202 184 : zend_string_release(json.s);
203 184 : if (!ok) {
204 4 : session->failed = true;
205 4 : session->running = false;
206 : }
207 :
208 184 : return ok;
209 : }
210 :
211 32 : static inline zend_string *lsp_psalm_ls_uri_for_path(zend_string *path)
212 : {
213 32 : return lsp_uri_from_path(path);
214 : }
215 :
216 32 : static inline const char *lsp_psalm_ls_basename(zend_string *path)
217 : {
218 : const char *slash;
219 :
220 32 : slash = lsp_last_path_separator_const(ZSTR_VAL(path));
221 :
222 32 : return slash ? slash + 1 : ZSTR_VAL(path);
223 : }
224 :
225 256 : static inline void lsp_psalm_ls_add_bool_arg(lsp_command *command, const char *name, bool value)
226 : {
227 : zend_string *arg;
228 :
229 256 : arg = strpprintf(0, "%s=%s", name, value ? "true" : "false");
230 256 : lsp_command_add(command, ZSTR_VAL(arg));
231 256 : zend_string_release(arg);
232 256 : }
233 :
234 64 : static inline bool lsp_psalm_ls_id_from_zval(zval *value, zend_long *id)
235 : {
236 : char *end;
237 : long parsed;
238 :
239 64 : if (!value) {
240 0 : return false;
241 : }
242 :
243 64 : if (Z_TYPE_P(value) == IS_LONG) {
244 60 : *id = Z_LVAL_P(value);
245 :
246 60 : return true;
247 : }
248 :
249 4 : if (Z_TYPE_P(value) != IS_STRING || Z_STRLEN_P(value) == 0) {
250 0 : return false;
251 : }
252 :
253 4 : errno = 0;
254 4 : parsed = strtol(Z_STRVAL_P(value), &end, 10);
255 4 : if (errno != 0 || end == Z_STRVAL_P(value) || *end != '\0') {
256 0 : return false;
257 : }
258 :
259 4 : *id = (zend_long) parsed;
260 :
261 4 : return true;
262 : }
263 :
264 32 : static inline bool lsp_psalm_ls_tool(zend_string *project_root, zend_string **tool, bool *language_server_bin)
265 : {
266 32 : *tool = lsp_tool_project_candidate(project_root, "psalm-language-server");
267 32 : *language_server_bin = true;
268 32 : if (*tool) {
269 32 : return true;
270 : }
271 :
272 0 : *tool = lsp_tool_project_candidate(project_root, "psalm");
273 0 : *language_server_bin = false;
274 :
275 0 : return *tool != NULL;
276 : }
277 :
278 32 : static inline void lsp_psalm_ls_build_command(lsp_server *server, zend_string *project_root, zend_string *config, lsp_command *command)
279 : {
280 32 : zend_string *tool = NULL, *root_arg, *config_arg, *debounce_arg;
281 32 : bool language_server_bin = true;
282 : uint32_t i;
283 :
284 32 : lsp_command_init(command);
285 32 : if (!lsp_psalm_ls_tool(project_root, &tool, &language_server_bin)) {
286 0 : return;
287 : }
288 :
289 32 : lsp_command_add(command, lsp_php_binary());
290 :
291 32 : for (i = 0; i < server->options.worker_php_arg_count; i++) {
292 0 : lsp_command_add_zstr(command, server->options.worker_php_args[i]);
293 : }
294 :
295 32 : lsp_command_add_zstr(command, tool);
296 :
297 32 : if (!language_server_bin) {
298 0 : lsp_command_add(command, "--language-server");
299 : }
300 :
301 32 : root_arg = strpprintf(0, "--root=%s", ZSTR_VAL(project_root));
302 32 : config_arg = strpprintf(0, "--config=%s", ZSTR_VAL(config));
303 32 : debounce_arg = strpprintf(0, "--on-change-debounce-ms=" ZEND_LONG_FMT, server->options.psalm_on_change_debounce_ms);
304 32 : lsp_command_add_zstr(command, root_arg);
305 32 : lsp_command_add_zstr(command, config_arg);
306 32 : lsp_command_add_zstr(command, debounce_arg);
307 :
308 32 : if (!server->options.psalm_on_change) {
309 4 : lsp_command_add(command, "--disable-on-change");
310 : }
311 :
312 32 : lsp_psalm_ls_add_bool_arg(command, "--enable-autocomplete", server->options.psalm_enable_autocomplete);
313 32 : lsp_psalm_ls_add_bool_arg(command, "--enable-provide-diagnostics", server->options.psalm_enable_diagnostics);
314 32 : lsp_psalm_ls_add_bool_arg(command, "--enable-provide-hover", server->options.psalm_enable_hover);
315 32 : lsp_psalm_ls_add_bool_arg(command, "--enable-provide-definition", server->options.psalm_enable_definition);
316 32 : lsp_psalm_ls_add_bool_arg(command, "--enable-provide-signature-help", server->options.psalm_enable_signature_help);
317 32 : lsp_psalm_ls_add_bool_arg(command, "--show-diagnostic-warnings", server->options.psalm_show_info);
318 32 : lsp_psalm_ls_add_bool_arg(command, "--in-memory", server->options.psalm_in_memory);
319 32 : lsp_psalm_ls_add_bool_arg(command, "--disable-xdebug", true);
320 32 : zend_string_release(root_arg);
321 32 : zend_string_release(config_arg);
322 32 : zend_string_release(debounce_arg);
323 32 : zend_string_release(tool);
324 : }
325 :
326 28 : static inline void lsp_psalm_ls_send_shutdown_exit(lsp_psalm_ls_session *session)
327 : {
328 : zval message, params;
329 : zend_long id;
330 :
331 28 : if (!session->running || !lsp_pipe_handle_valid(session->input_pipe)) {
332 0 : return;
333 : }
334 :
335 28 : id = session->next_id++;
336 28 : array_init(&message);
337 28 : add_assoc_string(&message, "jsonrpc", "2.0");
338 28 : add_assoc_long(&message, "id", id);
339 28 : add_assoc_string(&message, "method", "shutdown");
340 28 : array_init(¶ms);
341 28 : add_assoc_zval(&message, "params", ¶ms);
342 28 : lsp_psalm_ls_send_zval(session, &message);
343 28 : zval_ptr_dtor(&message);
344 :
345 28 : if (!lsp_pipe_handle_valid(session->input_pipe)) {
346 0 : return;
347 : }
348 :
349 28 : array_init(&message);
350 28 : add_assoc_string(&message, "jsonrpc", "2.0");
351 28 : add_assoc_string(&message, "method", "exit");
352 28 : lsp_psalm_ls_send_zval(session, &message);
353 28 : zval_ptr_dtor(&message);
354 : }
355 :
356 32 : static inline lsp_psalm_ls_session *lsp_psalm_ls_session_alloc(zend_string *project_root, zend_string *config)
357 : {
358 : lsp_psalm_ls_session *session;
359 :
360 32 : session = ecalloc(1, sizeof(*session));
361 32 : session->root = zend_string_copy(project_root);
362 32 : session->config = zend_string_copy(config);
363 32 : session->pid = LSP_INVALID_PROCESS_ID;
364 32 : session->input_pipe = LSP_INVALID_PIPE_HANDLE;
365 32 : session->output_pipe = LSP_INVALID_PIPE_HANDLE;
366 32 : session->error_pipe = LSP_INVALID_PIPE_HANDLE;
367 32 : session->next_id = 1;
368 32 : session->initialize_id = 0;
369 32 : zend_hash_init(&session->diagnostics, 8, NULL, ZVAL_PTR_DTOR, 0);
370 32 : zend_hash_init(&session->pending, 8, NULL, ZVAL_PTR_DTOR, 0);
371 :
372 32 : return session;
373 : }
374 :
375 64 : static inline void lsp_psalm_ls_session_stop(lsp_psalm_ls_session *session)
376 : {
377 : int status;
378 : bool exited;
379 :
380 64 : status = 0;
381 64 : exited = false;
382 64 : if (session->running && lsp_process_id_valid(session->pid)) {
383 28 : lsp_psalm_ls_send_shutdown_exit(session);
384 : }
385 :
386 64 : lsp_pipe_close(&session->input_pipe);
387 64 : lsp_pipe_close(&session->output_pipe);
388 64 : lsp_pipe_close(&session->error_pipe);
389 64 : if (lsp_process_id_valid(session->pid)) {
390 32 : exited = lsp_process_wait_timeout(session->pid, &status, LSP_PSALM_LS_SHUTDOWN_TIMEOUT);
391 32 : if (!exited) {
392 8 : lsp_process_terminate(session->pid);
393 8 : exited = lsp_process_wait_timeout(session->pid, &status, LSP_PSALM_LS_TERMINATE_TIMEOUT);
394 : }
395 32 : if (!exited) {
396 0 : lsp_process_terminate_force(session->pid);
397 0 : lsp_process_wait_timeout(session->pid, &status, LSP_PSALM_LS_KILL_TIMEOUT);
398 : }
399 32 : lsp_process_close(session->pid);
400 32 : session->pid = LSP_INVALID_PROCESS_ID;
401 : }
402 :
403 64 : session->running = false;
404 64 : }
405 :
406 32 : static inline void lsp_psalm_ls_session_free(lsp_psalm_ls_session *session)
407 : {
408 32 : if (!session) {
409 0 : return;
410 : }
411 :
412 32 : lsp_psalm_ls_session_stop(session);
413 32 : zend_hash_destroy(&session->pending);
414 32 : zend_hash_destroy(&session->diagnostics);
415 32 : lsp_psalm_ls_buffer_destroy(&session->output_buffer);
416 32 : lsp_psalm_ls_buffer_destroy(&session->error_buffer);
417 :
418 32 : if (session->config) {
419 32 : zend_string_release(session->config);
420 : }
421 :
422 32 : if (session->root) {
423 32 : zend_string_release(session->root);
424 : }
425 :
426 32 : efree(session);
427 : }
428 :
429 32 : static inline bool lsp_psalm_ls_start_process(lsp_server *server, lsp_psalm_ls_session *session)
430 : {
431 : lsp_command command;
432 : lsp_process_pipes pipes;
433 :
434 32 : lsp_command_init(&command);
435 32 : lsp_psalm_ls_build_command(server, session->root, session->config, &command);
436 32 : if (!command.argv || command.count == 0) {
437 0 : lsp_command_destroy(&command);
438 :
439 0 : return false;
440 : }
441 32 : if (!lsp_process_spawn_piped(&command, session->root, &pipes)) {
442 0 : lsp_command_destroy(&command);
443 :
444 0 : return false;
445 : }
446 :
447 32 : session->pid = pipes.process;
448 32 : session->input_pipe = pipes.input;
449 32 : session->output_pipe = pipes.output;
450 32 : session->error_pipe = pipes.error;
451 32 : session->running = true;
452 32 : lsp_command_destroy(&command);
453 :
454 32 : return true;
455 : }
456 :
457 28 : static inline void lsp_psalm_ls_send_initialized(lsp_psalm_ls_session *session)
458 : {
459 : zval message, params;
460 :
461 28 : array_init(&message);
462 28 : add_assoc_string(&message, "jsonrpc", "2.0");
463 28 : add_assoc_string(&message, "method", "initialized");
464 28 : array_init(¶ms);
465 28 : add_assoc_zval(&message, "params", ¶ms);
466 28 : lsp_psalm_ls_send_zval(session, &message);
467 28 : zval_ptr_dtor(&message);
468 28 : }
469 :
470 32 : static inline void lsp_psalm_ls_send_initialize(lsp_psalm_ls_session *session)
471 : {
472 : zval message, params, capabilities, workspace, folders, folder;
473 : zend_string *root_uri;
474 :
475 32 : root_uri = lsp_psalm_ls_uri_for_path(session->root);
476 32 : session->initialize_id = session->next_id++;
477 32 : array_init(&message);
478 32 : add_assoc_string(&message, "jsonrpc", "2.0");
479 32 : add_assoc_long(&message, "id", session->initialize_id);
480 32 : add_assoc_string(&message, "method", "initialize");
481 32 : array_init(¶ms);
482 32 : add_assoc_long(¶ms, "processId", (zend_long) lsp_current_process_id());
483 32 : add_assoc_str(¶ms, "rootUri", zend_string_copy(root_uri));
484 32 : add_assoc_str(¶ms, "rootPath", zend_string_copy(session->root));
485 32 : array_init(&capabilities);
486 32 : array_init(&workspace);
487 32 : add_assoc_bool(&workspace, "workspaceFolders", true);
488 32 : add_assoc_zval(&capabilities, "workspace", &workspace);
489 32 : add_assoc_zval(¶ms, "capabilities", &capabilities);
490 32 : array_init(&folders);
491 32 : array_init(&folder);
492 32 : add_assoc_str(&folder, "uri", zend_string_copy(root_uri));
493 32 : add_assoc_string(&folder, "name", lsp_psalm_ls_basename(session->root));
494 32 : add_next_index_zval(&folders, &folder);
495 32 : add_assoc_zval(¶ms, "workspaceFolders", &folders);
496 32 : add_assoc_zval(&message, "params", ¶ms);
497 32 : lsp_psalm_ls_send_zval(session, &message);
498 32 : zval_ptr_dtor(&message);
499 32 : zend_string_release(root_uri);
500 32 : }
501 :
502 0 : static inline bool lsp_psalm_ls_document_belongs(lsp_server *server, lsp_document *document, zend_string *project_root)
503 : {
504 : zend_string *root;
505 : bool result;
506 :
507 0 : root = lsp_document_project_root(server, document);
508 0 : result = zend_string_equals(root, project_root);
509 0 : zend_string_release(root);
510 :
511 0 : return result;
512 : }
513 :
514 28 : static inline void lsp_psalm_ls_send_document_open(lsp_psalm_ls_session *session, lsp_document *document)
515 : {
516 : zval message, params, td;
517 :
518 28 : if (!session->initialized) {
519 0 : return;
520 : }
521 :
522 28 : array_init(&message);
523 28 : add_assoc_string(&message, "jsonrpc", "2.0");
524 28 : add_assoc_string(&message, "method", "textDocument/didOpen");
525 28 : array_init(¶ms);
526 28 : array_init(&td);
527 28 : add_assoc_str(&td, "uri", zend_string_copy(document->uri));
528 28 : add_assoc_string(&td, "languageId", "php");
529 28 : add_assoc_long(&td, "version", document->version);
530 28 : add_assoc_str(&td, "text", zend_string_copy(document->text));
531 28 : add_assoc_zval(¶ms, "textDocument", &td);
532 28 : add_assoc_zval(&message, "params", ¶ms);
533 28 : lsp_psalm_ls_send_zval(session, &message);
534 28 : zval_ptr_dtor(&message);
535 : }
536 :
537 0 : static inline void lsp_psalm_ls_send_document_change(lsp_psalm_ls_session *session, lsp_document *document)
538 : {
539 : zval message, params, td, changes, change;
540 :
541 0 : if (!session->initialized) {
542 0 : return;
543 : }
544 :
545 0 : array_init(&message);
546 0 : add_assoc_string(&message, "jsonrpc", "2.0");
547 0 : add_assoc_string(&message, "method", "textDocument/didChange");
548 0 : array_init(¶ms);
549 0 : array_init(&td);
550 0 : add_assoc_str(&td, "uri", zend_string_copy(document->uri));
551 0 : add_assoc_long(&td, "version", document->version);
552 0 : add_assoc_zval(¶ms, "textDocument", &td);
553 0 : array_init(&changes);
554 0 : array_init(&change);
555 0 : add_assoc_str(&change, "text", zend_string_copy(document->text));
556 0 : add_next_index_zval(&changes, &change);
557 0 : add_assoc_zval(¶ms, "contentChanges", &changes);
558 0 : add_assoc_zval(&message, "params", ¶ms);
559 0 : lsp_psalm_ls_send_zval(session, &message);
560 0 : zval_ptr_dtor(&message);
561 : }
562 :
563 0 : static inline void lsp_psalm_ls_send_document_save(lsp_psalm_ls_session *session, lsp_document *document)
564 : {
565 : zval message, params, td;
566 :
567 0 : if (!session->initialized) {
568 0 : return;
569 : }
570 :
571 0 : array_init(&message);
572 0 : add_assoc_string(&message, "jsonrpc", "2.0");
573 0 : add_assoc_string(&message, "method", "textDocument/didSave");
574 0 : array_init(¶ms);
575 0 : array_init(&td);
576 0 : add_assoc_str(&td, "uri", zend_string_copy(document->uri));
577 0 : add_assoc_zval(¶ms, "textDocument", &td);
578 0 : add_assoc_str(¶ms, "text", zend_string_copy(document->text));
579 0 : add_assoc_zval(&message, "params", ¶ms);
580 0 : lsp_psalm_ls_send_zval(session, &message);
581 0 : zval_ptr_dtor(&message);
582 : }
583 :
584 0 : static inline void lsp_psalm_ls_send_document_close(lsp_psalm_ls_session *session, zend_string *uri)
585 : {
586 : zval message, params, td;
587 :
588 0 : if (!session->initialized) {
589 0 : return;
590 : }
591 :
592 0 : array_init(&message);
593 0 : add_assoc_string(&message, "jsonrpc", "2.0");
594 0 : add_assoc_string(&message, "method", "textDocument/didClose");
595 0 : array_init(¶ms);
596 0 : array_init(&td);
597 0 : add_assoc_str(&td, "uri", zend_string_copy(uri));
598 0 : add_assoc_zval(¶ms, "textDocument", &td);
599 0 : add_assoc_zval(&message, "params", ¶ms);
600 0 : lsp_psalm_ls_send_zval(session, &message);
601 0 : zval_ptr_dtor(&message);
602 : }
603 :
604 28 : static inline void lsp_psalm_ls_send_open_documents(lsp_server *server, lsp_psalm_ls_session *session)
605 : {
606 : lsp_document *document;
607 : zval *value;
608 :
609 28 : ZEND_HASH_FOREACH_VAL(&server->documents, value) {
610 0 : if (Z_TYPE_P(value) != IS_PTR) {
611 0 : continue;
612 : }
613 0 : document = (lsp_document *) Z_PTR_P(value);
614 0 : if (document && lsp_psalm_ls_document_belongs(server, document, session->root)) {
615 0 : lsp_psalm_ls_send_document_open(session, document);
616 : }
617 : } ZEND_HASH_FOREACH_END();
618 28 : }
619 :
620 4 : static inline void lsp_psalm_ls_completion_item_set_source(zval *item)
621 : {
622 : zval *data, new_data;
623 :
624 4 : if (Z_TYPE_P(item) != IS_ARRAY) {
625 4 : return;
626 : }
627 :
628 4 : SEPARATE_ARRAY(item);
629 4 : data = zend_hash_str_find(Z_ARRVAL_P(item), "data", sizeof("data") - 1);
630 4 : if (!data || Z_TYPE_P(data) != IS_ARRAY) {
631 4 : array_init(&new_data);
632 4 : add_assoc_string(&new_data, "source", "psalm-ls");
633 4 : add_assoc_zval(item, "data", &new_data);
634 :
635 4 : return;
636 : }
637 :
638 0 : SEPARATE_ARRAY(data);
639 0 : zend_hash_str_del(Z_ARRVAL_P(data), "source", sizeof("source") - 1);
640 0 : add_assoc_string(data, "source", "psalm-ls");
641 : }
642 :
643 16 : static inline void lsp_psalm_ls_store_completion_result(lsp_server *server, zval *pending, zval *result)
644 : {
645 : zend_string *cache_key;
646 : zval items, *source_items, *item, copy;
647 :
648 16 : cache_key = lsp_array_string(pending, "cacheKey");
649 16 : if (!cache_key || !result) {
650 0 : return;
651 : }
652 :
653 16 : source_items = result;
654 16 : if (Z_TYPE_P(result) == IS_ARRAY) {
655 16 : item = zend_hash_str_find(Z_ARRVAL_P(result), "items", sizeof("items") - 1);
656 16 : if (item && Z_TYPE_P(item) == IS_ARRAY) {
657 16 : source_items = item;
658 : }
659 : }
660 16 : if (Z_TYPE_P(source_items) != IS_ARRAY) {
661 0 : return;
662 : }
663 :
664 16 : array_init(&items);
665 20 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(source_items), item) {
666 4 : if (Z_TYPE_P(item) != IS_ARRAY) {
667 0 : continue;
668 : }
669 4 : ZVAL_COPY(©, item);
670 4 : lsp_psalm_ls_completion_item_set_source(©);
671 4 : add_next_index_zval(&items, ©);
672 : } ZEND_HASH_FOREACH_END();
673 :
674 16 : zend_hash_update(&server->completion_cache, cache_key, &items);
675 : }
676 :
677 8 : static inline zend_string *lsp_psalm_ls_trim_type_text(const char *value, size_t length)
678 : {
679 : const char *start, *end, *line_end, *colon, *fence;
680 : zend_string *type;
681 :
682 8 : start = value;
683 8 : end = value + length;
684 8 : while (start < end && isspace((unsigned char) *start)) {
685 0 : start++;
686 : }
687 8 : while (end > start && isspace((unsigned char) end[-1])) {
688 0 : end--;
689 : }
690 :
691 8 : if (end - start >= 3 && memcmp(start, "```", 3) == 0) {
692 8 : line_end = start;
693 56 : while (line_end < end && *line_end != '\n' && *line_end != '\r') {
694 48 : line_end++;
695 : }
696 8 : if (line_end < end) {
697 8 : start = line_end + 1;
698 8 : while (start < end && (*start == '\n' || *start == '\r')) {
699 0 : start++;
700 : }
701 : }
702 8 : fence = start;
703 552 : while (fence + 3 <= end && memcmp(fence, "```", 3) != 0) {
704 544 : fence++;
705 : }
706 8 : if (fence + 3 <= end) {
707 8 : end = fence;
708 : }
709 : }
710 :
711 8 : while (start < end && isspace((unsigned char) *start)) {
712 0 : start++;
713 : }
714 16 : while (end > start && isspace((unsigned char) end[-1])) {
715 8 : end--;
716 : }
717 :
718 8 : if (end - start >= 3 && memcmp(start, "php", 3) == 0 && (start + 3 == end || isspace((unsigned char) start[3]))) {
719 0 : start += 3;
720 0 : while (start < end && isspace((unsigned char) *start)) {
721 0 : start++;
722 : }
723 : }
724 :
725 8 : if (end - start >= 5 && memcmp(start, "<?php", 5) == 0) {
726 8 : line_end = start;
727 248 : while (line_end < end && *line_end != '\n' && *line_end != '\r') {
728 240 : line_end++;
729 : }
730 8 : if (line_end < end) {
731 8 : start = line_end + 1;
732 8 : while (start < end && isspace((unsigned char) *start)) {
733 0 : start++;
734 : }
735 : }
736 : }
737 :
738 8 : colon = memchr(start, ':', end - start);
739 8 : if (colon && colon + 1 < end && (memchr(start, '$', colon - start) || isdigit((unsigned char) *start))) {
740 0 : start = colon + 1;
741 0 : while (start < end && isspace((unsigned char) *start)) {
742 0 : start++;
743 : }
744 : }
745 :
746 8 : while (start < end && (*start == '`' || *start == '\'' || *start == '"')) {
747 0 : start++;
748 : }
749 8 : while (end > start && (end[-1] == '`' || end[-1] == '\'' || end[-1] == '"')) {
750 0 : end--;
751 : }
752 :
753 8 : if (end <= start) {
754 0 : return NULL;
755 : }
756 :
757 8 : type = zend_string_init(start, end - start, 0);
758 8 : if (lsp_type_is_unhelpful(type)) {
759 0 : zend_string_release(type);
760 :
761 0 : return NULL;
762 : }
763 :
764 8 : return type;
765 : }
766 :
767 20 : static inline zend_string *lsp_psalm_ls_hover_type_from_contents(zval *contents)
768 : {
769 : zend_string *type;
770 : zval *value_zv, *entry;
771 :
772 20 : if (!contents) {
773 12 : return NULL;
774 : }
775 :
776 8 : if (Z_TYPE_P(contents) == IS_STRING) {
777 0 : return lsp_psalm_ls_trim_type_text(Z_STRVAL_P(contents), Z_STRLEN_P(contents));
778 : }
779 :
780 8 : if (Z_TYPE_P(contents) != IS_ARRAY) {
781 0 : return NULL;
782 : }
783 :
784 8 : value_zv = zend_hash_str_find(Z_ARRVAL_P(contents), "value", sizeof("value") - 1);
785 8 : if (value_zv && Z_TYPE_P(value_zv) == IS_STRING) {
786 8 : return lsp_psalm_ls_trim_type_text(Z_STRVAL_P(value_zv), Z_STRLEN_P(value_zv));
787 : }
788 :
789 0 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(contents), entry) {
790 0 : type = lsp_psalm_ls_hover_type_from_contents(entry);
791 0 : if (type) {
792 0 : return type;
793 : }
794 : } ZEND_HASH_FOREACH_END();
795 :
796 0 : return NULL;
797 : }
798 :
799 20 : static inline void lsp_psalm_ls_store_hover_result(lsp_server *server, zval *pending, zval *result)
800 : {
801 : zend_string *cache_key, *symbol_key, *type, *uri, *word;
802 : zval *contents, cache_value;
803 :
804 20 : cache_key = lsp_array_string(pending, "cacheKey");
805 20 : if (!cache_key) {
806 8 : return;
807 : }
808 :
809 20 : contents = result && Z_TYPE_P(result) == IS_ARRAY ? zend_hash_str_find(Z_ARRVAL_P(result), "contents", sizeof("contents") - 1) : NULL;
810 20 : type = lsp_psalm_ls_hover_type_from_contents(contents);
811 20 : if (type) {
812 8 : ZVAL_STR_COPY(&cache_value, type);
813 8 : zend_hash_update(&server->type_cache, cache_key, &cache_value);
814 8 : uri = lsp_array_string(pending, "uri");
815 8 : word = lsp_array_string(pending, "word");
816 8 : if (uri && word && ZSTR_LEN(word) > 1 && ZSTR_VAL(word)[0] == '$') {
817 4 : symbol_key = strpprintf(0, "psalm-ls:symbol-type:%s:%s", ZSTR_VAL(uri), ZSTR_VAL(word));
818 4 : ZVAL_STR_COPY(&cache_value, type);
819 4 : zend_hash_update(&server->type_cache, symbol_key, &cache_value);
820 4 : zend_string_release(symbol_key);
821 : }
822 8 : zend_string_release(type);
823 :
824 8 : return;
825 : }
826 :
827 12 : ZVAL_FALSE(&cache_value);
828 12 : zend_hash_update(&server->type_cache, cache_key, &cache_value);
829 : }
830 :
831 32 : static inline void lsp_psalm_ls_notify_completion_ready(zend_string *uri)
832 : {
833 : zval params;
834 :
835 32 : array_init(¶ms);
836 32 : add_assoc_string(¶ms, "analyzer", "psalm-ls");
837 :
838 32 : if (uri) {
839 32 : add_assoc_str(¶ms, "uri", zend_string_copy(uri));
840 : }
841 :
842 32 : lsp_protocol_notify("lsparrot.php/completionReady", ¶ms);
843 :
844 32 : zval_ptr_dtor(¶ms);
845 32 : }
846 :
847 24 : static inline void lsp_psalm_ls_handle_diagnostics(lsp_server *server, lsp_psalm_ls_session *session, zval *params)
848 : {
849 : lsp_document *document;
850 : zend_string *uri;
851 : zval *diagnostics, diagnostics_copy;
852 :
853 24 : uri = lsp_array_string(params, "uri");
854 24 : diagnostics = lsp_array_find(params, "diagnostics");
855 24 : if (!uri || !diagnostics || Z_TYPE_P(diagnostics) != IS_ARRAY) {
856 0 : return;
857 : }
858 :
859 24 : ZVAL_COPY(&diagnostics_copy, diagnostics);
860 24 : zend_hash_update(&session->diagnostics, uri, &diagnostics_copy);
861 24 : lsp_analyzer_project_state(server, "psalm-ls", session->root, LSP_ANALYZER_PROJECT_READY);
862 24 : lsp_analyzer_project_status("psalm-ls", "idle", "Psalm language server diagnostics updated.", session->root);
863 :
864 24 : document = lsp_document_from_uri(server, uri);
865 24 : if (document) {
866 24 : lsp_publish_document_diagnostics(server, document);
867 : }
868 : }
869 :
870 64 : static inline void lsp_psalm_ls_handle_response(lsp_server *server, lsp_psalm_ls_session *session, zval *message)
871 : {
872 : zend_long id;
873 : zend_string *uri;
874 : zval *id_zv, *pending, *result, *uri_zv, *completion_ready_zv;
875 :
876 64 : id_zv = zend_hash_str_find(Z_ARRVAL_P(message), "id", sizeof("id") - 1);
877 64 : if (!lsp_psalm_ls_id_from_zval(id_zv, &id)) {
878 28 : return;
879 : }
880 :
881 64 : if (id == session->initialize_id) {
882 28 : session->initialized = true;
883 28 : lsp_analyzer_project_state(server, "psalm-ls", session->root, LSP_ANALYZER_PROJECT_READY);
884 28 : lsp_analyzer_project_status("psalm-ls", "idle", "Psalm language server ready.", session->root);
885 28 : lsp_psalm_ls_send_initialized(session);
886 28 : lsp_psalm_ls_send_open_documents(server, session);
887 :
888 28 : return;
889 : }
890 :
891 36 : pending = zend_hash_index_find(&session->pending, (zend_ulong) id);
892 36 : if (!pending || Z_TYPE_P(pending) != IS_ARRAY) {
893 0 : return;
894 : }
895 :
896 36 : result = zend_hash_str_find(Z_ARRVAL_P(message), "result", sizeof("result") - 1);
897 36 : if (lsp_zval_string_equals_literal(lsp_array_find(pending, "type"), "completion")) {
898 16 : lsp_psalm_ls_store_completion_result(server, pending, result);
899 16 : uri_zv = lsp_array_find(pending, "uri");
900 16 : uri = uri_zv && Z_TYPE_P(uri_zv) == IS_STRING ? Z_STR_P(uri_zv) : NULL;
901 16 : lsp_psalm_ls_notify_completion_ready(uri);
902 20 : } else if (lsp_zval_string_equals_literal(lsp_array_find(pending, "type"), "hover")) {
903 20 : lsp_psalm_ls_store_hover_result(server, pending, result);
904 20 : completion_ready_zv = lsp_array_find(pending, "completionReady");
905 20 : if (completion_ready_zv && zend_is_true(completion_ready_zv)) {
906 16 : uri_zv = lsp_array_find(pending, "uri");
907 16 : uri = uri_zv && Z_TYPE_P(uri_zv) == IS_STRING ? Z_STR_P(uri_zv) : NULL;
908 16 : lsp_psalm_ls_notify_completion_ready(uri);
909 : }
910 : }
911 :
912 36 : zend_hash_index_del(&session->pending, (zend_ulong) id);
913 : }
914 :
915 88 : static inline void lsp_psalm_ls_handle_message(lsp_server *server, lsp_psalm_ls_session *session, zend_string *body)
916 : {
917 : zval decoded, *method_zv, *params;
918 :
919 88 : ZVAL_UNDEF(&decoded);
920 88 : php_json_decode_ex(&decoded, ZSTR_VAL(body), ZSTR_LEN(body), PHP_JSON_OBJECT_AS_ARRAY, 512);
921 88 : if (Z_TYPE(decoded) != IS_ARRAY) {
922 0 : if (!Z_ISUNDEF(decoded)) {
923 0 : zval_ptr_dtor(&decoded);
924 : }
925 :
926 0 : return;
927 : }
928 :
929 88 : method_zv = zend_hash_str_find(Z_ARRVAL(decoded), "method", sizeof("method") - 1);
930 88 : if (method_zv && Z_TYPE_P(method_zv) == IS_STRING) {
931 24 : params = zend_hash_str_find(Z_ARRVAL(decoded), "params", sizeof("params") - 1);
932 24 : if (params && Z_TYPE_P(params) == IS_ARRAY && zend_string_equals_literal(Z_STR_P(method_zv), "textDocument/publishDiagnostics")) {
933 24 : lsp_psalm_ls_handle_diagnostics(server, session, params);
934 : }
935 : } else {
936 64 : lsp_psalm_ls_handle_response(server, session, &decoded);
937 : }
938 :
939 88 : zval_ptr_dtor(&decoded);
940 : }
941 :
942 1124 : static inline void lsp_psalm_ls_read_pipe(lsp_server *server, lsp_psalm_ls_session *session, lsp_pipe_handle pipe, lsp_psalm_ls_buffer *buffer, bool parse_messages)
943 : {
944 1124 : smart_str chunk = {0};
945 : zend_string *body;
946 : bool closed;
947 :
948 1124 : if (!lsp_pipe_handle_valid(pipe)) {
949 562 : return;
950 : }
951 :
952 1124 : closed = false;
953 1124 : if (!lsp_pipe_read_available(pipe, &chunk, &closed)) {
954 0 : session->failed = true;
955 0 : session->running = false;
956 :
957 0 : return;
958 : }
959 1124 : if (chunk.s) {
960 88 : lsp_psalm_ls_buffer_append(buffer, ZSTR_VAL(chunk.s), ZSTR_LEN(chunk.s));
961 88 : zend_string_release(chunk.s);
962 : }
963 1124 : if (closed) {
964 0 : session->failed = true;
965 0 : session->running = false;
966 0 : return;
967 : }
968 :
969 1124 : if (!parse_messages) {
970 562 : if (buffer->length > 65536) {
971 0 : buffer->length = 0;
972 : }
973 :
974 562 : return;
975 : }
976 :
977 650 : while (lsp_psalm_ls_next_body(buffer, &body)) {
978 88 : lsp_psalm_ls_handle_message(server, session, body);
979 88 : zend_string_release(body);
980 : }
981 : }
982 :
983 0 : static inline zend_string *lsp_psalm_ls_error_message(lsp_psalm_ls_buffer *buffer)
984 : {
985 : const char *value, *end, *line_start, *line_end, *marker;
986 :
987 0 : if (!buffer || buffer->length == 0) {
988 0 : return zend_string_init("Psalm language server exited.", sizeof("Psalm language server exited.") - 1, 0);
989 : }
990 :
991 0 : value = buffer->data;
992 0 : end = value + buffer->length;
993 0 : marker = strstr(value, "Error:");
994 0 : if (marker && marker < end) {
995 0 : line_start = marker;
996 : } else {
997 0 : line_start = value;
998 0 : while (line_start < end && isspace((unsigned char) *line_start)) {
999 0 : line_start++;
1000 : }
1001 : }
1002 :
1003 0 : line_end = line_start;
1004 0 : while (line_end < end && *line_end != '\r' && *line_end != '\n') {
1005 0 : line_end++;
1006 : }
1007 0 : while (line_end > line_start && isspace((unsigned char) line_end[-1])) {
1008 0 : line_end--;
1009 : }
1010 :
1011 0 : if (line_end <= line_start) {
1012 0 : return zend_string_init("Psalm language server exited.", sizeof("Psalm language server exited.") - 1, 0);
1013 : }
1014 :
1015 0 : return strpprintf(0, "Psalm language server failed: %.*s", (int) (line_end - line_start), line_start);
1016 : }
1017 :
1018 562 : static inline void lsp_psalm_ls_reap_session(lsp_server *server, lsp_psalm_ls_session *session)
1019 : {
1020 : zend_string *message;
1021 : int status;
1022 :
1023 562 : if (!session->running || !lsp_process_id_valid(session->pid)) {
1024 4 : return;
1025 : }
1026 :
1027 558 : status = 0;
1028 558 : if (lsp_process_wait_nonblocking(session->pid, &status)) {
1029 0 : session->running = false;
1030 0 : session->failed = true;
1031 0 : lsp_process_close(session->pid);
1032 0 : session->pid = LSP_INVALID_PROCESS_ID;
1033 : /* A session that gives up on restarts can linger in the table for
1034 : * the rest of the server's life; its pipes must not. */
1035 0 : lsp_pipe_close(&session->input_pipe);
1036 0 : lsp_pipe_close(&session->output_pipe);
1037 0 : lsp_pipe_close(&session->error_pipe);
1038 0 : lsp_analyzer_project_state(server, "psalm-ls", session->root, LSP_ANALYZER_PROJECT_ERROR);
1039 0 : message = lsp_psalm_ls_error_message(&session->error_buffer);
1040 0 : lsp_analyzer_project_status("psalm-ls", "error", ZSTR_VAL(message), session->root);
1041 0 : zend_string_release(message);
1042 : }
1043 : }
1044 :
1045 240 : static inline lsp_psalm_ls_session *lsp_psalm_ls_find_session(lsp_server *server, zend_string *project_root)
1046 : {
1047 : zval *value;
1048 :
1049 240 : value = zend_hash_find(&server->psalm_ls_projects, project_root);
1050 :
1051 240 : return value && Z_TYPE_P(value) == IS_PTR ? (lsp_psalm_ls_session *) Z_PTR_P(value) : NULL;
1052 : }
1053 :
1054 0 : static inline bool lsp_psalm_ls_project_tool_available(zend_string *project_root)
1055 : {
1056 : zend_string *tool;
1057 :
1058 0 : tool = lsp_tool_project_candidate(project_root, "psalm-language-server");
1059 0 : if (tool) {
1060 0 : zend_string_release(tool);
1061 :
1062 0 : return true;
1063 : }
1064 :
1065 0 : return false;
1066 : }
1067 :
1068 4 : static inline void lsp_psalm_ls_append_cached_items(lsp_server *server, zval *items, zend_string *key)
1069 : {
1070 : zval *cached, *item, copy;
1071 :
1072 4 : cached = zend_hash_find(&server->completion_cache, key);
1073 4 : if (!cached || Z_TYPE_P(cached) != IS_ARRAY) {
1074 0 : return;
1075 : }
1076 :
1077 8 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(cached), item) {
1078 4 : ZVAL_COPY(©, item);
1079 4 : add_next_index_zval(items, ©);
1080 : } ZEND_HASH_FOREACH_END();
1081 : }
1082 :
1083 24 : static inline zend_string *lsp_psalm_ls_completion_key(lsp_document *document, size_t offset, zend_string *prefix)
1084 : {
1085 24 : return strpprintf(0, "psalm-ls:%s:" ZEND_LONG_FMT ":%zu:%s", ZSTR_VAL(document->uri), document->version, offset, ZSTR_VAL(prefix));
1086 : }
1087 :
1088 20 : static inline zend_string *lsp_psalm_ls_hover_key(lsp_document *document, size_t offset, zend_string *word)
1089 : {
1090 20 : return lsp_type_cache_key("psalm-ls", document, word, offset);
1091 : }
1092 :
1093 16 : static inline zend_string *lsp_psalm_ls_symbol_type_key(lsp_document *document, zend_string *word)
1094 : {
1095 16 : return strpprintf(0, "psalm-ls:symbol-type:%s:%s", ZSTR_VAL(document->uri), ZSTR_VAL(word));
1096 : }
1097 :
1098 20 : static inline void lsp_psalm_ls_add_pending_completion(lsp_psalm_ls_session *session, zend_long id, zend_string *uri, zend_string *cache_key)
1099 : {
1100 : zval pending;
1101 :
1102 20 : array_init(&pending);
1103 20 : add_assoc_string(&pending, "type", "completion");
1104 20 : add_assoc_str(&pending, "uri", zend_string_copy(uri));
1105 20 : add_assoc_str(&pending, "cacheKey", zend_string_copy(cache_key));
1106 20 : zend_hash_index_update(&session->pending, (zend_ulong) id, &pending);
1107 20 : }
1108 :
1109 20 : static inline void lsp_psalm_ls_add_pending_hover(lsp_psalm_ls_session *session, zend_long id, zend_string *uri, zend_string *cache_key, zend_string *word, bool completion_ready)
1110 : {
1111 : zval pending;
1112 :
1113 20 : array_init(&pending);
1114 20 : add_assoc_string(&pending, "type", "hover");
1115 20 : add_assoc_str(&pending, "uri", zend_string_copy(uri));
1116 20 : add_assoc_str(&pending, "cacheKey", zend_string_copy(cache_key));
1117 20 : add_assoc_str(&pending, "word", zend_string_copy(word));
1118 20 : add_assoc_bool(&pending, "completionReady", completion_ready);
1119 20 : zend_hash_index_update(&session->pending, (zend_ulong) id, &pending);
1120 20 : }
1121 :
1122 32 : extern void lsp_psalm_ls_project_destroy(zval *value)
1123 : {
1124 32 : if (Z_TYPE_P(value) == IS_PTR) {
1125 32 : lsp_psalm_ls_session_free((lsp_psalm_ls_session *) Z_PTR_P(value));
1126 : }
1127 32 : }
1128 :
1129 : /* See lsp_runner_close_pipes_in_child: forked index workers must not hold
1130 : * references to the psalm-ls pipes. */
1131 568 : extern void lsp_psalm_ls_close_pipes_in_child(lsp_server *server)
1132 : {
1133 : lsp_psalm_ls_session *session;
1134 : zval *value;
1135 :
1136 568 : ZEND_HASH_FOREACH_VAL(&server->psalm_ls_projects, value) {
1137 0 : if (Z_TYPE_P(value) != IS_PTR) {
1138 0 : continue;
1139 : }
1140 :
1141 0 : session = (lsp_psalm_ls_session *) Z_PTR_P(value);
1142 0 : lsp_pipe_close(&session->input_pipe);
1143 0 : lsp_pipe_close(&session->output_pipe);
1144 0 : lsp_pipe_close(&session->error_pipe);
1145 : } ZEND_HASH_FOREACH_END();
1146 568 : }
1147 :
1148 548 : extern void lsp_psalm_ls_shutdown_all(lsp_server *server)
1149 : {
1150 : zval *value;
1151 :
1152 580 : ZEND_HASH_FOREACH_VAL(&server->psalm_ls_projects, value) {
1153 32 : if (Z_TYPE_P(value) == IS_PTR) {
1154 32 : lsp_psalm_ls_session_stop((lsp_psalm_ls_session *) Z_PTR_P(value));
1155 : }
1156 : } ZEND_HASH_FOREACH_END();
1157 548 : }
1158 :
1159 1948 : extern bool lsp_psalm_ls_enabled(lsp_server *server)
1160 : {
1161 1948 : return server->psalm_ls_enabled;
1162 : }
1163 :
1164 3176 : extern void lsp_psalm_ls_pump(lsp_server *server, double timeout)
1165 : {
1166 : lsp_psalm_ls_session *session;
1167 : zval *value;
1168 : double deadline;
1169 : bool again;
1170 :
1171 3176 : deadline = lsp_psalm_ls_now() + timeout;
1172 : do {
1173 3498 : again = false;
1174 4060 : ZEND_HASH_FOREACH_VAL(&server->psalm_ls_projects, value) {
1175 562 : if (Z_TYPE_P(value) != IS_PTR) {
1176 0 : continue;
1177 : }
1178 562 : session = (lsp_psalm_ls_session *) Z_PTR_P(value);
1179 562 : lsp_psalm_ls_reap_session(server, session);
1180 562 : lsp_psalm_ls_read_pipe(server, session, session->output_pipe, &session->output_buffer, true);
1181 562 : lsp_psalm_ls_read_pipe(server, session, session->error_pipe, &session->error_buffer, false);
1182 562 : if (session->running) {
1183 558 : again = true;
1184 : }
1185 : } ZEND_HASH_FOREACH_END();
1186 :
1187 3498 : if (timeout <= 0.0 || !again || lsp_psalm_ls_now() >= deadline) {
1188 : break;
1189 : }
1190 358 : lsp_sleep_milliseconds(10);
1191 358 : } while (lsp_psalm_ls_now() < deadline);
1192 3176 : }
1193 :
1194 : #define LSP_PSALM_LS_MAX_RESTARTS 3
1195 : #define LSP_PSALM_LS_RESTART_BACKOFF_SECONDS 5.0
1196 : #define LSP_PSALM_LS_HEALTHY_SECONDS 120.0
1197 :
1198 0 : static inline void lsp_psalm_ls_replay_open_documents(lsp_server *server, lsp_psalm_ls_session *session)
1199 : {
1200 : lsp_document *document;
1201 : zval *value;
1202 :
1203 0 : ZEND_HASH_FOREACH_VAL(&server->documents, value) {
1204 0 : if (Z_TYPE_P(value) != IS_PTR) {
1205 0 : continue;
1206 : }
1207 :
1208 0 : document = (lsp_document *) Z_PTR_P(value);
1209 0 : if (document && document->path && lsp_path_is_in_composer_analysis_paths(document->path, session->root)) {
1210 0 : lsp_psalm_ls_send_document_open(session, document);
1211 : }
1212 : } ZEND_HASH_FOREACH_END();
1213 0 : }
1214 :
1215 660 : extern bool lsp_psalm_ls_schedule_project(lsp_server *server, zend_string *project_root)
1216 : {
1217 : lsp_psalm_ls_session *session, *existing_session;
1218 : zend_string *config, *generated_config;
1219 : zval value;
1220 : uint32_t restart_count;
1221 : bool restarting;
1222 :
1223 660 : if (!lsp_psalm_ls_enabled(server)) {
1224 508 : return false;
1225 : }
1226 :
1227 152 : if (!lsp_composer_project_has_analysis_paths(project_root)) {
1228 0 : return false;
1229 : }
1230 :
1231 152 : restarting = false;
1232 152 : restart_count = 0;
1233 152 : existing_session = lsp_psalm_ls_find_session(server, project_root);
1234 152 : if (existing_session) {
1235 120 : if (existing_session->running || !existing_session->failed) {
1236 116 : return true;
1237 : }
1238 :
1239 : /* A run that stayed healthy long enough clears earlier strikes, so
1240 : * three crashes spread over a long session don't permanently disable
1241 : * the project. */
1242 4 : if (existing_session->restart_count > 0 &&
1243 0 : lsp_psalm_ls_now() - existing_session->started_at > LSP_PSALM_LS_HEALTHY_SECONDS
1244 : ) {
1245 0 : existing_session->restart_count = 0;
1246 : }
1247 :
1248 : /* The language server crashed. Restart with a bounded backoff so a
1249 : * transient failure recovers while a reproducible crash does not
1250 : * spawn processes forever. */
1251 4 : if (existing_session->restart_count >= LSP_PSALM_LS_MAX_RESTARTS ||
1252 4 : lsp_psalm_ls_now() - existing_session->started_at < LSP_PSALM_LS_RESTART_BACKOFF_SECONDS
1253 : ) {
1254 4 : return true;
1255 : }
1256 :
1257 0 : restart_count = existing_session->restart_count + 1;
1258 0 : restarting = true;
1259 : /* The dead session stays in the table (carrying the backoff
1260 : * bookkeeping) until the replacement actually starts; a failed spawn
1261 : * must not reset the strike counter. */
1262 : }
1263 :
1264 32 : if (server->options.analyzer_auto && server->options.psalm_transport == LSP_PSALM_TRANSPORT_AUTO && !lsp_psalm_ls_project_tool_available(project_root)) {
1265 0 : return false;
1266 : }
1267 :
1268 32 : config = lsp_psalm_config_file(project_root);
1269 32 : generated_config = lsp_psalm_ls_config_file(project_root, config, lsp_project_psalm_level(server, project_root), server->options.psalm_live_dead_code_diagnostics);
1270 32 : if (!generated_config && !config) {
1271 0 : if (restarting && existing_session) {
1272 0 : existing_session->restart_count = restart_count;
1273 0 : existing_session->started_at = lsp_psalm_ls_now();
1274 : }
1275 0 : lsp_analyzer_project_state(server, "psalm-ls", project_root, LSP_ANALYZER_PROJECT_ERROR);
1276 0 : lsp_analyzer_project_status("psalm-ls", "error", "Psalm config file could not be generated; skipping Psalm language server.", project_root);
1277 :
1278 0 : return true;
1279 : }
1280 :
1281 32 : session = lsp_psalm_ls_session_alloc(project_root, generated_config ? generated_config : config);
1282 32 : if (generated_config) {
1283 32 : zend_string_release(generated_config);
1284 : }
1285 :
1286 32 : if (config) {
1287 32 : zend_string_release(config);
1288 : }
1289 :
1290 32 : if (!lsp_psalm_ls_start_process(server, session)) {
1291 0 : lsp_psalm_ls_session_free(session);
1292 :
1293 0 : if (restarting && existing_session) {
1294 0 : existing_session->restart_count = restart_count;
1295 0 : existing_session->started_at = lsp_psalm_ls_now();
1296 : }
1297 :
1298 0 : if (server->options.psalm_transport == LSP_PSALM_TRANSPORT_AUTO) {
1299 0 : return false;
1300 : }
1301 :
1302 0 : lsp_analyzer_project_state(server, "psalm-ls", project_root, LSP_ANALYZER_PROJECT_ERROR);
1303 0 : lsp_analyzer_project_status("psalm-ls", "error", "Psalm language server could not be started.", project_root);
1304 :
1305 0 : return true;
1306 : }
1307 :
1308 32 : session->restart_count = restart_count;
1309 32 : session->started_at = lsp_psalm_ls_now();
1310 32 : ZVAL_PTR(&value, session);
1311 32 : zend_hash_update(&server->psalm_ls_projects, project_root, &value);
1312 32 : lsp_analyzer_project_state(server, "psalm-ls", project_root, LSP_ANALYZER_PROJECT_RUNNING);
1313 32 : lsp_analyzer_project_status("psalm-ls", "running", restarting ? "Restarting Psalm language server." : "Starting Psalm language server.", project_root);
1314 32 : lsp_psalm_ls_send_initialize(session);
1315 32 : lsp_psalm_ls_pump(server, 0.05);
1316 :
1317 32 : if (restarting) {
1318 : /* The fresh process knows nothing about already-open editors. */
1319 0 : lsp_psalm_ls_replay_open_documents(server, session);
1320 : }
1321 :
1322 32 : return true;
1323 : }
1324 :
1325 536 : extern void lsp_psalm_ls_document_open(lsp_server *server, lsp_document *document)
1326 : {
1327 : lsp_psalm_ls_session *session;
1328 : zend_string *project_root;
1329 :
1330 536 : if (!lsp_psalm_ls_enabled(server)) {
1331 504 : return;
1332 : }
1333 :
1334 32 : project_root = lsp_document_project_root(server, document);
1335 32 : if (!lsp_path_is_in_composer_analysis_paths(document->path, project_root)) {
1336 4 : zend_string_release(project_root);
1337 :
1338 4 : return;
1339 : }
1340 :
1341 28 : lsp_psalm_ls_schedule_project(server, project_root);
1342 :
1343 28 : session = lsp_psalm_ls_find_session(server, project_root);
1344 28 : if (session && session->running && !session->initialized) {
1345 0 : lsp_psalm_ls_pump(server, 0.05);
1346 : }
1347 28 : if (session && session->running) {
1348 28 : lsp_psalm_ls_send_document_open(session, document);
1349 : }
1350 :
1351 28 : zend_string_release(project_root);
1352 : }
1353 :
1354 24 : extern void lsp_psalm_ls_document_change(lsp_server *server, lsp_document *document)
1355 : {
1356 : lsp_psalm_ls_session *session;
1357 : zend_string *project_root;
1358 :
1359 24 : if (!lsp_psalm_ls_enabled(server) || !server->options.psalm_on_change) {
1360 24 : return;
1361 : }
1362 :
1363 0 : project_root = lsp_document_project_root(server, document);
1364 0 : if (!lsp_path_is_in_composer_analysis_paths(document->path, project_root)) {
1365 0 : zend_string_release(project_root);
1366 :
1367 0 : return;
1368 : }
1369 :
1370 0 : session = lsp_psalm_ls_find_session(server, project_root);
1371 0 : if (session && session->running && !session->initialized) {
1372 0 : lsp_psalm_ls_pump(server, 0.05);
1373 : }
1374 0 : if (session && session->running) {
1375 0 : lsp_psalm_ls_send_document_change(session, document);
1376 :
1377 0 : if (session->initialized) {
1378 0 : lsp_analyzer_project_state(server, "psalm-ls", project_root, LSP_ANALYZER_PROJECT_RUNNING);
1379 0 : lsp_analyzer_project_status("psalm-ls", "running", "Psalm language server analyzing changes.", project_root);
1380 : }
1381 : }
1382 :
1383 0 : zend_string_release(project_root);
1384 : }
1385 :
1386 12 : extern void lsp_psalm_ls_document_save(lsp_server *server, lsp_document *document)
1387 : {
1388 : lsp_psalm_ls_session *session;
1389 : zend_string *project_root;
1390 :
1391 12 : if (!lsp_psalm_ls_enabled(server)) {
1392 12 : return;
1393 : }
1394 :
1395 0 : project_root = lsp_document_project_root(server, document);
1396 0 : if (!lsp_path_is_in_composer_analysis_paths(document->path, project_root)) {
1397 0 : zend_string_release(project_root);
1398 :
1399 0 : return;
1400 : }
1401 :
1402 0 : lsp_psalm_ls_schedule_project(server, project_root);
1403 :
1404 0 : session = lsp_psalm_ls_find_session(server, project_root);
1405 0 : if (session && session->running && !session->initialized) {
1406 0 : lsp_psalm_ls_pump(server, 0.05);
1407 : }
1408 0 : if (session && session->running) {
1409 0 : lsp_psalm_ls_send_document_save(session, document);
1410 0 : if (session->initialized) {
1411 0 : lsp_analyzer_project_state(server, "psalm-ls", project_root, LSP_ANALYZER_PROJECT_RUNNING);
1412 0 : lsp_analyzer_project_status("psalm-ls", "running", "Psalm language server analyzing saved document.", project_root);
1413 : }
1414 : }
1415 :
1416 0 : zend_string_release(project_root);
1417 : }
1418 :
1419 4 : extern void lsp_psalm_ls_document_close(lsp_server *server, zend_string *uri)
1420 : {
1421 : lsp_psalm_ls_session *session;
1422 : zval *value;
1423 :
1424 4 : if (!lsp_psalm_ls_enabled(server)) {
1425 4 : return;
1426 : }
1427 :
1428 0 : ZEND_HASH_FOREACH_VAL(&server->psalm_ls_projects, value) {
1429 0 : if (Z_TYPE_P(value) != IS_PTR) {
1430 0 : continue;
1431 : }
1432 :
1433 0 : session = (lsp_psalm_ls_session *) Z_PTR_P(value);
1434 0 : lsp_psalm_ls_send_document_close(session, uri);
1435 0 : zend_hash_del(&session->diagnostics, uri);
1436 : } ZEND_HASH_FOREACH_END();
1437 : }
1438 :
1439 400 : extern void lsp_psalm_ls_append_diagnostics(lsp_server *server, lsp_document *document, zval *diagnostics)
1440 : {
1441 : lsp_psalm_ls_session *session;
1442 : zend_string *project_root;
1443 : zval *cached, *item, copy;
1444 :
1445 400 : if (!lsp_psalm_ls_enabled(server)) {
1446 392 : return;
1447 : }
1448 :
1449 24 : project_root = lsp_document_project_root(server, document);
1450 24 : if (!lsp_path_is_in_composer_analysis_paths(document->path, project_root)) {
1451 4 : zend_string_release(project_root);
1452 :
1453 4 : return;
1454 : }
1455 :
1456 20 : session = lsp_psalm_ls_find_session(server, project_root);
1457 20 : zend_string_release(project_root);
1458 20 : if (!session) {
1459 0 : return;
1460 : }
1461 :
1462 20 : cached = zend_hash_find(&session->diagnostics, document->uri);
1463 20 : if (!cached || Z_TYPE_P(cached) != IS_ARRAY) {
1464 12 : return;
1465 : }
1466 :
1467 12 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(cached), item) {
1468 4 : ZVAL_COPY(©, item);
1469 4 : add_next_index_zval(diagnostics, ©);
1470 : } ZEND_HASH_FOREACH_END();
1471 : }
1472 :
1473 28 : static inline zend_string *lsp_psalm_ls_type_for_position_ex(lsp_server *server, lsp_document *document, zval *position, size_t offset, zend_string *word, bool wait_for_response, bool notify_completion_ready)
1474 : {
1475 : lsp_psalm_ls_session *session;
1476 : zend_string *project_root, *key, *symbol_key, *type;
1477 : zend_long id;
1478 : zval message, params, td, pos, *cached;
1479 : double wait_timeout;
1480 :
1481 28 : if (!lsp_psalm_ls_enabled(server) || !server->options.psalm_enable_hover) {
1482 8 : return NULL;
1483 : }
1484 :
1485 20 : wait_timeout = wait_for_response ? (double) server->options.psalm_max_response_wait_ms / 1000.0 : 0.0;
1486 :
1487 20 : key = lsp_psalm_ls_hover_key(document, offset, word);
1488 20 : cached = zend_hash_find(&server->type_cache, key);
1489 20 : if (cached) {
1490 0 : if (Z_TYPE_P(cached) == IS_STRING) {
1491 0 : type = zend_string_copy(Z_STR_P(cached));
1492 0 : zend_string_release(key);
1493 :
1494 0 : return type;
1495 : }
1496 : }
1497 :
1498 20 : if (ZSTR_LEN(word) > 1 && ZSTR_VAL(word)[0] == '$') {
1499 16 : symbol_key = lsp_psalm_ls_symbol_type_key(document, word);
1500 16 : cached = zend_hash_find(&server->type_cache, symbol_key);
1501 16 : if (cached && Z_TYPE_P(cached) == IS_STRING) {
1502 0 : type = zend_string_copy(Z_STR_P(cached));
1503 0 : zend_string_release(symbol_key);
1504 0 : zend_string_release(key);
1505 :
1506 0 : return type;
1507 : }
1508 16 : zend_string_release(symbol_key);
1509 : }
1510 :
1511 20 : project_root = lsp_document_project_root(server, document);
1512 20 : if (!lsp_path_is_in_composer_analysis_paths(document->path, project_root)) {
1513 0 : zend_string_release(project_root);
1514 0 : zend_string_release(key);
1515 :
1516 0 : return NULL;
1517 : }
1518 :
1519 20 : lsp_psalm_ls_schedule_project(server, project_root);
1520 20 : session = lsp_psalm_ls_find_session(server, project_root);
1521 20 : zend_string_release(project_root);
1522 20 : if (session && session->running && !session->initialized) {
1523 0 : lsp_psalm_ls_pump(server, wait_timeout);
1524 : }
1525 20 : if (!session || !session->running || !session->initialized) {
1526 0 : zend_string_release(key);
1527 :
1528 0 : return NULL;
1529 : }
1530 :
1531 20 : id = session->next_id++;
1532 20 : array_init(&message);
1533 20 : add_assoc_string(&message, "jsonrpc", "2.0");
1534 20 : add_assoc_long(&message, "id", id);
1535 20 : add_assoc_string(&message, "method", "textDocument/hover");
1536 20 : array_init(¶ms);
1537 20 : array_init(&td);
1538 20 : add_assoc_str(&td, "uri", zend_string_copy(document->uri));
1539 20 : add_assoc_zval(¶ms, "textDocument", &td);
1540 20 : ZVAL_COPY(&pos, position);
1541 20 : add_assoc_zval(¶ms, "position", &pos);
1542 20 : add_assoc_zval(&message, "params", ¶ms);
1543 20 : lsp_psalm_ls_add_pending_hover(session, id, document->uri, key, word, notify_completion_ready);
1544 20 : if (lsp_psalm_ls_send_zval(session, &message)) {
1545 20 : lsp_psalm_ls_pump(server, wait_timeout);
1546 : }
1547 20 : zval_ptr_dtor(&message);
1548 :
1549 20 : cached = zend_hash_find(&server->type_cache, key);
1550 20 : if (cached && Z_TYPE_P(cached) == IS_STRING) {
1551 4 : type = zend_string_copy(Z_STR_P(cached));
1552 4 : zend_string_release(key);
1553 :
1554 4 : return type;
1555 : }
1556 :
1557 16 : zend_string_release(key);
1558 :
1559 16 : return NULL;
1560 : }
1561 :
1562 4 : extern zend_string *lsp_psalm_ls_type_for_position(lsp_server *server, lsp_document *document, zval *position, size_t offset, zend_string *word)
1563 : {
1564 4 : return lsp_psalm_ls_type_for_position_ex(server, document, position, offset, word, true, false);
1565 : }
1566 :
1567 24 : extern zend_string *lsp_psalm_ls_type_for_position_async(lsp_server *server, lsp_document *document, zval *position, size_t offset, zend_string *word)
1568 : {
1569 24 : return lsp_psalm_ls_type_for_position_ex(server, document, position, offset, word, false, true);
1570 : }
1571 :
1572 24 : extern bool lsp_psalm_ls_completion_cache_or_schedule(lsp_server *server, zval *items, lsp_document *document, zval *position, size_t offset, zend_string *prefix)
1573 : {
1574 : lsp_psalm_ls_session *session;
1575 : zend_string *project_root, *key;
1576 : zend_long id;
1577 : zval message, params, td, pos;
1578 24 : bool pending = false;
1579 :
1580 24 : if (!lsp_psalm_ls_enabled(server) || !server->options.psalm_enable_autocomplete) {
1581 0 : return false;
1582 : }
1583 :
1584 24 : key = lsp_psalm_ls_completion_key(document, offset, prefix);
1585 24 : if (zend_hash_exists(&server->completion_cache, key)) {
1586 4 : lsp_psalm_ls_append_cached_items(server, items, key);
1587 4 : zend_string_release(key);
1588 :
1589 4 : return false;
1590 : }
1591 :
1592 20 : project_root = lsp_document_project_root(server, document);
1593 20 : if (!lsp_path_is_in_composer_analysis_paths(document->path, project_root)) {
1594 0 : zend_string_release(project_root);
1595 0 : zend_string_release(key);
1596 :
1597 0 : return false;
1598 : }
1599 :
1600 20 : lsp_psalm_ls_schedule_project(server, project_root);
1601 20 : session = lsp_psalm_ls_find_session(server, project_root);
1602 20 : zend_string_release(project_root);
1603 20 : if (session && session->running && !session->initialized) {
1604 0 : lsp_psalm_ls_pump(server, 0.05);
1605 : }
1606 20 : if (!session || !session->running || !session->initialized) {
1607 0 : zend_string_release(key);
1608 :
1609 0 : return true;
1610 : }
1611 :
1612 20 : id = session->next_id++;
1613 20 : array_init(&message);
1614 20 : add_assoc_string(&message, "jsonrpc", "2.0");
1615 20 : add_assoc_long(&message, "id", id);
1616 20 : add_assoc_string(&message, "method", "textDocument/completion");
1617 20 : array_init(¶ms);
1618 20 : array_init(&td);
1619 20 : add_assoc_str(&td, "uri", zend_string_copy(document->uri));
1620 20 : add_assoc_zval(¶ms, "textDocument", &td);
1621 20 : ZVAL_COPY(&pos, position);
1622 20 : add_assoc_zval(¶ms, "position", &pos);
1623 20 : add_assoc_zval(&message, "params", ¶ms);
1624 20 : lsp_psalm_ls_add_pending_completion(session, id, document->uri, key);
1625 20 : if (lsp_psalm_ls_send_zval(session, &message)) {
1626 20 : pending = true;
1627 20 : lsp_psalm_ls_pump(server, 0.0);
1628 20 : if (zend_hash_exists(&server->completion_cache, key)) {
1629 0 : lsp_psalm_ls_append_cached_items(server, items, key);
1630 0 : pending = false;
1631 : }
1632 : }
1633 20 : zval_ptr_dtor(&message);
1634 20 : zend_string_release(key);
1635 :
1636 20 : return pending;
1637 : }
|