Skip to content

Commit cc4a6f7

Browse files
committed
Preserve up to 1 blank line between consecutive declarations when
reordering Fix #268
1 parent 04b8256 commit cc4a6f7

7 files changed

Lines changed: 129 additions & 33 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ This file documents the changes made to the formatter with each release.
3535
- Fix formatting with vs without `--reorder-code` not being idempotent in some situations (#207)
3636
- Fix code inside dedented region comments getting dedented too: region comments will now indent into the code (#172)
3737
- Fix dedented comment in a block's body (function body, loop body, if statement body...) causing following lines in the block to be dedented too (#252)
38+
- Preserve up to 1 blank line between consecutive declarations when reordering (#268)
3839
- Godot addon fixes:
3940
- Fix formatter undoes any changes in script if they have not been saved (#243)
4041
- Fix "Uninstall Formatter" command doesn't exist message (#189)

‎src/formatter.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,6 +2551,7 @@ fn process_source_reorder(
25512551
}
25522552
let mut previous_classification: Option<DeclarationKind> = None;
25532553
let mut previous_is_double_spaced = false;
2554+
let mut previous_child_index: Option<usize> = None;
25542555
let mut is_first = true;
25552556

25562557
let mut item_index = 0;
@@ -2560,13 +2561,20 @@ fn process_source_reorder(
25602561
item.classification,
25612562
DeclarationKind::Method | DeclarationKind::InnerClass
25622563
);
2564+
let is_in_source_order = match previous_child_index {
2565+
Some(index) => index < item.child_index,
2566+
None => false,
2567+
};
25632568
if !is_first {
25642569
if let Some(ref previous_child) = previous_classification {
25652570
if previous_child == &item.classification
25662571
&& !previous_is_double_spaced
25672572
&& !current_needs_two_blank
25682573
{
25692574
render_elements.push(RenderElement::HardLine);
2575+
if item.has_blank_line_before && is_in_source_order {
2576+
render_elements.push(RenderElement::BlankLine);
2577+
}
25702578
} else if previous_is_double_spaced || current_needs_two_blank {
25712579
let count = if current_needs_two_blank {
25722580
input.blank_lines_around_definitions
@@ -2597,6 +2605,7 @@ fn process_source_reorder(
25972605
is_first = false;
25982606
previous_classification = Some(item.classification);
25992607
previous_is_double_spaced = current_needs_two_blank;
2608+
previous_child_index = Some(item.child_index);
26002609

26012610
// output leading comments/annotations.
26022611
if item.classification != DeclarationKind::Docstring {

‎src/reorder.rs‎

Lines changed: 82 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ pub struct ReorderItem<'a> {
2929
pub leading_indices: Vec<usize>,
3030
/// Indices of trailing children (e.g. #endregion) glued to this decl.
3131
pub trailing_indices: Vec<usize>,
32+
/// Whether this declaration or its leading comments had a blank line before
33+
/// it in the source. This preserves blanks lines input by the user in their
34+
/// source code, up to 1. For example, blank lines used to separate groups
35+
/// of variables.
36+
pub has_blank_line_before: bool,
3237
/// Classification category (determines sort order).
3338
pub classification: DeclarationKind,
3439
/// Declaration name for tie-breaking within same category, borrowed from
@@ -70,6 +75,53 @@ pub enum MethodType {
7075
Custom, // all other user methods
7176
}
7277

78+
/// Checks whether a single `annotation` node's `identifier` child has text
79+
/// equal to `expected_name`.
80+
fn annotation_identifier_matches(
81+
annotation_node: Node,
82+
content: &str,
83+
expected_name: &str,
84+
) -> bool {
85+
let count = annotation_node.child_count();
86+
let mut child_index = 0;
87+
while child_index < count {
88+
if let Some(child) = annotation_node.child(child_index as u32) {
89+
if GDScriptNodeKind::get_kind_from_ast_node(child) == GDScriptNodeKind::Identifier {
90+
return get_node_text(child, content) == expected_name;
91+
}
92+
}
93+
child_index += 1;
94+
}
95+
false
96+
}
97+
98+
/// Slice the source string at a node's byte range.
99+
/// Tree-sitter byte offsets are always on UTF-8 char boundaries.
100+
fn get_node_text<'a>(node: Node<'a>, content: &'a str) -> &'a str {
101+
&content[node.start_byte()..node.end_byte()]
102+
}
103+
104+
/// Returns true when there are at least two line breaks between two byte
105+
/// offsets.
106+
fn has_blank_line(content: &str, from: usize, to: usize) -> bool {
107+
if to <= from {
108+
return false;
109+
}
110+
111+
let mut newline_count = 0;
112+
let mut byte_index = from;
113+
while byte_index < to {
114+
if content.as_bytes()[byte_index] == b'\n' {
115+
newline_count += 1;
116+
if newline_count == 2 {
117+
return true;
118+
}
119+
}
120+
byte_index += 1;
121+
}
122+
false
123+
}
124+
73125
/// Builds a `ReorderPlan` for the children of `parent` (typically a `source`
74126
/// node). `content` is the source string used for name extraction.
75127
pub fn build_reorder_plan<'a>(parent: Node<'a>, content: &'a str) -> ReorderPlan<'a> {
@@ -105,6 +157,7 @@ pub fn build_reorder_plan<'a>(parent: Node<'a>, content: &'a str) -> ReorderPlan
105157
sub_child: None,
106158
leading_indices: Vec::new(),
107159
trailing_indices: Vec::new(),
160+
has_blank_line_before: false,
108161
classification: child_classification.classification,
109162
name: child_classification.name,
110163
is_private,
@@ -119,6 +172,7 @@ pub fn build_reorder_plan<'a>(parent: Node<'a>, content: &'a str) -> ReorderPlan
119172
sub_child: Some(extends_index),
120173
leading_indices: Vec::new(),
121174
trailing_indices: Vec::new(),
175+
has_blank_line_before: false,
122176
classification: DeclarationKind::Extends,
123177
name: "",
124178
is_private: false,
@@ -168,7 +222,8 @@ pub fn build_reorder_plan<'a>(parent: Node<'a>, content: &'a str) -> ReorderPlan
168222
scan_index += 1;
169223
continue;
170224
};
171-
if !is_comment[scan_index] || !node_text(child, content).trim_start().starts_with("##")
225+
if !is_comment[scan_index]
226+
|| !get_node_text(child, content).trim_start().starts_with("##")
172227
{
173228
break;
174229
}
@@ -205,6 +260,7 @@ pub fn build_reorder_plan<'a>(parent: Node<'a>, content: &'a str) -> ReorderPlan
205260
sub_child: None,
206261
leading_indices: docstring_indices,
207262
trailing_indices: Vec::new(),
263+
has_blank_line_before: false,
208264
classification: DeclarationKind::Docstring,
209265
name: "",
210266
is_private: false,
@@ -240,6 +296,27 @@ pub fn build_reorder_plan<'a>(parent: Node<'a>, content: &'a str) -> ReorderPlan
240296
}
241297
items[declaration_index].leading_indices = leading;
242298

299+
// Determine if there is a blank line before the current declaration
300+
// that we need to preserve after reordering.
301+
if let Some(previous_declaration_child_index) = previous_declaration_end {
302+
let previous_declaration = parent.child(previous_declaration_child_index as u32);
303+
let first_item_child_index = if items[declaration_index].leading_indices.is_empty() {
304+
declaration_child_index
305+
} else {
306+
items[declaration_index].leading_indices[0]
307+
};
308+
let first_item_child = parent.child(first_item_child_index as u32);
309+
if let (Some(previous_declaration), Some(first_item_child)) =
310+
(previous_declaration, first_item_child)
311+
{
312+
items[declaration_index].has_blank_line_before = has_blank_line(
313+
content,
314+
previous_declaration.end_byte(),
315+
first_item_child.start_byte(),
316+
);
317+
}
318+
}
319+
243320
// Trailing: region-end between this decl and the next one, or all remaining comments.
244321
let mut trailing = Vec::new();
245322
let mut scan_index = declaration_child_index + 1;
@@ -297,7 +374,7 @@ fn classify_child<'a>(node: Node<'a>, content: &'a str) -> ChildClassification<'
297374
let kind = GDScriptNodeKind::get_kind_from_ast_node(node);
298375
match kind {
299376
GDScriptNodeKind::Annotation => {
300-
let name = node_text(node, content);
377+
let name = get_node_text(node, content);
301378
ChildClassification::new(DeclarationKind::ClassAnnotation, name)
302379
}
303380
GDScriptNodeKind::ClassName => {
@@ -311,7 +388,7 @@ fn classify_child<'a>(node: Node<'a>, content: &'a str) -> ChildClassification<'
311388
}
312389
}
313390
GDScriptNodeKind::Extends => {
314-
let text = node_text(node, content).trim();
391+
let text = get_node_text(node, content).trim();
315392
ChildClassification::new(DeclarationKind::Extends, text)
316393
}
317394
GDScriptNodeKind::Signal => {
@@ -366,7 +443,7 @@ fn classify_child<'a>(node: Node<'a>, content: &'a str) -> ChildClassification<'
366443
ChildClassification::new(DeclarationKind::InnerClass, name)
367444
}
368445
// This ensures the node is preserved during reorder.
369-
_ => ChildClassification::new(DeclarationKind::Unknown, node_text(node, content)),
446+
_ => ChildClassification::new(DeclarationKind::Unknown, get_node_text(node, content)),
370447
}
371448
}
372449

@@ -391,7 +468,7 @@ fn extract_name<'a>(node: Node<'a>, content: &'a str) -> Option<&'a str> {
391468
if node.field_name_for_child(child_index as u32) == Some("name") {
392469
return node
393470
.child(child_index as u32)
394-
.map(|child_node| node_text(child_node, content));
471+
.map(|child_node| get_node_text(child_node, content));
395472
}
396473
}
397474
None
@@ -467,33 +544,6 @@ fn annotations_contain_name(annotations_node: Node, content: &str, name: &str) -
467544
}
468545
false
469546
}
470-
471-
/// Checks whether a single `annotation` node's `identifier` child has text
472-
/// equal to `expected_name`.
473-
fn annotation_identifier_matches(
474-
annotation_node: Node,
475-
content: &str,
476-
expected_name: &str,
477-
) -> bool {
478-
let count = annotation_node.child_count();
479-
let mut child_index = 0;
480-
while child_index < count {
481-
if let Some(child) = annotation_node.child(child_index as u32) {
482-
if GDScriptNodeKind::get_kind_from_ast_node(child) == GDScriptNodeKind::Identifier {
483-
return node_text(child, content) == expected_name;
484-
}
485-
}
486-
child_index += 1;
487-
}
488-
false
489-
}
490-
491-
/// Slice the source string at a node's byte range.
492-
/// Tree-sitter byte offsets are always on UTF-8 char boundaries.
493-
fn node_text<'a>(node: Node<'a>, content: &'a str) -> &'a str {
494-
&content[node.start_byte()..node.end_byte()]
495-
}
496-
497547
/// Maps built-in virtual method names to Godot lifecycle priority.
498548
fn get_builtin_virtual_priority(method_name: &str) -> u8 {
499549
match method_name {

‎tests/reorder_code/expected/reorder_export_comment_with_annotation.gd‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ extends Node
44
#@export var speed_air_cap: float = 1
55
@export_subgroup("Dodge")
66
@export var speed_dodge: float = 15
7+
78
@export var speed_run: float = 20
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class_name MakeHumanAttachment
2+
extends Resource
3+
4+
@export var geometry: MakeHumanGeometry
5+
@export var material: MakeHumanMaterial
6+
7+
@export var x_scale: MakeHumanScale
8+
@export var y_scale: MakeHumanScale
9+
@export var z_scale: MakeHumanScale
10+
11+
## Display name of the attachment.
12+
@export var name: String
13+
14+
## Name of the attachment's author.
15+
@export var author: String
16+
17+
## License under which the attachment is distributed.
18+
@export var license: String

‎tests/reorder_code/input/reorder_export_comment_with_annotation.gd‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ extends Node
66
@export var speed_dodge: float = 15
77

88
@export var speed_run: float = 20
9-
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class_name MakeHumanAttachment
2+
extends Resource
3+
4+
@export var geometry: MakeHumanGeometry
5+
@export var material: MakeHumanMaterial
6+
7+
@export var x_scale: MakeHumanScale
8+
@export var y_scale: MakeHumanScale
9+
@export var z_scale: MakeHumanScale
10+
11+
## Display name of the attachment.
12+
@export var name: String
13+
14+
## Name of the attachment's author.
15+
@export var author: String
16+
17+
## License under which the attachment is distributed.
18+
@export var license: String

0 commit comments

Comments
 (0)