fmt: keep some line comments in multi line arrays (#8969)

pull/8989/head
Lukas Neubert 2021-02-26 08:01:03 +01:00 committed by GitHub
parent 2c609411dd
commit 5aebd646bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 95 additions and 58 deletions

View File

@ -1970,20 +1970,27 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
} }
} }
for i, c in node.pre_cmnts { for i, c in node.pre_cmnts {
f.comment(c, level: .indent, iembed: true)
if i < node.pre_cmnts.len - 1 { if i < node.pre_cmnts.len - 1 {
if c.pos.last_line < node.pre_cmnts[i + 1].pos.line_nr { if c.pos.last_line < node.pre_cmnts[i + 1].pos.line_nr {
f.comment(c, level: .indent)
f.writeln('') f.writeln('')
} else { } else {
f.comment(c, level: .indent, iembed: true)
f.write(' ') f.write(' ')
} }
} else { } else {
if c.pos.last_line < node.pos.last_line && node.exprs.len == 0 { if c.pos.last_line < node.pos.last_line {
f.comment(c, level: .indent)
if node.exprs.len == 0 {
f.writeln('') f.writeln('')
} else if node.exprs.len > 0 { }
} else {
f.comment(c, level: .indent, iembed: true)
if node.exprs.len > 0 {
f.write(' ') f.write(' ')
} }
} }
}
last_line_nr = c.pos.last_line last_line_nr = c.pos.last_line
} }
mut set_comma := false mut set_comma := false
@ -2022,12 +2029,13 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
} }
if cmt.pos.line_nr > expr_pos.last_line { if cmt.pos.line_nr > expr_pos.last_line {
f.writeln('') f.writeln('')
f.comment(cmt, {})
} else { } else {
f.write(' ') f.write(' ')
}
f.comment(cmt, iembed: true) f.comment(cmt, iembed: true)
} }
} }
}
if i == node.exprs.len - 1 { if i == node.exprs.len - 1 {
if is_new_line { if is_new_line {
if !set_comma { if !set_comma {

View File

@ -1,22 +1,4 @@
fn main() { fn main() {
arr := [
/* test 0 */
1,
/* test 1 */
2,
/* test 2 */
3, /* 3 */
4, /* 4-1 */ /* 4-2 */
]
arr2 := [
'foo',
/*
'bar',
'baz',
'spam',
*/
'eggs',
]
_ := []int{len: 10, cap: 10, init: 7} _ := []int{len: 10, cap: 10, init: 7}
_ := []map[string]string{len: 5, cap: 50, init: map{ _ := []map[string]string{len: 5, cap: 50, init: map{
'a': 'a' 'a': 'a'

View File

@ -0,0 +1,70 @@
fn main() {
_ := [
// pre comment
6,
// and after
]
_ := [
7,
// below expr
]
_ := [
8, /* I don't know why this still is a block comment */
9,
]
arr := [
// test 0
1,
// test 1
2,
// test 2
3, /* 3 */
4, /* 4-1 */ /* 4-2 */
]
}
fn only_comments_array() {
_ := [/* on a single line */ /* too */]
_ := [
// 1,
// 2,
// 3,
]
_ := [
/* whatever */ /* this is */ // 3,
// 4,
]
}
fn single_line_array_pre_comments() {
_ := [/* 2, */ 3]
_ := [/* 4, */ /* 5, */ 6]
_ := [/* cmt */ -4]
}
fn single_line_array_iembed_comments() {
_ := [1, /* betw single line */ 2]
// This caused a bug where the ´-´ was parsed as InfixExpr and not as part of an IntegerLiteral
_ := [1, /* cmt */ -4]
}
fn mixed_comments() {
_ := [
3 /* iembed */,
// keep line comment here
// and here
5,
]
}
fn keep_real_block_comment() {
_ := [
'foo',
/*
'bar',
'baz',
'spam',
*/
'eggs',
]
}

View File

@ -1,23 +0,0 @@
fn only_comments_array() {
arr := [
/* 1, */
/* 2, */
/* 3, */
/* 4, */
]
arr2 := [
/* 1, */ /* 2, */ /* 3, */
/* 4, */
]
}
fn array_pre_comments() {
_ := [/* 2, */ 3]
_ := [/* 4, */ /* 5, */ 6]
_ := [/* cmt */ -4]
}
fn negative_num_after_comment_expr() {
// This caused a bug where the ´-´ was parsed as InfixExpr and not as part of an IntegerLiteral
_ := [1, /* cmt */ -4]
}

View File

@ -40,8 +40,8 @@ fn main() {
e := c e := c
// more comments = more good // more comments = more good
arr := [ arr := [
/* block foo bar */ // block foo bar
/* inline foo bar */ // inline foo bar
0, 0,
] ]
// before arg comment // before arg comment

View File

@ -31,7 +31,7 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
'tos3', 'tos3',
'isnil', 'isnil',
'opt_ok2', 'opt_ok2',
/* utf8_str_visible_length is used by c/str.v */ // utf8_str_visible_length is used by c/str.v
'utf8_str_visible_length', 'utf8_str_visible_length',
'compare_ints', 'compare_ints',
'compare_u64s', 'compare_u64s',
@ -40,14 +40,14 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
'compare_u64s_reverse', 'compare_u64s_reverse',
'compare_strings_reverse', 'compare_strings_reverse',
'builtin_init', 'builtin_init',
/* byteptr and charptr */ // byteptr and charptr
'3.vstring', '3.vstring',
'3.vstring_with_len', '3.vstring_with_len',
'4.vstring', '4.vstring',
'4.vstring_with_len', '4.vstring_with_len',
/* byte. methods */ // byte. methods
'9.str_escaped', '9.str_escaped',
/* string. methods */ // string. methods
'18.add', '18.add',
'18.trim_space', '18.trim_space',
'18.replace', '18.replace',
@ -57,14 +57,14 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
'18.substr', '18.substr',
'18.at', '18.at',
'18.index_kmp', '18.index_kmp',
/* string. ==, !=, etc... */ // string. ==, !=, etc...
'18.eq', '18.eq',
'18.ne', '18.ne',
'18.lt', '18.lt',
'18.gt', '18.gt',
'18.le', '18.le',
'18.ge', '18.ge',
/* ustring. ==, !=, etc... */ // ustring. ==, !=, etc...
'19.eq', '19.eq',
'19.ne', '19.ne',
'19.lt', '19.lt',
@ -72,7 +72,7 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
'19.le', '19.le',
'19.ge', '19.ge',
'19.add', '19.add',
/* other array methods */ // other array methods
'21.get', '21.get',
'21.set', '21.set',
'21.get_unsafe', '21.get_unsafe',
@ -94,7 +94,7 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
'65557.reverse', '65557.reverse',
'65557.set', '65557.set',
'65557.set_unsafe', '65557.set_unsafe',
/* TODO: process the _vinit const initializations automatically too */ // TODO: process the _vinit const initializations automatically too
'os.getwd', 'os.getwd',
'os.init_os_args', 'os.init_os_args',
'os.init_os_args_wide', 'os.init_os_args_wide',

View File

@ -85,7 +85,7 @@ pub fn (mut p Preferences) fill_with_defaults() {
// should go into res.cache_manager.vopts, which is used as a salt for the cache hash. // should go into res.cache_manager.vopts, which is used as a salt for the cache hash.
p.cache_manager = vcache.new_cache_manager([ p.cache_manager = vcache.new_cache_manager([
@VHASH, @VHASH,
/* ensure that different v versions use separate build artefacts */ // ensure that different v versions use separate build artefacts
'$p.backend | $p.os | $p.ccompiler | $p.is_prod | $p.sanitize', '$p.backend | $p.os | $p.ccompiler | $p.is_prod | $p.sanitize',
p.cflags.trim_space(), p.cflags.trim_space(),
p.third_party_option.trim_space(), p.third_party_option.trim_space(),