Skip to content

Commit

Permalink
ast, parser, fmt: implement inline comments (#19012)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 31, 2023
1 parent 0f861db commit 8861538
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 113 deletions.
13 changes: 8 additions & 5 deletions vlib/v/ast/ast.v
Expand Up @@ -532,10 +532,11 @@ pub:
method_idx int
rec_mut bool // is receiver mutable
rec_share ShareType
language Language // V, C, JS
file_mode Language // whether *the file*, where a function was a '.c.v', '.js.v' etc.
no_body bool // just a definition `fn C.malloc()`
is_builtin bool // this function is defined in builtin/strconv
language Language // V, C, JS
file_mode Language // whether *the file*, where a function was a '.c.v', '.js.v' etc.
no_body bool // just a definition `fn C.malloc()`
is_builtin bool // this function is defined in builtin/strconv
name_pos token.Pos
body_pos token.Pos // function bodys position
file string
generic_names []string
Expand Down Expand Up @@ -634,7 +635,8 @@ pub:
type_pos token.Pos
is_hidden bool // interface first arg
pub mut:
typ Type
typ Type
comments []Comment
}

pub fn (p &Param) specifier() string {
Expand Down Expand Up @@ -1195,6 +1197,7 @@ pub:
val_var string
is_range bool
pos token.Pos
kv_pos token.Pos
comments []Comment
val_is_mut bool // `for mut val in vals {` means that modifying `val` will modify the array
// and the array cannot be indexed inside the loop
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/ast/str.v
Expand Up @@ -218,7 +218,7 @@ struct StringifyModReplacement {
weight int
}

fn shorten_full_name_based_on_aliases(input string, m2a map[string]string) string {
pub fn shorten_full_name_based_on_aliases(input string, m2a map[string]string) string {
if m2a.len == 0 || -1 == input.index_u8(`.`) {
// a simple typename, like `string` or `[]bool`; no module aliasings apply,
// (or there just are not any mappings)
Expand Down
8 changes: 4 additions & 4 deletions vlib/v/fmt/comments.v
Expand Up @@ -14,14 +14,12 @@ pub enum CommentsLevel {
// - has_nl: adds an newline at the end of a list of comments
// - inline: line comments will be on the same line as the last statement
// - level: either .keep (don't indent), or .indent (increment indentation)
// - iembed: a /* ... */ block comment used inside expressions; // comments the whole line
// - prev_line: the line number of the previous token to save linebreaks
[minify; params]
pub struct CommentsOptions {
has_nl bool = true
inline bool
level CommentsLevel
iembed bool
prev_line int = -1
}

Expand All @@ -44,7 +42,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
if options.level == .indent {
f.indent++
}
if options.iembed {
if node.is_inline && !node.is_multi {
x := node.text.trim_left('\x01').trim_space()
if x.contains('\n') {
f.writeln('/*')
Expand Down Expand Up @@ -105,7 +103,9 @@ pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) {
f.write(' ')
}
f.comment(c, options)
if !options.iembed && (i < comments.len - 1 || options.has_nl) {
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) {
f.writeln('')
}
prev_line = c.pos.last_line
Expand Down

0 comments on commit 8861538

Please sign in to comment.