fmt: keep empty line between if statement and comment (#8846)
parent
901aa83e65
commit
a86bf3254a
|
@ -15,11 +15,13 @@ enum CommentsLevel {
|
||||||
// - inline: single-line comments will be on the same line as the last statement
|
// - inline: single-line comments will be on the same line as the last statement
|
||||||
// - iembed: a /* ... */ embedded comment; used in expressions; // comments the whole line
|
// - iembed: a /* ... */ embedded comment; used in expressions; // comments the whole line
|
||||||
// - level: either .keep (don't indent), or .indent (increment indentation)
|
// - level: either .keep (don't indent), or .indent (increment indentation)
|
||||||
|
// - prev_line: the line number of the previous token
|
||||||
struct CommentsOptions {
|
struct CommentsOptions {
|
||||||
has_nl bool = true
|
has_nl bool = true
|
||||||
inline bool
|
inline bool
|
||||||
level CommentsLevel
|
level CommentsLevel
|
||||||
iembed bool
|
iembed bool
|
||||||
|
prev_line int = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
|
pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
|
||||||
|
@ -79,14 +81,19 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) {
|
pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) {
|
||||||
|
mut prev_line := options.prev_line
|
||||||
for i, c in comments {
|
for i, c in comments {
|
||||||
if !f.out.last_n(1)[0].is_space() {
|
if !f.out.last_n(1)[0].is_space() {
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
}
|
}
|
||||||
|
if options.prev_line > -1 && c.pos.line_nr > prev_line + 1 {
|
||||||
|
f.writeln('')
|
||||||
|
}
|
||||||
f.comment(c, options)
|
f.comment(c, options)
|
||||||
if !options.iembed && (i < comments.len - 1 || options.has_nl) {
|
if !options.iembed && (i < comments.len - 1 || options.has_nl) {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
|
prev_line = c.pos.last_line
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -102,7 +102,7 @@ pub fn (mut f Fmt) write(s string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) writeln(s string) {
|
pub fn (mut f Fmt) writeln(s string) {
|
||||||
if f.indent > 0 && f.empty_line {
|
if f.indent > 0 && f.empty_line && s.len > 0 {
|
||||||
f.write_indent()
|
f.write_indent()
|
||||||
}
|
}
|
||||||
f.out.writeln(s)
|
f.out.writeln(s)
|
||||||
|
@ -1657,10 +1657,10 @@ pub fn (mut f Fmt) wrap_infix(start_pos int, start_len int, ignore_paren bool) {
|
||||||
|
|
||||||
pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
|
pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
|
||||||
dollar := if node.is_comptime { '$' } else { '' }
|
dollar := if node.is_comptime { '$' } else { '' }
|
||||||
mut single_line := node.branches.len == 2 && node.has_else
|
mut is_ternary := node.branches.len == 2 && node.has_else
|
||||||
&& branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1])
|
&& branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1])
|
||||||
&& (node.is_expr || f.is_assign || f.is_struct_init || f.single_line_fields)
|
&& (node.is_expr || f.is_assign || f.is_struct_init || f.single_line_fields)
|
||||||
f.single_line_if = single_line
|
f.single_line_if = is_ternary
|
||||||
start_pos := f.out.len
|
start_pos := f.out.len
|
||||||
start_len := f.line_len
|
start_len := f.line_len
|
||||||
for {
|
for {
|
||||||
|
@ -1693,20 +1693,20 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.write('{')
|
f.write('{')
|
||||||
if single_line {
|
if is_ternary {
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
} else {
|
} else {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
f.stmts(branch.stmts)
|
f.stmts(branch.stmts)
|
||||||
if single_line {
|
if is_ternary {
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// When a single line if is really long, write it again as multiline,
|
// When a single line if is really long, write it again as multiline,
|
||||||
// except it is part of an InfixExpr.
|
// except it is part of an InfixExpr.
|
||||||
if single_line && f.line_len > fmt.max_len.last() && !f.buffering {
|
if is_ternary && f.line_len > fmt.max_len.last() && !f.buffering {
|
||||||
single_line = false
|
is_ternary = false
|
||||||
f.single_line_if = false
|
f.single_line_if = false
|
||||||
f.out.go_back_to(start_pos)
|
f.out.go_back_to(start_pos)
|
||||||
f.line_len = start_len
|
f.line_len = start_len
|
||||||
|
@ -1719,7 +1719,10 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
|
||||||
f.single_line_if = false
|
f.single_line_if = false
|
||||||
if node.post_comments.len > 0 {
|
if node.post_comments.len > 0 {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
f.comments(node.post_comments, has_nl: false)
|
f.comments(node.post_comments,
|
||||||
|
has_nl: false
|
||||||
|
prev_line: node.branches.last().body_pos.last_line
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -82,3 +82,17 @@ fn linebreaks_in_block_comments() {
|
||||||
eggs
|
eggs
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn between_if_branches() {
|
||||||
|
if spam {
|
||||||
|
}
|
||||||
|
// remove the empty line above
|
||||||
|
else if eggs {
|
||||||
|
}
|
||||||
|
|
||||||
|
if spam2 {
|
||||||
|
}
|
||||||
|
// remove the empty line below
|
||||||
|
else {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -65,3 +65,19 @@ fn linebreaks_in_block_comments() {
|
||||||
spaces make no difference there
|
spaces make no difference there
|
||||||
eggs */
|
eggs */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn between_if_branches() {
|
||||||
|
if spam {
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove the empty line above
|
||||||
|
else if eggs {
|
||||||
|
}
|
||||||
|
|
||||||
|
if spam2 {
|
||||||
|
}
|
||||||
|
// remove the empty line below
|
||||||
|
|
||||||
|
else {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -20,9 +20,6 @@ fn main() {
|
||||||
u := User{
|
u := User{
|
||||||
name: 'Peter'
|
name: 'Peter'
|
||||||
}
|
}
|
||||||
if true {
|
|
||||||
}
|
|
||||||
// some comment after an if without else
|
|
||||||
n := sizeof(User)
|
n := sizeof(User)
|
||||||
// else
|
// else
|
||||||
// else {
|
// else {
|
||||||
|
@ -60,3 +57,15 @@ fn linebreaks_in_block_comments() {
|
||||||
no problem
|
no problem
|
||||||
*****/
|
*****/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn ifs_comments_and_empty_lines() {
|
||||||
|
if true {
|
||||||
|
}
|
||||||
|
// some comment after an if without else
|
||||||
|
if false {
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is parsed as post_comment of the if but does not really belong there
|
||||||
|
// thereore keep the empty line
|
||||||
|
something_else()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue