Skip to content

Commit a23f89e

Browse files
authored
fmt: simplify the processing logic for removing inline comments (#19297)
1 parent f18086c commit a23f89e

File tree

6 files changed

+19
-59
lines changed

6 files changed

+19
-59
lines changed

cmd/tools/vast/vast.v

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,6 @@ fn (t Tree) comment(node ast.Comment) &Node {
479479
obj.add_terse('ast_type', t.string_node('Comment'))
480480
obj.add('text', t.string_node(node.text))
481481
obj.add('is_multi', t.bool_node(node.is_multi))
482-
obj.add('is_inline', t.bool_node(node.is_inline))
483482
obj.add('pos', t.pos(node.pos))
484483
return obj
485484
}

vlib/v/ast/ast.v

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,10 +1814,9 @@ pub mut:
18141814

18151815
pub struct Comment {
18161816
pub:
1817-
text string
1818-
is_multi bool // true only for /* comment */, that use many lines
1819-
is_inline bool // true for all /* comment */ comments
1820-
pos token.Pos
1817+
text string
1818+
is_multi bool // true only for /* comment */, that use many lines
1819+
pos token.Pos
18211820
}
18221821

18231822
pub struct ConcatExpr {

vlib/v/ast/str.v

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
171171
// (node.is_variadic && i == node.params.len - 2)
172172
pre_comments := param.comments.filter(it.pos.pos < param.pos.pos)
173173
if pre_comments.len > 0 {
174-
if i == 0 && !pre_comments.last().is_inline {
174+
if i == 0 {
175175
is_wrap_needed = true
176176
f.write_string('\n\t')
177177
}
@@ -235,28 +235,19 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
235235

236236
fn write_comments(comments []Comment, mut f strings.Builder) {
237237
for i, c in comments {
238-
if !f.last_n(1)[0].is_space() {
239-
f.write_string(' ')
240-
}
241238
write_comment(c, mut f)
242-
if c.is_inline && i < comments.len - 1 && !c.is_multi {
243-
f.write_string(' ')
244-
} else if (!c.is_inline || c.is_multi) && i < comments.len - 1 {
239+
if i < comments.len - 1 {
245240
f.writeln('')
246241
}
247242
}
248243
}
249244

250245
fn write_comment(node Comment, mut f strings.Builder) {
251-
if node.is_inline {
246+
if node.is_multi {
252247
x := node.text.trim_left('\x01').trim_space()
253-
if x.contains('\n') {
254-
f.writeln('/*')
255-
f.writeln(x)
256-
f.write_string('*/')
257-
} else {
258-
f.write_string('/* ${x} */')
259-
}
248+
f.writeln('/*')
249+
f.writeln(x)
250+
f.write_string('*/')
260251
} else {
261252
mut s := node.text.trim_left('\x01').trim_right(' ')
262253
mut out_s := '//'

vlib/v/fmt/comments.v

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
4242
if options.level == .indent {
4343
f.indent++
4444
}
45-
if node.is_inline && !node.is_multi {
46-
x := node.text.trim_left('\x01').trim_space()
47-
if x.contains('\n') {
48-
f.writeln('/*')
49-
f.writeln(x)
50-
f.write('*/')
51-
} else {
52-
f.write('/* ${x} */')
53-
}
54-
} else if !node.text.contains('\n') {
45+
if !node.text.contains('\n') {
5546
is_separate_line := !options.inline || node.text.starts_with('\x01')
5647
mut s := node.text.trim_left('\x01').trim_right(' ')
5748
mut out_s := '//'
@@ -103,9 +94,7 @@ pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) {
10394
f.write(' ')
10495
}
10596
f.comment(c, options)
106-
if c.is_inline && i < comments.len - 1 && !c.is_multi {
107-
f.write(' ')
108-
} else if (!c.is_inline || c.is_multi) && (i < comments.len - 1 || options.has_nl) {
97+
if i < comments.len - 1 || options.has_nl {
10998
f.writeln('')
11099
}
111100
prev_line = c.pos.last_line

vlib/v/fmt/fmt.v

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,30 +1747,14 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
17471747
f.writeln('')
17481748
f.comment(cmt)
17491749
} else {
1750-
if cmt.is_inline {
1751-
f.write(' ')
1752-
f.comment(cmt)
1753-
if !set_comma && cmt.pos.line_nr == expr_pos.last_line
1754-
&& cmt.pos.pos < expr_pos.pos {
1755-
f.write(',')
1756-
set_comma = true
1757-
} else {
1758-
if !cmt.is_inline {
1759-
// a // comment, transformed to a /**/ one, needs a comma too
1760-
f.write(',')
1761-
set_comma = true
1762-
}
1763-
}
1764-
} else {
1765-
if !set_comma {
1766-
f.write(',')
1767-
set_comma = true
1768-
}
1769-
f.write(' ')
1770-
f.comment(cmt)
1771-
if !line_break {
1772-
f.writeln('')
1773-
}
1750+
if !set_comma {
1751+
f.write(',')
1752+
set_comma = true
1753+
}
1754+
f.write(' ')
1755+
f.comment(cmt)
1756+
if !line_break {
1757+
f.writeln('')
17741758
}
17751759
}
17761760
}

vlib/v/parser/parser.v

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,6 @@ fn (mut p Parser) comment() ast.Comment {
883883
text := p.tok.lit
884884
num_newlines := text.count('\n')
885885
is_multi := num_newlines > 0
886-
is_inline := text.len + 4 == p.tok.len // 4: `/` `*` `*` `/`
887886
pos.last_line = pos.line_nr + num_newlines
888887
p.next()
889888
// Filter out false positive space indent vet errors inside comments
@@ -894,7 +893,6 @@ fn (mut p Parser) comment() ast.Comment {
894893
return ast.Comment{
895894
text: text
896895
is_multi: is_multi
897-
is_inline: is_inline
898896
pos: pos
899897
}
900898
}

0 commit comments

Comments
 (0)