diff --git a/src/generation/generate.rs b/src/generation/generate.rs index b3d2997..31f0a61 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -403,6 +403,15 @@ fn gen_surrounded_by_tokens<'a, 'b>( context, ))); if let Some(leading_comments) = context.comments.get(&close_token_start) { + // the separating newline is emitted elsewhere, so only add a newline for a blank line in the source + if let Some(start) = leading_comments.first().map(|c| c.start()) { + let comment_start_line = context.text_info.line_index(start); + if let Some(prev_token) = context.token_finder.get_previous_token(&Range::from_byte_index(start)) + && comment_start_line > context.text_info.line_index(prev_token.end()) + 1 + { + items.push_signal(Signal::NewLine); + } + } items.extend(ir_helpers::with_indent(gen_comments_as_statements( leading_comments.iter(), None, diff --git a/tests/specs/comments/Comments_BlankLineBeforeTrailing.txt b/tests/specs/comments/Comments_BlankLineBeforeTrailing.txt new file mode 100644 index 0000000..6e13b53 --- /dev/null +++ b/tests/specs/comments/Comments_BlankLineBeforeTrailing.txt @@ -0,0 +1,64 @@ +-- /file.jsonc -- +== should preserve a blank line before trailing comments at the end of an object (#34) == +{ + // This is a setting. + "foo": "", + + // These settings are temporarily commented out. + // "baz": "", + // "qux": "" +} + +[expect] +{ + // This is a setting. + "foo": "", + + // These settings are temporarily commented out. + // "baz": "", + // "qux": "" +} + +== should preserve a blank line before trailing comments at the end of an array == +[ + "foo", + + // trailing comment + // another +] + +[expect] +[ + "foo", + + // trailing comment + // another +] + +== should not add a blank line before trailing comments when the source had none == +{ + "foo": "" + // trailing comment +} + +[expect] +{ + "foo": "", + // trailing comment +} + +== should collapse multiple blank lines before trailing comments to one == +{ + "foo": "" + + + + // trailing comment +} + +[expect] +{ + "foo": "", + + // trailing comment +}