1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: fix unreasonable wrap after if in if very_long && condition { (fix #15635) (#15995)

This commit is contained in:
shove 2022-10-09 00:31:00 +08:00 committed by GitHub
parent ce1ba2ad02
commit cd96a43030
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 6 deletions

View File

@ -94,9 +94,7 @@ pub fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Typ
c.error('illegal format specifier `${fmt:c}` for type `${c.table.get_type_name(ftyp)}`',
node.fmt_poss[i])
}
// v fmt doesn't format this correctly
if
c.table.final_sym(typ).kind in [.array, .array_fixed, .struct_, .interface_, .none_, .map, .sum_type]
if c.table.final_sym(typ).kind in [.array, .array_fixed, .struct_, .interface_, .none_, .map, .sum_type]
&& fmt in [`E`, `F`, `G`, `e`, `f`, `g`, `d`, `u`, `x`, `X`, `o`, `c`, `p`, `b`] {
c.error('illegal format specifier `${fmt:c}` for type `${c.table.get_type_name(ftyp)}`',
node.fmt_poss[i])

View File

@ -152,7 +152,8 @@ pub struct RemoveNewLineConfig {
imports_buffer bool // Work on f.out_imports instead of f.out
}
pub fn (mut f Fmt) remove_new_line(cfg RemoveNewLineConfig) {
// When the removal action actually occurs, the string of the last line after the removal is returned
pub fn (mut f Fmt) remove_new_line(cfg RemoveNewLineConfig) string {
mut buffer := if cfg.imports_buffer { unsafe { &f.out_imports } } else { unsafe { &f.out } }
mut i := 0
for i = buffer.len - 1; i >= 0; i-- {
@ -160,8 +161,23 @@ pub fn (mut f Fmt) remove_new_line(cfg RemoveNewLineConfig) {
break
}
}
if i == buffer.len - 1 {
return ''
}
buffer.go_back(buffer.len - i - 1)
f.empty_line = false
mut line_len := 0
mut last_line_str := []u8{}
for i = buffer.len - 1; i >= 0; i-- {
ch := buffer.byte_at(i)
if ch == `\n` {
break
}
line_len += if ch == `\t` { 4 } else { 1 }
last_line_str << ch
}
f.line_len = line_len
return last_line_str.reverse().bytestr()
}
//=== Specialized write methods ===//
@ -2181,10 +2197,17 @@ fn (mut f Fmt) write_splitted_infix(conditions []string, penalties []int, ignore
f.write_splitted_infix(conds, pens, true, is_cond)
continue
}
mut is_wrap_needed := true
if i == 0 {
f.remove_new_line()
last_line_str := f.remove_new_line().trim_space()
if last_line_str in ['if', 'for'] {
f.write(' ')
is_wrap_needed = false
}
}
if is_wrap_needed {
f.writeln('')
}
f.writeln('')
f.indent++
f.write(c)
f.indent--

View File

@ -31,3 +31,9 @@ fn main() {
}
}
}
if c.table.final_sym(typ).kind in [.array, .array_fixed, .struct_, .interface_, .none_, .map, .sum_type]
&& fmt in [`E`, `F`, `G`, `e`, `f`, `g`, `d`, `u`, `x`, `X`, `o`, `c`, `p`, `b`] {
c.error('illegal format specifier `${fmt:c}` for type `${c.table.get_type_name(ftyp)}`',
node.fmt_poss[i])
}

View File

@ -22,3 +22,9 @@ fn main() {
}
}
}
if c.table.final_sym(typ).kind in [.array, .array_fixed, .struct_, .interface_, .none_, .map, .sum_type]
&& fmt in [`E`, `F`, `G`, `e`, `f`, `g`, `d`, `u`, `x`, `X`, `o`, `c`, `p`, `b`] {
c.error('illegal format specifier `${fmt:c}` for type `${c.table.get_type_name(ftyp)}`',
node.fmt_poss[i])
}