Skip to content

Commit 8861538

Browse files
authored
ast, parser, fmt: implement inline comments (#19012)
1 parent 0f861db commit 8861538

15 files changed

+294
-113
lines changed

vlib/v/ast/ast.v

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,11 @@ pub:
532532
method_idx int
533533
rec_mut bool // is receiver mutable
534534
rec_share ShareType
535-
language Language // V, C, JS
536-
file_mode Language // whether *the file*, where a function was a '.c.v', '.js.v' etc.
537-
no_body bool // just a definition `fn C.malloc()`
538-
is_builtin bool // this function is defined in builtin/strconv
535+
language Language // V, C, JS
536+
file_mode Language // whether *the file*, where a function was a '.c.v', '.js.v' etc.
537+
no_body bool // just a definition `fn C.malloc()`
538+
is_builtin bool // this function is defined in builtin/strconv
539+
name_pos token.Pos
539540
body_pos token.Pos // function bodys position
540541
file string
541542
generic_names []string
@@ -634,7 +635,8 @@ pub:
634635
type_pos token.Pos
635636
is_hidden bool // interface first arg
636637
pub mut:
637-
typ Type
638+
typ Type
639+
comments []Comment
638640
}
639641

640642
pub fn (p &Param) specifier() string {
@@ -1195,6 +1197,7 @@ pub:
11951197
val_var string
11961198
is_range bool
11971199
pos token.Pos
1200+
kv_pos token.Pos
11981201
comments []Comment
11991202
val_is_mut bool // `for mut val in vals {` means that modifying `val` will modify the array
12001203
// and the array cannot be indexed inside the loop

vlib/v/ast/str.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ struct StringifyModReplacement {
218218
weight int
219219
}
220220

221-
fn shorten_full_name_based_on_aliases(input string, m2a map[string]string) string {
221+
pub fn shorten_full_name_based_on_aliases(input string, m2a map[string]string) string {
222222
if m2a.len == 0 || -1 == input.index_u8(`.`) {
223223
// a simple typename, like `string` or `[]bool`; no module aliasings apply,
224224
// (or there just are not any mappings)

vlib/v/fmt/comments.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ pub enum CommentsLevel {
1414
// - has_nl: adds an newline at the end of a list of comments
1515
// - inline: line comments will be on the same line as the last statement
1616
// - level: either .keep (don't indent), or .indent (increment indentation)
17-
// - iembed: a /* ... */ block comment used inside expressions; // comments the whole line
1817
// - prev_line: the line number of the previous token to save linebreaks
1918
[minify; params]
2019
pub struct CommentsOptions {
2120
has_nl bool = true
2221
inline bool
2322
level CommentsLevel
24-
iembed bool
2523
prev_line int = -1
2624
}
2725

@@ -44,7 +42,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
4442
if options.level == .indent {
4543
f.indent++
4644
}
47-
if options.iembed {
45+
if node.is_inline && !node.is_multi {
4846
x := node.text.trim_left('\x01').trim_space()
4947
if x.contains('\n') {
5048
f.writeln('/*')
@@ -105,7 +103,9 @@ pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) {
105103
f.write(' ')
106104
}
107105
f.comment(c, options)
108-
if !options.iembed && (i < comments.len - 1 || options.has_nl) {
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) {
109109
f.writeln('')
110110
}
111111
prev_line = c.pos.last_line

0 commit comments

Comments
 (0)