fmt: fix formating a file in an oscillating manner (fix #22223, fix #22026) (#22232)

This commit is contained in:
yuyi 2024-09-17 14:47:38 +08:00 committed by GitHub
parent c004d0c899
commit acf6b344f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
99 changed files with 681 additions and 655 deletions

View file

@ -157,18 +157,18 @@ jobs:
echo "Build v-analyzer release"
v build.vsh release
- name: Format vlang/v-analyzer
if: ${{ !cancelled() && steps.build.outcome == 'success' }}
run: |
cd /tmp/v-analyzer
set +e
v fmt -c .
exit_code=$?
if [[ $exit_code -ne 0 && $exit_code -ne 5 ]]; then
# Don't fail if there are only internal errors (exit code 5).
v fmt -diff .
exit 1
fi
# - name: Format vlang/v-analyzer
# if: ${{ !cancelled() && steps.build.outcome == 'success' }}
# run: |
# cd /tmp/v-analyzer
# set +e
# v fmt -c .
# exit_code=$?
# if [[ $exit_code -ne 0 && $exit_code -ne 5 ]]; then
# # Don't fail if there are only internal errors (exit code 5).
# v fmt -diff .
# exit 1
# fi
- name: Build vlang/go2v
if: ${{ !cancelled() && steps.build.outcome == 'success' && matrix.os != 'macos-14' }}

View file

@ -31,10 +31,10 @@ const second_hand_color = gx.red
struct App {
minutes_tic []f32 = [f32(center - tw), tp, center + tw, tp, center + tw, tp, center + tw,
tp + 1 * th, center - tw, tp + 1 * th]
hours_tic []f32 = [f32(center - tw), tp, center + tw, tp, center + tw, tp, center + tw, tp + 2 * th,
center - tw, tp + 2 * th]
hours3_tic []f32 = [f32(center - tw), tp, center + tw, tp, center + tw, tp, center + tw, tp + 3 * th,
center - tw, tp + 3 * th]
hours_tic []f32 = [f32(center - tw), tp, center + tw, tp, center + tw, tp, center + tw,
tp + 2 * th, center - tw, tp + 2 * th]
hours3_tic []f32 = [f32(center - tw), tp, center + tw, tp, center + tw, tp, center + tw,
tp + 3 * th, center - tw, tp + 3 * th]
hour_hand []f32 = [f32(329), 161, 350, 140, 371, 161, 371, 413, 329, 413]
minute_hand []f32 = [f32(334.25), 40.25, 350, 24.5, 365.75, 40.25, 365.75, 427, 334.25, 427]

View file

@ -166,9 +166,9 @@ const test_cases = [
chunk_number: 0
chaining_value: [u32(0xbb99f549), 0x3b4b2903, 0x436d199e, 0x6eea5980, 0x82ebb968,
0x33cc3c4a, 0x90f4944b, 0x9480e10a]
block_words: [u32(0x37383920), 0x33343536, 0x20303132, 0x7778797a, 0x73747576, 0x6f707172,
0x6b6c6d6e, 0x6768696a, 0x63646566, 0x5a206162, 0x56575859, 0x52535455, 0x4e4f5051,
0x4a4b4c4d, 0x46474849, 0x00434445]
block_words: [u32(0x37383920), 0x33343536, 0x20303132, 0x7778797a, 0x73747576,
0x6f707172, 0x6b6c6d6e, 0x6768696a, 0x63646566, 0x5a206162, 0x56575859, 0x52535455,
0x4e4f5051, 0x4a4b4c4d, 0x46474849, 0x00434445]
flags: 0x0a
}
words: [u32(0x589a304d), 0x49f8a607, 0x55a03867, 0xe4fec410, 0x1a6bb2f6, 0x11dfecb3,
@ -187,9 +187,9 @@ const test_cases = [
chunk_number: 0
chaining_value: [u32(0xbb99f549), 0x3b4b2903, 0x436d199e, 0x6eea5980, 0x82ebb968,
0x33cc3c4a, 0x90f4944b, 0x9480e10a]
block_words: [u32(0x37383920), 0x33343536, 0x20303132, 0x7778797a, 0x73747576, 0x6f707172,
0x6b6c6d6e, 0x6768696a, 0x63646566, 0x5a206162, 0x56575859, 0x52535455, 0x4e4f5051,
0x4a4b4c4d, 0x46474849, 0x42434445]
block_words: [u32(0x37383920), 0x33343536, 0x20303132, 0x7778797a, 0x73747576,
0x6f707172, 0x6b6c6d6e, 0x6768696a, 0x63646566, 0x5a206162, 0x56575859, 0x52535455,
0x4e4f5051, 0x4a4b4c4d, 0x46474849, 0x42434445]
flags: 0x0a
}
words: [u32(0xd0d12158), 0x8802f9a4, 0x5bd125fb, 0xf2751b9d, 0x8fb2a4d2, 0x27744bfa,
@ -227,9 +227,9 @@ const test_cases = [
chunk_number: 0
chaining_value: [u32(0x78fd494b), 0xcd7eeddd, 0x0cb98e9b, 0x7a6a754e, 0x38ff2d32,
0x88c4ca4c, 0xbc7baf18, 0xf7684da9]
block_words: [u32(0x207a7978), 0x33323130, 0x37363534, 0x39203938, 0x35363738, 0x31323334,
0x797a2030, 0x75767778, 0x71727374, 0x6d6e6f70, 0x696a6b6c, 0x65666768, 0x61626364,
0x58595a20, 0x54555657, 0x00515253]
block_words: [u32(0x207a7978), 0x33323130, 0x37363534, 0x39203938, 0x35363738,
0x31323334, 0x797a2030, 0x75767778, 0x71727374, 0x6d6e6f70, 0x696a6b6c, 0x65666768,
0x61626364, 0x58595a20, 0x54555657, 0x00515253]
flags: 0x0a
}
words: [u32(0x96bcc611), 0x8ccfc351, 0x89ec78f7, 0x2f748832, 0xf75ee10a, 0xc739f876,
@ -248,9 +248,9 @@ const test_cases = [
chunk_number: 0
chaining_value: [u32(0x78fd494b), 0xcd7eeddd, 0x0cb98e9b, 0x7a6a754e, 0x38ff2d32,
0x88c4ca4c, 0xbc7baf18, 0xf7684da9]
block_words: [u32(0x207a7978), 0x33323130, 0x37363534, 0x39203938, 0x35363738, 0x31323334,
0x797a2030, 0x75767778, 0x71727374, 0x6d6e6f70, 0x696a6b6c, 0x65666768, 0x61626364,
0x58595a20, 0x54555657, 0x50515253]
block_words: [u32(0x207a7978), 0x33323130, 0x37363534, 0x39203938, 0x35363738,
0x31323334, 0x797a2030, 0x75767778, 0x71727374, 0x6d6e6f70, 0x696a6b6c, 0x65666768,
0x61626364, 0x58595a20, 0x54555657, 0x50515253]
flags: 0x0a
}
words: [u32(0x50dbfcc6), 0x7dd05a7f, 0xa641cc37, 0x11721a4e, 0x6f33eea2, 0x834877a1,

View file

@ -1775,8 +1775,8 @@ const db = {
}
'application/octet-stream': MimeType{
source: 'iana'
extensions: ['bin', 'dms', 'lrf', 'mar', 'so', 'dist', 'distz', 'pkg', 'bpk', 'dump', 'elc',
'deploy', 'exe', 'dll', 'deb', 'dmg', 'iso', 'img', 'msi', 'msp', 'msm', 'buffer']
extensions: ['bin', 'dms', 'lrf', 'mar', 'so', 'dist', 'distz', 'pkg', 'bpk', 'dump',
'elc', 'deploy', 'exe', 'dll', 'deb', 'dmg', 'iso', 'img', 'msi', 'msp', 'msm', 'buffer']
compressible: false
charset: ''
}

View file

@ -338,6 +338,7 @@ pub:
i int
has_default_expr bool
has_prev_newline bool
has_break_line bool
attrs []Attr
is_pub bool
default_val string
@ -468,6 +469,7 @@ pub:
end_comments []Comment
next_comments []Comment
has_prev_newline bool
has_break_line bool
pub mut:
expr Expr // `val1`
name string // 'field1'
@ -573,6 +575,7 @@ pub:
method_idx int
rec_mut bool // is receiver mutable
has_prev_newline bool
has_break_line bool
rec_share ShareType
language Language // V, C, JS
file_mode Language // whether *the file*, where a function was a '.c.v', '.js.v' etc.
@ -1381,6 +1384,7 @@ pub:
next_comments []Comment // comments between current EnumField and next EnumField
has_expr bool // true, when .expr has a value
has_prev_newline bool // empty newline before Enumfield
has_break_line bool
attrs []Attr
pub mut:
expr Expr // the value of current EnumField; 123 in `ename = 123`

View file

@ -13,6 +13,7 @@ mut:
struct AlignConfig {
pub:
ignore_newline bool // ignore newline or comment
use_break_line bool
use_threshold bool
threshold int = 25
}
@ -38,13 +39,14 @@ fn (mut fa FieldAlign) add_new_info(len int, line int) {
}
@[direct_array_access]
fn (mut fa FieldAlign) add_info(len int, line int) {
fn (mut fa FieldAlign) add_info(len int, line int, has_break_line bool) {
if fa.infos.len == 0 {
fa.add_new_info(len, line)
return
}
i := fa.infos.len - 1
if !fa.cfg.ignore_newline && line - fa.infos[i].line_nr > 1 {
if !fa.cfg.ignore_newline && ((fa.cfg.use_break_line && has_break_line)
|| (!fa.cfg.use_break_line && line - fa.infos[i].line_nr > 1)) {
fa.add_new_info(len, line)
return
}

View file

@ -1039,28 +1039,28 @@ pub fn (mut f Fmt) enum_decl(node ast.EnumDecl) {
f.writeln('enum ${name} {')
f.comments(node.comments, same_line: true, level: .indent)
mut value_align := new_field_align()
mut attr_align := new_field_align()
mut comment_align := new_field_align()
mut value_align := new_field_align(use_break_line: true)
mut attr_align := new_field_align(use_threshold: true)
mut comment_align := new_field_align(use_threshold: true)
for field in node.fields {
if field.has_expr {
value_align.add_info(field.name.len, field.pos.line_nr)
value_align.add_info(field.name.len, field.pos.line_nr, field.has_break_line)
}
attrs_len := inline_attrs_len(field.attrs)
if field.attrs.len > 0 {
if field.has_expr {
attr_align.add_info(field.expr.str().len + 2, field.pos.line_nr)
attr_align.add_info(field.expr.str().len + 2, field.pos.line_nr, field.has_break_line)
} else {
attr_align.add_info(field.name.len, field.pos.line_nr)
attr_align.add_info(field.name.len, field.pos.line_nr, field.has_break_line)
}
}
if field.comments.len > 0 {
if field.attrs.len > 0 {
comment_align.add_info(attrs_len, field.pos.line_nr)
comment_align.add_info(attrs_len, field.pos.line_nr, field.has_break_line)
} else if field.has_expr {
comment_align.add_info(field.expr.str().len + 2, field.pos.line_nr)
comment_align.add_info(field.expr.str().len + 2, field.pos.line_nr, field.has_break_line)
} else {
comment_align.add_info(field.name.len, field.pos.line_nr)
comment_align.add_info(field.name.len, field.pos.line_nr, field.has_break_line)
}
}
}
@ -1397,7 +1397,7 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
}
}
mut type_align := new_field_align()
mut type_align := new_field_align(use_break_line: true)
mut comment_align := new_field_align(use_threshold: true)
mut default_expr_align := new_field_align(use_threshold: true)
mut attr_align := new_field_align(use_threshold: true)
@ -1412,7 +1412,7 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
end_comments := method.comments.filter(it.pos.pos > method.pos.pos)
if end_comments.len > 0 {
method_str := f.table.stringify_fn_decl(&method, f.cur_mod, f.mod2alias, false).all_after_first('fn ')
method_comment_align.add_info(method_str.len, method.pos.line_nr)
method_comment_align.add_info(method_str.len, method.pos.line_nr, method.has_break_line)
}
}
@ -1464,12 +1464,12 @@ pub fn (mut f Fmt) calculate_alignment(fields []ast.StructField, mut type_align
field_types << ft
attrs_len := inline_attrs_len(field.attrs)
end_pos := field.pos.pos + field.pos.len
type_align.add_info(field.name.len, field.pos.line_nr)
type_align.add_info(field.name.len, field.pos.line_nr, field.has_break_line)
if field.has_default_expr {
default_expr_align.add_info(ft.len, field.pos.line_nr)
default_expr_align.add_info(ft.len, field.pos.line_nr, field.has_break_line)
}
if field.attrs.len > 0 {
attr_align.add_info(ft.len, field.pos.line_nr)
attr_align.add_info(ft.len, field.pos.line_nr, field.has_break_line)
}
for comment in field.comments {
if comment.pos.pos >= end_pos {
@ -1478,7 +1478,7 @@ pub fn (mut f Fmt) calculate_alignment(fields []ast.StructField, mut type_align
if prev_state != AlignState.has_attributes {
comment_align.add_new_info(attrs_len, comment.pos.line_nr)
} else {
comment_align.add_info(attrs_len, comment.pos.line_nr)
comment_align.add_info(attrs_len, comment.pos.line_nr, field.has_break_line)
}
prev_state = AlignState.has_attributes
} else if field.has_default_expr {
@ -1486,14 +1486,15 @@ pub fn (mut f Fmt) calculate_alignment(fields []ast.StructField, mut type_align
comment_align.add_new_info(field.default_expr.str().len + 2,
comment.pos.line_nr)
} else {
comment_align.add_info(field.default_expr.str().len + 2, comment.pos.line_nr)
comment_align.add_info(field.default_expr.str().len + 2, comment.pos.line_nr,
field.has_break_line)
}
prev_state = AlignState.has_default_expression
} else {
if prev_state != AlignState.has_everything {
comment_align.add_new_info(ft.len, comment.pos.line_nr)
} else {
comment_align.add_info(ft.len, comment.pos.line_nr)
comment_align.add_info(ft.len, comment.pos.line_nr, field.has_break_line)
}
prev_state = AlignState.has_everything
}

View file

@ -26,7 +26,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
f.writeln(' {}')
return
}
mut type_align := new_field_align()
mut type_align := new_field_align(use_break_line: true)
mut default_expr_align := new_field_align(use_threshold: true)
mut attr_align := new_field_align(use_threshold: true)
mut comment_align := new_field_align(use_threshold: true)
@ -278,12 +278,13 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
}
f.comments(node.update_expr_comments, same_line: true, has_nl: true, level: .keep)
}
mut value_align := new_field_align()
mut value_align := new_field_align(use_break_line: true)
mut comment_align := new_field_align(use_threshold: true)
for init_field in node.init_fields {
value_align.add_info(init_field.name.len, init_field.pos.line_nr)
value_align.add_info(init_field.name.len, init_field.pos.line_nr, init_field.has_break_line)
if init_field.end_comments.len > 0 {
comment_align.add_info(init_field.expr.str().len, init_field.pos.line_nr)
comment_align.add_info(init_field.expr.str().len, init_field.pos.line_nr,
init_field.has_break_line)
}
}
for i, init_field in node.init_fields {

View file

@ -523,6 +523,12 @@ fn (p &Parser) has_prev_newline() bool {
return false
}
fn (p &Parser) has_prev_line_comment_or_label() bool {
return p.prev_tok.kind == .colon || (p.prev_tok.kind == .comment
&& p.tok.line_nr - p.prev_tok.line_nr == 1
&& p.prev_tok.line_nr - p.peek_token(-2).line_nr > 0)
}
fn (p &Parser) is_array_type() bool {
mut i := 1
mut tok := p.tok
@ -4171,6 +4177,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
pre_comments := p.eat_comments()
pos := p.tok.pos()
has_prev_newline := p.has_prev_newline()
has_break_line := has_prev_newline || p.has_prev_line_comment_or_label()
val := p.check_name()
vals << val
mut expr := ast.empty_expr
@ -4198,6 +4205,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
expr: expr
has_expr: has_expr
has_prev_newline: has_prev_newline
has_break_line: has_break_line
pre_comments: pre_comments
comments: comments
next_comments: next_comments

View file

@ -193,6 +193,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
|| 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)
has_prev_newline := p.has_prev_newline()
has_break_line := has_prev_newline || p.has_prev_line_comment_or_label()
mut field_name := ''
mut typ := ast.no_type
mut type_pos := token.Pos{}
@ -311,6 +312,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
default_expr: default_expr
has_default_expr: has_default_expr
has_prev_newline: has_prev_newline
has_break_line: has_break_line
attrs: p.attrs
is_pub: is_embed || is_field_pub
is_mut: is_embed || is_field_mut
@ -431,6 +433,7 @@ fn (mut p Parser) struct_init(typ_str string, kind ast.StructInitKind, is_option
mut has_update_expr := false
mut update_expr_pos := token.Pos{}
mut has_prev_newline := false
mut has_break_line := false
for p.tok.kind !in [.rcbr, .rpar, .eof] {
mut field_name := ''
mut expr := ast.empty_expr
@ -457,6 +460,7 @@ fn (mut p Parser) struct_init(typ_str string, kind ast.StructInitKind, is_option
prev_comments = p.eat_comments()
first_field_pos = p.tok.pos()
has_prev_newline = p.has_prev_newline()
has_break_line = has_prev_newline || p.has_prev_line_comment_or_label()
field_name = p.check_name()
p.check(.colon)
expr = p.expr(0)
@ -491,6 +495,7 @@ fn (mut p Parser) struct_init(typ_str string, kind ast.StructInitKind, is_option
next_comments: nline_comments
parent_type: typ
has_prev_newline: has_prev_newline
has_break_line: has_break_line
}
}
}
@ -665,6 +670,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
if p.peek_tok.kind == .lpar {
method_start_pos := p.tok.pos()
has_prev_newline := p.has_prev_newline()
has_break_line := has_prev_newline || p.has_prev_line_comment_or_label()
line_nr := p.tok.line_nr
name := p.check_name()
@ -698,6 +704,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
pos: method_start_pos.extend(p.prev_tok.pos())
scope: p.scope
has_prev_newline: has_prev_newline
has_break_line: has_break_line
}
if p.tok.kind.is_start_of_type() && p.tok.line_nr == line_nr {
method.return_type_pos = p.tok.pos()
@ -726,6 +733,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
// interface fields
field_pos := p.tok.pos()
has_prev_newline := p.has_prev_newline()
has_break_line := has_prev_newline || p.has_prev_line_comment_or_label()
field_name := p.check_name()
mut type_pos := p.tok.pos()
field_typ := p.parse_type()
@ -739,6 +747,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
comments: comments
is_pub: true
has_prev_newline: has_prev_newline
has_break_line: has_break_line
}
info.fields << ast.StructField{
name: field_name
@ -746,6 +755,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
is_pub: true
is_mut: is_mut
has_prev_newline: has_prev_newline
has_break_line: has_break_line
}
}
}