From 47835031850cb3188d5b4b119cc629e393a11ca7 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Mon, 28 Dec 2020 17:14:55 +0100 Subject: [PATCH] fmt: keep interface comments between methods (#7650) --- vlib/v/ast/ast.v | 1 + vlib/v/fmt/fmt.v | 1 + .../interface_declaration_comments_keep.vv | 27 +++++++++++++++++++ vlib/v/parser/struct.v | 4 ++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 4fb613dea7..72f08e71d2 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -312,6 +312,7 @@ pub mut: stmts []Stmt return_type table.Type comments []Comment // comments *after* the header, but *before* `{`; used for InterfaceDecl + next_comments []Comment // coments that are one line after the decl; used for InterfaceDecl source_file &File = 0 scope &Scope } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index d6e2e13abc..93d041188c 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -715,6 +715,7 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) { f.write(method.stringify(f.table, f.cur_mod, f.mod2alias).after('fn ')) f.comments(method.comments, inline: true, has_nl: false, level: .indent) f.writeln('') + f.comments(method.next_comments, inline: false, has_nl: true, level: .indent) } f.writeln('}\n') } diff --git a/vlib/v/fmt/tests/interface_declaration_comments_keep.vv b/vlib/v/fmt/tests/interface_declaration_comments_keep.vv index d4404263db..60c8312c77 100644 --- a/vlib/v/fmt/tests/interface_declaration_comments_keep.vv +++ b/vlib/v/fmt/tests/interface_declaration_comments_keep.vv @@ -2,3 +2,30 @@ pub interface ReaderWriter { read(mut buf []byte) ?int // from Reader write(buf []byte) ?int // from Writer } + +interface Speaker { + // first + speak() string + // between + foo() string + foo2() string + // last +} + +interface Baz { + // first + speak() string + // comment + // more between + foo() string + foo2() string + // last +} + +interface Bar { + speak() string // after + foo() string + speak2() string // also after + // and between + foo2() string +} diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 086284d562..f2fd7a9eef 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -455,8 +455,10 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { if p.tok.kind.is_start_of_type() && p.tok.line_nr == line_nr { method.return_type = p.parse_type() } - mcomments := p.eat_comments() + mcomments := p.eat_line_end_comments() + mnext_comments := p.eat_comments() method.comments = mcomments + method.next_comments = mnext_comments methods << method // println('register method $name') ts.register_method(name: name, params: args, return_type: method.return_type, is_pub: true)