Skip to content

Commit d196d50

Browse files
committed
Make reorder code preserve code regions + refactor reorder module to
shave off some code Close #149
1 parent 19a9239 commit d196d50

10 files changed

Lines changed: 385 additions & 410 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This file documents the changes made to the formatter with each release.
77
### Added
88

99
- Linter: add support for stdin (#327)
10+
- The reorder code option now preserves code regions and reorders within each region separately. This gives you more control over your code structure if you want different things grouped together. If you don't use code regions, the option works like before (#149)
1011

1112
### Fixed
1213

‎src/formatter.rs‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3431,25 +3431,49 @@ fn process_source_reorder(context: &mut FormatterContext, node: tree_sitter::Nod
34313431
process_children_with_spacing(context, node);
34323432
return;
34333433
}
3434-
let mut previous_classification: Option<DeclarationKind> = None;
3434+
let mut previous_declaration_kind: Option<DeclarationKind> = None;
34353435
let mut previous_is_double_spaced = false;
34363436
let mut previous_child_index: Option<u32> = None;
3437+
let mut previous_was_region_start = false;
34373438
let mut is_first = true;
34383439

34393440
let mut item_index = 0;
34403441
while item_index < plan.items.len() {
34413442
let item = &plan.items[item_index];
3443+
let child_kind = node
3444+
.child(item.child_index)
3445+
.map(GDScriptNodeKind::get_kind_from_ast_node);
3446+
let is_region_start = child_kind == Some(GDScriptNodeKind::RegionStart);
3447+
let is_region_end = child_kind == Some(GDScriptNodeKind::RegionEnd);
3448+
3449+
// If we are at the beginning of a region, we need to look at the next
3450+
// declaration to know how much spacing to use. E.g. if a region
3451+
// starts with a function, we need to add two lines
3452+
let mut declaration_kind_used_for_spacing = item.declaration_kind;
3453+
if is_region_start {
3454+
let mut next_item_index = item_index + 1;
3455+
while next_item_index < plan.items.len() {
3456+
let next_item = &plan.items[next_item_index];
3457+
if next_item.declaration_kind != DeclarationKind::RegionStartOrEnd {
3458+
declaration_kind_used_for_spacing = next_item.declaration_kind;
3459+
break;
3460+
}
3461+
next_item_index += 1;
3462+
}
3463+
}
34423464
let current_needs_two_blank = matches!(
3443-
item.classification,
3465+
declaration_kind_used_for_spacing,
34443466
DeclarationKind::Method | DeclarationKind::InnerClass
34453467
);
34463468
let is_in_source_order = match previous_child_index {
34473469
Some(index) => index < item.child_index,
34483470
None => false,
34493471
};
3450-
if !is_first {
3451-
if let Some(ref previous_child) = previous_classification {
3452-
if previous_child == &item.classification
3472+
if !is_first && (is_region_end || previous_was_region_start) {
3473+
context.render_elements.push(RenderElement::HardLine);
3474+
} else if !is_first {
3475+
if let Some(ref previous_child) = previous_declaration_kind {
3476+
if previous_child == &declaration_kind_used_for_spacing
34533477
&& !previous_is_double_spaced
34543478
&& !current_needs_two_blank
34553479
{
@@ -3469,13 +3493,13 @@ fn process_source_reorder(context: &mut FormatterContext, node: tree_sitter::Nod
34693493
DeclarationKind::ClassAnnotation
34703494
| DeclarationKind::ClassName
34713495
| DeclarationKind::Extends
3472-
| DeclarationKind::Docstring
3496+
| DeclarationKind::ClassDocstring
34733497
) && matches!(
3474-
item.classification,
3498+
declaration_kind_used_for_spacing,
34753499
DeclarationKind::ClassAnnotation
34763500
| DeclarationKind::ClassName
34773501
| DeclarationKind::Extends
3478-
| DeclarationKind::Docstring
3502+
| DeclarationKind::ClassDocstring
34793503
) {
34803504
context.render_elements.push(RenderElement::HardLine);
34813505
} else {
@@ -3485,12 +3509,17 @@ fn process_source_reorder(context: &mut FormatterContext, node: tree_sitter::Nod
34853509
}
34863510
}
34873511
is_first = false;
3488-
previous_classification = Some(item.classification);
3489-
previous_is_double_spaced = current_needs_two_blank;
3512+
if item.declaration_kind != DeclarationKind::RegionStartOrEnd
3513+
|| previous_declaration_kind.is_none()
3514+
{
3515+
previous_declaration_kind = Some(item.declaration_kind);
3516+
previous_is_double_spaced = current_needs_two_blank;
3517+
}
3518+
previous_was_region_start = is_region_start;
34903519
previous_child_index = Some(item.child_index);
34913520

34923521
// Output source children attached before the declaration.
3493-
if item.classification != DeclarationKind::Docstring {
3522+
if item.declaration_kind != DeclarationKind::ClassDocstring {
34943523
let declaration_start_byte = match node.child(item.child_index) {
34953524
Some(child) => child.start_byte(),
34963525
None => 0,
@@ -3526,7 +3555,7 @@ fn process_source_reorder(context: &mut FormatterContext, node: tree_sitter::Nod
35263555
}
35273556
}
35283557

3529-
if item.classification == DeclarationKind::Docstring {
3558+
if item.declaration_kind == DeclarationKind::ClassDocstring {
35303559
let mut docstring_index = 0;
35313560
while docstring_index < item.child_indices_attached_before_declaration.len() {
35323561
let docstring_child_index =

0 commit comments

Comments
 (0)