Skip to content

Commit ae093b9

Browse files
committed
move params and returns parsing to comments.go
1 parent 640af08 commit ae093b9

File tree

2 files changed

+82
-78
lines changed

2 files changed

+82
-78
lines changed

comment.go

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"go/ast"
55
"go/token"
6+
"html/template"
7+
"regexp"
68
"strings"
79
)
810

@@ -11,7 +13,30 @@ type AllComments struct {
1113
Comments []*ast.CommentGroup
1214
}
1315

14-
func (a *AllComments) findCommentFor(i int) string {
16+
type CommentStruct struct {
17+
Description string
18+
Params []Param
19+
Returns []Return
20+
}
21+
22+
func IsParamSpec(line string) bool {
23+
matched, err := regexp.MatchString("^ @param", line)
24+
if err != nil {
25+
panic(err)
26+
}
27+
return matched
28+
}
29+
30+
func IsReturnSpec(line string) bool {
31+
matched, err := regexp.MatchString("^ @return", line)
32+
if err != nil {
33+
panic(err)
34+
}
35+
return matched
36+
}
37+
38+
func (a *AllComments) findCommentFor(i int) CommentStruct {
39+
var methodComments CommentStruct
1540
var comments *ast.CommentGroup
1641
var result []string
1742
found := false
@@ -32,9 +57,57 @@ func (a *AllComments) findCommentFor(i int) string {
3257
if found {
3358
for _, comment := range comments.List {
3459
comment.Text = strings.Replace(comment.Text, "//", "", 1)
35-
result = append(result, comment.Text)
60+
if IsParamSpec(comment.Text) {
61+
methodComments.Params = append(methodComments.Params, ExtractParam(comment.Text))
62+
} else if IsReturnSpec(comment.Text) {
63+
methodComments.Returns = append(methodComments.Returns, ExtractReturn(comment.Text))
64+
} else {
65+
result = append(result, comment.Text)
66+
}
3667
}
3768
}
69+
methodComments.Description = strings.Join(result, "\n")
3870

39-
return strings.Join(result, "\n")
71+
return methodComments
72+
}
73+
74+
func ExtractParam(line string) Param {
75+
param := Param{}
76+
words := strings.Split(line, " ")
77+
words = words[1:len(words)]
78+
// fmt.Println(words)
79+
if len(words) > 1 {
80+
// fmt.Println(words[1])
81+
param.Name = words[1]
82+
}
83+
if len(words) > 2 {
84+
// fmt.Println(words[2])
85+
class := words[2]
86+
class = strings.Replace(class, "[", "", 1)
87+
class = strings.Replace(class, "]", "", 1)
88+
param.Class = class
89+
}
90+
if len(words) > 3 {
91+
// fmt.Println(words[3:len(words)])
92+
theRest := strings.Join(words[3:len(words)], " ")
93+
param.Description = template.HTML(theRest)
94+
}
95+
return param
96+
}
97+
98+
func ExtractReturn(line string) Return {
99+
r := Return{}
100+
words := strings.Split(line, " ")
101+
words = words[1:len(words)]
102+
if len(words) > 1 {
103+
class := words[1]
104+
class = strings.Replace(class, "[", "", 1)
105+
class = strings.Replace(class, "]", "", 1)
106+
r.Class = class
107+
}
108+
if len(words) > 2 {
109+
theRest := strings.Join(words[3:len(words)], " ")
110+
r.Description = template.HTML(theRest)
111+
}
112+
return r
40113
}

parser.go

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"go/token"
99
"html/template"
1010
"io/ioutil"
11-
"regexp"
1211
"strings"
1312
)
1413

@@ -73,7 +72,8 @@ func classFromFile(filepath string) Class {
7372
return class
7473
}
7574
// Retrieve class comments
76-
class.Comment = template.HTML(allComments.findCommentFor(class.Line))
75+
comments := allComments.findCommentFor(class.Line)
76+
class.Comment = template.HTML(comments.Description)
7777

7878
// Return class if there is not built-in methods
7979
if methods == nil {
@@ -95,10 +95,10 @@ func classFromFile(filepath string) Class {
9595
method.FnLine = fset.Position(thisExpr.Key.(*ast.Ident).NamePos).Line
9696
}
9797
if name == "Fn" {
98-
comments := allComments.findCommentFor(method.FnLine)
99-
method.Params = ExtractParams(comments)
100-
method.Returns = ExtractReturns(comments)
101-
method.Comment = template.HTML(comments)
98+
methodComments := allComments.findCommentFor(method.FnLine)
99+
method.Params = methodComments.Params
100+
method.Returns = methodComments.Returns
101+
method.Comment = template.HTML(methodComments.Description)
102102
}
103103
}
104104
allMethods = append(allMethods, method)
@@ -119,72 +119,3 @@ func Write(filepath string, classes []Class) {
119119
panic(err)
120120
}
121121
}
122-
123-
func ExtractParams(comments string) []Param {
124-
params := []Param{}
125-
lines := strings.Split(comments, "\n")
126-
for _, line := range lines {
127-
matched, err := regexp.MatchString("^ @param", line)
128-
if err != nil {
129-
panic(err)
130-
}
131-
if matched {
132-
fmt.Println("MATCHED!!!")
133-
fmt.Println(line)
134-
param := Param{}
135-
words := strings.Split(line, " ")
136-
words = words[1:len(words)]
137-
fmt.Println(words)
138-
if len(words) > 1 {
139-
fmt.Println(words[1])
140-
param.Name = words[1]
141-
}
142-
if len(words) > 2 {
143-
fmt.Println(words[2])
144-
class := words[2]
145-
class = strings.Replace(class, "[", "", 1)
146-
class = strings.Replace(class, "]", "", 1)
147-
param.Class = class
148-
}
149-
if len(words) > 3 {
150-
fmt.Println(words[3:len(words)])
151-
theRest := strings.Join(words[3:len(words)], " ")
152-
param.Description = template.HTML(theRest)
153-
}
154-
if param.Name != "" {
155-
params = append(params, param)
156-
}
157-
}
158-
}
159-
return params
160-
}
161-
162-
func ExtractReturns(comments string) []Return {
163-
returns := []Return{}
164-
lines := strings.Split(comments, "\n")
165-
for _, line := range lines {
166-
matched, err := regexp.MatchString("^ @return", line)
167-
if err != nil {
168-
panic(err)
169-
}
170-
if matched {
171-
r := Return{}
172-
words := strings.Split(line, " ")
173-
words = words[1:len(words)]
174-
if len(words) > 1 {
175-
class := words[1]
176-
class = strings.Replace(class, "[", "", 1)
177-
class = strings.Replace(class, "]", "", 1)
178-
r.Class = class
179-
}
180-
if len(words) > 2 {
181-
theRest := strings.Join(words[3:len(words)], " ")
182-
r.Description = template.HTML(theRest)
183-
}
184-
if r.Class != "" {
185-
returns = append(returns, r)
186-
}
187-
}
188-
}
189-
return returns
190-
}

0 commit comments

Comments
 (0)