fmt: move comments related methods to extra file (#8028)

pull/8107/head^2
Lukas Neubert 2021-01-15 07:51:07 +01:00 committed by GitHub
parent 0ca36aafe2
commit 20c80db426
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 90 deletions

View File

@ -0,0 +1,96 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module fmt
import v.ast
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
// - level: either .keep (don't indent), or .indent (increment indentation)
struct CommentsOptions {
has_nl bool = true
inline bool
level CommentsLevel
iembed bool
}
pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
if node.text.starts_with('#!') {
f.writeln(node.text)
return
}
if options.iembed {
x := node.text.trim_left('\x01')
if x.contains('\n') {
f.writeln('/*')
f.writeln(x.trim_space())
f.write('*/')
} else {
f.write('/* ${x.trim(' ')} */')
}
return
}
if !node.text.contains('\n') {
is_separate_line := !options.inline || node.text.starts_with('\x01')
mut s := node.text.trim_left('\x01')
mut out_s := '//'
if s != '' {
match s[0] {
`a`...`z`, `A`...`Z`, `0`...`9` { out_s += ' ' }
else {}
}
out_s += s
}
if !is_separate_line && f.indent > 0 {
f.remove_new_line() // delete the generated \n
f.write(' ')
}
f.write(out_s)
return
}
lines := node.text.trim_space().split_into_lines()
f.writeln('/*')
for line in lines {
f.writeln(line)
f.empty_line = false
}
f.empty_line = true
f.write('*/')
}
pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) {
for i, c in comments {
if !f.out.last_n(1)[0].is_space() {
f.write(' ')
}
if options.level == .indent {
f.indent++
}
f.comment(c, options)
if i < comments.len - 1 || options.has_nl {
f.writeln('')
}
if options.level == .indent {
f.indent--
}
}
}
pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) {
// Handle comments after last field
for comment in comments {
f.indent++
f.empty_line = true
f.comment(comment, inline: true)
f.writeln('')
f.indent--
}
}

View File

@ -819,17 +819,6 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.writeln('}\n') f.writeln('}\n')
} }
pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) {
// Handle comments after last field
for comment in comments {
f.indent++
f.empty_line = true
f.comment(comment, inline: true)
f.writeln('')
f.indent--
}
}
pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) { pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
if node.is_pub { if node.is_pub {
f.write('pub ') f.write('pub ')
@ -1393,85 +1382,6 @@ fn inline_attrs_len(attrs []table.Attr) int {
return n return n
} }
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
// - level: either .keep (don't indent), or .indent (increment indentation)
struct CommentsOptions {
has_nl bool = true
inline bool
level CommentsLevel
iembed bool
}
pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
if node.text.starts_with('#!') {
f.writeln(node.text)
return
}
if options.iembed {
x := node.text.trim_left('\x01')
if x.contains('\n') {
f.writeln('/*')
f.writeln(x.trim_space())
f.write('*/')
} else {
f.write('/* ${x.trim(' ')} */')
}
return
}
if !node.text.contains('\n') {
is_separate_line := !options.inline || node.text.starts_with('\x01')
mut s := node.text.trim_left('\x01')
mut out_s := '//'
if s != '' {
match s[0] {
`a`...`z`, `A`...`Z`, `0`...`9` { out_s += ' ' }
else {}
}
out_s += s
}
if !is_separate_line && f.indent > 0 {
f.remove_new_line() // delete the generated \n
f.write(' ')
}
f.write(out_s)
return
}
lines := node.text.trim_space().split_into_lines()
f.writeln('/*')
for line in lines {
f.writeln(line)
f.empty_line = false
}
f.empty_line = true
f.write('*/')
}
pub fn (mut f Fmt) comments(comments []ast.Comment, options CommentsOptions) {
for i, c in comments {
if !f.out.last_n(1)[0].is_space() {
f.write(' ')
}
if options.level == .indent {
f.indent++
}
f.comment(c, options)
if i < comments.len - 1 || options.has_nl {
f.writeln('')
}
if options.level == .indent {
f.indent--
}
}
}
pub fn (mut f Fmt) fn_decl(node ast.FnDecl) { pub fn (mut f Fmt) fn_decl(node ast.FnDecl) {
// println('$it.name find_comment($it.pos.line_nr)') // println('$it.name find_comment($it.pos.line_nr)')
// f.find_comment(it.pos.line_nr) // f.find_comment(it.pos.line_nr)