-
Notifications
You must be signed in to change notification settings - Fork 658
/
formatters.go
186 lines (154 loc) · 4.79 KB
/
formatters.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"strings"
)
//-------------------------------------------------------------------------
// formatter interfaces
//-------------------------------------------------------------------------
type formatter interface {
write_candidates(candidates []candidate, num int)
}
//-------------------------------------------------------------------------
// nice_formatter (just for testing, simple textual output)
//-------------------------------------------------------------------------
type nice_formatter struct{}
func (*nice_formatter) write_candidates(candidates []candidate, num int) {
if candidates == nil {
fmt.Printf("Nothing to complete.\n")
return
}
fmt.Printf("Found %d candidates:\n", len(candidates))
for _, c := range candidates {
abbr := fmt.Sprintf("%s %s %s", c.Class, c.Name, c.Type)
if c.Class == decl_func {
abbr = fmt.Sprintf("%s %s%s", c.Class, c.Name, c.Type[len("func"):])
}
fmt.Printf(" %s\n", abbr)
}
}
//-------------------------------------------------------------------------
// vim_formatter
//-------------------------------------------------------------------------
type vim_formatter struct{}
func (*vim_formatter) write_candidates(candidates []candidate, num int) {
if candidates == nil {
fmt.Print("[0, []]")
return
}
fmt.Printf("[%d, [", num)
for i, c := range candidates {
if i != 0 {
fmt.Printf(", ")
}
word := c.Name
if c.Class == decl_func {
word += "("
if strings.HasPrefix(c.Type, "func()") {
word += ")"
}
}
abbr := fmt.Sprintf("%s %s %s", c.Class, c.Name, c.Type)
if c.Class == decl_func {
abbr = fmt.Sprintf("%s %s%s", c.Class, c.Name, c.Type[len("func"):])
}
fmt.Printf("{'word': '%s', 'abbr': '%s', 'info': '%s'}", word, abbr, abbr)
}
fmt.Printf("]]")
}
//-------------------------------------------------------------------------
// godit_formatter
//-------------------------------------------------------------------------
type godit_formatter struct{}
func (*godit_formatter) write_candidates(candidates []candidate, num int) {
fmt.Printf("%d,,%d\n", num, len(candidates))
for _, c := range candidates {
contents := c.Name
if c.Class == decl_func {
contents += "("
if strings.HasPrefix(c.Type, "func()") {
contents += ")"
}
}
display := fmt.Sprintf("%s %s %s", c.Class, c.Name, c.Type)
if c.Class == decl_func {
display = fmt.Sprintf("%s %s%s", c.Class, c.Name, c.Type[len("func"):])
}
fmt.Printf("%s,,%s\n", display, contents)
}
}
//-------------------------------------------------------------------------
// emacs_formatter
//-------------------------------------------------------------------------
type emacs_formatter struct{}
func (*emacs_formatter) write_candidates(candidates []candidate, num int) {
for _, c := range candidates {
var hint string
switch {
case c.Class == decl_func:
hint = c.Type
case c.Type == "":
hint = c.Class.String()
default:
hint = c.Class.String() + " " + c.Type
}
fmt.Printf("%s,,%s\n", c.Name, hint)
}
}
//-------------------------------------------------------------------------
// csv_formatter
//-------------------------------------------------------------------------
type csv_formatter struct{}
func (*csv_formatter) write_candidates(candidates []candidate, num int) {
for _, c := range candidates {
fmt.Printf("%s,,%s,,%s\n", c.Class, c.Name, c.Type)
}
}
//-------------------------------------------------------------------------
// csv_with_package_formatter
//-------------------------------------------------------------------------
type csv_with_package_formatter struct{}
func (*csv_with_package_formatter) write_candidates(candidates []candidate, num int) {
for _, c := range candidates {
fmt.Printf("%s,,%s,,%s,,%s\n", c.Class, c.Name, c.Type, c.Package)
}
}
//-------------------------------------------------------------------------
// json_formatter
//-------------------------------------------------------------------------
type json_formatter struct{}
func (*json_formatter) write_candidates(candidates []candidate, num int) {
if candidates == nil {
fmt.Print("[]")
return
}
fmt.Printf(`[%d, [`, num)
for i, c := range candidates {
if i != 0 {
fmt.Printf(", ")
}
fmt.Printf(`{"class": "%s", "name": "%s", "type": "%s", "package": "%s"}`,
c.Class, c.Name, c.Type, c.Package)
}
fmt.Print("]]")
}
//-------------------------------------------------------------------------
func get_formatter(name string) formatter {
switch name {
case "vim":
return new(vim_formatter)
case "emacs":
return new(emacs_formatter)
case "nice":
return new(nice_formatter)
case "csv":
return new(csv_formatter)
case "csv-with-package":
return new(csv_with_package_formatter)
case "json":
return new(json_formatter)
case "godit":
return new(godit_formatter)
}
return new(nice_formatter)
}