diff --git a/crates/parol-ls/data/expected/options_default/line_comments_in_scanner_directives.par b/crates/parol-ls/data/expected/options_default/line_comments_in_scanner_directives.par new file mode 100644 index 00000000..d3c77a06 --- /dev/null +++ b/crates/parol-ls/data/expected/options_default/line_comments_in_scanner_directives.par @@ -0,0 +1,20 @@ +%start Scanners // LC1 +%comment "Formatting test" // LC2 +%line_comment "#" // LC3 +%line_comment '//' // LC4 +%block_comment '/*' '*/' // LC5 +%user_type UserType1 = UserDefinedTypeName1 // LC6 +%user_type UserType2 = UserDefinedTypeName2 // LC7 + +%scanner Esc { + %auto_newline_off // LC8 + %auto_ws_off // LC9 + %line_comment '//' // LC10 + %block_comment '/*' '*/' // LC11 +} + +%% + +Scanners + : "xxx" + ; diff --git a/crates/parol-ls/data/expected/options_default/multiple_scanner_states.par b/crates/parol-ls/data/expected/options_default/multiple_scanner_states.par index 19b73fec..0aa84856 100644 --- a/crates/parol-ls/data/expected/options_default/multiple_scanner_states.par +++ b/crates/parol-ls/data/expected/options_default/multiple_scanner_states.par @@ -6,14 +6,12 @@ %user_type UserType1 = UserDefinedTypeName1 %user_type UserType2 = UserDefinedTypeName2 - %scanner Esc { %auto_newline_off %auto_ws_off %line_comment '//' %block_comment '/*' '*/' } - %scanner Val { %auto_newline_off %auto_ws_off diff --git a/crates/parol-ls/data/expected/options_default/single_scanner_state.par b/crates/parol-ls/data/expected/options_default/single_scanner_state.par index 68f8304a..4fe892a1 100644 --- a/crates/parol-ls/data/expected/options_default/single_scanner_state.par +++ b/crates/parol-ls/data/expected/options_default/single_scanner_state.par @@ -6,7 +6,6 @@ %user_type UserType1 = UserDefinedTypeName1 %user_type UserType2 = UserDefinedTypeName2 - %scanner Esc { %auto_newline_off %auto_ws_off diff --git a/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/line_comments_in_scanner_directives.par b/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/line_comments_in_scanner_directives.par new file mode 100644 index 00000000..784258e3 --- /dev/null +++ b/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/line_comments_in_scanner_directives.par @@ -0,0 +1,19 @@ +%start Scanners // LC1 +%comment "Formatting test" // LC2 +%line_comment "#" // LC3 +%line_comment '//' // LC4 +%block_comment '/*' '*/' // LC5 +%user_type UserType1 = UserDefinedTypeName1 // LC6 +%user_type UserType2 = UserDefinedTypeName2 // LC7 + +%scanner Esc { + %auto_newline_off // LC8 + %auto_ws_off // LC9 + %line_comment '//' // LC10 + %block_comment '/*' '*/' // LC11 +} + +%% + +Scanners + : "xxx"; diff --git a/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/multiple_scanner_states.par b/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/multiple_scanner_states.par index 3ee99206..0adf44d8 100644 --- a/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/multiple_scanner_states.par +++ b/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/multiple_scanner_states.par @@ -6,14 +6,12 @@ %user_type UserType1 = UserDefinedTypeName1 %user_type UserType2 = UserDefinedTypeName2 - %scanner Esc { %auto_newline_off %auto_ws_off %line_comment '//' %block_comment '/*' '*/' } - %scanner Val { %auto_newline_off %auto_ws_off diff --git a/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/single_scanner_state.par b/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/single_scanner_state.par index 8c99bd58..67cf96c1 100644 --- a/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/single_scanner_state.par +++ b/crates/parol-ls/data/expected/prod_semicolon_on_nl_false/single_scanner_state.par @@ -6,7 +6,6 @@ %user_type UserType1 = UserDefinedTypeName1 %user_type UserType2 = UserDefinedTypeName2 - %scanner Esc { %auto_newline_off %auto_ws_off diff --git a/crates/parol-ls/data/input/line_comments_in_scanner_directives.par b/crates/parol-ls/data/input/line_comments_in_scanner_directives.par new file mode 100644 index 00000000..127dab8f --- /dev/null +++ b/crates/parol-ls/data/input/line_comments_in_scanner_directives.par @@ -0,0 +1,15 @@ +%start Scanners // LC1 +%comment "Formatting test" // LC2 +%line_comment "#" // LC3 +%line_comment '//' // LC4 +%block_comment '/*' '*/' // LC5 +%user_type UserType1 = UserDefinedTypeName1 // LC6 +%user_type UserType2 = UserDefinedTypeName2 // LC7 +%scanner Esc { + %auto_newline_off // LC8 + %auto_ws_off // LC9 + %line_comment '//' // LC10 + %block_comment '/*' '*/' // LC11 +} +%% +Scanners: "xxx"; \ No newline at end of file diff --git a/crates/parol-ls/src/format.rs b/crates/parol-ls/src/format.rs index 13b14aa8..61d66e19 100644 --- a/crates/parol-ls/src/format.rs +++ b/crates/parol-ls/src/format.rs @@ -333,26 +333,51 @@ impl Fmt for Declaration { .with_padding(Padding::Left) .with_trimming(Trimming::TrimRight); match self { - Declaration::PercentTitleStringComments(title) => format!( - "{} {}{}\n", - title.percent_title, - title.string.txt(options), - handle_comments(&title.comments, &comment_options), - ), - Declaration::PercentCommentStringComments(comment) => format!( - "{} {}{}\n", - comment.percent_comment, - comment.string.txt(options), - handle_comments(&comment.comments, &comment_options), - ), + Declaration::PercentTitleStringComments(title) => { + let end_comment = handle_comments(&title.comments, &comment_options); + let nl = if Line::ends_with_nl(&end_comment) { + "" + } else { + "\n" + }; + format!( + "{} {}{}{}", + title.percent_title, + title.string.txt(options), + end_comment, + nl, + ) + } + Declaration::PercentCommentStringComments(comment) => { + let end_comment = handle_comments(&comment.comments, &comment_options); + let nl = if Line::ends_with_nl(&end_comment) { + "" + } else { + "\n" + }; + format!( + "{} {}{}{}", + comment.percent_comment, + comment.string.txt(options), + end_comment, + nl, + ) + } Declaration::PercentUserUnderscoreTypeIdentifierEquUserTypeNameComments(user_type) => { + let end_comment = handle_comments(&user_type.comments, &comment_options); + let nl = if Line::ends_with_nl(&end_comment) { + "" + } else { + "\n" + }; format!( - "{} {} {} {}{}\n", + "{} {} {} {}{}{}", user_type.percent_user_underscore_type, user_type.identifier.txt(options), user_type.equ, user_type.user_type_name.txt(options), - handle_comments(&user_type.comments, &comment_options), + end_comment, + nl, ) } Declaration::ScannerDirectives(scanner_directives) => { @@ -603,11 +628,17 @@ impl Fmt for ScannerDirectives { } impl Fmt for ScannerState { fn txt(&self, options: &FmtOptions) -> String { + let nl_after_opening_brace = if self.scanner_state_list.is_empty() { + "" + } else { + "\n" + }; format!( - "\n{} {} {}\n{}{}\n", + "{} {} {}{}{}{}\n", self.percent_scanner, self.identifier.txt(options), self.l_brace, + nl_after_opening_brace, self.scanner_state_list .iter() .fold(String::new(), |mut acc, s| { @@ -672,23 +703,30 @@ impl Fmt for SimpleTokenOpt { } impl Fmt for StartDeclaration { fn txt(&self, options: &FmtOptions) -> String { - let comment_options_top = if let Some(last_comment) = self.comments.comments_list.last() { - match &*last_comment.comment { - Comment::BlockComment(_) => { - options.clone().with_line_end(LineEnd::ForceSingleNewline) - } - Comment::LineComment(_) => options.clone(), - } - } else { + let comment_options_top = if self.comments.comments_list.is_empty() { options.clone() + } else { + options.clone().with_line_end(LineEnd::ForceSingleNewline) }; - let comment_options_bottom = options.clone().with_padding(Padding::Left); + let (delim_after_comment_button, comment_options_bottom) = + if self.comments0.comments_list.is_empty() { + ("\n", options.clone()) + } else { + ( + "", + options + .clone() + .with_line_end(LineEnd::ForceSingleNewline) + .with_padding(Padding::Left), + ) + }; format!( - "{}{} {}{}\n", + "{}{} {}{}{}", handle_comments(&self.comments, &comment_options_top), self.percent_start, self.identifier.txt(options), handle_comments(&self.comments0, &comment_options_bottom), + delim_after_comment_button, ) } } @@ -784,33 +822,63 @@ fn handle_scanner_directives( ) -> String { let comment_options = options.clone().with_padding(Padding::Left); match scanner_directives { - ScannerDirectives::PercentLineUnderscoreCommentTokenLiteralComments(l) => format!( - "{} {}{}\n", - l.percent_line_underscore_comment, - l.token_literal.txt(options), - handle_comments(&l.comments, &comment_options), - ), + ScannerDirectives::PercentLineUnderscoreCommentTokenLiteralComments(l) => { + let end_comment = handle_comments(&l.comments, &comment_options); + let nl = if Line::ends_with_nl(&end_comment) { + "" + } else { + "\n" + }; + format!( + "{} {}{}{}", + l.percent_line_underscore_comment, + l.token_literal.txt(options), + end_comment, + nl, + ) + } ScannerDirectives::PercentBlockUnderscoreCommentTokenLiteralTokenLiteralComments(b) => { + let end_comment = handle_comments(&b.comments, &comment_options); + let nl = if Line::ends_with_nl(&end_comment) { + "" + } else { + "\n" + }; format!( - "{} {} {}{}\n", + "{} {} {}{}{}", b.percent_block_underscore_comment, b.token_literal.txt(options), b.token_literal0.txt(options), - handle_comments(&b.comments, &comment_options), + end_comment, + nl, ) } - ScannerDirectives::PercentAutoUnderscoreNewlineUnderscoreOffComments(n) => format!( - "{}{}\n", - n.percent_auto_underscore_newline_underscore_off, - handle_comments(&n.comments, &comment_options), - ), + ScannerDirectives::PercentAutoUnderscoreNewlineUnderscoreOffComments(n) => { + let end_comment = handle_comments(&n.comments, &comment_options); + let nl = if Line::ends_with_nl(&end_comment) { + "" + } else { + "\n" + }; + format!( + "{}{}{}", + n.percent_auto_underscore_newline_underscore_off, end_comment, nl, + ) + } - ScannerDirectives::PercentAutoUnderscoreWsUnderscoreOffComments(w) => format!( - "{}{}\n", - w.percent_auto_underscore_ws_underscore_off, - handle_comments(&w.comments, &comment_options), - ), + ScannerDirectives::PercentAutoUnderscoreWsUnderscoreOffComments(w) => { + let end_comment = handle_comments(&w.comments, &comment_options); + let nl = if Line::ends_with_nl(&end_comment) { + "" + } else { + "\n" + }; + format!( + "{}{}{}", + w.percent_auto_underscore_ws_underscore_off, end_comment, nl, + ) + } } }