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 : /* Multi-root workspace bookkeeping and workspace/willRenameFiles.
19 : *
20 : * Roots: the first workspace folder stays server->root (compatibility anchor
21 : * for the index cache location); every folder lives in server->workspace_roots
22 : * and the indexer/analyzer scheduling iterate all of them.
23 : *
24 : * willRenameFiles: renaming Foo.php to Bar.php renames the class Foo declared
25 : * in it project-wide (through the ordinary rename machinery, so the family
26 : * scoping rules apply), and moving a file across PSR-4 directories rewrites
27 : * its namespace declaration to the mapping of the destination. Edits target
28 : * the OLD uris, per the LSP contract (they apply before the file moves). */
29 :
30 8 : extern void lsp_workspace_roots_add(lsp_server *server, zend_string *root)
31 : {
32 8 : if (!root || ZSTR_LEN(root) == 0) {
33 0 : return;
34 : }
35 :
36 8 : zend_hash_add_empty_element(&server->workspace_roots, root);
37 : }
38 :
39 0 : extern void lsp_workspace_roots_remove(lsp_server *server, zend_string *root)
40 : {
41 0 : if (root) {
42 0 : zend_hash_del(&server->workspace_roots, root);
43 : }
44 0 : }
45 :
46 1280 : extern bool lsp_path_is_under_any_root(lsp_server *server, zend_string *path)
47 : {
48 : zend_string *root;
49 :
50 1280 : if (lsp_path_is_same_or_under(path, server->root)) {
51 946 : return true;
52 : }
53 :
54 346 : ZEND_HASH_FOREACH_STR_KEY(&server->workspace_roots, root) {
55 24 : if (root && lsp_path_is_same_or_under(path, root)) {
56 12 : return true;
57 : }
58 : } ZEND_HASH_FOREACH_END();
59 :
60 322 : return false;
61 : }
62 :
63 : /* Iterate every configured root exactly once (server->root included even
64 : * when the client sent no workspaceFolders). */
65 2114 : extern void lsp_workspace_roots_each(lsp_server *server, void (*callback)(lsp_server *server, zend_string *root, void *context), void *context)
66 : {
67 : zend_string *root;
68 2114 : bool saw_primary = false;
69 :
70 2138 : ZEND_HASH_FOREACH_STR_KEY(&server->workspace_roots, root) {
71 24 : if (!root) {
72 0 : continue;
73 : }
74 :
75 24 : if (zend_string_equals(root, server->root)) {
76 12 : saw_primary = true;
77 : }
78 :
79 24 : callback(server, root, context);
80 : } ZEND_HASH_FOREACH_END();
81 :
82 2114 : if (!saw_primary) {
83 2102 : callback(server, server->root, context);
84 : }
85 2114 : }
86 :
87 548 : extern void lsp_workspace_parse_folders(lsp_server *server, zval *params)
88 : {
89 : zval *folders, *folder;
90 : zend_string *uri, *path;
91 548 : bool first = true;
92 :
93 548 : folders = lsp_array_find(params, "workspaceFolders");
94 548 : if (!folders || Z_TYPE_P(folders) != IS_ARRAY) {
95 544 : return;
96 : }
97 :
98 12 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(folders), folder) {
99 8 : uri = lsp_array_string(folder, "uri");
100 8 : if (!uri) {
101 0 : continue;
102 : }
103 :
104 8 : path = lsp_uri_to_path(uri);
105 8 : if (first) {
106 : /* rootUri (handled by the caller) and the first folder are the
107 : * same in practice; keep server->root pointing at it. */
108 4 : zend_string_release(server->root);
109 4 : server->root = zend_string_copy(path);
110 4 : first = false;
111 : }
112 :
113 8 : lsp_workspace_roots_add(server, path);
114 8 : zend_string_release(path);
115 : } ZEND_HASH_FOREACH_END();
116 : }
117 :
118 0 : extern void lsp_workspace_did_change_folders(lsp_server *server, zval *params)
119 : {
120 : zval *event, *added, *removed, *folder;
121 : zend_string *uri, *path;
122 0 : bool changed = false;
123 :
124 0 : event = lsp_array_find(params, "event");
125 0 : added = event ? lsp_array_find(event, "added") : NULL;
126 0 : removed = event ? lsp_array_find(event, "removed") : NULL;
127 :
128 0 : if (removed && Z_TYPE_P(removed) == IS_ARRAY) {
129 0 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(removed), folder) {
130 0 : uri = lsp_array_string(folder, "uri");
131 0 : if (!uri) {
132 0 : continue;
133 : }
134 :
135 0 : path = lsp_uri_to_path(uri);
136 0 : lsp_workspace_roots_remove(server, path);
137 0 : zend_string_release(path);
138 0 : changed = true;
139 : } ZEND_HASH_FOREACH_END();
140 : }
141 :
142 0 : if (added && Z_TYPE_P(added) == IS_ARRAY) {
143 0 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(added), folder) {
144 0 : uri = lsp_array_string(folder, "uri");
145 0 : if (!uri) {
146 0 : continue;
147 : }
148 :
149 0 : path = lsp_uri_to_path(uri);
150 0 : lsp_workspace_roots_add(server, path);
151 0 : zend_string_release(path);
152 0 : changed = true;
153 : } ZEND_HASH_FOREACH_END();
154 : }
155 :
156 0 : if (changed) {
157 : /* server->root is treated as always in scope; when the client removed
158 : * the primary folder, promote the first remaining one so the stale
159 : * root stops being indexed and analyzed. */
160 0 : if (zend_hash_num_elements(&server->workspace_roots) > 0 &&
161 0 : !zend_hash_exists(&server->workspace_roots, server->root)
162 : ) {
163 0 : zend_string *replacement = NULL, *candidate;
164 :
165 0 : ZEND_HASH_FOREACH_STR_KEY(&server->workspace_roots, candidate) {
166 0 : if (candidate) {
167 0 : replacement = candidate;
168 0 : break;
169 : }
170 : } ZEND_HASH_FOREACH_END();
171 :
172 0 : if (replacement) {
173 0 : zend_string_release(server->root);
174 0 : server->root = zend_string_copy(replacement);
175 : }
176 : }
177 :
178 : /* The symbol index and analyzers are root-derived; rebuild both. */
179 0 : lsp_index_stop_worker(server);
180 0 : lsp_build_project_index(server);
181 0 : lsp_schedule_workspace_analyzers(server);
182 : }
183 0 : }
184 :
185 : /* ----------------------------------------------------------------------
186 : * workspace/willRenameFiles
187 : * ---------------------------------------------------------------------- */
188 :
189 16 : static inline zend_string *lsp_workspace_php_basename(zend_string *path)
190 : {
191 16 : const char *value = ZSTR_VAL(path), *slash, *end;
192 :
193 16 : slash = strrchr(value, '/');
194 16 : slash = slash ? slash + 1 : value;
195 16 : end = value + ZSTR_LEN(path);
196 16 : if ((size_t) (end - slash) > sizeof(".php") - 1 && strcasecmp(end - (sizeof(".php") - 1), ".php") == 0) {
197 16 : end -= sizeof(".php") - 1;
198 : }
199 :
200 16 : if (end <= slash) {
201 0 : return NULL;
202 : }
203 :
204 16 : return zend_string_init(slash, (size_t) (end - slash), 0);
205 : }
206 :
207 16 : static inline zend_string *lsp_workspace_dirname(zend_string *path)
208 : {
209 16 : const char *value = ZSTR_VAL(path), *slash = strrchr(value, '/');
210 :
211 16 : if (!slash || slash == value) {
212 0 : return zend_string_init("/", 1, 0);
213 : }
214 :
215 16 : return zend_string_init(value, (size_t) (slash - value), 0);
216 : }
217 :
218 : /* Locate the class-like whose DECLARED short name (the token right after the
219 : * class/interface/trait/enum keyword -- never extends/implements clauses)
220 : * matches the file's basename, returning the byte offset of that name.
221 : * Anchoring on the keyword prevents `class Bar extends Baz` in Baz.php from
222 : * hijacking a rename of Baz.php into renaming Baz project-wide. */
223 4 : static inline bool lsp_workspace_declared_name_offset(zend_string *contents, zend_string *short_name, size_t *name_offset)
224 : {
225 : static const char *keywords[4] = { "class", "interface", "trait", "enum" };
226 4 : zend_long body_depth = 0;
227 : const char *value;
228 4 : size_t search_start = 0, class_start, body_start, body_end, header_end, p, keyword_end, name_start, i;
229 :
230 4 : value = ZSTR_VAL(contents);
231 4 : while (lsp_find_class_header_from(contents, search_start, &class_start, &body_start, &body_end, &body_depth)) {
232 4 : header_end = body_start > 0 ? body_start - 1 : body_start;
233 :
234 4 : for (p = class_start; p < header_end; p++) {
235 4 : for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
236 4 : if (!lsp_keyword_at_slice(value, p, header_end, keywords[i], &keyword_end)) {
237 0 : continue;
238 : }
239 :
240 4 : name_start = keyword_end;
241 8 : while (name_start < header_end && isspace((unsigned char) value[name_start])) {
242 4 : name_start++;
243 : }
244 :
245 4 : if (name_start + ZSTR_LEN(short_name) <= header_end &&
246 4 : strncasecmp(value + name_start, ZSTR_VAL(short_name), ZSTR_LEN(short_name)) == 0 &&
247 4 : !lsp_doc_is_identifier_char(value[name_start + ZSTR_LEN(short_name)])
248 : ) {
249 4 : *name_offset = name_start;
250 :
251 4 : return true;
252 : }
253 :
254 : /* Declared name differs: stop scanning this header (extends/
255 : * implements names must not match). */
256 0 : p = header_end;
257 0 : break;
258 : }
259 : }
260 :
261 0 : search_start = body_end + 1;
262 : }
263 :
264 0 : return false;
265 : }
266 :
267 : /* Nearest ancestor directory (starting at file_dir itself) containing a
268 : * composer.json; bounded walk so renames outside any project resolve to
269 : * nothing rather than scanning to the filesystem root. */
270 4 : static inline zend_string *lsp_workspace_nearest_composer_root(zend_string *file_dir)
271 : {
272 : zend_string *dir, *composer, *parent;
273 : uint32_t depth;
274 : const char *slash;
275 :
276 4 : dir = zend_string_copy(file_dir);
277 12 : for (depth = 0; depth < 16; depth++) {
278 12 : composer = lsp_join_path2(dir, "composer.json");
279 12 : if (lsp_is_regular_file(composer)) {
280 4 : zend_string_release(composer);
281 :
282 4 : return dir;
283 : }
284 8 : zend_string_release(composer);
285 :
286 8 : slash = strrchr(ZSTR_VAL(dir), '/');
287 8 : if (!slash || slash == ZSTR_VAL(dir)) {
288 : break;
289 : }
290 :
291 8 : parent = zend_string_init(ZSTR_VAL(dir), (size_t) (slash - ZSTR_VAL(dir)), 0);
292 8 : zend_string_release(dir);
293 8 : dir = parent;
294 : }
295 :
296 0 : zend_string_release(dir);
297 :
298 0 : return NULL;
299 : }
300 :
301 4 : static inline zend_string *lsp_workspace_join_relative(zend_string *base, zend_string *relative)
302 : {
303 4 : smart_str joined = {0};
304 4 : size_t length = ZSTR_LEN(relative);
305 :
306 4 : smart_str_appendl(&joined, ZSTR_VAL(base), ZSTR_LEN(base));
307 4 : if (length > 0) {
308 8 : while (length > 0 && (ZSTR_VAL(relative)[length - 1] == '/' || ZSTR_VAL(relative)[length - 1] == '\\')) {
309 4 : length--;
310 : }
311 4 : if (length > 0) {
312 4 : smart_str_appendc(&joined, '/');
313 4 : smart_str_appendl(&joined, ZSTR_VAL(relative), length);
314 : }
315 : }
316 :
317 4 : smart_str_0(&joined);
318 :
319 4 : return joined.s ? joined.s : zend_string_copy(base);
320 : }
321 :
322 4 : static inline void lsp_workspace_psr4_consider(zend_string *project_root, zend_string *prefix, zend_string *dir, zend_string *file_dir, zend_string **best, size_t *best_length)
323 : {
324 : zend_string *mapped_dir;
325 4 : smart_str ns = {0};
326 : size_t p, relative_start;
327 :
328 4 : mapped_dir = lsp_workspace_join_relative(project_root, dir);
329 4 : if (!lsp_path_is_same_or_under(file_dir, mapped_dir) || ZSTR_LEN(mapped_dir) < *best_length) {
330 0 : zend_string_release(mapped_dir);
331 :
332 0 : return;
333 : }
334 :
335 4 : smart_str_appendl(&ns, ZSTR_VAL(prefix), ZSTR_LEN(prefix));
336 8 : while (ns.s && ZSTR_LEN(ns.s) > 0 && ZSTR_VAL(ns.s)[ZSTR_LEN(ns.s) - 1] == '\\') {
337 4 : ZSTR_LEN(ns.s)--;
338 : }
339 :
340 4 : if (ZSTR_LEN(file_dir) > ZSTR_LEN(mapped_dir)) {
341 4 : relative_start = ZSTR_LEN(mapped_dir) + 1;
342 : /* A global-namespace mapping ("" prefix) must not produce a leading
343 : * backslash. */
344 4 : if (ns.s && ZSTR_LEN(ns.s) > 0) {
345 4 : smart_str_appendc(&ns, '\\');
346 : }
347 16 : for (p = relative_start; p < ZSTR_LEN(file_dir); p++) {
348 12 : smart_str_appendc(&ns, ZSTR_VAL(file_dir)[p] == '/' ? '\\' : ZSTR_VAL(file_dir)[p]);
349 : }
350 : }
351 :
352 4 : smart_str_0(&ns);
353 :
354 4 : if (*best) {
355 0 : zend_string_release(*best);
356 : }
357 4 : *best = ns.s ? ns.s : zend_string_init("", 0, 0);
358 4 : *best_length = ZSTR_LEN(mapped_dir);
359 :
360 4 : zend_string_release(mapped_dir);
361 : }
362 :
363 : /* PSR-4 namespace for a directory according to the owning composer project:
364 : * the longest matching mapped directory wins. NULL when no mapping covers
365 : * the directory. */
366 4 : static inline zend_string *lsp_workspace_psr4_namespace_for_dir(lsp_server *server, zend_string *file_dir)
367 : {
368 : zval *decoded, *autoload_zv, *psr4, *dirs_zv, *dir_zv;
369 4 : zend_string *project_root, *prefix, *best = NULL;
370 4 : const char *sections[2] = { "autoload", "autoload-dev" };
371 4 : size_t best_length = 0, i;
372 :
373 : (void) server;
374 :
375 4 : project_root = lsp_workspace_nearest_composer_root(file_dir);
376 4 : if (!project_root) {
377 0 : return NULL;
378 : }
379 :
380 4 : decoded = lsp_composer_json_decoded(project_root);
381 4 : if (!decoded) {
382 0 : zend_string_release(project_root);
383 :
384 0 : return NULL;
385 : }
386 :
387 12 : for (i = 0; i < 2; i++) {
388 8 : autoload_zv = zend_hash_str_find(Z_ARRVAL_P(decoded), sections[i], strlen(sections[i]));
389 4 : psr4 = autoload_zv && Z_TYPE_P(autoload_zv) == IS_ARRAY
390 4 : ? zend_hash_str_find(Z_ARRVAL_P(autoload_zv), "psr-4", sizeof("psr-4") - 1)
391 8 : : NULL
392 : ;
393 8 : if (!psr4 || Z_TYPE_P(psr4) != IS_ARRAY) {
394 4 : continue;
395 : }
396 :
397 8 : ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(psr4), prefix, dirs_zv) {
398 4 : if (!prefix) {
399 0 : continue;
400 : }
401 :
402 4 : if (Z_TYPE_P(dirs_zv) == IS_STRING) {
403 4 : lsp_workspace_psr4_consider(project_root, prefix, Z_STR_P(dirs_zv), file_dir, &best, &best_length);
404 0 : } else if (Z_TYPE_P(dirs_zv) == IS_ARRAY) {
405 0 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(dirs_zv), dir_zv) {
406 0 : if (Z_TYPE_P(dir_zv) == IS_STRING) {
407 0 : lsp_workspace_psr4_consider(project_root, prefix, Z_STR_P(dir_zv), file_dir, &best, &best_length);
408 : }
409 : } ZEND_HASH_FOREACH_END();
410 : }
411 : } ZEND_HASH_FOREACH_END();
412 : }
413 :
414 4 : zend_string_release(project_root);
415 :
416 4 : return best;
417 : }
418 :
419 : /* Locate the first namespace declaration via the token stream (a raw text
420 : * scan would happily edit the word "namespace" inside a comment). Outputs the
421 : * name span (for rewrites) and the whole-statement span including the
422 : * trailing ';' and newline (for removals when the destination maps to the
423 : * global namespace). */
424 4 : static inline bool lsp_workspace_namespace_decl_range(zend_string *contents, size_t *start_offset, size_t *end_offset, size_t *stmt_start, size_t *stmt_end)
425 : {
426 : zval tokens_zv, *token;
427 : HashTable *tokens;
428 : zend_string *text;
429 : zend_long id;
430 : uint32_t i, count;
431 : size_t token_offset;
432 4 : bool found = false, in_declaration = false;
433 :
434 4 : *start_offset = 0;
435 4 : *end_offset = 0;
436 4 : *stmt_start = 0;
437 4 : *stmt_end = 0;
438 :
439 4 : ZVAL_UNDEF(&tokens_zv);
440 4 : lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
441 4 : if (Z_TYPE(tokens_zv) != IS_ARRAY) {
442 0 : if (!Z_ISUNDEF(tokens_zv)) {
443 0 : zval_ptr_dtor(&tokens_zv);
444 : }
445 :
446 0 : return false;
447 : }
448 :
449 4 : tokens = Z_ARRVAL(tokens_zv);
450 4 : count = zend_hash_num_elements(tokens);
451 20 : for (i = 0; i < count; i++) {
452 20 : token = zend_hash_index_find(tokens, i);
453 20 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
454 0 : continue;
455 : }
456 :
457 20 : id = lsp_token_long(token, "id", 0);
458 20 : token_offset = (size_t) lsp_token_long(token, "offset", 0);
459 :
460 20 : if (!in_declaration) {
461 8 : if (id == T_NAMESPACE) {
462 4 : in_declaration = true;
463 4 : *stmt_start = token_offset;
464 : }
465 8 : continue;
466 : }
467 :
468 12 : if (id == T_WHITESPACE || id == T_COMMENT || id == T_DOC_COMMENT) {
469 4 : continue;
470 : }
471 :
472 8 : if (id == ';' || id == '{') {
473 4 : if (id == ';') {
474 4 : *stmt_end = token_offset + 1;
475 : /* Swallow one trailing newline so a removal leaves no blank
476 : * line behind. */
477 4 : if (*stmt_end < ZSTR_LEN(contents) && ZSTR_VAL(contents)[*stmt_end] == '\n') {
478 4 : (*stmt_end)++;
479 : }
480 : } else {
481 0 : *stmt_end = 0;
482 : }
483 4 : break;
484 : }
485 :
486 4 : text = lsp_token_string(token, "text");
487 4 : if (!text) {
488 0 : break;
489 : }
490 :
491 4 : if (!found) {
492 4 : *start_offset = token_offset;
493 4 : found = true;
494 : }
495 4 : *end_offset = token_offset + ZSTR_LEN(text);
496 : }
497 :
498 4 : zval_ptr_dtor(&tokens_zv);
499 :
500 4 : return found && *end_offset > *start_offset;
501 : }
502 :
503 4 : static inline void lsp_workspace_append_text_edit(zval *changes, zend_string *uri, zend_string *contents, size_t start_offset, size_t end_offset, zend_string *new_text)
504 : {
505 : zval *entry, edits, edit, range;
506 :
507 4 : entry = zend_hash_find(Z_ARRVAL_P(changes), uri);
508 4 : if (!entry) {
509 4 : array_init(&edits);
510 4 : entry = zend_hash_update(Z_ARRVAL_P(changes), uri, &edits);
511 : }
512 :
513 4 : array_init(&edit);
514 4 : lsp_range_from_offsets(contents, start_offset, end_offset, &range);
515 4 : add_assoc_zval(&edit, "range", &range);
516 4 : add_assoc_str(&edit, "newText", zend_string_copy(new_text));
517 4 : add_next_index_zval(entry, &edit);
518 4 : }
519 :
520 8 : extern void lsp_lsparrot_will_rename_files(lsp_server *server, zval *return_value, zval *params)
521 : {
522 : lsp_document *document;
523 : zval *files, *file, *changes_zv, rename_params, rename_result, position, *result_changes, *edits, changes;
524 : zend_string *old_uri, *new_uri, *old_path, *new_path, *old_name, *new_name, *old_dir, *new_dir, *expected_ns, *current_ns, *uri_key;
525 : size_t name_offset, ns_start, ns_end, stmt_start, stmt_end;
526 : zend_long line, character;
527 8 : bool any = false;
528 :
529 8 : files = lsp_array_find(params, "files");
530 8 : if (!files || Z_TYPE_P(files) != IS_ARRAY) {
531 0 : ZVAL_NULL(return_value);
532 :
533 0 : return;
534 : }
535 :
536 8 : lsp_index_join_worker(server);
537 8 : array_init(&changes);
538 :
539 16 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(files), file) {
540 8 : old_uri = lsp_array_string(file, "oldUri");
541 8 : new_uri = lsp_array_string(file, "newUri");
542 8 : if (!old_uri || !new_uri) {
543 0 : continue;
544 : }
545 :
546 8 : old_path = lsp_uri_to_path(old_uri);
547 8 : new_path = lsp_uri_to_path(new_uri);
548 8 : old_name = lsp_workspace_php_basename(old_path);
549 8 : new_name = lsp_workspace_php_basename(new_path);
550 :
551 8 : document = lsp_document_from_uri(server, old_uri);
552 :
553 : /* 1. Renamed basename: rename the class declared under the old
554 : * name project-wide through the ordinary rename machinery. */
555 12 : if (document && old_name && new_name && !zend_string_equals(old_name, new_name) &&
556 8 : lsp_doc_is_identifier_start(ZSTR_VAL(new_name)[0]) &&
557 4 : lsp_workspace_declared_name_offset(document->text, old_name, &name_offset)
558 : ) {
559 : zval range_zv, *start;
560 :
561 4 : lsp_range_from_offsets(document->text, name_offset, name_offset, &range_zv);
562 4 : start = lsp_array_find(&range_zv, "start");
563 4 : line = lsp_array_long(start, "line", 0);
564 4 : character = lsp_array_long(start, "character", 0);
565 4 : zval_ptr_dtor(&range_zv);
566 :
567 4 : array_init(&rename_params);
568 4 : array_init(&position);
569 4 : add_assoc_long(&position, "line", line);
570 4 : add_assoc_long(&position, "character", character);
571 4 : add_assoc_zval(&rename_params, "position", &position);
572 4 : add_assoc_str(&rename_params, "newName", zend_string_copy(new_name));
573 :
574 4 : ZVAL_UNDEF(&rename_result);
575 4 : lsp_lsparrot_rename(server, &rename_result, document, &rename_params);
576 4 : zval_ptr_dtor(&rename_params);
577 :
578 4 : result_changes = Z_TYPE(rename_result) == IS_ARRAY ? lsp_array_find(&rename_result, "changes") : NULL;
579 4 : if (result_changes && Z_TYPE_P(result_changes) == IS_ARRAY) {
580 16 : ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(result_changes), uri_key, edits) {
581 : zval *existing, copy, edit_copy, *single;
582 :
583 12 : if (!uri_key || Z_TYPE_P(edits) != IS_ARRAY) {
584 0 : continue;
585 : }
586 :
587 12 : existing = zend_hash_find(Z_ARRVAL(changes), uri_key);
588 12 : if (existing) {
589 0 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(edits), single) {
590 0 : ZVAL_COPY(&edit_copy, single);
591 0 : add_next_index_zval(existing, &edit_copy);
592 : } ZEND_HASH_FOREACH_END();
593 : } else {
594 12 : ZVAL_COPY(©, edits);
595 12 : zend_hash_update(Z_ARRVAL(changes), uri_key, ©);
596 : }
597 :
598 12 : any = true;
599 : } ZEND_HASH_FOREACH_END();
600 : }
601 :
602 4 : if (!Z_ISUNDEF(rename_result)) {
603 4 : zval_ptr_dtor(&rename_result);
604 : }
605 : }
606 :
607 : /* 2. Moved across PSR-4 directories: rewrite the namespace
608 : * declaration to the destination mapping. */
609 8 : if (document) {
610 8 : old_dir = lsp_workspace_dirname(old_path);
611 8 : new_dir = lsp_workspace_dirname(new_path);
612 :
613 8 : if (!zend_string_equals(old_dir, new_dir)) {
614 4 : expected_ns = lsp_workspace_psr4_namespace_for_dir(server, new_dir);
615 4 : if (expected_ns) {
616 4 : current_ns = lsp_document_namespace(document->text);
617 8 : if (!zend_string_equals(current_ns, expected_ns) &&
618 4 : lsp_workspace_namespace_decl_range(document->text, &ns_start, &ns_end, &stmt_start, &stmt_end)
619 : ) {
620 4 : if (ZSTR_LEN(expected_ns) > 0) {
621 4 : lsp_workspace_append_text_edit(&changes, old_uri, document->text, ns_start, ns_end, expected_ns);
622 4 : any = true;
623 0 : } else if (ZSTR_LEN(current_ns) > 0 && stmt_end > stmt_start) {
624 : /* Destination maps to the global namespace:
625 : * remove the whole declaration statement. */
626 0 : lsp_workspace_append_text_edit(&changes, old_uri, document->text, stmt_start, stmt_end, zend_empty_string);
627 0 : any = true;
628 : }
629 : }
630 4 : if (current_ns != zend_empty_string) {
631 4 : zend_string_release(current_ns);
632 : }
633 4 : zend_string_release(expected_ns);
634 : }
635 : }
636 :
637 8 : zend_string_release(old_dir);
638 8 : zend_string_release(new_dir);
639 : }
640 :
641 8 : if (old_name) {
642 8 : zend_string_release(old_name);
643 : }
644 8 : if (new_name) {
645 8 : zend_string_release(new_name);
646 : }
647 8 : zend_string_release(old_path);
648 8 : zend_string_release(new_path);
649 : } ZEND_HASH_FOREACH_END();
650 :
651 8 : if (!any) {
652 0 : zval_ptr_dtor(&changes);
653 0 : ZVAL_NULL(return_value);
654 :
655 0 : return;
656 : }
657 :
658 8 : array_init(return_value);
659 8 : changes_zv = &changes;
660 8 : add_assoc_zval(return_value, "changes", changes_zv);
661 : }
|