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) { 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)) 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) before_comments := field.comments.filter(it.pos.pos < field.pos.pos)
between_comments := field.comments[before_comments.len..].filter(it.pos.pos < end_pos) end_comments := field.comments.filter(it.pos.pos > field.pos.pos)
after_type_comments := field.comments[(before_comments.len + between_comments.len)..] if before_comments.len > 0 {
f.comments(before_comments, level: .indent)
}
f.write('\t${field.name} ${ft}') f.write('\t${field.name} ${ft}')
if after_type_comments.len > 0 { if end_comments.len > 0 {
f.comments(after_type_comments, level: .indent) f.comments(end_comments, level: .indent)
} else { } else {
f.writeln('') 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 info.methods << tmethod
} else { } else {
// interface fields // interface fields
mut comments := p.eat_comments()
field_pos := p.tok.pos() field_pos := p.tok.pos()
field_name := p.check_name() field_name := p.check_name()
mut type_pos := p.tok.pos() mut type_pos := p.tok.pos()
field_typ := p.parse_type() field_typ := p.parse_type()
type_pos = type_pos.extend(p.prev_tok.pos()) type_pos = type_pos.extend(p.prev_tok.pos())
mut comments := []ast.Comment{} comments << p.eat_comments(same_line: true)
for p.tok.kind == .comment {
comments << p.comment()
if p.tok.kind == .rcbr {
break
}
}
fields << ast.StructField{ fields << ast.StructField{
name: field_name name: field_name
pos: field_pos pos: field_pos