From 8755f40430d589f5859e18dec7af0d3f549b81b9 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Mon, 1 Feb 2021 14:45:08 +0100 Subject: [PATCH] fmt: keep comments after imports (#8483) --- vlib/v/ast/ast.v | 3 +- vlib/v/fmt/comments.v | 30 ++++++++++++++++++- vlib/v/fmt/fmt.v | 2 +- .../tests/comment_after_import_expected.vv | 6 ---- .../v/fmt/tests/comment_after_import_input.vv | 4 --- vlib/v/fmt/tests/comments_keep.vv | 2 ++ vlib/v/fmt/tests/import_single_input.vv | 5 ---- ...ngle_expected.vv => import_single_keep.vv} | 0 vlib/v/parser/parser.v | 1 + 9 files changed, 35 insertions(+), 18 deletions(-) delete mode 100644 vlib/v/fmt/tests/comment_after_import_expected.vv delete mode 100644 vlib/v/fmt/tests/comment_after_import_input.vv delete mode 100644 vlib/v/fmt/tests/import_single_input.vv rename vlib/v/fmt/tests/{import_single_expected.vv => import_single_keep.vv} (100%) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index c1f1390ec6..e297b59263 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -283,7 +283,8 @@ pub: mod_pos token.Position alias_pos token.Position pub mut: - syms []ImportSymbol // the list of symbols in `import {symbol1, symbol2}` + syms []ImportSymbol // the list of symbols in `import {symbol1, symbol2}` + comments []Comment } // import symbol,for import {symbol} syntax diff --git a/vlib/v/fmt/comments.v b/vlib/v/fmt/comments.v index 840b5746fe..b57c715bf7 100644 --- a/vlib/v/fmt/comments.v +++ b/vlib/v/fmt/comments.v @@ -14,7 +14,7 @@ enum CommentsLevel { // - has_nl: adds an newline at the end of the list of comments // - inline: single-line comments will be on the same line as the last statement // - iembed: a /* ... */ embedded comment; used in expressions; // comments the whole line -// - level: either .keep (don't indent), or .indent (increment indentation) +// - level: either .keep (don't indent), or .indent (increment indentation) struct CommentsOptions { has_nl bool = true inline bool @@ -105,6 +105,34 @@ pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) { } } +pub fn (mut f Fmt) import_comments(comments []ast.Comment, options CommentsOptions) { + if comments.len == 0 { + return + } + if options.inline { + mut i := 0 + for i = f.out_imports.len - 1; i >= 0; i-- { + if !f.out_imports.buf[i].is_space() { // != `\n` { + break + } + } + f.out_imports.go_back(f.out_imports.len - i - 1) + } + for c in comments { + ctext := c.text.trim_left('\x01') + if ctext == '' { + continue + } + mut out_s := if options.inline { ' ' } else { '' } + out_s += '//' + if is_first_char_alphanumeric(ctext) { + out_s += ' ' + } + out_s += ctext + f.out_imports.writeln(out_s) + } +} + fn is_first_char_alphanumeric(s string) bool { return match s[0] { `a`...`z`, `A`...`Z`, `0`...`9` { true } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 70922ae229..3a7cc9a40e 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -22,7 +22,6 @@ pub mut: table &table.Table out_imports strings.Builder out strings.Builder - out_save strings.Builder indent int empty_line bool line_len int @@ -272,6 +271,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) num_imports++ } if num_imports > 0 { diff --git a/vlib/v/fmt/tests/comment_after_import_expected.vv b/vlib/v/fmt/tests/comment_after_import_expected.vv deleted file mode 100644 index ca9039b6ce..0000000000 --- a/vlib/v/fmt/tests/comment_after_import_expected.vv +++ /dev/null @@ -1,6 +0,0 @@ -import semver - -// as semver - -fn main() { -} diff --git a/vlib/v/fmt/tests/comment_after_import_input.vv b/vlib/v/fmt/tests/comment_after_import_input.vv deleted file mode 100644 index fbb98871ba..0000000000 --- a/vlib/v/fmt/tests/comment_after_import_input.vv +++ /dev/null @@ -1,4 +0,0 @@ -import semver// as semver - -fn main() { -} diff --git a/vlib/v/fmt/tests/comments_keep.vv b/vlib/v/fmt/tests/comments_keep.vv index 5e63a4edbc..ac4fee3bde 100644 --- a/vlib/v/fmt/tests/comments_keep.vv +++ b/vlib/v/fmt/tests/comments_keep.vv @@ -1,3 +1,5 @@ +import semver // as sv + enum Abc { a b // after a value diff --git a/vlib/v/fmt/tests/import_single_input.vv b/vlib/v/fmt/tests/import_single_input.vv deleted file mode 100644 index 29d4d36294..0000000000 --- a/vlib/v/fmt/tests/import_single_input.vv +++ /dev/null @@ -1,5 +0,0 @@ -import os - -fn main() { - os.system('echo hi') -} diff --git a/vlib/v/fmt/tests/import_single_expected.vv b/vlib/v/fmt/tests/import_single_keep.vv similarity index 100% rename from vlib/v/fmt/tests/import_single_expected.vv rename to vlib/v/fmt/tests/import_single_keep.vv diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 9e39157b24..efcccc1767 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1907,6 +1907,7 @@ fn (mut p Parser) import_stmt() ast.Import { return import_node } } + import_node.comments = p.eat_line_end_comments() p.imports[mod_alias] = mod_name // if mod_name !in p.table.imports { p.table.imports << mod_name