Skip to content

Commit 6f5c089

Browse files
committed
Reorder code: Fix inline comments wrapping two separate lines.
1 parent 5aecc43 commit 6f5c089

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/reorder.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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(),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extends Node
2+
3+
var test = 1 # Inline comment
4+
5+
6+
func _ready():
7+
pass
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func _ready():
2+
pass
3+
4+
extends Node
5+
6+
var test = 1 # Inline comment
7+

0 commit comments

Comments
 (0)