fmt: cleanup comments code (#8901)
parent
7f6c4caa01
commit
18aecde9e5
|
@ -5,18 +5,18 @@ module fmt
|
||||||
|
|
||||||
import v.ast
|
import v.ast
|
||||||
|
|
||||||
enum CommentsLevel {
|
pub enum CommentsLevel {
|
||||||
keep
|
keep
|
||||||
indent
|
indent
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommentsOptions defines the way comments are going to be written
|
// CommentsOptions defines the way comments are going to be written
|
||||||
// - has_nl: adds an newline at the end of the list of comments
|
// - has_nl: adds an newline at the end of a list of comments
|
||||||
// - inline: single-line comments will be on the same line as the last statement
|
// - inline: 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)
|
||||||
// - prev_line: the line number of the previous token
|
// - iembed: a /* ... */ block comment used inside expressions; // comments the whole line
|
||||||
struct CommentsOptions {
|
// - prev_line: the line number of the previous token to save linebreaks
|
||||||
|
pub struct CommentsOptions {
|
||||||
has_nl bool = true
|
has_nl bool = true
|
||||||
inline bool
|
inline bool
|
||||||
level CommentsLevel
|
level CommentsLevel
|
||||||
|
@ -25,6 +25,7 @@ struct CommentsOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
|
pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
|
||||||
|
// Shebang in .vsh files
|
||||||
if node.text.starts_with('#!') {
|
if node.text.starts_with('#!') {
|
||||||
f.writeln(node.text)
|
f.writeln(node.text)
|
||||||
return
|
return
|
||||||
|
@ -33,13 +34,13 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
|
||||||
f.indent++
|
f.indent++
|
||||||
}
|
}
|
||||||
if options.iembed {
|
if options.iembed {
|
||||||
x := node.text.trim_left('\x01')
|
x := node.text.trim_left('\x01').trim_space()
|
||||||
if x.contains('\n') {
|
if x.contains('\n') {
|
||||||
f.writeln('/*')
|
f.writeln('/*')
|
||||||
f.writeln(x.trim_space())
|
f.writeln(x)
|
||||||
f.write('*/')
|
f.write('*/')
|
||||||
} else {
|
} else {
|
||||||
f.write('/* ${x.trim(' ')} */')
|
f.write('/* $x */')
|
||||||
}
|
}
|
||||||
} else if !node.text.contains('\n') {
|
} else if !node.text.contains('\n') {
|
||||||
is_separate_line := !options.inline || node.text.starts_with('\x01')
|
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
|
out_s += s
|
||||||
}
|
}
|
||||||
if !is_separate_line && f.indent > 0 {
|
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(' ')
|
||||||
}
|
}
|
||||||
f.write(out_s)
|
f.write(out_s)
|
||||||
|
@ -69,7 +70,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
|
||||||
f.empty_line = false
|
f.empty_line = false
|
||||||
}
|
}
|
||||||
if no_new_lines {
|
if no_new_lines {
|
||||||
f.remove_new_line()
|
f.remove_new_line({})
|
||||||
} else {
|
} else {
|
||||||
f.empty_line = true
|
f.empty_line = true
|
||||||
}
|
}
|
||||||
|
@ -118,21 +119,14 @@ pub fn (mut f Fmt) import_comments(comments []ast.Comment, options CommentsOptio
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if options.inline {
|
if options.inline {
|
||||||
mut i := 0
|
f.remove_new_line(imports_buffer: true)
|
||||||
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 {
|
for c in comments {
|
||||||
ctext := c.text.trim_left('\x01')
|
ctext := c.text.trim_left('\x01')
|
||||||
if ctext == '' {
|
if ctext == '' {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
mut out_s := if options.inline { ' ' } else { '' }
|
mut out_s := if options.inline { ' ' } else { '' } + '//'
|
||||||
out_s += '//'
|
|
||||||
if is_first_char_alphanumeric(ctext) {
|
if is_first_char_alphanumeric(ctext) {
|
||||||
out_s += ' '
|
out_s += ' '
|
||||||
}
|
}
|
||||||
|
|
|
@ -152,14 +152,19 @@ pub fn (mut f Fmt) wrap_long_line(penalty_idx int, add_indent bool) bool {
|
||||||
return true
|
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
|
mut i := 0
|
||||||
for i = f.out.len - 1; i >= 0; i-- {
|
for i = buffer.len - 1; i >= 0; i-- {
|
||||||
if !f.out.buf[i].is_space() { // != `\n` {
|
if !buffer.buf[i].is_space() { // != `\n` {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.out.go_back(f.out.len - i - 1)
|
buffer.go_back(buffer.len - i - 1)
|
||||||
f.empty_line = false
|
f.empty_line = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1883,7 +1888,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
|
||||||
}
|
}
|
||||||
f.stmts(branch.stmts)
|
f.stmts(branch.stmts)
|
||||||
if single_line {
|
if single_line {
|
||||||
f.remove_new_line()
|
f.remove_new_line({})
|
||||||
f.writeln(' }')
|
f.writeln(' }')
|
||||||
} else {
|
} else {
|
||||||
f.writeln('}')
|
f.writeln('}')
|
||||||
|
@ -2181,7 +2186,7 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
|
||||||
single_line_fields = false
|
single_line_fields = false
|
||||||
f.out.go_back_to(fields_start)
|
f.out.go_back_to(fields_start)
|
||||||
f.line_len = fields_start
|
f.line_len = fields_start
|
||||||
f.remove_new_line()
|
f.remove_new_line({})
|
||||||
continue fields_loop
|
continue fields_loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2394,7 +2399,7 @@ pub fn (mut f Fmt) for_c_stmt(node ast.ForCStmt) {
|
||||||
f.expr(node.cond)
|
f.expr(node.cond)
|
||||||
f.write('; ')
|
f.write('; ')
|
||||||
f.stmt(node.inc)
|
f.stmt(node.inc)
|
||||||
f.remove_new_line()
|
f.remove_new_line({})
|
||||||
f.write(' {')
|
f.write(' {')
|
||||||
if node.stmts.len > 0 || node.pos.line_nr < node.pos.last_line {
|
if node.stmts.len > 0 || node.pos.line_nr < node.pos.last_line {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
|
|
|
@ -950,6 +950,8 @@ fn (mut s Scanner) text_scan() token.Token {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if is_separate_line_comment {
|
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
|
comment = '\x01' + comment
|
||||||
}
|
}
|
||||||
return s.new_token(.comment, comment, comment.len + 2)
|
return s.new_token(.comment, comment, comment.len + 2)
|
||||||
|
|
Loading…
Reference in New Issue