Skip to content

Commit 2ad937f

Browse files
committed
Merge pull request #17 from lmars/easytags-compat
Vim easytags compatibility
2 parents 730b5fe + c7de4bc commit 2ad937f

File tree

4 files changed

+107
-14
lines changed

4 files changed

+107
-14
lines changed

fields.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
)
7+
8+
// FieldSet is a set of extension fields to include in a tag.
9+
type FieldSet map[TagField]bool
10+
11+
// Includes tests whether the given field is included in the set.
12+
func (f FieldSet) Includes(field TagField) bool {
13+
b, ok := f[field]
14+
return ok && b
15+
}
16+
17+
// ErrInvalidFields is an error returned when attempting to parse invalid
18+
// fields.
19+
type ErrInvalidFields struct {
20+
Fields string
21+
}
22+
23+
func (e ErrInvalidFields) Error() string {
24+
return fmt.Sprintf("invalid fields: %s", e.Fields)
25+
}
26+
27+
// currently only "+l" is supported
28+
var fieldsPattern = regexp.MustCompile(`^\+l$`)
29+
30+
func parseFields(fields string) (FieldSet, error) {
31+
if fields == "" {
32+
return FieldSet{}, nil
33+
}
34+
if fieldsPattern.MatchString(fields) {
35+
return FieldSet{Language: true}, nil
36+
}
37+
return FieldSet{}, ErrInvalidFields{fields}
38+
}

fields_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestParseFieldsEmpty(t *testing.T) {
8+
_, err := parseFields("")
9+
if err != nil {
10+
t.Fatalf("unexpected error from parseFields: %s", err)
11+
}
12+
}
13+
14+
func TestParseFieldsLanguage(t *testing.T) {
15+
set, err := parseFields("+l")
16+
if err != nil {
17+
t.Fatalf("unexpected error from parseFields: %s", err)
18+
}
19+
if !set.Includes(Language) {
20+
t.Fatal("expected set to include Language")
21+
}
22+
}
23+
24+
func TestParseFieldsInvalid(t *testing.T) {
25+
_, err := parseFields("junk")
26+
if err == nil {
27+
t.Fatal("expected parseFields to return error")
28+
}
29+
if _, ok := err.(ErrInvalidFields); !ok {
30+
t.Fatalf("expected parseFields to return error of type ErrInvalidFields, got %T", err)
31+
}
32+
}

main.go

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,29 @@ var (
2929
sortOutput bool
3030
silent bool
3131
relative bool
32+
listLangs bool
33+
fields string
3234
)
3335

36+
// ignore unknown flags
37+
var flags = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
38+
3439
// Initialize flags.
3540
func init() {
36-
flag.BoolVar(&printVersion, "v", false, "print version.")
37-
flag.StringVar(&inputFile, "L", "", `source file names are read from the specified file. If file is "-", input is read from standard in.`)
38-
flag.StringVar(&outputFile, "f", "", `write output to specified file. If file is "-", output is written to standard out.`)
39-
flag.BoolVar(&recurse, "R", false, "recurse into directories in the file list.")
40-
flag.BoolVar(&sortOutput, "sort", true, "sort tags.")
41-
flag.BoolVar(&silent, "silent", false, "do not produce any output on error.")
42-
flag.BoolVar(&relative, "tag-relative", false, "file paths should be relative to the directory containing the tag file.")
43-
44-
flag.Usage = func() {
41+
flags.BoolVar(&printVersion, "v", false, "print version.")
42+
flags.StringVar(&inputFile, "L", "", `source file names are read from the specified file. If file is "-", input is read from standard in.`)
43+
flags.StringVar(&outputFile, "f", "", `write output to specified file. If file is "-", output is written to standard out.`)
44+
flags.BoolVar(&recurse, "R", false, "recurse into directories in the file list.")
45+
flags.BoolVar(&sortOutput, "sort", true, "sort tags.")
46+
flags.BoolVar(&silent, "silent", false, "do not produce any output on error.")
47+
flags.BoolVar(&relative, "tag-relative", false, "file paths should be relative to the directory containing the tag file.")
48+
flags.BoolVar(&listLangs, "list-languages", false, "list supported languages.")
49+
flags.StringVar(&fields, "fields", "", "include selected extension fields (only +l).")
50+
51+
flags.Usage = func() {
4552
fmt.Fprintf(os.Stderr, "gotags version %s\n\n", Version)
4653
fmt.Fprintf(os.Stderr, "Usage: %s [options] file(s)\n\n", os.Args[0])
47-
flag.PrintDefaults()
54+
flags.PrintDefaults()
4855
}
4956
}
5057

@@ -109,7 +116,7 @@ func readNames(names []string) ([]string, error) {
109116
func getFileNames() ([]string, error) {
110117
var names []string
111118

112-
names = append(names, flag.Args()...)
119+
names = append(names, flags.Args()...)
113120
names, err := readNames(names)
114121
if err != nil {
115122
return nil, err
@@ -126,23 +133,28 @@ func getFileNames() ([]string, error) {
126133
}
127134

128135
func main() {
129-
flag.Parse()
136+
flags.Parse(os.Args[1:])
130137

131138
if printVersion {
132139
fmt.Printf("gotags version %s\n", Version)
133140
return
134141
}
135142

143+
if listLangs {
144+
fmt.Println("Go")
145+
return
146+
}
147+
136148
files, err := getFileNames()
137149
if err != nil {
138150
fmt.Fprintf(os.Stderr, "cannot get specified files\n\n")
139-
flag.Usage()
151+
flags.Usage()
140152
os.Exit(1)
141153
}
142154

143155
if len(files) == 0 && len(inputFile) == 0 {
144156
fmt.Fprintf(os.Stderr, "no file specified\n\n")
145-
flag.Usage()
157+
flags.Usage()
146158
os.Exit(1)
147159
}
148160

@@ -157,6 +169,13 @@ func main() {
157169
}
158170
}
159171

172+
fieldSet, err := parseFields(fields)
173+
if err != nil {
174+
fmt.Fprintf(os.Stderr, "%s\n\n", err)
175+
flags.Usage()
176+
os.Exit(1)
177+
}
178+
160179
tags := []Tag{}
161180
for _, file := range files {
162181
ts, err := Parse(file, relative, basedir)
@@ -171,6 +190,9 @@ func main() {
171190

172191
output := createMetaTags()
173192
for _, tag := range tags {
193+
if fieldSet.Includes(Language) {
194+
tag.Fields[Language] = "Go"
195+
}
174196
output = append(output, tag.String())
175197
}
176198

tag.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
ReceiverType TagField = "ctype"
2929
Line TagField = "line"
3030
InterfaceType TagField = "ntype"
31+
Language TagField = "language"
3132
)
3233

3334
// TagType represents the type of a tag in a tag line.

0 commit comments

Comments
 (0)