Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SecondClosestCommentLines to go2idl types.Type #18743

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 12 additions & 9 deletions cmd/libs/go2idl/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
41 changes: 41 additions & 0 deletions cmd/libs/go2idl/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions cmd/libs/go2idl/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down