@@ -234,7 +234,7 @@ fn extract_tokens_to_reorder(
234234 content : & str ,
235235) -> Result < Vec < GDScriptTokensWithComments > , Box < dyn std:: error:: Error > > {
236236 let root = tree. root_node ( ) ;
237- let mut elements = Vec :: new ( ) ;
237+ let mut elements: Vec < GDScriptTokensWithComments > = Vec :: new ( ) ;
238238
239239 // This query covers all top-level elements (direct children of source)
240240 // We need to capture everything so nothing gets lost
@@ -353,7 +353,33 @@ fn extract_tokens_to_reorder(
353353 // This may look inefficient but in practice it should not have much impact
354354 if class_docstring_comments_rows. contains ( & node. start_position ( ) . row ) {
355355 continue ;
356- } else {
356+ }
357+
358+ // Here we look for inline comments after declarations, and if
359+ // so, we attach them as inline to the declaration. For
360+ // example:
361+ //
362+ // var test = 1 # inline comment
363+ //
364+ // Without this code, the comment would wrap to the next line.
365+ let mut handled_inline = false ;
366+ if let Some ( last_element) = elements. last_mut ( ) {
367+ let last_end = last_element. end_byte ;
368+ let comment_start = node. start_byte ( ) ;
369+ if last_end <= comment_start && comment_start <= content. len ( ) {
370+ if let Some ( spacing) = content. get ( last_end..comment_start) {
371+ let has_newline = spacing. contains ( '\n' ) || spacing. contains ( '\r' ) ;
372+ if !has_newline {
373+ last_element. original_text . push_str ( spacing) ;
374+ last_element. original_text . push_str ( & text) ;
375+ last_element. end_byte = node. end_byte ( ) ;
376+ handled_inline = true ;
377+ }
378+ }
379+ }
380+ }
381+
382+ if !handled_inline {
357383 pending_comments. push ( PendingAttachment {
358384 start_byte : node. start_byte ( ) ,
359385 text : text. clone ( ) ,
0 commit comments