1mod doc;
31pub mod literal;
32mod lower;
33mod trivia;
34
35use std::fmt;
36
37use gdck_config::FormatConfig;
38use gdck_syntax::{Element, SyntaxKind, SyntaxNode, SyntaxTree};
39
40use crate::lower::Lowerer;
41use crate::trivia::Trivia;
42
43#[derive(Debug, Clone, PartialEq, Eq)]
45pub enum FormatError {
46 Unparseable,
48 SafetyCheckFailed(&'static str),
50}
51
52impl fmt::Display for FormatError {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 match self {
55 Self::Unparseable => f.write_str("cannot format a file with syntax errors"),
56 Self::SafetyCheckFailed(what) => {
57 write!(f, "formatting was rejected by a safety check: {what}")
58 }
59 }
60 }
61}
62
63impl std::error::Error for FormatError {}
64
65pub fn format(tree: &SyntaxTree, config: &FormatConfig) -> Result<String, FormatError> {
73 if tree.has_errors() {
74 return Err(FormatError::Unparseable);
75 }
76
77 let output = render(tree, config);
78
79 if !config.safety_checks {
80 return Ok(output);
81 }
82
83 let reparsed = gdck_syntax::parse(&output);
84 if reparsed.has_errors() {
85 return Err(FormatError::SafetyCheckFailed(
86 "the formatted output does not parse",
87 ));
88 }
89 if canonical(tree) != canonical(&reparsed) {
90 return Err(FormatError::SafetyCheckFailed(
91 "formatting changed the code",
92 ));
93 }
94 let before = Trivia::collect(tree);
95 let after = Trivia::collect(&reparsed);
96 if before.all_comments() != after.all_comments() {
97 return Err(FormatError::SafetyCheckFailed("a comment was lost"));
98 }
99 let second = render(&reparsed, config);
100 if second != output {
101 return Err(FormatError::SafetyCheckFailed(
102 "formatting is not idempotent",
103 ));
104 }
105
106 Ok(output)
107}
108
109pub fn format_source(source: &str, config: &FormatConfig) -> Result<String, FormatError> {
115 format(&gdck_syntax::parse(source), config)
116}
117
118fn render(tree: &SyntaxTree, config: &FormatConfig) -> String {
119 let trivia = Trivia::collect(tree);
120 let lowerer = Lowerer::new(tree, &trivia, config.class_declaration);
121 let document = lowerer.source_file(tree.root());
122 let mut output = doc::render(&document, config.line_length as usize, config.indent);
123
124 while output.ends_with('\n') {
127 output.pop();
128 }
129 if !output.is_empty() {
130 output.push('\n');
131 }
132 output
133}
134
135#[derive(Debug, Clone, PartialEq, Eq)]
137enum Step {
138 Enter(SyntaxKind),
139 Token(SyntaxKind, String),
140 Operator(&'static str),
142}
143
144fn canonical(tree: &SyntaxTree) -> Vec<Step> {
161 let mut steps = Vec::new();
162 walk(tree.root(), tree.text(), &mut steps);
163 steps
164}
165
166fn walk(node: SyntaxNode<'_>, source: &str, steps: &mut Vec<Step>) {
167 match node.kind() {
168 SyntaxKind::ParenExpr => {
171 for child in node.child_nodes() {
172 walk(child, source, steps);
173 }
174 return;
175 }
176 SyntaxKind::ClassDecl => {
177 walk_class_decl(node, source, steps);
178 return;
179 }
180 SyntaxKind::Initializer => {
181 steps.push(Step::Enter(SyntaxKind::Initializer));
182 let inferred = node
184 .child_tokens()
185 .any(|token| matches!(token.kind, SyntaxKind::ColonEq | SyntaxKind::Colon));
186 steps.push(Step::Operator(if inferred { ":=" } else { "=" }));
187 for child in node.child_nodes() {
188 walk(child, source, steps);
189 }
190 return;
191 }
192 _ => {}
193 }
194
195 steps.push(Step::Enter(node.kind()));
196 for element in node.children() {
197 match element {
198 Element::Node(id) => walk(node.tree().node(id), source, steps),
199 Element::Token(token) => push_token(token, source, steps),
200 }
201 }
202}
203
204fn walk_class_decl(node: SyntaxNode<'_>, source: &str, steps: &mut Vec<Step>) {
210 steps.push(Step::Enter(SyntaxKind::ClassDecl));
211
212 let block = node.child_node_of(SyntaxKind::Block);
213 let mut members: Vec<SyntaxNode<'_>> = block
214 .map(|block| block.child_nodes().collect())
215 .unwrap_or_default();
216
217 let mut extends = node.child_node_of(SyntaxKind::ExtendsDecl);
218 if extends.is_none() {
219 let body_level = members
220 .iter()
221 .position(|member| member.kind() == SyntaxKind::ExtendsDecl);
222 if let Some(index) = body_level {
223 extends = Some(members.remove(index));
224 }
225 }
226
227 for token in node.child_tokens() {
228 push_token(token, source, steps);
229 }
230 if let Some(extends) = extends {
231 walk(extends, source, steps);
232 }
233 if block.is_some() {
234 steps.push(Step::Enter(SyntaxKind::Block));
235 for member in members {
236 walk(member, source, steps);
237 }
238 }
239}
240
241fn push_token(token: gdck_syntax::Token, source: &str, steps: &mut Vec<Step>) {
242 if token.kind.is_trivia()
243 || matches!(
244 token.kind,
245 SyntaxKind::Indent
246 | SyntaxKind::Dedent
247 | SyntaxKind::Eof
248 | SyntaxKind::Comma
250 | SyntaxKind::Semicolon
251 )
252 {
253 return;
254 }
255 steps.push(Step::Token(
256 token.kind,
257 normalize_for_comparison(token, source),
258 ));
259}
260
261fn normalize_for_comparison(token: gdck_syntax::Token, source: &str) -> String {
264 let text = token.text(source);
265 match token.kind {
266 SyntaxKind::Int | SyntaxKind::Float => literal::normalize_number(text),
267 SyntaxKind::Str
268 | SyntaxKind::StringName
269 | SyntaxKind::NodePath
270 | SyntaxKind::GetNode
271 | SyntaxKind::UniqueNode => literal::normalize_string(text),
272 _ => text.to_string(),
273 }
274}
275
276#[cfg(test)]
277mod tests {
278 use super::*;
279
280 fn check(source: &str, expected: &str) {
281 let formatted = format_source(source, &FormatConfig::default())
282 .unwrap_or_else(|error| panic!("failed to format {source:?}: {error}"));
283 assert_eq!(formatted, expected, "\ninput was:\n{source}");
284 }
285
286 fn check_stable(source: &str) {
288 check(source, source);
289 }
290
291 #[test]
292 fn refuses_to_format_unparseable_input() {
293 let tree = gdck_syntax::parse("func f(:\n");
294 assert_eq!(
295 format(&tree, &FormatConfig::default()),
296 Err(FormatError::Unparseable)
297 );
298 }
299
300 #[test]
301 fn an_empty_file_stays_empty() {
302 check("", "");
303 }
304
305 #[test]
306 fn carriage_returns_are_normalised_away() {
307 check("var x = 1\r\nvar y = 2\r\n", "var x = 1\nvar y = 2\n");
309 }
310
311 #[test]
312 fn a_file_ends_with_exactly_one_newline() {
313 check("var x = 1", "var x = 1\n");
314 check("var x = 1\n\n\n", "var x = 1\n");
315 }
316
317 #[test]
318 fn operators_get_one_space_and_commas_one_after() {
319 check(
320 "func f():\n\tposition.x=5\n\tmy_array = [4,5,6]\n\tdict [\"key\"] = 5\n\tprint ( \"foo\" )\n",
321 "func f():\n\tposition.x = 5\n\tmy_array = [4, 5, 6]\n\tdict[\"key\"] = 5\n\tprint(\"foo\")\n",
322 );
323 }
324
325 #[test]
326 fn an_inner_class_declares_its_parent_on_one_line() {
327 check_stable("class Child extends Parent:\n\tpass\n");
329 check(
330 "class Child:\n\textends Parent\n\tpass\n",
331 "class Child extends Parent:\n\tpass\n",
332 );
333 }
334
335 #[test]
336 fn a_file_level_class_declares_its_parent_on_the_next_line() {
337 check(
339 "class_name Player extends Node\n",
340 "class_name Player\nextends Node\n",
341 );
342 check_stable("class_name Player\nextends Node\n");
343 }
344
345 #[test]
346 fn a_project_can_keep_the_joined_class_declaration() {
347 let mut joined = FormatConfig::default();
351 joined.class_declaration = gdck_config::ClassDeclaration::SingleLine;
352 for source in [
353 "class_name Player extends Node\n",
354 "class_name Player\nextends Node\n",
355 ] {
356 assert_eq!(
357 format_source(source, &joined).expect("formats"),
358 "class_name Player extends Node\n",
359 "\ninput was:\n{source}"
360 );
361 }
362 assert_eq!(
364 format_source("extends Node\n", &joined).expect("formats"),
365 "extends Node\n"
366 );
367 }
368
369 #[test]
370 fn a_comment_between_class_name_and_extends_survives() {
371 let source = "class_name Player\n# why we extend\nextends Node\n";
375 check_stable(source);
376 let mut joined = FormatConfig::default();
377 joined.class_declaration = gdck_config::ClassDeclaration::SingleLine;
378 assert_eq!(format_source(source, &joined).expect("formats"), source);
379 }
380
381 #[test]
382 fn abstract_stays_on_the_inner_class_line() {
383 check_stable("@abstract class MyNode extends Node:\n\tpass\n");
384 }
385
386 #[test]
387 fn a_functions_annotations_take_a_line_each() {
388 check_stable("@rpc(\"any_peer\")\nfunc ping() -> void:\n\tpass\n");
391 check(
392 "@rpc(\"any_peer\") func ping() -> void:\n\tpass\n",
393 "@rpc(\"any_peer\")\nfunc ping() -> void:\n\tpass\n",
394 );
395 check_stable("@export_range(0, 10) var lives = 3\n");
396 check_stable("@abstract\nclass_name Shape\n\n\n@abstract func area() -> float\n");
399 }
400
401 #[test]
402 fn one_statement_per_line() {
403 check(
404 "func f():\n\tif flag: print(\"flagged\")\n",
405 "func f():\n\tif flag:\n\t\tprint(\"flagged\")\n",
406 );
407 check("var a = 1; var b = 2\n", "var a = 1\nvar b = 2\n");
408 }
409
410 #[test]
411 fn the_ternary_operator_is_the_exception_to_that() {
412 check_stable("func f():\n\tnext_state = \"idle\" if is_on_floor() else \"fall\"\n");
413 }
414
415 #[test]
416 fn definitions_get_two_blank_lines_at_file_level() {
417 check(
418 "func a():\n\tpass\nfunc b():\n\tpass\n",
419 "func a():\n\tpass\n\n\nfunc b():\n\tpass\n",
420 );
421 }
422
423 #[test]
424 fn definitions_get_one_blank_line_inside_a_class() {
425 check_stable("class State:\n\tvar foo = 0\n\n\tfunc _init():\n\t\tprint(\"Hello!\")\n");
427 }
428
429 #[test]
430 fn blank_line_runs_collapse_to_one() {
431 check("var a = 1\n\n\n\nvar b = 2\n", "var a = 1\n\nvar b = 2\n");
432 }
433
434 #[test]
435 fn redundant_parentheses_are_dropped() {
436 check(
437 "func f():\n\tif (is_colliding()):\n\t\tqueue_free()\n",
438 "func f():\n\tif is_colliding():\n\t\tqueue_free()\n",
439 );
440 }
441
442 #[test]
443 fn parentheses_that_carry_meaning_are_kept() {
444 check_stable("var x = (a + b) * c\n");
445 check_stable("func f():\n\tif (foo and bar) or not baz:\n\t\tprint(\"yes\")\n");
446 }
447
448 #[test]
449 fn a_single_line_dictionary_gets_spaces_inside_its_braces() {
450 check(
451 "var my_dictionary = {key = \"value\"}\n",
452 "var my_dictionary = { key = \"value\" }\n",
453 );
454 check_stable("var empty = {}\n");
455 }
456
457 #[test]
458 fn collections_take_one_indent_level_and_a_trailing_comma() {
459 let long = "var party = [\"Godot\", \"Godette\", \"Steve\", \"a name quite long indeed\", \"and one more that certainly pushes it over\"]\n";
460 check(
461 long,
462 "var party = [\n\t\"Godot\",\n\t\"Godette\",\n\t\"Steve\",\n\t\"a name quite long indeed\",\n\t\"and one more that certainly pushes it over\",\n]\n",
463 );
464 }
465
466 #[test]
467 fn a_short_collection_stays_on_one_line_without_a_trailing_comma() {
468 check("var array = [1, 2, 3,]\n", "var array = [1, 2, 3]\n");
469 check(
472 "var array = [\n\t1,\n\t2\n]\n",
473 "var array = [\n\t1,\n\t2,\n]\n",
474 );
475 }
476
477 #[test]
478 fn comments_stay_with_what_they_document() {
479 check_stable("# Sets things up.\nfunc _ready():\n\tpass\n");
480 check_stable("var x = 1 # why\n");
481 check(
484 "var a = 1\n# Documents f.\nfunc f():\n\tpass\n",
485 "var a = 1\n\n\n# Documents f.\nfunc f():\n\tpass\n",
486 );
487 }
488
489 #[test]
490 fn a_trailing_comment_keeps_one_space_before_it() {
491 check("var x = 1 # why\n", "var x = 1 # why\n");
492 }
493
494 #[test]
495 fn comments_at_the_end_of_a_file_survive() {
496 check_stable("var x = 1\n\n# the end\n");
497 }
498
499 #[test]
500 fn a_lambda_written_inline_stays_inline() {
501 check_stable("var double = func(x): return x * 2\n");
502 }
503
504 #[test]
505 fn wrapped_expressions_take_two_indent_levels() {
506 check_stable(
509 "var position = Vector2(250, 350)\n\n\nfunc f():\n\tif (\n\t\t\tposition.x > 200\n\t\t\tand position.x < 400\n\t\t\tand position.y > 300\n\t\t\tand position.y < 400\n\t):\n\t\tpass\n",
510 );
511 }
512
513 #[test]
514 fn a_multi_line_lambda_keeps_its_block() {
515 check_stable(
516 "func f():\n\tbutton.pressed.connect(\n\t\t\tfunc() -> void:\n\t\t\t\tdo_something(),\n\t)\n",
517 );
518 }
519
520 #[test]
534 fn a_lambda_closing_a_nested_call_ends_with_a_comma() {
535 check_stable(
536 "func f():\n\
537 \tbox.add_child(\n\
538 \t\t\tmake_button(\n\
539 \t\t\t\t\t\"a long label here to force the formatter to wrap this\",\n\
540 \t\t\t\t\tfunc() -> void:\n\
541 \t\t\t\t\t\tdo_something(),\n\
542 \t\t\t)\n\
543 \t)\n",
544 );
545 }
546
547 #[test]
548 fn a_single_line_lambda_gains_no_comma() {
549 check_stable("func f():\n\tbutton.pressed.connect(func(): do_something())\n");
551 }
552
553 #[test]
559 fn parens_around_a_lambda_block_close_on_its_last_line() {
560 check(
564 "func f():\n\tassert((func() -> bool:\n\t\treturn check_something_here()).call())\n",
565 "func f():\n\
566 \tassert(\n\
567 \t\t\t(func() -> bool:\n\
568 \t\t\t\treturn check_something_here()).call()\n\
569 \t)\n",
570 );
571 check(
572 "func f():\n\
573 \tassert((func() -> bool:\n\
574 \t\tvar ok: bool = probe_the_thing_for_a_while(argument_one, argument_two)\n\
575 \t\treturn ok).call())\n",
576 "func f():\n\
577 \tassert(\n\
578 \t\t\t(func() -> bool:\n\
579 \t\t\t\tvar ok: bool = probe_the_thing_for_a_while(argument_one, argument_two)\n\
580 \t\t\t\treturn ok).call()\n\
581 \t)\n",
582 );
583 }
584
585 #[test]
591 fn a_standalone_annotation_keeps_its_own_line() {
592 check_stable(
593 "func f():\n\
594 \t@warning_ignore_start(\"integer_division\")\n\
595 \tvar halved := total / 2\n\
596 \t@warning_ignore_restore(\"integer_division\")\n\
597 \treturn halved\n",
598 );
599 check_stable(
600 "@export_category(\"Stats\")\n\
601 @export_group(\"Health\", \"health_\")\n\
602 var health_max := 10\n\
603 @export_subgroup(\"Regen\")\n\
604 var health_regen := 1.0\n",
605 );
606 }
607
608 #[test]
611 fn an_annotation_about_a_variable_stays_beside_it() {
612 check(
613 "@export_range(0, 10)\nvar lives := 3\n",
614 "@export_range(0, 10) var lives := 3\n",
615 );
616 }
617
618 #[test]
619 fn accessors_keep_the_form_they_were_written_in() {
620 check_stable("var health = max_health:\n\tset(new_health):\n\t\thealth = new_health\n");
621 check_stable("var is_active = true:\n\tset = set_is_active\n");
622 }
623
624 #[test]
630 fn a_setget_property_keeps_the_comma_between_its_accessors() {
631 check_stable("var p:\n\tset = __set,\n\tget = __get\n");
632 check_stable("var p:\n\tget = __get,\n\tset = __set\n");
633 }
634
635 #[test]
638 fn block_bodied_accessors_are_not_comma_separated() {
639 check_stable("var p:\n\tset(x):\n\t\t_p = x\n\tget:\n\t\treturn _p\n");
640 }
641
642 #[test]
643 fn the_safety_check_catches_a_lost_comment() {
644 let tree = gdck_syntax::parse("# a\nvar x = 1 # b\n## c\nfunc f():\n\tpass\n");
647 assert!(format(&tree, &FormatConfig::default()).is_ok());
648 }
649
650 #[test]
651 fn a_comment_moved_onto_its_own_line_keeps_no_inline_space() {
652 check(
655 "var x = {\n\tname = # why\n\t1\n}\n",
656 "var x = {\n\t# why\n\tname = 1,\n}\n",
657 );
658 }
659
660 #[test]
661 fn formatting_is_idempotent_on_awkward_input() {
662 let source = "class_name A extends B\nvar x={'k':1,}\nfunc f(a,b=2):\n\tif (a): return\n";
663 let first = format_source(source, &FormatConfig::default()).expect("formats");
664 let second = format_source(&first, &FormatConfig::default()).expect("formats");
665 assert_eq!(first, second);
666 }
667}