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

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

This commit is contained in:
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,18 +1970,25 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
}
}
for i, c in node.pre_cmnts {
f.comment(c, level: .indent, iembed: true)
if i < node.pre_cmnts.len - 1 {
if c.pos.last_line < node.pre_cmnts[i + 1].pos.line_nr {
f.comment(c, level: .indent)
f.writeln('')
} else {
f.comment(c, level: .indent, iembed: true)
f.write(' ')
}
} else {
if c.pos.last_line < node.pos.last_line && node.exprs.len == 0 {
f.writeln('')
} else if node.exprs.len > 0 {
f.write(' ')
if c.pos.last_line < node.pos.last_line {
f.comment(c, level: .indent)
if node.exprs.len == 0 {
f.writeln('')
}
} else {
f.comment(c, level: .indent, iembed: true)
if node.exprs.len > 0 {
f.write(' ')
}
}
}
last_line_nr = c.pos.last_line
@ -2022,10 +2029,11 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
}
if cmt.pos.line_nr > expr_pos.last_line {
f.writeln('')
f.comment(cmt, {})
} else {
f.write(' ')
f.comment(cmt, iembed: true)
}
f.comment(cmt, iembed: true)
}
}
if i == node.exprs.len - 1 {

View File

@ -1,22 +1,4 @@
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}
_ := []map[string]string{len: 5, cap: 50, init: map{
'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
// more comments = more good
arr := [
/* block foo bar */
/* inline foo bar */
// block foo bar
// inline foo bar
0,
]
// before arg comment

View File

@ -31,7 +31,7 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
'tos3',
'isnil',
'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',
'compare_ints',
'compare_u64s',
@ -40,14 +40,14 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
'compare_u64s_reverse',
'compare_strings_reverse',
'builtin_init',
/* byteptr and charptr */
// byteptr and charptr
'3.vstring',
'3.vstring_with_len',
'4.vstring',
'4.vstring_with_len',
/* byte. methods */
// byte. methods
'9.str_escaped',
/* string. methods */
// string. methods
'18.add',
'18.trim_space',
'18.replace',
@ -57,14 +57,14 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
'18.substr',
'18.at',
'18.index_kmp',
/* string. ==, !=, etc... */
// string. ==, !=, etc...
'18.eq',
'18.ne',
'18.lt',
'18.gt',
'18.le',
'18.ge',
/* ustring. ==, !=, etc... */
// ustring. ==, !=, etc...
'19.eq',
'19.ne',
'19.lt',
@ -72,7 +72,7 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
'19.le',
'19.ge',
'19.add',
/* other array methods */
// other array methods
'21.get',
'21.set',
'21.get_unsafe',
@ -94,7 +94,7 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
'65557.reverse',
'65557.set',
'65557.set_unsafe',
/* TODO: process the _vinit const initializations automatically too */
// TODO: process the _vinit const initializations automatically too
'os.getwd',
'os.init_os_args',
'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.
p.cache_manager = vcache.new_cache_manager([
@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.cflags.trim_space(),
p.third_party_option.trim_space(),