Skip to content

Commit

Permalink
fmt: simplify formatting assign statement comments (#19524)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Oct 8, 2023
1 parent e3ba197 commit 05d940a
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 51 deletions.
1 change: 0 additions & 1 deletion cmd/tools/vast/vast.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
13 changes: 0 additions & 13 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')
Expand All @@ -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(', ')
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/fmt/tests/comments_expected.vv
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 4 additions & 10 deletions vlib/v/parser/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/parser/if_match.v
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 4 additions & 9 deletions vlib/v/parser/lock.v
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,19 @@ 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
}
p.next()
}
}
return exprs, comments
return exprs
}

fn (mut p Parser) lock_expr() ast.LockExpr {
Expand All @@ -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
Expand All @@ -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',
Expand All @@ -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
Expand Down
18 changes: 6 additions & 12 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -1754,22 +1754,19 @@ 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
}
p.next()
}
}
return exprs, comments
return exprs
}

fn (mut p Parser) is_attributes() bool {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 05d940a

Please sign in to comment.