@@ -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.
75127pub 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.
498548fn get_builtin_virtual_priority ( method_name : & str ) -> u8 {
499549 match method_name {
0 commit comments