Keep trailing comments at the end of a file

This commit is contained in:
Joshua Warner 2021-12-05 12:18:00 -08:00
parent bd5b9700ca
commit f3dd9e411f
2 changed files with 34 additions and 13 deletions

View File

@ -5,7 +5,6 @@ extern crate roc_fmt;
#[cfg(test)]
mod test_fmt {
use bumpalo::collections::String;
use bumpalo::Bump;
use roc_fmt::annotation::{Formattable, Newlines, Parens};
use roc_fmt::def::fmt_def;
@ -49,7 +48,6 @@ mod test_fmt {
fn module_formats_to(src: &str, expected: &str) {
let arena = Bump::new();
let src = src.trim_end();
let expected = expected.trim_end();
match module::parse_header(&arena, State::new(src.as_bytes())) {
Ok((actual, state)) => {
@ -2571,19 +2569,35 @@ mod test_fmt {
fn single_line_interface() {
module_formats_same(indoc!(
r#"
interface Foo exposes [] imports []
"#
interface Foo exposes [] imports []"#
));
}
#[test]
fn defs_with_trailing_comment() {
// TODO: make the formatter add a space between '42' and # below:
module_formats_to(
indoc!(
r#"
interface Foo exposes [] imports []
a = 42 # Yay greetings"#
),
indoc!(
r#"
interface Foo exposes [] imports []
a = 42# Yay greetings
"#
),
);
}
#[test]
fn multiline_interface() {
module_formats_same(indoc!(
r#"
interface Foo
exposes []
imports []
"#
imports []"#
));
}
@ -2593,8 +2607,7 @@ mod test_fmt {
r#"
interface Foo
exposes [ Bar, Baz, a, b ]
imports []
"#
imports []"#
));
}
@ -2604,8 +2617,7 @@ mod test_fmt {
r#"
interface Foo
exposes [ Bar, Baz, a, b ]
imports [ Blah, Thing.{ foo, bar }, Stuff ]
"#
imports [ Blah, Thing.{ foo, bar }, Stuff ]"#
));
}
@ -2613,8 +2625,7 @@ mod test_fmt {
fn single_line_app() {
module_formats_same(indoc!(
r#"
app "Foo" packages { base: "platform" } imports [] provides [ main ] to base
"#
app "Foo" packages { base: "platform" } imports [] provides [ main ] to base"#
));
}
@ -2638,7 +2649,7 @@ mod test_fmt {
putLine :Str -> Effect {}, \
putInt :I64 -> Effect {}, \
getInt :Effect { value : I64, errorCode : [ A, B ], isError : Bool } \
} ",
}",
);
}

View File

@ -363,6 +363,16 @@ fn eat_line_comment<'a>(
}
}
// We made it to the end of the bytes. This means there's a comment without a trailing newline.
let delta = (col - initial_col) as usize;
let comment = unsafe { std::str::from_utf8_unchecked(&initial[..delta]) };
if is_doc_comment {
comments_and_newlines.push(CommentOrNewline::DocComment(comment));
} else {
comments_and_newlines.push(CommentOrNewline::LineComment(comment));
}
Good {
row,
col,