parser/fmt: fix comments in structs/consts
parent
000eaca6be
commit
4e447db883
|
@ -122,7 +122,7 @@ pub struct StructField {
|
||||||
pub:
|
pub:
|
||||||
name string
|
name string
|
||||||
pos token.Position
|
pos token.Position
|
||||||
comment Comment
|
comments []Comment
|
||||||
default_expr Expr
|
default_expr Expr
|
||||||
has_default_expr bool
|
has_default_expr bool
|
||||||
attrs []string
|
attrs []string
|
||||||
|
@ -147,7 +147,7 @@ pub:
|
||||||
pos token.Position
|
pos token.Position
|
||||||
pub mut:
|
pub mut:
|
||||||
typ table.Type
|
typ table.Type
|
||||||
comment Comment
|
comments []Comment
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ConstDecl {
|
pub struct ConstDecl {
|
||||||
|
@ -170,6 +170,7 @@ pub:
|
||||||
language table.Language
|
language table.Language
|
||||||
is_union bool
|
is_union bool
|
||||||
attrs []string
|
attrs []string
|
||||||
|
end_comments []Comment
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InterfaceDecl {
|
pub struct InterfaceDecl {
|
||||||
|
|
|
@ -498,8 +498,16 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||||
f.writeln('$name {')
|
f.writeln('$name {')
|
||||||
mut max := 0
|
mut max := 0
|
||||||
for field in node.fields {
|
for field in node.fields {
|
||||||
if field.name.len > max {
|
end_pos := field.pos.pos + field.pos.len
|
||||||
max = field.name.len
|
mut comments_len := 0 // Length of comments between field name and type
|
||||||
|
for comment in field.comments {
|
||||||
|
if comment.pos.pos >= end_pos { break }
|
||||||
|
if comment.pos.pos > field.pos.pos {
|
||||||
|
comments_len += '/* ${comment.text} */ '.len
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if comments_len + field.name.len > max {
|
||||||
|
max = comments_len + field.name.len
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i, field in node.fields {
|
for i, field in node.fields {
|
||||||
|
@ -510,11 +518,9 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||||
} else if i == node.pub_mut_pos {
|
} else if i == node.pub_mut_pos {
|
||||||
f.writeln('pub mut:')
|
f.writeln('pub mut:')
|
||||||
}
|
}
|
||||||
if field.comment.text != '' && field.comment.pos.line_nr < field.pos.line_nr {
|
end_pos := field.pos.pos + field.pos.len
|
||||||
// Comment on the previous line
|
comments := field.comments
|
||||||
f.write('\t')
|
if comments.len == 0 {
|
||||||
f.comment(field.comment)
|
|
||||||
}
|
|
||||||
f.write('\t$field.name ')
|
f.write('\t$field.name ')
|
||||||
f.write(strings.repeat(` `, max - field.name.len))
|
f.write(strings.repeat(` `, max - field.name.len))
|
||||||
f.write(f.type_to_str(field.typ))
|
f.write(f.type_to_str(field.typ))
|
||||||
|
@ -525,17 +531,49 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||||
f.write(' = ')
|
f.write(' = ')
|
||||||
f.struct_field_expr(field.default_expr)
|
f.struct_field_expr(field.default_expr)
|
||||||
}
|
}
|
||||||
// f.write('// $field.pos.line_nr')
|
f.write('\n')
|
||||||
if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr {
|
continue
|
||||||
// Same line comment
|
|
||||||
f.write(' ')
|
|
||||||
f.comment(field.comment)
|
|
||||||
} else {
|
|
||||||
// if field.comment.text != '' {
|
|
||||||
// f.write (' // com linenr=$field.comment.pos.line_nr')
|
|
||||||
// }
|
|
||||||
f.writeln('')
|
|
||||||
}
|
}
|
||||||
|
// Handle comments before field
|
||||||
|
mut j := 0
|
||||||
|
for j < comments.len && comments[j].pos.pos < field.pos.pos {
|
||||||
|
f.indent++
|
||||||
|
f.empty_line = true
|
||||||
|
f.comment(comments[j])
|
||||||
|
f.indent--
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
f.write('\t$field.name ')
|
||||||
|
// Handle comments between field name and type
|
||||||
|
mut comments_len := 0
|
||||||
|
for j < comments.len && comments[j].pos.pos < end_pos {
|
||||||
|
comment := '/* ${comments[j].text} */ ' // TODO: handle in a function
|
||||||
|
comments_len += comment.len
|
||||||
|
f.write(comment)
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
f.write(strings.repeat(` `, max - field.name.len - comments_len))
|
||||||
|
f.write(f.type_to_str(field.typ))
|
||||||
|
if field.attrs.len > 0 {
|
||||||
|
f.write(' [' + field.attrs.join(';') + ']')
|
||||||
|
}
|
||||||
|
if field.has_default_expr {
|
||||||
|
f.write(' = ')
|
||||||
|
f.struct_field_expr(field.default_expr)
|
||||||
|
}
|
||||||
|
// Handle comments after field type (same line)
|
||||||
|
for j < comments.len && field.pos.line_nr == comments[j].pos.line_nr{
|
||||||
|
f.write(' // ${comments[j].text}') // TODO: handle in a function
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
f.write('\n')
|
||||||
|
}
|
||||||
|
// Handle comments after last field
|
||||||
|
for comment in node.end_comments {
|
||||||
|
f.indent++
|
||||||
|
f.empty_line = true
|
||||||
|
f.comment(comment)
|
||||||
|
f.indent--
|
||||||
}
|
}
|
||||||
f.writeln('}\n')
|
f.writeln('}\n')
|
||||||
}
|
}
|
||||||
|
@ -1359,9 +1397,11 @@ pub fn (mut f Fmt) const_decl(it ast.ConstDecl) {
|
||||||
}
|
}
|
||||||
f.indent++
|
f.indent++
|
||||||
for field in it.fields {
|
for field in it.fields {
|
||||||
if field.comment.text != '' {
|
comments := field.comments
|
||||||
f.comment(field.comment)
|
mut j := 0
|
||||||
// f.writeln('// ' + field.comment.text)
|
for j < comments.len && comments[j].pos.pos < field.pos.pos {
|
||||||
|
f.comment(comments[j])
|
||||||
|
j++
|
||||||
}
|
}
|
||||||
name := field.name.after('.')
|
name := field.name.after('.')
|
||||||
f.write('$name ')
|
f.write('$name ')
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
const (
|
const (
|
||||||
|
// pi
|
||||||
// pi
|
// pi
|
||||||
pi = 3.14
|
pi = 3.14
|
||||||
|
// phi
|
||||||
|
// phi
|
||||||
|
// phi
|
||||||
phi = 1.618
|
phi = 1.618
|
||||||
// Euler's constant
|
// Euler's constant
|
||||||
eulers = 2.7182
|
eulers = 2.7182
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
const (
|
const (
|
||||||
// pi
|
// pi
|
||||||
|
// pi
|
||||||
pi=3.14
|
pi=3.14
|
||||||
|
// phi
|
||||||
|
// phi
|
||||||
|
// phi
|
||||||
phi=1.618
|
phi=1.618
|
||||||
//Euler's constant
|
//Euler's constant
|
||||||
eulers=2.7182
|
eulers=2.7182
|
||||||
|
|
|
@ -22,3 +22,22 @@ fn new_user() User {
|
||||||
age: 19
|
age: 19
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct SomeStruct {
|
||||||
|
mut:
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
|
somefield /* 4 */ /* 5 */ int // 6 // 7 // 8
|
||||||
|
/*
|
||||||
|
9
|
||||||
|
10
|
||||||
|
*/
|
||||||
|
somefield2 /* 11 */ int // 12
|
||||||
|
pub:
|
||||||
|
somefield3 int
|
||||||
|
/*
|
||||||
|
13
|
||||||
|
14
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
|
@ -24,3 +24,21 @@ User
|
||||||
age: 19
|
age: 19
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct SomeStruct {
|
||||||
|
// 1
|
||||||
|
mut:
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
|
somefield /*4*/ /*5*/ int /*6*/ /*7*/ /*8*/ /*
|
||||||
|
9
|
||||||
|
10
|
||||||
|
*/
|
||||||
|
somefield2 /*11*/ int // 12
|
||||||
|
pub:
|
||||||
|
somefield3 int
|
||||||
|
/*
|
||||||
|
13
|
||||||
|
14
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
|
@ -471,6 +471,7 @@ pub fn (mut p Parser) comment() ast.Comment {
|
||||||
p.next()
|
p.next()
|
||||||
// p.next_with_comment()
|
// p.next_with_comment()
|
||||||
return ast.Comment{
|
return ast.Comment{
|
||||||
|
is_multi: text.contains('\n')
|
||||||
text: text
|
text: text
|
||||||
pos: pos
|
pos: pos
|
||||||
}
|
}
|
||||||
|
@ -1328,9 +1329,10 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
|
||||||
p.next() // (
|
p.next() // (
|
||||||
mut fields := []ast.ConstField{}
|
mut fields := []ast.ConstField{}
|
||||||
for p.tok.kind != .rpar {
|
for p.tok.kind != .rpar {
|
||||||
mut comment := ast.Comment{}
|
mut comments := []ast.Comment{}
|
||||||
if p.tok.kind == .comment {
|
for p.tok.kind == .comment {
|
||||||
comment = p.comment()
|
comments << p.comment()
|
||||||
|
if p.tok.kind == .rpar {break}
|
||||||
}
|
}
|
||||||
pos := p.tok.position()
|
pos := p.tok.position()
|
||||||
name := p.check_name()
|
name := p.check_name()
|
||||||
|
@ -1347,7 +1349,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
|
||||||
name: full_name
|
name: full_name
|
||||||
expr: expr
|
expr: expr
|
||||||
pos: pos
|
pos: pos
|
||||||
comment: comment
|
comments: comments
|
||||||
}
|
}
|
||||||
fields << field
|
fields << field
|
||||||
p.global_scope.register(field.name, field)
|
p.global_scope.register(field.name, field)
|
||||||
|
|
|
@ -55,12 +55,18 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||||
mut is_field_mut := false
|
mut is_field_mut := false
|
||||||
mut is_field_pub := false
|
mut is_field_pub := false
|
||||||
mut is_field_global := false
|
mut is_field_global := false
|
||||||
|
mut end_comments := []ast.Comment{}
|
||||||
if !no_body {
|
if !no_body {
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
for p.tok.kind != .rcbr {
|
for p.tok.kind != .rcbr {
|
||||||
mut comment := ast.Comment{}
|
mut comments := []ast.Comment{}
|
||||||
if p.tok.kind == .comment {
|
for p.tok.kind == .comment {
|
||||||
comment = p.comment()
|
comments << p.comment()
|
||||||
|
if p.tok.kind == .rcbr {break}
|
||||||
|
}
|
||||||
|
if p.tok.kind == .rcbr {
|
||||||
|
end_comments = comments
|
||||||
|
break
|
||||||
}
|
}
|
||||||
if p.tok.kind == .key_pub {
|
if p.tok.kind == .key_pub {
|
||||||
p.next()
|
p.next()
|
||||||
|
@ -104,17 +110,43 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||||
is_field_mut = true
|
is_field_mut = true
|
||||||
is_field_global = true
|
is_field_global = true
|
||||||
}
|
}
|
||||||
|
for p.tok.kind == .comment {
|
||||||
|
comments << p.comment()
|
||||||
|
if p.tok.kind == .rcbr {break}
|
||||||
|
}
|
||||||
field_start_pos := p.tok.position()
|
field_start_pos := p.tok.position()
|
||||||
field_name := p.check_name()
|
field_name := p.check_name()
|
||||||
// p.warn('field $field_name')
|
// p.warn('field $field_name')
|
||||||
|
|
||||||
|
for p.tok.kind == .comment {
|
||||||
|
comments << p.comment()
|
||||||
|
if p.tok.kind == .rcbr {break}
|
||||||
|
}
|
||||||
|
|
||||||
|
// println(p.tok.position())
|
||||||
typ := p.parse_type()
|
typ := p.parse_type()
|
||||||
field_pos := field_start_pos.extend(p.tok.position())
|
// field_pos := field_start_pos.extend(p.tok.position())
|
||||||
|
field_pos := token.Position{
|
||||||
|
line_nr: field_start_pos.line_nr
|
||||||
|
pos: field_start_pos.pos
|
||||||
|
len: p.tok.position().pos - field_start_pos.pos
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
if name == '_net_module_s' {
|
if name == '_net_module_s' {
|
||||||
s := p.table.get_type_symbol(typ)
|
s := p.table.get_type_symbol(typ)
|
||||||
println('XXXX' + s.str())
|
println('XXXX' + s.str())
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
// Comments after type (same line)
|
||||||
|
line_pos := field_pos.line_nr
|
||||||
|
for p.tok.kind == .comment && line_pos + 1 == p.tok.line_nr{
|
||||||
|
if p.tok.lit.contains('\n') {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
comments << p.comment()
|
||||||
|
if p.tok.kind == .rcbr {break}
|
||||||
|
}
|
||||||
|
|
||||||
mut attrs := []string{}
|
mut attrs := []string{}
|
||||||
if p.tok.kind == .lsbr {
|
if p.tok.kind == .lsbr {
|
||||||
parsed_attrs := p.attributes(false)
|
parsed_attrs := p.attributes(false)
|
||||||
|
@ -137,15 +169,12 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||||
}
|
}
|
||||||
has_default_expr = true
|
has_default_expr = true
|
||||||
}
|
}
|
||||||
if p.tok.kind == .comment {
|
|
||||||
comment = p.comment()
|
|
||||||
}
|
|
||||||
// TODO merge table and ast Fields?
|
// TODO merge table and ast Fields?
|
||||||
ast_fields << ast.StructField{
|
ast_fields << ast.StructField{
|
||||||
name: field_name
|
name: field_name
|
||||||
pos: field_pos
|
pos: field_pos
|
||||||
typ: typ
|
typ: typ
|
||||||
comment: comment
|
comments: comments
|
||||||
default_expr: default_expr
|
default_expr: default_expr
|
||||||
has_default_expr: has_default_expr
|
has_default_expr: has_default_expr
|
||||||
attrs: attrs
|
attrs: attrs
|
||||||
|
@ -209,6 +238,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||||
language: language
|
language: language
|
||||||
is_union: is_union
|
is_union: is_union
|
||||||
attrs: p.attrs
|
attrs: p.attrs
|
||||||
|
end_comments: end_comments
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue