From d365b8f9b0205a17489c3a4628ed355792dc6059 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 8 Oct 2023 18:32:22 +0800 Subject: [PATCH] fmt: simplify formatting assign statement comments (#19524) --- cmd/tools/vast/vast.v | 1 - vlib/v/ast/ast.v | 1 - vlib/v/checker/assign.v | 1 - vlib/v/fmt/fmt.v | 13 ------------- vlib/v/fmt/tests/comments_expected.vv | 3 +-- vlib/v/parser/assign.v | 14 ++++---------- vlib/v/parser/if_match.v | 4 ++-- vlib/v/parser/lock.v | 13 ++++--------- vlib/v/parser/parser.v | 18 ++++++------------ 9 files changed, 17 insertions(+), 51 deletions(-) diff --git a/cmd/tools/vast/vast.v b/cmd/tools/vast/vast.v index a453cae832c234..0beb6d93eaca05 100644 --- a/cmd/tools/vast/vast.v +++ b/cmd/tools/vast/vast.v @@ -862,7 +862,6 @@ fn (t Tree) assign_stmt(node ast.AssignStmt) &Node { obj.add_terse('is_simple', t.bool_node(node.is_simple)) obj.add_terse('has_cross_var', t.bool_node(node.has_cross_var)) obj.add('pos', t.pos(node.pos)) - obj.add('comments', t.array_node_comment(node.comments)) obj.add('end_comments', t.array_node_comment(node.end_comments)) return obj } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 31057836302a57..c64929d2f0b6a9 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1269,7 +1269,6 @@ pub struct AssignStmt { pub: op token.Kind // include: =,:=,+=,-=,*=,/= and so on; for a list of all the assign operators, see vlib/token/token.v pos token.Pos - comments []Comment end_comments []Comment pub mut: right []Expr diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 148cd47256e960..96fa35f10e4a53 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -656,7 +656,6 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.', node = ast.AssignStmt{ op: .assign pos: node.pos - comments: node.comments end_comments: node.end_comments left: node.left right: [ diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index a3936ecc07e2ba..140e9f4dc520f4 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -831,14 +831,7 @@ pub fn (mut f Fmt) assert_stmt(node ast.AssertStmt) { } pub fn (mut f Fmt) assign_stmt(node ast.AssignStmt) { - mut sum_len := 0 for i, left in node.left { - pre_comments := node.comments[sum_len..].filter(it.pos.pos < left.pos().pos) - sum_len += pre_comments.len - if pre_comments.len > 0 { - f.comments(pre_comments) - f.write(' ') - } f.expr(left) if i < node.left.len - 1 { f.write(', ') @@ -847,12 +840,6 @@ pub fn (mut f Fmt) assign_stmt(node ast.AssignStmt) { f.is_assign = true f.write(' ${node.op.str()} ') for i, val in node.right { - pre_comments := node.comments[sum_len..].filter(it.pos.pos < val.pos().pos) - sum_len += pre_comments.len - if pre_comments.len > 0 { - f.comments(pre_comments) - f.write(' ') - } f.expr(val) if i < node.right.len - 1 { f.write(', ') diff --git a/vlib/v/fmt/tests/comments_expected.vv b/vlib/v/fmt/tests/comments_expected.vv index af45cece85ff4e..abff1c18af6642 100644 --- a/vlib/v/fmt/tests/comments_expected.vv +++ b/vlib/v/fmt/tests/comments_expected.vv @@ -10,8 +10,7 @@ fn fun() int { } fn mr_fun() (int, int) { - return 1, // one comment - 2 // another comment + return 1, 2 // another comment } fn single_line_blocks() { diff --git a/vlib/v/parser/assign.v b/vlib/v/parser/assign.v index 4a449b05104287..e69d66eb9470ce 100644 --- a/vlib/v/parser/assign.v +++ b/vlib/v/parser/assign.v @@ -9,13 +9,13 @@ fn (mut p Parser) assign_stmt() ast.Stmt { mut defer_vars := p.defer_vars.clone() p.defer_vars = []ast.Ident{} - exprs, comments := p.expr_list() + exprs := p.expr_list() if !(p.inside_defer && p.tok.kind == .decl_assign) { defer_vars << p.defer_vars } p.defer_vars = defer_vars - return p.partial_assign_stmt(exprs, comments) + return p.partial_assign_stmt(exprs) } const max_expr_level = 100 @@ -184,18 +184,13 @@ fn (mut p Parser) check_cross_variables(exprs []ast.Expr, val ast.Expr) bool { return false } -fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comment) ast.Stmt { +fn (mut p Parser) partial_assign_stmt(left []ast.Expr) ast.Stmt { p.is_stmt_ident = false op := p.tok.kind mut pos := p.tok.pos() p.next() - mut comments := []ast.Comment{cap: 2 * left_comments.len + 1} - comments << left_comments - comments << p.eat_comments() - mut right_comments := []ast.Comment{} mut right := []ast.Expr{cap: left.len} - right, right_comments = p.expr_list() - comments << right_comments + right = p.expr_list() end_comments := p.eat_comments(same_line: true) mut has_cross_var := false mut is_static := false @@ -291,7 +286,6 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme op: op left: left right: right - comments: comments end_comments: end_comments pos: pos has_cross_var: has_cross_var diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index 97eee28ab964ce..e272cc36985953 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -401,13 +401,13 @@ fn (mut p Parser) select_expr() ast.SelectExpr { } p.inside_match = true p.inside_select = true - exprs, comments := p.expr_list() + exprs := p.expr_list() if exprs.len != 1 { p.error('only one expression allowed as `select` key') return ast.SelectExpr{} } if p.tok.kind in [.assign, .decl_assign] { - stmt = p.partial_assign_stmt(exprs, comments) + stmt = p.partial_assign_stmt(exprs) } else { stmt = ast.ExprStmt{ expr: exprs[0] diff --git a/vlib/v/parser/lock.v b/vlib/v/parser/lock.v index 1f5dbe00254ec4..f6a1983af53f5d 100644 --- a/vlib/v/parser/lock.v +++ b/vlib/v/parser/lock.v @@ -44,14 +44,11 @@ fn (mut p Parser) lockable() ast.Expr { } // like `expr_list()` but only lockables are allowed, `{` starts lock block (not struct literal) -fn (mut p Parser) lockable_list() ([]ast.Expr, []ast.Comment) { +fn (mut p Parser) lockable_list() []ast.Expr { mut exprs := []ast.Expr{} - mut comments := []ast.Comment{} for { expr := p.lockable() - if expr is ast.Comment { - comments << expr - } else { + if expr !is ast.Comment { exprs << expr if p.tok.kind != .comma { break @@ -59,7 +56,7 @@ fn (mut p Parser) lockable_list() ([]ast.Expr, []ast.Comment) { p.next() } } - return exprs, comments + return exprs } fn (mut p Parser) lock_expr() ast.LockExpr { @@ -68,7 +65,6 @@ fn (mut p Parser) lock_expr() ast.LockExpr { p.open_scope() mut pos := p.tok.pos() mut lockeds := []ast.Expr{} - mut comments := []ast.Comment{} mut is_rlocked := []bool{} for { is_rlock := p.tok.kind == .key_rlock @@ -80,7 +76,7 @@ fn (mut p Parser) lock_expr() ast.LockExpr { break } if p.tok.kind == .name { - exprs, comms := p.lockable_list() + exprs := p.lockable_list() for e in exprs { if !e.is_lockable() { p.error_with_pos('`${e}` cannot be locked - only `x` or `x.y` are supported', @@ -89,7 +85,6 @@ fn (mut p Parser) lock_expr() ast.LockExpr { lockeds << e is_rlocked << is_rlock } - comments << comms } if p.tok.kind == .lcbr { break diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index b68c3f9d7fd7de..b9c6e7ae1676c9 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1754,14 +1754,11 @@ fn (mut p Parser) asm_ios(output bool) []ast.AsmIO { return res } -fn (mut p Parser) expr_list() ([]ast.Expr, []ast.Comment) { +fn (mut p Parser) expr_list() []ast.Expr { mut exprs := []ast.Expr{} - mut comments := []ast.Comment{} for { expr := p.expr(0) - if expr is ast.Comment { - comments << expr - } else { + if expr !is ast.Comment { exprs << expr if p.tok.kind != .comma { break @@ -1769,7 +1766,7 @@ fn (mut p Parser) expr_list() ([]ast.Expr, []ast.Comment) { p.next() } } - return exprs, comments + return exprs } fn (mut p Parser) is_attributes() bool { @@ -2139,7 +2136,7 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt { mut defer_vars := p.defer_vars.clone() p.defer_vars = []ast.Ident{} - left, left_comments := p.expr_list() + left := p.expr_list() if !(p.inside_defer && p.tok.kind == .decl_assign) { defer_vars << p.defer_vars @@ -2157,7 +2154,7 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt { } // TODO remove translated if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() { - return p.partial_assign_stmt(left, left_comments) + return p.partial_assign_stmt(left) } else if !p.pref.translated && !p.is_translated && !p.pref.is_fmt && !p.pref.is_vet && tok.kind !in [.key_if, .key_match, .key_lock, .key_rlock, .key_select] { for node in left { @@ -2176,7 +2173,6 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt { return ast.ExprStmt{ expr: left0 pos: left0.pos() - comments: left_comments is_expr: p.inside_for } } @@ -2186,7 +2182,6 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt { pos: tok.pos() } pos: pos - comments: left_comments } } @@ -3861,8 +3856,7 @@ fn (mut p Parser) return_stmt() ast.Return { } } // return exprs - exprs, comments2 := p.expr_list() - comments << comments2 + exprs := p.expr_list() end_pos := exprs.last().pos() return ast.Return{ exprs: exprs