Skip to content

Commit 112a127

Browse files
authored
parser, fmt: fix formatting interface method with pre-comments (#18998)
1 parent 8586f18 commit 112a127

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

vlib/v/fmt/fmt.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,9 +1305,14 @@ pub fn (mut f Fmt) interface_field(field ast.StructField) {
13051305
}
13061306

13071307
pub fn (mut f Fmt) interface_method(method ast.FnDecl) {
1308+
before_comments := method.comments.filter(it.pos.pos < method.pos.pos)
1309+
end_comments := method.comments.filter(it.pos.pos > method.pos.pos)
1310+
if before_comments.len > 0 {
1311+
f.comments(before_comments, level: .indent)
1312+
}
13081313
f.write('\t')
13091314
f.write(method.stringify_fn_decl(f.table, f.cur_mod, f.mod2alias).all_after_first('fn '))
1310-
f.comments(method.comments, inline: true, has_nl: false, level: .indent)
1315+
f.comments(end_comments, inline: true, has_nl: false, level: .indent)
13111316
f.writeln('')
13121317
f.comments(method.next_comments, inline: false, has_nl: true, level: .indent)
13131318
for param in method.params {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module main
2+
3+
fn main() {
4+
}
5+
6+
pub interface IOperateResult {
7+
mut:
8+
// is_success
9+
is_success bool
10+
// is_show_msg
11+
is_show_msg bool
12+
// msg
13+
msg string
14+
// comment before method def
15+
set_msg(msg string) // comment after method def
16+
}

vlib/v/parser/struct.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
646646
p.peek_tok.pos())
647647
return ast.InterfaceDecl{}
648648
}
649+
mut comments := p.eat_comments()
649650
if p.peek_tok.kind == .lpar {
650651
method_start_pos := p.tok.pos()
651652
line_nr := p.tok.line_nr
@@ -687,9 +688,9 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
687688
method.return_type_pos = method.return_type_pos.extend(p.tok.pos())
688689
method.pos = method.pos.extend(method.return_type_pos)
689690
}
690-
mcomments := p.eat_comments(same_line: true)
691+
comments << p.eat_comments(same_line: true)
691692
mnext_comments := p.eat_comments()
692-
method.comments = mcomments
693+
method.comments = comments
693694
method.next_comments = mnext_comments
694695
methods << method
695696
tmethod := ast.Fn{
@@ -706,7 +707,6 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
706707
info.methods << tmethod
707708
} else {
708709
// interface fields
709-
mut comments := p.eat_comments()
710710
field_pos := p.tok.pos()
711711
field_name := p.check_name()
712712
mut type_pos := p.tok.pos()

0 commit comments

Comments
 (0)