Skip to content

Commit

Permalink
fmt: keep comments between and after imports (#8637)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Neubert authored Feb 8, 2021
1 parent cb1f63f commit 8cb01ba
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
5 changes: 3 additions & 2 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,9 @@ pub:
mod_pos token.Position
alias_pos token.Position
pub mut:
syms []ImportSymbol // the list of symbols in `import {symbol1, symbol2}`
comments []Comment
syms []ImportSymbol // the list of symbols in `import {symbol1, symbol2}`
comments []Comment
next_comments []Comment
}

// import symbol,for import {symbol} syntax
Expand Down
1 change: 1 addition & 0 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
already_imported[import_text] = true
f.out_imports.writeln(import_text)
f.import_comments(imp.comments, inline: true)
f.import_comments(imp.next_comments, {})
num_imports++
}
if num_imports > 0 {
Expand Down
8 changes: 7 additions & 1 deletion vlib/v/fmt/tests/import_keep.vv
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// import time This should be commented out
import os
// between
import rand
// first between
// second between
import semver
// after the last import
// also after the last import

// another comment after imports
// this comment does not belong to the imports anymore
1 change: 1 addition & 0 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,7 @@ fn (mut p Parser) import_stmt() ast.Import {
}
}
import_node.comments = p.eat_comments(same_line: true)
import_node.next_comments = p.eat_comments(follow_up: true)
p.imports[mod_alias] = mod_name
// if mod_name !in p.table.imports {
p.table.imports << mod_name
Expand Down

0 comments on commit 8cb01ba

Please sign in to comment.