From 18aecde9e5fbab06f5165970dace610ff232082a Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Mon, 22 Feb 2021 17:43:54 +0100 Subject: [PATCH] fmt: cleanup comments code (#8901) --- vlib/v/fmt/comments.v | 34 ++++++++++++++-------------------- vlib/v/fmt/fmt.v | 19 ++++++++++++------- vlib/v/scanner/scanner.v | 2 ++ 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/vlib/v/fmt/comments.v b/vlib/v/fmt/comments.v index b09db45430..a1a4c63701 100644 --- a/vlib/v/fmt/comments.v +++ b/vlib/v/fmt/comments.v @@ -5,18 +5,18 @@ module fmt import v.ast -enum CommentsLevel { +pub enum CommentsLevel { keep indent } // CommentsOptions defines the way comments are going to be written -// - 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 +// - 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) -// - prev_line: the line number of the previous token -struct CommentsOptions { +// - iembed: a /* ... */ block comment used inside expressions; // comments the whole line +// - prev_line: the line number of the previous token to save linebreaks +pub struct CommentsOptions { has_nl bool = true inline bool level CommentsLevel @@ -25,6 +25,7 @@ struct CommentsOptions { } pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { + // Shebang in .vsh files if node.text.starts_with('#!') { f.writeln(node.text) return @@ -33,13 +34,13 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { f.indent++ } if options.iembed { - x := node.text.trim_left('\x01') + x := node.text.trim_left('\x01').trim_space() if x.contains('\n') { f.writeln('/*') - f.writeln(x.trim_space()) + f.writeln(x) f.write('*/') } else { - f.write('/* ${x.trim(' ')} */') + f.write('/* $x */') } } else if !node.text.contains('\n') { is_separate_line := !options.inline || node.text.starts_with('\x01') @@ -52,7 +53,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { out_s += s } if !is_separate_line && f.indent > 0 { - f.remove_new_line() // delete the generated \n + f.remove_new_line({}) // delete the generated \n f.write(' ') } f.write(out_s) @@ -69,7 +70,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { f.empty_line = false } if no_new_lines { - f.remove_new_line() + f.remove_new_line({}) } else { f.empty_line = true } @@ -118,21 +119,14 @@ pub fn (mut f Fmt) import_comments(comments []ast.Comment, options CommentsOptio 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) + f.remove_new_line(imports_buffer: true) } for c in comments { ctext := c.text.trim_left('\x01') if ctext == '' { continue } - mut out_s := if options.inline { ' ' } else { '' } - out_s += '//' + mut out_s := if options.inline { ' ' } else { '' } + '//' if is_first_char_alphanumeric(ctext) { out_s += ' ' } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 681dd2b471..996309b255 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -152,14 +152,19 @@ pub fn (mut f Fmt) wrap_long_line(penalty_idx int, add_indent bool) bool { return true } -pub fn (mut f Fmt) remove_new_line() { +pub struct RemoveNewLineConfig { + imports_buffer bool // Work on f.out_imports instead of f.out +} + +pub fn (mut f Fmt) remove_new_line(cfg RemoveNewLineConfig) { + mut buffer := if cfg.imports_buffer { &f.out_imports } else { &f.out } mut i := 0 - for i = f.out.len - 1; i >= 0; i-- { - if !f.out.buf[i].is_space() { // != `\n` { + for i = buffer.len - 1; i >= 0; i-- { + if !buffer.buf[i].is_space() { // != `\n` { break } } - f.out.go_back(f.out.len - i - 1) + buffer.go_back(buffer.len - i - 1) f.empty_line = false } @@ -1883,7 +1888,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) { } f.stmts(branch.stmts) if single_line { - f.remove_new_line() + f.remove_new_line({}) f.writeln(' }') } else { f.writeln('}') @@ -2181,7 +2186,7 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) { single_line_fields = false f.out.go_back_to(fields_start) f.line_len = fields_start - f.remove_new_line() + f.remove_new_line({}) continue fields_loop } } @@ -2394,7 +2399,7 @@ pub fn (mut f Fmt) for_c_stmt(node ast.ForCStmt) { f.expr(node.cond) f.write('; ') f.stmt(node.inc) - f.remove_new_line() + f.remove_new_line({}) f.write(' {') if node.stmts.len > 0 || node.pos.line_nr < node.pos.last_line { f.writeln('') diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index b7e4d5dcee..feaf1eab05 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -950,6 +950,8 @@ fn (mut s Scanner) text_scan() token.Token { } } if is_separate_line_comment { + // NB: ´\x01´ is used to preserve the initial whitespace in comments + // that are on a separate line comment = '\x01' + comment } return s.new_token(.comment, comment, comment.len + 2)