forked from jstemmer/gotags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag.go
88 lines (76 loc) · 1.6 KB
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
)
// Tag represents a single tag.
type Tag struct {
Name string
File string
Address string
Type TagType
Fields map[TagField]string
}
type TagField string
const (
// Tag fields
Access TagField = "access"
Signature TagField = "signature"
TypeField TagField = "type"
ReceiverType TagField = "ctype"
Line TagField = "line"
InterfaceType TagField = "ntype"
)
type TagType string
const (
// Tag types
Package TagType = "p"
Import TagType = "i"
Constant TagType = "c"
Variable TagType = "v"
Type TagType = "t"
Interface TagType = "n"
Field TagType = "w"
Embedded TagType = "e"
Method TagType = "m"
Constructor TagType = "r"
Function TagType = "f"
)
// NewTag creates a new Tag.
func NewTag(name, file string, line int, tagType TagType) Tag {
l := strconv.Itoa(line)
return Tag{
Name: name,
File: file,
Address: l,
Type: tagType,
Fields: map[TagField]string{Line: l},
}
}
// The tags file format string representation of this tag.
func (t Tag) String() string {
var b bytes.Buffer
b.WriteString(t.Name)
b.WriteByte('\t')
b.WriteString(t.File)
b.WriteByte('\t')
b.WriteString(t.Address)
b.WriteString(";\"\t")
b.WriteString(string(t.Type))
b.WriteByte('\t')
fields := make([]string, 0, len(t.Fields))
i := 0
for k, v := range t.Fields {
if len(v) == 0 {
continue
}
fields = append(fields, fmt.Sprintf("%s:%s", k, v))
i++
}
sort.Sort(sort.StringSlice(fields))
b.WriteString(strings.Join(fields, "\t"))
return b.String()
}