-
Notifications
You must be signed in to change notification settings - Fork 658
/
config.go
240 lines (214 loc) · 7.62 KB
/
config.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"regexp"
"strconv"
)
//-------------------------------------------------------------------------
// config
//
// Structure represents persistent config storage of the gocode daemon. Usually
// the config is located somewhere in ~/.config/gocode directory.
//-------------------------------------------------------------------------
type config struct {
ProposeBuiltins bool `json:"propose-builtins"`
LibPath string `json:"lib-path"`
CustomPkgPrefix string `json:"custom-pkg-prefix"`
CustomVendorDir string `json:"custom-vendor-dir"`
Autobuild bool `json:"autobuild"`
ForceDebugOutput string `json:"force-debug-output"`
PackageLookupMode string `json:"package-lookup-mode"`
CloseTimeout int `json:"close-timeout"`
UnimportedPackages bool `json:"unimported-packages"`
Partials bool `json:"partials"`
IgnoreCase bool `json:"ignore-case"`
ClassFiltering bool `json:"class-filtering"`
}
var g_config_desc = map[string]string{
"propose-builtins": "If set to {true}, gocode will add built-in types, functions and constants to autocompletion proposals.",
"lib-path": "A string option. Allows you to add search paths for packages. By default, gocode only searches {$GOPATH/pkg/$GOOS_$GOARCH} and {$GOROOT/pkg/$GOOS_$GOARCH} in terms of previously existed environment variables. Also you can specify multiple paths using ':' (colon) as a separator (on Windows use semicolon ';'). The paths specified by {lib-path} are prepended to the default ones.",
"custom-pkg-prefix": "",
"custom-vendor-dir": "",
"autobuild": "If set to {true}, gocode will try to automatically build out-of-date packages when their source files are modified, in order to obtain the freshest autocomplete results for them. This feature is experimental.",
"force-debug-output": "If is not empty, gocode will forcefully redirect the logging into that file. Also forces enabling of the debug mode on the server side.",
"package-lookup-mode": "If set to {go}, use standard Go package lookup rules. If set to {gb}, use gb-specific lookup rules. See {https://github.com/constabulary/gb} for details.",
"close-timeout": "If there have been no completion requests after this number of seconds, the gocode process will terminate. Default is 30 minutes.",
"unimported-packages": "If set to {true}, gocode will try to import certain known packages automatically for identifiers which cannot be resolved otherwise. Currently only a limited set of standard library packages is supported.",
"partials": "If set to {false}, gocode will not filter autocompletion results based on entered prefix before the cursor. Instead it will return all available autocompletion results viable for a given context. Whether this option is set to {true} or {false}, gocode will return a valid prefix length for output formats which support it. Setting this option to a non-default value may result in editor misbehaviour.",
"ignore-case": "If set to {true}, gocode will perform case-insensitive matching when doing prefix-based filtering.",
"class-filtering": "Enables or disables gocode's feature where it performs class-based filtering if partial input matches corresponding class keyword: const, var, type, func, package.",
}
var g_default_config = config{
ProposeBuiltins: false,
LibPath: "",
CustomPkgPrefix: "",
Autobuild: false,
ForceDebugOutput: "",
PackageLookupMode: "go",
CloseTimeout: 1800,
UnimportedPackages: false,
Partials: true,
IgnoreCase: false,
ClassFiltering: true,
}
var g_config = g_default_config
var g_string_to_bool = map[string]bool{
"t": true,
"true": true,
"y": true,
"yes": true,
"on": true,
"1": true,
"f": false,
"false": false,
"n": false,
"no": false,
"off": false,
"0": false,
}
func set_value(v reflect.Value, value string) {
switch t := v; t.Kind() {
case reflect.Bool:
v, ok := g_string_to_bool[value]
if ok {
t.SetBool(v)
}
case reflect.String:
t.SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(value, 10, 64)
if err == nil {
t.SetInt(v)
}
case reflect.Float32, reflect.Float64:
v, err := strconv.ParseFloat(value, 64)
if err == nil {
t.SetFloat(v)
}
}
}
func list_value(v reflect.Value, name string, w io.Writer) {
switch t := v; t.Kind() {
case reflect.Bool:
fmt.Fprintf(w, "%s %v\n", name, t.Bool())
case reflect.String:
fmt.Fprintf(w, "%s \"%v\"\n", name, t.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fmt.Fprintf(w, "%s %v\n", name, t.Int())
case reflect.Float32, reflect.Float64:
fmt.Fprintf(w, "%s %v\n", name, t.Float())
}
}
func (this *config) list() string {
str, typ := this.value_and_type()
buf := bytes.NewBuffer(make([]byte, 0, 256))
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
name := typ.Field(i).Tag.Get("json")
list_value(v, name, buf)
}
return buf.String()
}
func (this *config) list_option(name string) string {
str, typ := this.value_and_type()
buf := bytes.NewBuffer(make([]byte, 0, 256))
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
nm := typ.Field(i).Tag.Get("json")
if nm == name {
list_value(v, name, buf)
}
}
return buf.String()
}
func (this *config) set_option(name, value string) string {
str, typ := this.value_and_type()
buf := bytes.NewBuffer(make([]byte, 0, 256))
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
nm := typ.Field(i).Tag.Get("json")
if nm == name {
set_value(v, value)
list_value(v, name, buf)
}
}
this.write()
return buf.String()
}
func (this *config) value_and_type() (reflect.Value, reflect.Type) {
v := reflect.ValueOf(this).Elem()
return v, v.Type()
}
func (this *config) write() error {
data, err := json.Marshal(this)
if err != nil {
return err
}
// make sure config dir exists
dir := config_dir()
if !file_exists(dir) {
os.MkdirAll(dir, 0755)
}
f, err := os.Create(config_file())
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
return err
}
return nil
}
func (this *config) read() error {
data, err := ioutil.ReadFile(config_file())
if err != nil {
return err
}
err = json.Unmarshal(data, this)
if err != nil {
return err
}
return nil
}
func quoted(v interface{}) string {
switch v.(type) {
case string:
return fmt.Sprintf("%q", v)
case int:
return fmt.Sprint(v)
case bool:
return fmt.Sprint(v)
default:
panic("unreachable")
}
}
var descRE = regexp.MustCompile(`{[^}]+}`)
func preprocess_desc(v string) string {
return descRE.ReplaceAllStringFunc(v, func(v string) string {
return color_cyan + v[1:len(v)-1] + color_none
})
}
func (this *config) options() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%sConfig file location%s: %s\n", color_white_bold, color_none, config_file())
dv := reflect.ValueOf(g_default_config)
v, t := this.value_and_type()
for i, n := 0, t.NumField(); i < n; i++ {
f := t.Field(i)
index := f.Index
tag := f.Tag.Get("json")
fmt.Fprintf(&buf, "\n%s%s%s\n", color_yellow_bold, tag, color_none)
fmt.Fprintf(&buf, "%stype%s: %s\n", color_yellow, color_none, f.Type)
fmt.Fprintf(&buf, "%svalue%s: %s\n", color_yellow, color_none, quoted(v.FieldByIndex(index).Interface()))
fmt.Fprintf(&buf, "%sdefault%s: %s\n", color_yellow, color_none, quoted(dv.FieldByIndex(index).Interface()))
fmt.Fprintf(&buf, "%sdescription%s: %s\n", color_yellow, color_none, preprocess_desc(g_config_desc[tag]))
}
return buf.String()
}