add SecondClosestCommentLines to go2idl types.Type

pull/6/head
Chao Xu 2015-12-15 17:24:57 -08:00
parent 582c84b562
commit 5f0509a351
3 changed files with 70 additions and 9 deletions

View File

@ -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)
}

View File

@ -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",

View File

@ -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