Skip to content

Commit

Permalink
Add --tag-relative flag
Browse files Browse the repository at this point in the history
This allows you, in combination with the -f flag, to specify whether tag
file names should be relative to the output directory.
  • Loading branch information
jstemmer committed Oct 4, 2014
1 parent dd6fc3d commit c4febc3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ gotags is a [ctags][]-compatible tag generator for [Go][].
-f="": write output to specified file. If file is "-", output is written to standard out.
-silent=false: do not produce any output on error.
-sort=true: sort tags.
-tag-relative=false: file paths should be relative to the directory containing the tag file.
-v=false: print version.

## Vim [Tagbar][] configuration
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"sort"
"strings"
Expand All @@ -27,6 +28,7 @@ var (
recurse bool
sortOutput bool
silent bool
relative bool
)

// Initialize flags.
Expand All @@ -37,6 +39,7 @@ func init() {
flag.BoolVar(&recurse, "R", false, "recurse into directories in the file list.")
flag.BoolVar(&sortOutput, "sort", true, "sort tags.")
flag.BoolVar(&silent, "silent", false, "do not produce any output on error.")
flag.BoolVar(&relative, "tag-relative", false, "file paths should be relative to the directory containing the tag file.")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "gotags version %s\n\n", Version)
Expand Down Expand Up @@ -145,7 +148,7 @@ func main() {

tags := []Tag{}
for _, file := range files {
ts, err := Parse(file)
ts, err := Parse(file, relative, path.Dir(outputFile))
if err != nil {
if !silent {
fmt.Fprintf(os.Stderr, "parse error: %s\n\n", err)
Expand Down
34 changes: 25 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@ import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
)

// tagParser contains the data needed while parsing.
type tagParser struct {
fset *token.FileSet
tags []Tag // list of created tags
types []string // all types we encounter, used to determine the constructors
fset *token.FileSet
tags []Tag // list of created tags
types []string // all types we encounter, used to determine the constructors
relative bool // should filenames be relative to basepath
basepath string // output file directory
}

// Parse parses the source in filename and returns a list of tags.
func Parse(filename string) ([]Tag, error) {
// Parse parses the source in filename and returns a list of tags. If relative
// is true, the filenames in the list of tags are relative to basepath.
func Parse(filename string, relative bool, basepath string) ([]Tag, error) {
p := &tagParser{
fset: token.NewFileSet(),
tags: []Tag{},
types: make([]string, 0),
fset: token.NewFileSet(),
tags: []Tag{},
types: make([]string, 0),
relative: relative,
basepath: basepath,
}

f, err := parser.ParseFile(p.fset, filename, nil, 0)
Expand Down Expand Up @@ -199,7 +206,16 @@ func (p *tagParser) parseInterfaceMethods(name string, s *ast.InterfaceType) {

// createTag creates a new tag, using pos to find the filename and set the line number.
func (p *tagParser) createTag(name string, pos token.Pos, tagType TagType) Tag {
return NewTag(name, p.fset.File(pos).Name(), p.fset.Position(pos).Line, tagType)
f := p.fset.File(pos).Name()
if p.relative {
if rel, err := filepath.Rel(p.basepath, f); err != nil {
// log error, but continue
fmt.Fprintf(os.Stderr, "could not determine relative path: %s\n", err)
} else {
f = rel
}
}
return NewTag(name, f, p.fset.Position(pos).Line, tagType)
}

// belongsToReceiver checks if a function with these return types belongs to
Expand Down
11 changes: 9 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type F map[TagField]string

var testCases = []struct {
filename string
relative bool
basepath string
tags []Tag
}{
{filename: "tests/const.go-src", tags: []Tag{
Expand Down Expand Up @@ -82,11 +84,14 @@ var testCases = []struct {
tag("C", 8, "v", F{"access": "public"}),
tag("D", 9, "v", F{"access": "public"}),
}},
{filename: "tests/simple.go-src", relative: true, basepath: "dir", tags: []Tag{
Tag{Name: "main", File: "../tests/simple.go-src", Address: "1", Type: "p", Fields: F{"line": "1"}},
}},
}

func TestParse(t *testing.T) {
for _, testCase := range testCases {
tags, err := Parse(testCase.filename)
tags, err := Parse(testCase.filename, testCase.relative, testCase.basepath)
if err != nil {
t.Errorf("[%s] Parse error: %s", testCase.filename, err)
continue
Expand All @@ -98,7 +103,9 @@ func TestParse(t *testing.T) {
}

for i, tag := range testCase.tags {
tag.File = testCase.filename
if len(tag.File) == 0 {
tag.File = testCase.filename
}
if tags[i].String() != tag.String() {
t.Errorf("[%s] tag(%d)\n is:%s\nwant:%s", testCase.filename, i, tags[i].String(), tag.String())
}
Expand Down
1 change: 1 addition & 0 deletions tests/simple.go-src
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main

0 comments on commit c4febc3

Please sign in to comment.