1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

go: remove unused code (comments)

This commit is contained in:
Alexander Medvednikov 2022-06-28 11:14:40 +03:00
parent ce6bc2c26d
commit 64b8284419
4 changed files with 6 additions and 333 deletions

View File

@ -1,59 +0,0 @@
// Copyright (c) 2019-2022 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 golang
import v.mathutil
const struct_field_align_threshold = 8
struct AlignInfo {
mut:
line_nr int
max_len int
max_type_len int
}
/*
[params]
struct AddInfoConfig {
use_threshold bool
}
fn (mut infos []AlignInfo) add_new_info(len int, type_len int, line int) {
infos << AlignInfo{
line_nr: line
max_len: len
max_type_len: type_len
}
}
[direct_array_access]
fn (mut infos []AlignInfo) add_info(len int, type_len int, line int, cfg AddInfoConfig) {
if infos.len == 0 {
infos.add_new_info(len, type_len, line)
return
}
i := infos.len - 1
if line - infos[i].line_nr > 1 {
infos.add_new_info(len, type_len, line)
return
}
if cfg.use_threshold {
len_diff := mathutil.abs(infos[i].max_len - len) +
mathutil.abs(infos[i].max_type_len - type_len)
if len_diff >= fmt.struct_field_align_threshold {
infos.add_new_info(len, type_len, line)
return
}
}
infos[i].line_nr = line
if len > infos[i].max_len {
infos[i].max_len = len
}
if type_len > infos[i].max_type_len {
infos[i].max_type_len = type_len
}
}
*/

View File

@ -1,142 +0,0 @@
// Copyright (c) 2019-2022 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 golang
import v.ast
pub enum CommentsLevel {
keep
indent
}
// CommentsOptions defines the way comments are going to be written
// - 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)
// - iembed: a /* ... */ block comment used inside expressions; // comments the whole line
// - prev_line: the line number of the previous token to save linebreaks
[minify; params]
pub struct CommentsOptions {
has_nl bool = true
inline bool
level CommentsLevel
iembed bool
prev_line int = -1
}
pub fn (mut f Gen) comment(node ast.Comment, options CommentsOptions) {
// Shebang in .vsh files
if node.text.starts_with('#!') {
f.writeln(node.text)
return
}
if options.level == .indent {
f.indent++
}
if options.iembed {
x := node.text.trim_left('\x01').trim_space()
if x.contains('\n') {
f.writeln('/*')
f.writeln(x)
f.write('*/')
} else {
f.write('/* $x */')
}
} else if !node.text.contains('\n') {
is_separate_line := !options.inline || node.text.starts_with('\x01')
mut s := node.text.trim_left('\x01').trim_right(' ')
mut out_s := '//'
if s != '' {
if is_char_alphanumeric(s[0]) {
out_s += ' '
}
out_s += s
}
if !is_separate_line && f.indent > 0 {
f.remove_new_line() // delete the generated \n
f.write(' ')
}
f.write(out_s)
} else {
lines := node.text.trim_space().split_into_lines()
start_break := is_char_alphanumeric(node.text[0]) || node.text[0].is_space()
end_break := is_char_alphanumeric(node.text.trim('\t').bytes().last())
|| node.text.bytes().last().is_space()
f.write('/*')
if start_break {
f.writeln('')
}
for line in lines {
f.writeln(line.trim_right(' '))
f.empty_line = false
}
if end_break {
f.empty_line = true
} else {
f.remove_new_line()
}
f.write('*/')
}
if options.level == .indent {
f.indent--
}
}
pub fn (mut f Gen) comments(comments []ast.Comment, options CommentsOptions) {
mut prev_line := options.prev_line
for i, c in comments {
if options.prev_line > -1 && ((c.pos.line_nr > prev_line && f.out.last_n(1) != '\n')
|| (c.pos.line_nr > prev_line + 1 && f.out.last_n(2) != '\n\n')) {
f.writeln('')
}
if !f.out.last_n(1)[0].is_space() {
f.write(' ')
}
f.comment(c, options)
if !options.iembed && (i < comments.len - 1 || options.has_nl) {
f.writeln('')
}
prev_line = c.pos.last_line
}
}
pub fn (mut f Gen) comments_before_field(comments []ast.Comment) {
// They behave the same as comments after the last field. This alias is just for clarity.
f.comments_after_last_field(comments)
}
pub fn (mut f Gen) comments_after_last_field(comments []ast.Comment) {
for comment in comments {
f.indent++
f.empty_line = true
f.comment(comment, inline: true)
f.writeln('')
f.indent--
}
}
pub fn (mut f Gen) import_comments(comments []ast.Comment, options CommentsOptions) {
if comments.len == 0 {
return
}
if options.inline {
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 { '' } + '//'
if is_char_alphanumeric(ctext[0]) {
out_s += ' '
}
out_s += ctext
f.out_imports.writeln(out_s)
}
}
fn is_char_alphanumeric(c u8) bool {
return c.is_letter() || c.is_digit()
}

View File

@ -294,8 +294,6 @@ pub fn (mut f Gen) 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 {
@ -541,9 +539,7 @@ pub fn (mut f Gen) expr(node_ ast.Expr) {
ast.CharLiteral {
f.char_literal(node)
}
ast.Comment {
f.comment(node, inline: true)
}
ast.Comment {}
ast.ComptimeCall {
f.comptime_call(node)
}
@ -738,7 +734,6 @@ pub fn (mut f Gen) assign_stmt(node ast.AssignStmt) {
f.write(', ')
}
}
f.comments(node.end_comments, has_nl: false, inline: true, level: .keep)
if !f.single_line_if {
f.writeln('')
}
@ -822,13 +817,6 @@ pub fn (mut f Gen) const_decl(node ast.ConstDecl) {
if i > align_infos[align_idx].last_idx {
align_idx++
}
if field.comments.len > 0 {
if f.should_insert_newline_before_node(ast.Expr(field.comments[0]), prev_field) {
f.writeln('')
}
f.comments(field.comments, inline: true)
prev_field = ast.Expr(field.comments.last())
}
if node.is_block && f.should_insert_newline_before_node(field, prev_field) {
f.writeln('')
}
@ -837,7 +825,6 @@ pub fn (mut f Gen) const_decl(node ast.ConstDecl) {
f.write(strings.repeat(` `, align_infos[align_idx].max - field.name.len))
f.write('= ')
f.expr(field.expr)
f.comments(field.end_comments, inline: true)
if node.is_block && field.end_comments.len == 0 {
f.writeln('')
} else {
@ -846,13 +833,11 @@ pub fn (mut f Gen) const_decl(node ast.ConstDecl) {
if node.end_comments.len > 0 && node.end_comments[0].text.contains('\n') {
f.writeln('\n')
}
f.comments(node.end_comments, inline: true)
}
prev_field = field
}
if node.is_block {
f.comments_after_last_field(node.end_comments)
} else if node.end_comments.len == 0 {
// If no single line comments after the const expr is present
f.writeln('')
@ -875,7 +860,6 @@ pub fn (mut f Gen) defer_stmt(node ast.DeferStmt) {
}
pub fn (mut f Gen) expr_stmt(node ast.ExprStmt) {
f.comments(node.comments)
f.expr(node.expr)
if !f.single_line_if {
f.writeln('')
@ -893,16 +877,13 @@ pub fn (mut f Gen) enum_decl(node ast.EnumDecl) {
return
}
f.writeln('enum $name {')
f.comments(node.comments, inline: true, level: .indent)
for field in node.fields {
f.write('\t$field.name')
if field.has_expr {
f.write(' = ')
f.expr(field.expr)
}
f.comments(field.comments, inline: true, has_nl: false, level: .indent)
f.writeln('')
f.comments(field.next_comments, inline: false, has_nl: true, level: .indent)
}
f.writeln('}\n')
}
@ -1036,7 +1017,6 @@ pub fn (mut f Gen) global_decl(node ast.GlobalDecl) {
}
}
for field in node.fields {
f.comments(field.comments, inline: true)
f.write('$field.name ')
f.write(strings.repeat(` `, max - field.name.len))
if field.has_expr {
@ -1050,7 +1030,6 @@ pub fn (mut f Gen) global_decl(node ast.GlobalDecl) {
}
f.mark_types_import_as_used(field.typ)
}
f.comments_after_last_field(node.end_comments)
if node.is_block {
f.indent--
f.writeln(')')
@ -1090,10 +1069,8 @@ pub fn (mut f Gen) interface_decl(node ast.InterfaceDecl) {
if node.fields.len > 0 || node.methods.len > 0 || node.pos.line_nr < node.pos.last_line {
f.writeln('')
}
f.comments_before_field(node.pre_comments)
for embed in node.embeds {
f.write('\t$embed.name')
f.comments(embed.comments, inline: true, has_nl: false, level: .indent)
f.writeln('')
}
immut_fields := if node.mut_pos < 0 { node.fields } else { node.fields[..node.mut_pos] }
@ -1131,24 +1108,15 @@ pub fn (mut f Gen) interface_decl(node ast.InterfaceDecl) {
pub fn (mut f Gen) interface_field(field ast.StructField) {
mut ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias))
end_pos := field.pos.pos + field.pos.len
before_comments := field.comments.filter(it.pos.pos < field.pos.pos)
between_comments := field.comments[before_comments.len..].filter(it.pos.pos < end_pos)
after_type_comments := field.comments[(before_comments.len + between_comments.len)..]
f.write('\t$field.name $ft')
if after_type_comments.len > 0 {
f.comments(after_type_comments, level: .indent)
} else {
f.writeln('')
}
f.writeln('')
f.mark_types_import_as_used(field.typ)
}
pub fn (mut f Gen) interface_method(method ast.FnDecl) {
f.write('\t')
f.write(method.stringify(f.table, f.cur_mod, f.mod2alias).after('fn '))
f.comments(method.comments, inline: true, has_nl: false, level: .indent)
f.writeln('')
f.comments(method.next_comments, inline: false, has_nl: true, level: .indent)
for param in method.params {
f.mark_types_import_as_used(param.typ)
}
@ -1168,7 +1136,6 @@ pub fn (mut f Gen) module_stmt(mod ast.Module) {
}
pub fn (mut f Gen) return_stmt(node ast.Return) {
f.comments(node.comments)
f.write('return')
if node.exprs.len > 0 {
f.write(' ')
@ -1253,7 +1220,6 @@ pub fn (mut f Gen) alias_type_decl(node ast.AliasTypeDecl) {
ptype := f.table.type_to_str_using_aliases(node.parent_type, f.mod2alias)
f.write('type $node.name = $ptype')
f.comments(node.comments, has_nl: false)
f.mark_types_import_as_used(node.parent_type)
}
@ -1307,7 +1273,6 @@ pub fn (mut f Gen) fn_type_decl(node ast.FnTypeDecl) {
f.write(' !')
}
f.comments(node.comments, has_nl: false)
f.writeln('')
}
@ -1431,7 +1396,6 @@ pub fn (mut f Gen) at_expr(node ast.AtExpr) {
pub fn (mut f Gen) call_expr(node ast.CallExpr) {
for arg in node.args {
f.comments(arg.comments)
}
mut is_method_newline := false
if node.is_method {
@ -1468,7 +1432,6 @@ pub fn (mut f Gen) call_expr(node ast.CallExpr) {
f.call_args(node.args)
f.write(')')
f.or_expr(node.or_block)
f.comments(node.comments, has_nl: false)
if is_method_newline {
f.indent--
}
@ -1659,15 +1622,9 @@ pub fn (mut f Gen) if_expr(node ast.IfExpr) {
for i, branch in node.branches {
if i == 0 {
// first `if`
f.comments(branch.comments)
} else {
// `else`, close previous branch
if branch.comments.len > 0 {
f.writeln('}')
f.comments(branch.comments)
} else {
f.write('} ')
}
f.write('} ')
f.write('${dollar}else ')
}
if i < node.branches.len - 1 || !node.has_else {
@ -1700,15 +1657,11 @@ pub fn (mut f Gen) if_expr(node ast.IfExpr) {
f.single_line_if = false
if node.post_comments.len > 0 {
f.writeln('')
f.comments(node.post_comments,
has_nl: false
prev_line: node.branches.last().body_pos.last_line
)
}
}
fn branch_is_single_line(b ast.IfBranch) bool {
if b.stmts.len == 1 && b.comments.len == 0 && stmt_is_single_line(b.stmts[0])
if b.stmts.len == 1 && stmt_is_single_line(b.stmts[0])
&& b.pos.line_nr == b.stmts[0].pos.line_nr {
return true
}
@ -1918,14 +1871,12 @@ pub fn (mut f Gen) map_init(node ast.MapInit) {
f.write('{}')
} else {
f.writeln('{')
f.comments(node.pre_cmnts, level: .indent)
f.write('}')
}
return
}
f.writeln('{')
f.indent++
f.comments(node.pre_cmnts)
mut max_field_len := 0
mut skeys := []string{}
for key in node.keys {
@ -1946,7 +1897,6 @@ pub fn (mut f Gen) map_init(node ast.MapInit) {
// when the values are struct values, and the code will no longer parse properly
f.write(',')
}
f.comments(node.comments[i], prev_line: node.vals[i].pos().last_line, has_nl: false)
f.writeln('')
}
f.indent--
@ -1962,7 +1912,6 @@ fn (mut f Gen) match_branch(branch ast.MatchBranch, single_line bool) {
f.write(estr)
if j < branch.ecmnts.len && branch.ecmnts[j].len > 0 {
f.write(' ')
f.comments(branch.ecmnts[j], iembed: true)
}
if j < branch.exprs.len - 1 {
f.write(', ')
@ -1989,7 +1938,6 @@ fn (mut f Gen) match_branch(branch ast.MatchBranch, single_line bool) {
f.writeln('}')
}
}
f.comments(branch.post_comments, inline: true)
}
pub fn (mut f Gen) match_expr(node ast.MatchExpr) {
@ -2000,7 +1948,6 @@ pub fn (mut f Gen) match_expr(node ast.MatchExpr) {
}
f.writeln(' {')
f.indent++
f.comments(node.comments)
mut single_line := true
for branch in node.branches {
if branch.stmts.len > 1 || branch.pos.line_nr < branch.pos.last_line {
@ -2139,7 +2086,6 @@ pub fn (mut f Gen) select_expr(node ast.SelectExpr) {
f.indent++
for branch in node.branches {
if branch.comment.text != '' {
f.comment(branch.comment, inline: true)
f.writeln('')
}
if branch.is_else {
@ -2158,9 +2104,6 @@ pub fn (mut f Gen) select_expr(node ast.SelectExpr) {
f.stmts(branch.stmts)
}
f.writeln('}')
if branch.post_comments.len > 0 {
f.comments(branch.post_comments, inline: true)
}
}
f.indent--
f.write('}')

View File

@ -26,9 +26,6 @@ pub fn (mut f Gen) struct_decl(node ast.StructDecl) {
f.writeln(' {}')
return
}
mut field_aligns := []AlignInfo{}
mut comment_aligns := []AlignInfo{}
mut default_expr_aligns := []AlignInfo{}
mut field_types := []string{cap: node.fields.len}
for i, field in node.fields {
ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias))
@ -46,17 +43,7 @@ pub fn (mut f Gen) struct_decl(node ast.StructDecl) {
for embed in node.embeds {
f.mark_types_import_as_used(embed.typ)
styp := f.table.type_to_str_using_aliases(embed.typ, f.mod2alias)
pre_comments := embed.comments.filter(it.pos.pos < embed.pos.pos)
comments := embed.comments[pre_comments.len..]
f.comments_before_field(pre_comments)
if comments.len == 0 {
f.writeln('\t$styp')
} else {
f.write('\t$styp')
f.comments(comments, level: .indent)
}
f.writeln('\t$styp')
}
mut field_align_i := 0
mut comment_align_i := 0
@ -76,33 +63,19 @@ pub fn (mut f Gen) struct_decl(node ast.StructDecl) {
} else if i > 0 {
// keep one empty line between fields (exclude one after mut:, pub:, ...)
mut before_last_line := node.fields[i - 1].pos.line_nr
if node.fields[i - 1].comments.len > 0 {
before_last_line = mu.max(before_last_line, node.fields[i - 1].comments.last().pos.last_line)
}
if node.fields[i - 1].has_default_expr {
before_last_line = mu.max(before_last_line, node.fields[i - 1].default_expr.pos().last_line)
}
mut next_first_line := field.pos.line_nr
if field.comments.len > 0 {
next_first_line = mu.min(next_first_line, field.comments[0].pos.line_nr)
}
if next_first_line - before_last_line > 1 {
f.writeln('')
}
}
end_pos := field.pos.pos + field.pos.len
before_comments := field.comments.filter(it.pos.pos < field.pos.pos)
between_comments := field.comments[before_comments.len..].filter(it.pos.pos < end_pos)
after_type_comments := field.comments[(before_comments.len + between_comments.len)..]
// Handle comments before the field
f.comments_before_field(before_comments)
volatile_prefix := if field.is_volatile { 'volatile ' } else { '' }
f.write('\t$volatile_prefix$field.name ')
// Handle comments between field name and type
before_len := f.line_len
f.comments(between_comments, iembed: true, has_nl: false)
comments_len := f.line_len - before_len
// mut field_align := field_aligns[field_align_i]
// if field_align.line_nr < field.pos.line_nr {
// field_align_i++
@ -118,13 +91,6 @@ pub fn (mut f Gen) struct_decl(node ast.StructDecl) {
f.single_line_attrs(field.attrs, inline: true)
}
if field.has_default_expr {
mut align := default_expr_aligns[default_expr_align_i]
if align.line_nr < field.pos.line_nr {
default_expr_align_i++
align = default_expr_aligns[default_expr_align_i]
}
pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len
f.write(strings.repeat(` `, pad_len))
f.write(' = ')
if !expr_is_single_line(field.default_expr) {
f.indent++
@ -136,28 +102,8 @@ pub fn (mut f Gen) struct_decl(node ast.StructDecl) {
inc_indent = false
}
}
// Handle comments after field type
if after_type_comments.len > 0 {
if after_type_comments[0].pos.line_nr > field.pos.line_nr {
f.writeln('')
} else {
if !field.has_default_expr {
mut align := comment_aligns[comment_align_i]
if align.line_nr < field.pos.line_nr {
comment_align_i++
align = comment_aligns[comment_align_i]
}
pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len
f.write(strings.repeat(` `, pad_len))
}
f.write(' ')
}
f.comments(after_type_comments, level: .indent)
} else {
f.writeln('')
}
f.writeln('')
}
f.comments_after_last_field(node.end_comments)
f.writeln('}\n')
}
@ -183,7 +129,6 @@ pub fn (mut f Gen) struct_init(node ast.StructInit) {
f.write('$name{}')
} else {
f.writeln('$name{')
f.comments(node.pre_comments, inline: true, has_nl: true, level: .indent)
f.write('}')
}
f.mark_import_as_used(name)
@ -232,7 +177,6 @@ pub fn (mut f Gen) struct_init(node ast.StructInit) {
f.writeln('')
f.indent++
}
f.comments(node.pre_comments, inline: true, has_nl: true, level: .keep)
if node.has_update_expr {
f.write('...')
f.expr(node.update_expr)
@ -243,12 +187,10 @@ pub fn (mut f Gen) struct_init(node ast.StructInit) {
} else {
f.writeln('')
}
f.comments(node.update_expr_comments, inline: true, has_nl: true, level: .keep)
}
for i, field in node.fields {
f.write('$field.name: ')
f.expr(field.expr)
f.comments(field.comments, inline: true, has_nl: false, level: .indent)
if single_line_fields {
if i < node.fields.len - 1 {
f.write(', ')
@ -256,17 +198,6 @@ pub fn (mut f Gen) struct_init(node ast.StructInit) {
} else {
f.writeln('')
}
f.comments(field.next_comments, inline: false, has_nl: true, level: .keep)
if single_line_fields && (field.comments.len > 0
|| field.next_comments.len > 0
|| !expr_is_single_line(field.expr)
|| f.line_len > max_len.last()) {
single_line_fields = false
f.out.go_back_to(fields_start)
f.line_len = fields_start
f.remove_new_line()
continue fields_loop
}
}
break
}