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

parser, fmt: fix formatting interface method with pre-comments (#18998)

This commit is contained in:
yuyi 2023-07-29 20:40:45 +08:00 committed by GitHub
parent 8586f18383
commit 112a1278bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View File

@ -1305,9 +1305,14 @@ pub fn (mut f Fmt) interface_field(field ast.StructField) {
} }
pub fn (mut f Fmt) interface_method(method ast.FnDecl) { pub fn (mut f Fmt) interface_method(method ast.FnDecl) {
before_comments := method.comments.filter(it.pos.pos < method.pos.pos)
end_comments := method.comments.filter(it.pos.pos > method.pos.pos)
if before_comments.len > 0 {
f.comments(before_comments, level: .indent)
}
f.write('\t') f.write('\t')
f.write(method.stringify_fn_decl(f.table, f.cur_mod, f.mod2alias).all_after_first('fn ')) f.write(method.stringify_fn_decl(f.table, f.cur_mod, f.mod2alias).all_after_first('fn '))
f.comments(method.comments, inline: true, has_nl: false, level: .indent) f.comments(end_comments, inline: true, has_nl: false, level: .indent)
f.writeln('') f.writeln('')
f.comments(method.next_comments, inline: false, has_nl: true, level: .indent) f.comments(method.next_comments, inline: false, has_nl: true, level: .indent)
for param in method.params { for param in method.params {

View File

@ -0,0 +1,16 @@
module main
fn main() {
}
pub interface IOperateResult {
mut:
// is_success
is_success bool
// is_show_msg
is_show_msg bool
// msg
msg string
// comment before method def
set_msg(msg string) // comment after method def
}

View File

@ -646,6 +646,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
p.peek_tok.pos()) p.peek_tok.pos())
return ast.InterfaceDecl{} return ast.InterfaceDecl{}
} }
mut comments := p.eat_comments()
if p.peek_tok.kind == .lpar { if p.peek_tok.kind == .lpar {
method_start_pos := p.tok.pos() method_start_pos := p.tok.pos()
line_nr := p.tok.line_nr line_nr := p.tok.line_nr
@ -687,9 +688,9 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
method.return_type_pos = method.return_type_pos.extend(p.tok.pos()) method.return_type_pos = method.return_type_pos.extend(p.tok.pos())
method.pos = method.pos.extend(method.return_type_pos) method.pos = method.pos.extend(method.return_type_pos)
} }
mcomments := p.eat_comments(same_line: true) comments << p.eat_comments(same_line: true)
mnext_comments := p.eat_comments() mnext_comments := p.eat_comments()
method.comments = mcomments method.comments = comments
method.next_comments = mnext_comments method.next_comments = mnext_comments
methods << method methods << method
tmethod := ast.Fn{ tmethod := ast.Fn{
@ -706,7 +707,6 @@ 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()