Skip to content

Commit 31ebae4

Browse files
committed
reorder: fix region end being attached to the wrong function
Addresses this comment in #149: #149 (comment)
1 parent ff0091c commit 31ebae4

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

src/reorder.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,24 @@ fn extract_tokens_to_reorder(
393393
});
394394
}
395395
"region_end" => {
396-
region_end_comment = Some(text.clone());
396+
// Attach #endregion to the most recently seen function so that
397+
// when reordering the end stays with the last function in the
398+
// region. Because functions change order, that can still change
399+
// the region but it prevents issues with the end region jumping
400+
// to a different function
401+
let mut attached = false;
402+
for element in elements.iter_mut().rev() {
403+
if matches!(element.token_kind, GDScriptTokenKind::Method(_, _, _)) {
404+
element.trailing_comments.push(text.clone());
405+
attached = true;
406+
break;
407+
}
408+
}
409+
if !attached {
410+
// We didn't find a function to attach to, so we save this
411+
// to handle down below
412+
region_end_comment = Some(text.clone());
413+
}
397414
}
398415
"annotation" => {
399416
if let Some(element) = reorderable_element {
@@ -505,26 +522,6 @@ fn extract_tokens_to_reorder(
505522
let combined_comments =
506523
merge_pending_texts(&pending_annotations, &pending_comments);
507524

508-
// We store trailing #endregion comments to attach them to
509-
// the most recent function that has a #region comment at
510-
// the top, to move them along with the function when
511-
// reordering
512-
if let Some(region_end) = region_end_comment.take() {
513-
for i in (0..elements.len()).rev() {
514-
if matches!(elements[i].token_kind, GDScriptTokenKind::Method(_, _, _))
515-
{
516-
let has_region = elements[i]
517-
.attached_comments
518-
.iter()
519-
.any(|c| c.trim().starts_with("#region"));
520-
if has_region {
521-
elements[i].trailing_comments.push(region_end.clone());
522-
break;
523-
}
524-
}
525-
}
526-
}
527-
528525
elements.push(GDScriptTokensWithComments {
529526
token_kind: element,
530527
attached_comments: combined_comments,
@@ -562,15 +559,7 @@ fn extract_tokens_to_reorder(
562559
// create a standalone element at the end). This avoids the formatter
563560
// deleting or losing the directive when we reorder code blocks.
564561
if let Some(region_end) = region_end_comment.take() {
565-
if let Some(target_element) = elements.iter_mut().rev().find(|element| {
566-
matches!(element.token_kind, GDScriptTokenKind::Method(_, _, _))
567-
&& element
568-
.attached_comments
569-
.iter()
570-
.any(|c| c.trim().starts_with("#region"))
571-
}) {
572-
target_element.trailing_comments.push(region_end);
573-
} else if let Some(last_element) = elements.last_mut() {
562+
if let Some(last_element) = elements.last_mut() {
574563
last_element.trailing_comments.push(region_end);
575564
} else {
576565
elements.push(GDScriptTokensWithComments {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extends Node
2+
3+
4+
#region Godot Hooks
5+
func _ready():
6+
pass
7+
8+
9+
func _physics_process(delta):
10+
pass
11+
#endregion
12+
13+
14+
func _input():
15+
pass
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extends Node
2+
3+
4+
#region Godot Hooks
5+
func _ready():
6+
pass
7+
8+
9+
func _input():
10+
pass
11+
12+
13+
func _physics_process(delta):
14+
pass
15+
#endregion

0 commit comments

Comments
 (0)