From f3dd9e411f4485fcdc37bc89c16cd8c2f8817a4c Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Sun, 5 Dec 2021 12:18:00 -0800 Subject: [PATCH] Keep trailing comments at the end of a file --- compiler/fmt/tests/test_fmt.rs | 37 +++++++++++++++++++++----------- compiler/parse/src/blankspace.rs | 10 +++++++++ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/compiler/fmt/tests/test_fmt.rs b/compiler/fmt/tests/test_fmt.rs index 6adbed8ee3..89d864caff 100644 --- a/compiler/fmt/tests/test_fmt.rs +++ b/compiler/fmt/tests/test_fmt.rs @@ -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 } \ - } ", + }", ); } diff --git a/compiler/parse/src/blankspace.rs b/compiler/parse/src/blankspace.rs index ff10152e84..f2b753b914 100644 --- a/compiler/parse/src/blankspace.rs +++ b/compiler/parse/src/blankspace.rs @@ -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,