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

parser, fmt: fix formatting interface fields with pre-comments (fix #18980) (#18988)

This commit is contained in:
yuyi 2023-07-28 16:42:10 +08:00 committed by GitHub
parent e0aba77cc5
commit b25288338c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 12 deletions

View File

@ -1290,13 +1290,14 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
pub fn (mut f Fmt) interface_field(field ast.StructField) {
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)..]
end_comments := field.comments.filter(it.pos.pos > field.pos.pos)
if before_comments.len > 0 {
f.comments(before_comments, level: .indent)
}
f.write('\t${field.name} ${ft}')
if after_type_comments.len > 0 {
f.comments(after_type_comments, level: .indent)
if end_comments.len > 0 {
f.comments(end_comments, level: .indent)
} else {
f.writeln('')
}

View File

@ -0,0 +1,14 @@
module main
fn main() {
}
pub interface IOperateInfo {
mut:
// title
title string
// msg
msg string
// id
id string
}

View File

@ -702,18 +702,13 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
info.methods << tmethod
} else {
// interface fields
mut comments := p.eat_comments()
field_pos := p.tok.pos()
field_name := p.check_name()
mut type_pos := p.tok.pos()
field_typ := p.parse_type()
type_pos = type_pos.extend(p.prev_tok.pos())
mut comments := []ast.Comment{}
for p.tok.kind == .comment {
comments << p.comment()
if p.tok.kind == .rcbr {
break
}
}
comments << p.eat_comments(same_line: true)
fields << ast.StructField{
name: field_name
pos: field_pos