@@ -5,22 +5,29 @@ import (
5
5
"go/ast"
6
6
"go/parser"
7
7
"go/token"
8
+ "os"
9
+ "path/filepath"
8
10
"strings"
9
11
)
10
12
11
13
// tagParser contains the data needed while parsing.
12
14
type tagParser struct {
13
- fset * token.FileSet
14
- tags []Tag // list of created tags
15
- types []string // all types we encounter, used to determine the constructors
15
+ fset * token.FileSet
16
+ tags []Tag // list of created tags
17
+ types []string // all types we encounter, used to determine the constructors
18
+ relative bool // should filenames be relative to basepath
19
+ basepath string // output file directory
16
20
}
17
21
18
- // Parse parses the source in filename and returns a list of tags.
19
- func Parse (filename string ) ([]Tag , error ) {
22
+ // Parse parses the source in filename and returns a list of tags. If relative
23
+ // is true, the filenames in the list of tags are relative to basepath.
24
+ func Parse (filename string , relative bool , basepath string ) ([]Tag , error ) {
20
25
p := & tagParser {
21
- fset : token .NewFileSet (),
22
- tags : []Tag {},
23
- types : make ([]string , 0 ),
26
+ fset : token .NewFileSet (),
27
+ tags : []Tag {},
28
+ types : make ([]string , 0 ),
29
+ relative : relative ,
30
+ basepath : basepath ,
24
31
}
25
32
26
33
f , err := parser .ParseFile (p .fset , filename , nil , 0 )
@@ -199,7 +206,16 @@ func (p *tagParser) parseInterfaceMethods(name string, s *ast.InterfaceType) {
199
206
200
207
// createTag creates a new tag, using pos to find the filename and set the line number.
201
208
func (p * tagParser ) createTag (name string , pos token.Pos , tagType TagType ) Tag {
202
- return NewTag (name , p .fset .File (pos ).Name (), p .fset .Position (pos ).Line , tagType )
209
+ f := p .fset .File (pos ).Name ()
210
+ if p .relative {
211
+ if rel , err := filepath .Rel (p .basepath , f ); err != nil {
212
+ // log error, but continue
213
+ fmt .Fprintf (os .Stderr , "could not determine relative path: %s\n " , err )
214
+ } else {
215
+ f = rel
216
+ }
217
+ }
218
+ return NewTag (name , f , p .fset .Position (pos ).Line , tagType )
203
219
}
204
220
205
221
// belongsToReceiver checks if a function with these return types belongs to
0 commit comments