From 5f0509a351b3ac3ece0bc07611af34d6886d85d5 Mon Sep 17 00:00:00 2001 From: Chao Xu Date: Tue, 15 Dec 2015 17:24:57 -0800 Subject: [PATCH] add SecondClosestCommentLines to go2idl types.Type --- cmd/libs/go2idl/parser/parse.go | 21 ++++++++------ cmd/libs/go2idl/parser/parse_test.go | 41 ++++++++++++++++++++++++++++ cmd/libs/go2idl/types/types.go | 17 ++++++++++++ 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/cmd/libs/go2idl/parser/parse.go b/cmd/libs/go2idl/parser/parse.go index 0cc02d6f32..44454c2dc1 100644 --- a/cmd/libs/go2idl/parser/parse.go +++ b/cmd/libs/go2idl/parser/parse.go @@ -300,7 +300,13 @@ func (b *Builder) FindTypes() (types.Universe, error) { tn, ok := obj.(*tc.TypeName) if ok { t := b.walkType(u, nil, tn.Type()) - t.CommentLines = b.priorCommentLines(obj.Pos()) + c1 := b.priorCommentLines(obj.Pos(), 1) + t.CommentLines = c1.Text() + if c1 == nil { + t.SecondClosestCommentLines = b.priorCommentLines(obj.Pos(), 2).Text() + } else { + t.SecondClosestCommentLines = b.priorCommentLines(c1.List[0].Slash, 2).Text() + } } tf, ok := obj.(*tc.Func) // We only care about functions, not concrete/abstract methods. @@ -319,14 +325,11 @@ func (b *Builder) FindTypes() (types.Universe, error) { return u, nil } -// if there's a comment on the line before pos, return its text, otherwise "". -func (b *Builder) priorCommentLines(pos token.Pos) string { +// if there's a comment on the line `lines` before pos, return its text, otherwise "". +func (b *Builder) priorCommentLines(pos token.Pos, lines int) *ast.CommentGroup { position := b.fset.Position(pos) - key := fileLine{position.Filename, position.Line - 1} - if c, ok := b.endLineToCommentGroup[key]; ok { - return c.Text() - } - return "" + key := fileLine{position.Filename, position.Line - lines} + return b.endLineToCommentGroup[key] } func tcFuncNameToName(in string) types.Name { @@ -401,7 +404,7 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t Embedded: f.Anonymous(), Tags: t.Tag(i), Type: b.walkType(u, nil, f.Type()), - CommentLines: b.priorCommentLines(f.Pos()), + CommentLines: b.priorCommentLines(f.Pos(), 1).Text(), } out.Members = append(out.Members, m) } diff --git a/cmd/libs/go2idl/parser/parse_test.go b/cmd/libs/go2idl/parser/parse_test.go index 57bfe6582a..c20edd666f 100644 --- a/cmd/libs/go2idl/parser/parse_test.go +++ b/cmd/libs/go2idl/parser/parse_test.go @@ -211,6 +211,47 @@ type Blah struct { } } +func TestParseSecondClosestCommentLines(t *testing.T) { + const fileName = "base/foo/proto/foo.go" + testCases := []struct { + testFile map[string]string + expected string + }{ + { + map[string]string{fileName: `package foo +// Blah's SecondClosestCommentLines. +// Another line. + +// Blah is a test. +// A test, I tell you. +type Blah struct { + a int +} +`}, + "Blah's SecondClosestCommentLines.\nAnother line.\n", + }, + { + map[string]string{fileName: `package foo +// Blah's SecondClosestCommentLines. +// Another line. + +type Blah struct { + a int +} +`}, + "Blah's SecondClosestCommentLines.\nAnother line.\n", + }, + } + for _, test := range testCases { + _, u, o := construct(t, test.testFile, namer.NewPublicNamer(0)) + t.Logf("%#v", o) + blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"}) + if e, a := test.expected, blahT.SecondClosestCommentLines; e != a { + t.Errorf("struct second closest comment wrong, wanted %v, got %v", e, a) + } + } +} + func TestTypeKindParse(t *testing.T) { var testFiles = map[string]string{ "a/foo.go": "package a\ntype Test string\n", diff --git a/cmd/libs/go2idl/types/types.go b/cmd/libs/go2idl/types/types.go index e2420c1027..6aaabe6994 100644 --- a/cmd/libs/go2idl/types/types.go +++ b/cmd/libs/go2idl/types/types.go @@ -231,6 +231,23 @@ type Type struct { // they will be recorded here. CommentLines string + // If there are comment lines preceding the `CommentLines`, they will be + // recorded here. There are two cases: + // --- + // SecondClosestCommentLines + // a blank line + // CommentLines + // type definition + // --- + // + // or + // --- + // SecondClosestCommentLines + // a blank line + // type definition + // --- + SecondClosestCommentLines string + // If Kind == Struct Members []Member