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 : /* Token-based whitespace formatter: re-indents lines from the brace/paren/
19 : * bracket depth carried by the token stream, keeps switch bodies one level
20 : * inside their case labels, indents continuation lines of split expressions,
21 : * aligns doc-comment continuation lines, trims trailing whitespace, and
22 : * guarantees a final newline. Everything the lexer marks as string, heredoc,
23 : * or inline-HTML interior is copied verbatim -- the formatter must never
24 : * change runtime values. Alternative-syntax control flow (endif/endforeach/
25 : * ...) has no brace anchors, so files using it fall back to trim-only. */
26 :
27 : #define LSP_FORMAT_LINE_UNSET 0xFFu
28 : #define LSP_FORMAT_LINE_CODE 0u
29 : #define LSP_FORMAT_LINE_LOCKED 1u
30 : #define LSP_FORMAT_LINE_BLANK 2u
31 : #define LSP_FORMAT_LINE_COMMENT 3u
32 :
33 : #define LSP_FORMAT_MAX_INDENT_UNITS 64
34 :
35 : typedef struct _lsp_format_options {
36 : bool reindent;
37 : bool trim_trailing;
38 : bool final_newline;
39 : bool insert_spaces;
40 : uint32_t indent_size;
41 : } lsp_format_options;
42 :
43 : typedef struct _lsp_format_line {
44 : int32_t indent;
45 : uint8_t kind;
46 : } lsp_format_line;
47 :
48 3068 : static inline bool lsp_format_token_is_whitespace(zval *token)
49 : {
50 3068 : return lsp_token_long(token, "id", 0) == T_WHITESPACE;
51 : }
52 :
53 2160 : static inline bool lsp_format_token_is_comment(zend_long id)
54 : {
55 2160 : return id == T_COMMENT || id == T_DOC_COMMENT;
56 : }
57 :
58 48 : static inline bool lsp_format_id_locks_interior(zend_long id)
59 : {
60 48 : return id == T_CONSTANT_ENCAPSED_STRING ||
61 96 : id == T_ENCAPSED_AND_WHITESPACE ||
62 : id == T_INLINE_HTML
63 : ;
64 : }
65 :
66 1704 : static inline bool lsp_format_id_disables_reindent(zend_long id)
67 : {
68 1704 : return id == T_ENDIF ||
69 1704 : id == T_ENDFOR ||
70 1704 : id == T_ENDFOREACH ||
71 1704 : id == T_ENDWHILE ||
72 3408 : id == T_ENDSWITCH ||
73 : id == T_ENDDECLARE
74 : ;
75 : }
76 :
77 : /* Operators that signal a statement continued from (or onto) another line;
78 : * used to give split expressions one extra indent level. ':' stays out of the
79 : * previous-line set because 'case 1:' legitimately ends a line. */
80 744 : static inline bool lsp_format_text_is_continuation_op(zend_string *text, bool line_start)
81 : {
82 : static const char *shared[] = {
83 : "->", "?->", "::", ".", "=>", "?\?", "&&", "||",
84 : "=", ".=", "+=", "-=", "*=", "/=", "%=", "**=", "?\?=",
85 : "|=", "&=", "^=", "<<=", ">>=",
86 : "+", "-", "*", "/", "%", "**",
87 : "==", "===", "!=", "!==", "<=>", "<=", ">=",
88 : "?", "and", "or", "xor", "instanceof",
89 : };
90 : size_t i, length;
91 :
92 744 : if (!text || ZSTR_LEN(text) == 0 || ZSTR_LEN(text) > sizeof("instanceof") - 1) {
93 24 : return false;
94 : }
95 :
96 29076 : for (i = 0; i < sizeof(shared) / sizeof(shared[0]); i++) {
97 28368 : length = strlen(shared[i]);
98 28368 : if (ZSTR_LEN(text) == length && strncasecmp(ZSTR_VAL(text), shared[i], length) == 0) {
99 12 : return true;
100 : }
101 : }
102 :
103 708 : return line_start && zend_string_equals_literal(text, ":");
104 : }
105 :
106 : static inline bool lsp_format_char_is_opener(zend_long id)
107 : {
108 : return id == '(' || id == '[' || id == '{';
109 : }
110 :
111 648 : static inline bool lsp_format_char_is_closer(zend_long id)
112 : {
113 648 : return id == ')' || id == ']' || id == '}';
114 : }
115 :
116 1908 : static inline size_t lsp_format_line_of_offset(const size_t *line_starts, size_t line_count, size_t offset)
117 : {
118 1908 : size_t low = 0, high = line_count;
119 :
120 11448 : while (low + 1 < high) {
121 9540 : size_t mid = low + (high - low) / 2;
122 9540 : if (line_starts[mid] <= offset) {
123 5328 : low = mid;
124 : } else {
125 4212 : high = mid;
126 : }
127 : }
128 :
129 1908 : return low;
130 : }
131 :
132 48 : static inline void lsp_format_mark_interior(lsp_format_line *lines, size_t line_count, size_t from_line, size_t to_line, uint8_t kind, int32_t indent)
133 : {
134 : size_t line;
135 :
136 180 : for (line = from_line; line <= to_line && line < line_count; line++) {
137 132 : if (lines[line].kind == LSP_FORMAT_LINE_UNSET) {
138 108 : lines[line].kind = kind;
139 108 : lines[line].indent = indent;
140 : }
141 : }
142 48 : }
143 :
144 1704 : static inline size_t lsp_format_count_newlines(zend_string *text)
145 : {
146 1704 : const char *p = ZSTR_VAL(text), *end = p + ZSTR_LEN(text);
147 1704 : size_t count = 0;
148 :
149 1836 : while (p < end) {
150 1788 : p = memchr(p, '\n', (size_t) (end - p));
151 1788 : if (!p) {
152 1656 : break;
153 : }
154 132 : count++;
155 132 : p++;
156 : }
157 :
158 1704 : return count;
159 : }
160 :
161 24 : static inline bool lsp_format_array_bool(zval *array, const char *key, bool fallback)
162 : {
163 24 : zval *value = lsp_array_find(array, key);
164 :
165 24 : if (!value) {
166 0 : return fallback;
167 : }
168 :
169 24 : if (Z_TYPE_P(value) == IS_TRUE) {
170 24 : return true;
171 : }
172 :
173 0 : if (Z_TYPE_P(value) == IS_FALSE) {
174 0 : return false;
175 : }
176 :
177 0 : return fallback;
178 : }
179 :
180 : /* Resolve the effective formatting behavior: LSP FormattingOptions from the
181 : * request (tabSize/insertSpaces) filtered through the server-side overrides
182 : * configured via start_lsp(['formatting' => ...]). */
183 24 : static inline void lsp_format_options_resolve(lsp_server *server, zval *params, lsp_format_options *options)
184 : {
185 : zval *client;
186 : zend_long tab_size;
187 :
188 24 : options->reindent = server->options.formatting_reindent;
189 24 : options->trim_trailing = server->options.formatting_trim_trailing_whitespace;
190 24 : options->final_newline = server->options.formatting_insert_final_newline;
191 24 : options->insert_spaces = true;
192 24 : options->indent_size = 4;
193 :
194 24 : client = params ? lsp_array_find(params, "options") : NULL;
195 24 : if (client && Z_TYPE_P(client) == IS_ARRAY) {
196 24 : options->insert_spaces = lsp_format_array_bool(client, "insertSpaces", true);
197 24 : tab_size = lsp_array_long(client, "tabSize", 4);
198 24 : if (tab_size >= 1 && tab_size <= 16) {
199 24 : options->indent_size = (uint32_t) tab_size;
200 : }
201 : }
202 :
203 24 : if (server->options.formatting_indent_style == LSP_INDENT_STYLE_SPACE) {
204 0 : options->insert_spaces = true;
205 24 : } else if (server->options.formatting_indent_style == LSP_INDENT_STYLE_TAB) {
206 8 : options->insert_spaces = false;
207 : }
208 :
209 24 : if (server->options.formatting_indent_size > 0) {
210 0 : options->indent_size = (uint32_t) server->options.formatting_indent_size;
211 : }
212 24 : }
213 :
214 : /* One pass over the token stream fills the per-line plan: target indent units
215 : * for code lines, locked spans for string/HTML interiors and heredocs, and
216 : * doc-comment continuations. Returns false when the file uses alternative
217 : * syntax and only trims may be applied. */
218 24 : static inline bool lsp_format_plan_lines(zend_string *text, const size_t *line_starts, size_t line_count, lsp_format_line *lines)
219 : {
220 : zval tokens_zv, *token, *peek;
221 : HashTable *tokens;
222 : zend_string *token_text, *prev_code_text;
223 : zend_long id, peek_id, depth, curly, paren, switch_size, switch_capacity, *switch_stack;
224 : zend_long tmp_depth, tmp_curly, tmp_switch, pending_switch_paren;
225 : size_t token_offset, token_line, token_end_line, newlines, seen_line, prev_code_line, peek_offset;
226 : uint32_t i, j, count;
227 : int32_t units;
228 : bool reindent_supported, pending_switch, first_on_line;
229 :
230 24 : ZVAL_UNDEF(&tokens_zv);
231 24 : lsp_lsparrot_tokens_to_zval(&tokens_zv, text);
232 24 : if (Z_TYPE(tokens_zv) != IS_ARRAY) {
233 0 : if (!Z_ISUNDEF(tokens_zv)) {
234 0 : zval_ptr_dtor(&tokens_zv);
235 : }
236 :
237 0 : return false;
238 : }
239 :
240 24 : tokens = Z_ARRVAL(tokens_zv);
241 24 : count = zend_hash_num_elements(tokens);
242 24 : depth = 0;
243 24 : curly = 0;
244 24 : paren = 0;
245 24 : switch_size = 0;
246 24 : switch_capacity = 8;
247 24 : switch_stack = emalloc(sizeof(zend_long) * (size_t) switch_capacity);
248 24 : pending_switch = false;
249 24 : pending_switch_paren = 0;
250 24 : prev_code_text = NULL;
251 24 : prev_code_line = (size_t) -1;
252 24 : seen_line = (size_t) -1;
253 24 : reindent_supported = true;
254 :
255 2812 : for (i = 0; i < count; i++) {
256 2788 : token = zend_hash_index_find(tokens, i);
257 2788 : if (!token || Z_TYPE_P(token) != IS_ARRAY || lsp_format_token_is_whitespace(token)) {
258 1084 : continue;
259 : }
260 :
261 1704 : id = lsp_token_long(token, "id", 0);
262 1704 : token_text = lsp_token_string(token, "text");
263 1704 : token_offset = (size_t) lsp_token_long(token, "offset", 0);
264 1704 : token_line = lsp_format_line_of_offset(line_starts, line_count, token_offset);
265 :
266 1704 : if (lsp_format_id_disables_reindent(id)) {
267 0 : reindent_supported = false;
268 : }
269 :
270 : /* First significant token on a not-yet-planned line decides that
271 : * line's indent. */
272 1704 : first_on_line = token_line != seen_line;
273 1704 : if (first_on_line) {
274 564 : seen_line = token_line;
275 : }
276 :
277 1704 : if (first_on_line && token_line < line_count && lines[token_line].kind == LSP_FORMAT_LINE_UNSET) {
278 540 : if (lsp_format_char_is_closer(id)) {
279 : /* Consume the run of closers that starts the line so
280 : * `));` outdents past every bracket it closes. */
281 108 : tmp_depth = depth;
282 108 : tmp_curly = curly;
283 108 : tmp_switch = switch_size;
284 304 : for (j = i; j < count; j++) {
285 280 : peek = zend_hash_index_find(tokens, j);
286 280 : if (!peek || Z_TYPE_P(peek) != IS_ARRAY) {
287 0 : continue;
288 : }
289 280 : if (lsp_format_token_is_whitespace(peek)) {
290 88 : continue;
291 : }
292 192 : peek_id = lsp_token_long(peek, "id", 0);
293 192 : peek_offset = (size_t) lsp_token_long(peek, "offset", 0);
294 192 : if (lsp_format_line_of_offset(line_starts, line_count, peek_offset) != token_line || !lsp_format_char_is_closer(peek_id)) {
295 : break;
296 : }
297 108 : if (peek_id == '}') {
298 108 : if (tmp_switch > 0 && switch_stack[tmp_switch - 1] == tmp_curly) {
299 12 : tmp_switch--;
300 : }
301 108 : tmp_curly--;
302 : }
303 108 : tmp_depth--;
304 : }
305 :
306 108 : units = (int32_t) (tmp_depth + tmp_switch);
307 : } else {
308 432 : units = (int32_t) (depth + switch_size);
309 432 : if ((id == T_CASE || id == T_DEFAULT) && switch_size > 0 && switch_stack[switch_size - 1] == curly) {
310 : /* case/default labels sit at the switch level; only
311 : * their bodies take the extra unit. Enum cases and
312 : * match arms never satisfy the stack check. */
313 24 : units -= 1;
314 792 : } else if (prev_code_text && prev_code_line != (size_t) -1 && prev_code_line < token_line && (
315 384 : lsp_format_text_is_continuation_op(prev_code_text, false) ||
316 384 : (!lsp_format_token_is_comment(id) && lsp_format_text_is_continuation_op(token_text, true))
317 : )) {
318 12 : units += 1;
319 : }
320 : }
321 :
322 540 : if (units < 0) {
323 0 : units = 0;
324 : }
325 540 : if (units > LSP_FORMAT_MAX_INDENT_UNITS) {
326 0 : units = LSP_FORMAT_MAX_INDENT_UNITS;
327 : }
328 :
329 540 : lines[token_line].kind = LSP_FORMAT_LINE_CODE;
330 540 : lines[token_line].indent = units;
331 : }
332 :
333 : /* Multi-line tokens dictate what may happen to the lines they
334 : * cover. */
335 1704 : if (token_text) {
336 1704 : newlines = lsp_format_count_newlines(token_text);
337 1704 : if (newlines > 0) {
338 72 : token_end_line = token_line + newlines;
339 72 : if (lsp_format_token_is_comment(id) && ZSTR_LEN(token_text) >= 2 && ZSTR_VAL(token_text)[1] == '*') {
340 48 : lsp_format_mark_interior(lines, line_count, token_line + 1, token_end_line,
341 : LSP_FORMAT_LINE_COMMENT,
342 24 : token_line < line_count && lines[token_line].kind == LSP_FORMAT_LINE_CODE ? lines[token_line].indent : -1);
343 48 : } else if (lsp_format_id_locks_interior(id)) {
344 12 : lsp_format_mark_interior(lines, line_count, token_line + 1, token_end_line, LSP_FORMAT_LINE_LOCKED, -1);
345 : }
346 : }
347 : }
348 :
349 1704 : if (id == T_START_HEREDOC) {
350 : /* Everything through the closing label is semantic content;
351 : * the label's own indentation defines the stripped margin. */
352 24 : for (j = i + 1; j < count; j++) {
353 24 : peek = zend_hash_index_find(tokens, j);
354 24 : if (!peek || Z_TYPE_P(peek) != IS_ARRAY) {
355 0 : continue;
356 : }
357 24 : if (lsp_token_long(peek, "id", 0) == T_END_HEREDOC) {
358 12 : peek_offset = (size_t) lsp_token_long(peek, "offset", 0);
359 12 : token_end_line = lsp_format_line_of_offset(line_starts, line_count, peek_offset);
360 12 : lsp_format_mark_interior(lines, line_count, token_line + 1, token_end_line, LSP_FORMAT_LINE_LOCKED, -1);
361 12 : break;
362 : }
363 : }
364 : }
365 :
366 1704 : if (id == T_INLINE_HTML && token_line < line_count && lines[token_line].kind == LSP_FORMAT_LINE_CODE) {
367 : /* Template markup owns its own line layout. */
368 0 : lines[token_line].kind = LSP_FORMAT_LINE_LOCKED;
369 0 : lines[token_line].indent = -1;
370 : }
371 :
372 : /* State updates. */
373 1704 : if (id == T_SWITCH) {
374 12 : pending_switch = true;
375 12 : pending_switch_paren = paren;
376 1692 : } else if (pending_switch && id == ';') {
377 0 : pending_switch = false;
378 : }
379 :
380 1704 : if (id == '(') {
381 100 : paren++;
382 100 : depth++;
383 1604 : } else if (id == ')') {
384 100 : paren--;
385 100 : depth--;
386 1504 : } else if (id == '[' || id == T_ATTRIBUTE) {
387 24 : depth++;
388 1480 : } else if (id == ']') {
389 24 : depth--;
390 1456 : } else if (id == '{' || id == T_CURLY_OPEN || id == T_DOLLAR_OPEN_CURLY_BRACES) {
391 108 : curly++;
392 108 : depth++;
393 108 : if (pending_switch && id == '{' && paren == pending_switch_paren) {
394 12 : if (switch_size == switch_capacity) {
395 0 : switch_capacity *= 2;
396 0 : switch_stack = erealloc(switch_stack, sizeof(zend_long) * (size_t) switch_capacity);
397 : }
398 12 : switch_stack[switch_size++] = curly;
399 12 : pending_switch = false;
400 : }
401 1348 : } else if (id == '}') {
402 108 : if (switch_size > 0 && switch_stack[switch_size - 1] == curly) {
403 12 : switch_size--;
404 : }
405 108 : curly--;
406 108 : depth--;
407 : }
408 :
409 1704 : if (!lsp_format_token_is_comment(id)) {
410 1680 : prev_code_text = token_text;
411 1680 : prev_code_line = token_line;
412 : }
413 : }
414 :
415 24 : efree(switch_stack);
416 24 : zval_ptr_dtor(&tokens_zv);
417 :
418 24 : return reindent_supported;
419 : }
420 :
421 380 : static inline void lsp_format_append_indent(smart_str *out, const lsp_format_options *options, int32_t units)
422 : {
423 : int32_t i;
424 :
425 380 : if (units <= 0) {
426 100 : return;
427 : }
428 :
429 280 : if (options->insert_spaces) {
430 460 : for (i = 0; i < units; i++) {
431 308 : smart_str_appendl(out, " ", options->indent_size <= 16 ? options->indent_size : 16);
432 : }
433 : } else {
434 376 : for (i = 0; i < units; i++) {
435 248 : smart_str_appendc(out, '\t');
436 : }
437 : }
438 : }
439 :
440 : /* Emit lines [from_line, to_line] of the formatting plan. */
441 24 : static inline zend_string *lsp_format_emit(zend_string *text, const size_t *line_starts, size_t line_count, const lsp_format_line *lines, const lsp_format_options *options, bool reindent, size_t from_line, size_t to_line, bool whole_document)
442 : {
443 24 : smart_str out = {0};
444 24 : const char *value = ZSTR_VAL(text), *line_start, *line_end, *content_start, *trim_end;
445 : size_t line, next_start;
446 : bool has_newline;
447 :
448 448 : for (line = from_line; line <= to_line && line < line_count; line++) {
449 424 : line_start = value + line_starts[line];
450 424 : next_start = line + 1 < line_count ? line_starts[line + 1] : ZSTR_LEN(text);
451 424 : line_end = value + next_start;
452 424 : has_newline = line_end > line_start && line_end[-1] == '\n';
453 424 : if (has_newline) {
454 408 : line_end--;
455 : }
456 :
457 424 : if (lines[line].kind == LSP_FORMAT_LINE_LOCKED) {
458 24 : smart_str_appendl(&out, line_start, (size_t) (line_end - line_start));
459 24 : if (has_newline) {
460 24 : smart_str_appendc(&out, '\n');
461 : }
462 24 : continue;
463 : }
464 :
465 400 : trim_end = line_end;
466 400 : if (options->trim_trailing || lines[line].kind == LSP_FORMAT_LINE_BLANK || lines[line].kind == LSP_FORMAT_LINE_UNSET) {
467 400 : if (trim_end > line_start && trim_end[-1] == '\r') {
468 0 : trim_end--;
469 : }
470 464 : while (trim_end > line_start && (trim_end[-1] == ' ' || trim_end[-1] == '\t')) {
471 64 : trim_end--;
472 : }
473 0 : } else if (trim_end > line_start && trim_end[-1] == '\r') {
474 0 : trim_end--;
475 : }
476 :
477 400 : content_start = line_start;
478 748 : while (content_start < trim_end && (*content_start == ' ' || *content_start == '\t')) {
479 348 : content_start++;
480 : }
481 :
482 400 : if (content_start >= trim_end) {
483 : /* Blank (or whitespace-only) line. */
484 20 : if (!options->trim_trailing) {
485 0 : smart_str_appendl(&out, line_start, (size_t) (trim_end - line_start));
486 : }
487 20 : if (has_newline) {
488 20 : smart_str_appendc(&out, '\n');
489 : }
490 20 : continue;
491 : }
492 :
493 380 : if (reindent && lines[line].kind == LSP_FORMAT_LINE_CODE && lines[line].indent >= 0) {
494 352 : lsp_format_append_indent(&out, options, lines[line].indent);
495 352 : smart_str_appendl(&out, content_start, (size_t) (trim_end - content_start));
496 28 : } else if (reindent && lines[line].kind == LSP_FORMAT_LINE_COMMENT && lines[line].indent >= 0 && *content_start == '*') {
497 28 : lsp_format_append_indent(&out, options, lines[line].indent);
498 28 : smart_str_appendc(&out, ' ');
499 28 : smart_str_appendl(&out, content_start, (size_t) (trim_end - content_start));
500 : } else {
501 : /* Keep the author's leading whitespace, only trim the tail. */
502 0 : smart_str_appendl(&out, line_start, (size_t) (trim_end - line_start));
503 : }
504 :
505 380 : if (has_newline) {
506 364 : smart_str_appendc(&out, '\n');
507 : }
508 : }
509 :
510 24 : if (whole_document && options->final_newline) {
511 16 : if (!out.s || ZSTR_LEN(out.s) == 0 || ZSTR_VAL(out.s)[ZSTR_LEN(out.s) - 1] != '\n') {
512 16 : smart_str_appendc(&out, '\n');
513 : }
514 : }
515 :
516 24 : smart_str_0(&out);
517 :
518 24 : return out.s ? out.s : zend_empty_string;
519 : }
520 :
521 24 : static inline size_t *lsp_format_line_starts(zend_string *text, size_t *line_count)
522 : {
523 24 : const char *value = ZSTR_VAL(text), *p, *end = value + ZSTR_LEN(text);
524 : size_t *starts, count, i;
525 :
526 24 : count = 1;
527 9508 : for (p = value; p < end; p++) {
528 9484 : if (*p == '\n') {
529 672 : count++;
530 : }
531 : }
532 :
533 24 : starts = emalloc(sizeof(size_t) * count);
534 24 : starts[0] = 0;
535 24 : i = 1;
536 9508 : for (p = value; p < end; p++) {
537 9484 : if (*p == '\n' && i < count) {
538 672 : starts[i++] = (size_t) (p - value) + 1;
539 : }
540 : }
541 :
542 24 : *line_count = count;
543 :
544 24 : return starts;
545 : }
546 :
547 24 : static inline zend_string *lsp_format_document_lines(lsp_server *server, lsp_document *document, zval *params, size_t from_line, size_t to_line, size_t *out_start_offset, size_t *out_end_offset, bool whole_document)
548 : {
549 : lsp_format_options options;
550 : lsp_format_line *lines;
551 : zend_string *formatted;
552 : size_t *line_starts, line_count, i;
553 : bool reindent;
554 :
555 24 : lsp_format_options_resolve(server, params, &options);
556 24 : line_starts = lsp_format_line_starts(document->text, &line_count);
557 24 : lines = emalloc(sizeof(lsp_format_line) * line_count);
558 720 : for (i = 0; i < line_count; i++) {
559 696 : lines[i].kind = LSP_FORMAT_LINE_UNSET;
560 696 : lines[i].indent = -1;
561 : }
562 :
563 : /* The plan must ALWAYS run: it is what marks string/heredoc/inline-HTML
564 : * interiors as locked so trimming can never alter runtime values. Only
565 : * the reindent decision depends on its verdict. */
566 24 : reindent = lsp_format_plan_lines(document->text, line_starts, line_count, lines) && options.reindent;
567 :
568 24 : if (to_line >= line_count) {
569 16 : to_line = line_count > 0 ? line_count - 1 : 0;
570 : }
571 24 : if (from_line > to_line) {
572 0 : from_line = to_line;
573 : }
574 :
575 24 : *out_start_offset = line_starts[from_line];
576 24 : *out_end_offset = to_line + 1 < line_count ? line_starts[to_line + 1] : ZSTR_LEN(document->text);
577 :
578 24 : formatted = lsp_format_emit(document->text, line_starts, line_count, lines, &options, reindent, from_line, to_line, whole_document);
579 :
580 24 : efree(lines);
581 24 : efree(line_starts);
582 :
583 24 : return formatted;
584 : }
585 :
586 24 : static inline void lsp_format_reply_edit(zval *return_value, lsp_document *document, zend_string *formatted, size_t start_offset, size_t end_offset)
587 : {
588 : zval edit, range;
589 :
590 24 : array_init(return_value);
591 24 : if (ZSTR_LEN(formatted) == end_offset - start_offset &&
592 0 : memcmp(ZSTR_VAL(formatted), ZSTR_VAL(document->text) + start_offset, ZSTR_LEN(formatted)) == 0
593 : ) {
594 0 : zend_string_release(formatted);
595 :
596 0 : return;
597 : }
598 :
599 24 : array_init(&edit);
600 24 : lsp_range_from_offsets(document->text, start_offset, end_offset, &range);
601 24 : add_assoc_zval(&edit, "range", &range);
602 24 : add_assoc_str(&edit, "newText", formatted);
603 24 : add_next_index_zval(return_value, &edit);
604 : }
605 :
606 20 : extern void lsp_lsparrot_formatting(lsp_server *server, zval *return_value, lsp_document *document, zval *params)
607 : {
608 : zend_string *formatted;
609 : size_t start_offset, end_offset;
610 :
611 20 : if (!server->options.formatting_enabled) {
612 4 : ZVAL_NULL(return_value);
613 :
614 4 : return;
615 : }
616 :
617 16 : formatted = lsp_format_document_lines(server, document, params, 0, SIZE_MAX - 1, &start_offset, &end_offset, true);
618 16 : lsp_format_reply_edit(return_value, document, formatted, 0, ZSTR_LEN(document->text));
619 : }
620 :
621 8 : extern void lsp_lsparrot_range_formatting(lsp_server *server, zval *return_value, lsp_document *document, zval *params)
622 : {
623 : zval *range, *start, *end;
624 : zend_string *formatted;
625 : zend_long start_line, end_line, end_character;
626 : size_t start_offset, end_offset;
627 :
628 8 : if (!server->options.formatting_enabled) {
629 0 : ZVAL_NULL(return_value);
630 :
631 0 : return;
632 : }
633 :
634 8 : range = lsp_array_find(params, "range");
635 8 : start = range ? lsp_array_find(range, "start") : NULL;
636 8 : end = range ? lsp_array_find(range, "end") : NULL;
637 8 : if (!start || !end) {
638 0 : array_init(return_value);
639 :
640 0 : return;
641 : }
642 :
643 8 : start_line = lsp_array_long(start, "line", 0);
644 8 : end_line = lsp_array_long(end, "line", 0);
645 8 : end_character = lsp_array_long(end, "character", 0);
646 8 : if (start_line < 0) {
647 0 : start_line = 0;
648 : }
649 8 : if (end_line < start_line) {
650 0 : end_line = start_line;
651 : }
652 : /* A range ending at column 0 of a later line excludes that line. */
653 8 : if (end_character == 0 && end_line > start_line) {
654 4 : end_line--;
655 : }
656 :
657 8 : formatted = lsp_format_document_lines(server, document, params, (size_t) start_line, (size_t) end_line, &start_offset, &end_offset, false);
658 8 : lsp_format_reply_edit(return_value, document, formatted, start_offset, end_offset);
659 : }
|