Skip to content

Commit

Permalink
fmt: simplify the processing logic for removing inline comments (#19297)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 7, 2023
1 parent f18086c commit a23f89e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 59 deletions.
1 change: 0 additions & 1 deletion cmd/tools/vast/vast.v
Expand Up @@ -479,7 +479,6 @@ fn (t Tree) comment(node ast.Comment) &Node {
obj.add_terse('ast_type', t.string_node('Comment'))
obj.add('text', t.string_node(node.text))
obj.add('is_multi', t.bool_node(node.is_multi))
obj.add('is_inline', t.bool_node(node.is_inline))
obj.add('pos', t.pos(node.pos))
return obj
}
Expand Down
7 changes: 3 additions & 4 deletions vlib/v/ast/ast.v
Expand Up @@ -1814,10 +1814,9 @@ pub mut:

pub struct Comment {
pub:
text string
is_multi bool // true only for /* comment */, that use many lines
is_inline bool // true for all /* comment */ comments
pos token.Pos
text string
is_multi bool // true only for /* comment */, that use many lines
pos token.Pos
}

pub struct ConcatExpr {
Expand Down
21 changes: 6 additions & 15 deletions vlib/v/ast/str.v
Expand Up @@ -171,7 +171,7 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
// (node.is_variadic && i == node.params.len - 2)
pre_comments := param.comments.filter(it.pos.pos < param.pos.pos)
if pre_comments.len > 0 {
if i == 0 && !pre_comments.last().is_inline {
if i == 0 {
is_wrap_needed = true
f.write_string('\n\t')
}
Expand Down Expand Up @@ -235,28 +235,19 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m

fn write_comments(comments []Comment, mut f strings.Builder) {
for i, c in comments {
if !f.last_n(1)[0].is_space() {
f.write_string(' ')
}
write_comment(c, mut f)
if c.is_inline && i < comments.len - 1 && !c.is_multi {
f.write_string(' ')
} else if (!c.is_inline || c.is_multi) && i < comments.len - 1 {
if i < comments.len - 1 {
f.writeln('')
}
}
}

fn write_comment(node Comment, mut f strings.Builder) {
if node.is_inline {
if node.is_multi {
x := node.text.trim_left('\x01').trim_space()
if x.contains('\n') {
f.writeln('/*')
f.writeln(x)
f.write_string('*/')
} else {
f.write_string('/* ${x} */')
}
f.writeln('/*')
f.writeln(x)
f.write_string('*/')
} else {
mut s := node.text.trim_left('\x01').trim_right(' ')
mut out_s := '//'
Expand Down
15 changes: 2 additions & 13 deletions vlib/v/fmt/comments.v
Expand Up @@ -42,16 +42,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
if options.level == .indent {
f.indent++
}
if node.is_inline && !node.is_multi {
x := node.text.trim_left('\x01').trim_space()
if x.contains('\n') {
f.writeln('/*')
f.writeln(x)
f.write('*/')
} else {
f.write('/* ${x} */')
}
} else if !node.text.contains('\n') {
if !node.text.contains('\n') {
is_separate_line := !options.inline || node.text.starts_with('\x01')
mut s := node.text.trim_left('\x01').trim_right(' ')
mut out_s := '//'
Expand Down Expand Up @@ -103,9 +94,7 @@ pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) {
f.write(' ')
}
f.comment(c, options)
if c.is_inline && i < comments.len - 1 && !c.is_multi {
f.write(' ')
} else if (!c.is_inline || c.is_multi) && (i < comments.len - 1 || options.has_nl) {
if i < comments.len - 1 || options.has_nl {
f.writeln('')
}
prev_line = c.pos.last_line
Expand Down
32 changes: 8 additions & 24 deletions vlib/v/fmt/fmt.v
Expand Up @@ -1747,30 +1747,14 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
f.writeln('')
f.comment(cmt)
} else {
if cmt.is_inline {
f.write(' ')
f.comment(cmt)
if !set_comma && cmt.pos.line_nr == expr_pos.last_line
&& cmt.pos.pos < expr_pos.pos {
f.write(',')
set_comma = true
} else {
if !cmt.is_inline {
// a // comment, transformed to a /**/ one, needs a comma too
f.write(',')
set_comma = true
}
}
} else {
if !set_comma {
f.write(',')
set_comma = true
}
f.write(' ')
f.comment(cmt)
if !line_break {
f.writeln('')
}
if !set_comma {
f.write(',')
set_comma = true
}
f.write(' ')
f.comment(cmt)
if !line_break {
f.writeln('')
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions vlib/v/parser/parser.v
Expand Up @@ -883,7 +883,6 @@ fn (mut p Parser) comment() ast.Comment {
text := p.tok.lit
num_newlines := text.count('\n')
is_multi := num_newlines > 0
is_inline := text.len + 4 == p.tok.len // 4: `/` `*` `*` `/`
pos.last_line = pos.line_nr + num_newlines
p.next()
// Filter out false positive space indent vet errors inside comments
Expand All @@ -894,7 +893,6 @@ fn (mut p Parser) comment() ast.Comment {
return ast.Comment{
text: text
is_multi: is_multi
is_inline: is_inline
pos: pos
}
}
Expand Down

0 comments on commit a23f89e

Please sign in to comment.