mirror of
https://github.com/vlang/v.git
synced 2025-09-12 08:57:09 -04:00
ast, parser, fmt: cleanup fmt of struct fields with empty line (#22014)
This commit is contained in:
parent
5baad3a3a9
commit
6e2ae7c2ed
@ -333,6 +333,7 @@ pub:
|
|||||||
comments []Comment
|
comments []Comment
|
||||||
i int
|
i int
|
||||||
has_default_expr bool
|
has_default_expr bool
|
||||||
|
has_prev_newline bool
|
||||||
attrs []Attr
|
attrs []Attr
|
||||||
is_pub bool
|
is_pub bool
|
||||||
default_val string
|
default_val string
|
||||||
|
@ -77,29 +77,9 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
|
|||||||
i == node.module_pos {
|
i == node.module_pos {
|
||||||
f.writeln('module:')
|
f.writeln('module:')
|
||||||
}
|
}
|
||||||
i > 0 {
|
i > 0 && field.has_prev_newline {
|
||||||
// keep one empty line between fields (exclude one after mut:, pub:, ...)
|
|
||||||
last_field := node.fields[i - 1]
|
|
||||||
before_last_line := if last_field.comments.len > 0
|
|
||||||
&& last_field.pos.line_nr < last_field.comments.last().pos.last_line {
|
|
||||||
last_field.comments.last().pos.last_line
|
|
||||||
} else if last_field.has_default_expr {
|
|
||||||
last_field.default_expr.pos().last_line
|
|
||||||
} else {
|
|
||||||
last_field.pos.line_nr
|
|
||||||
}
|
|
||||||
|
|
||||||
next_first_line := if field.comments.len > 0
|
|
||||||
&& field.pos.line_nr > field.comments[0].pos.line_nr {
|
|
||||||
field.comments[0].pos.line_nr
|
|
||||||
} else {
|
|
||||||
field.pos.line_nr
|
|
||||||
}
|
|
||||||
|
|
||||||
if next_first_line - before_last_line > 1 {
|
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
mut pre_cmts, mut end_cmts, mut next_line_cmts := []ast.Comment{}, []ast.Comment{}, []ast.Comment{}
|
mut pre_cmts, mut end_cmts, mut next_line_cmts := []ast.Comment{}, []ast.Comment{}, []ast.Comment{}
|
||||||
|
@ -186,11 +186,13 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
|
|||||||
&& (p.peek_tok.kind != .lsbr || p.peek_token(2).kind != .rsbr))
|
&& (p.peek_tok.kind != .lsbr || p.peek_token(2).kind != .rsbr))
|
||||||
|| p.peek_tok.kind == .dot) && language == .v && p.peek_tok.kind != .key_fn
|
|| p.peek_tok.kind == .dot) && language == .v && p.peek_tok.kind != .key_fn
|
||||||
is_on_top := ast_fields.len == 0 && !(is_field_pub || is_field_mut || is_field_global)
|
is_on_top := ast_fields.len == 0 && !(is_field_pub || is_field_mut || is_field_global)
|
||||||
|
has_prev_newline := p.tok.line_nr - p.prev_tok.line_nr - p.prev_tok.lit.count('\n') > 1
|
||||||
mut field_name := ''
|
mut field_name := ''
|
||||||
mut typ := ast.Type(0)
|
mut typ := ast.Type(0)
|
||||||
mut type_pos := token.Pos{}
|
mut type_pos := token.Pos{}
|
||||||
mut field_pos := token.Pos{}
|
mut field_pos := token.Pos{}
|
||||||
mut option_pos := token.Pos{}
|
mut option_pos := token.Pos{}
|
||||||
|
|
||||||
if is_embed {
|
if is_embed {
|
||||||
// struct embedding
|
// struct embedding
|
||||||
type_pos = p.tok.pos()
|
type_pos = p.tok.pos()
|
||||||
@ -305,6 +307,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
|
|||||||
i: i
|
i: i
|
||||||
default_expr: default_expr
|
default_expr: default_expr
|
||||||
has_default_expr: has_default_expr
|
has_default_expr: has_default_expr
|
||||||
|
has_prev_newline: has_prev_newline
|
||||||
attrs: p.attrs
|
attrs: p.attrs
|
||||||
is_pub: is_embed || is_field_pub
|
is_pub: is_embed || is_field_pub
|
||||||
is_mut: is_embed || is_field_mut
|
is_mut: is_embed || is_field_mut
|
||||||
|
Loading…
x
Reference in New Issue
Block a user