diff options
author | Agniva De Sarker <agnivade@yahoo.co.in> | 2019-02-04 13:01:11 +0530 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2019-03-06 06:05:15 +0000 |
commit | 43e8fd4ef1ae24f1505bd34708fc30aa2b736c52 (patch) | |
tree | a76ab2d8046878bd417f7c98d99a97dd7b5af3e7 /src/go/parser/parser.go | |
parent | 6dde1fd792ba4ce41fcce50fc1c66b38666d6924 (diff) | |
download | go-43e8fd4ef1ae24f1505bd34708fc30aa2b736c52.tar.gz go-43e8fd4ef1ae24f1505bd34708fc30aa2b736c52.zip |
go/parser: include more comments in a struct or interface
While parsing inside a struct or an interface, skipping over empty lines
too to collect the next group of comments. We do not need to skip
over more than 1 empty line since gofmt already removes multiple
empty consecutive lines.
Fixes #10858
Change-Id: I0c97b65b5fc44e225e5dc7871ace24f43419ce08
Reviewed-on: https://go-review.googlesource.com/c/go/+/161177
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/go/parser/parser.go')
-rw-r--r-- | src/go/parser/parser.go | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go index ba16b65224..9294bb6b3e 100644 --- a/src/go/parser/parser.go +++ b/src/go/parser/parser.go @@ -63,6 +63,7 @@ type parser struct { topScope *ast.Scope // top-most scope; may be pkgScope unresolved []*ast.Ident // unresolved identifiers imports []*ast.ImportSpec // list of imports + inStruct bool // if set, parser is parsing a struct or interface (for comment collection) // Label scopes // (maintained by open/close LabelScope) @@ -337,7 +338,15 @@ func (p *parser) next() { // consume successor comments, if any endline = -1 for p.tok == token.COMMENT { - comment, endline = p.consumeCommentGroup(1) + n := 1 + // When inside a struct (or interface), we don't want to lose comments + // separated from individual field (or method) documentation by empty + // lines. Allow for some white space in this case and collect those + // comments as a group. See issue #10858 for details. + if p.inStruct { + n = 2 + } + comment, endline = p.consumeCommentGroup(n) } if endline+1 == p.file.Line(p.pos) { @@ -748,6 +757,7 @@ func (p *parser) parseStructType() *ast.StructType { } pos := p.expect(token.STRUCT) + p.inStruct = true lbrace := p.expect(token.LBRACE) scope := ast.NewScope(nil) // struct scope var list []*ast.Field @@ -758,6 +768,7 @@ func (p *parser) parseStructType() *ast.StructType { list = append(list, p.parseFieldDecl(scope)) } rbrace := p.expect(token.RBRACE) + p.inStruct = false return &ast.StructType{ Struct: pos, @@ -959,6 +970,7 @@ func (p *parser) parseInterfaceType() *ast.InterfaceType { } pos := p.expect(token.INTERFACE) + p.inStruct = true lbrace := p.expect(token.LBRACE) scope := ast.NewScope(nil) // interface scope var list []*ast.Field @@ -966,6 +978,7 @@ func (p *parser) parseInterfaceType() *ast.InterfaceType { list = append(list, p.parseMethodSpec(scope)) } rbrace := p.expect(token.RBRACE) + p.inStruct = false return &ast.InterfaceType{ Interface: pos, |