Skip to content

Commit b5b6c26

Browse files
committed
Add "ignore-case" and "partials" config options.
Also add new command "gocode options" which prints config options in an extended form (unlike "gocode set").
1 parent 0444ce1 commit b5b6c26

File tree

7 files changed

+126
-6
lines changed

7 files changed

+126
-6
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ You can change all available options using `gocode set` command. The config file
151151

152152
- *propose-builtins*
153153

154-
A boolean option. If **true**, gocode will add built-in types, functions and constants to an autocompletion proposals. Default: **false**.
154+
A boolean option. If **true**, gocode will add built-in types, functions and constants to autocompletion proposals. Default: **false**.
155155

156156
- *lib-path*
157157

@@ -173,6 +173,18 @@ You can change all available options using `gocode set` command. The config file
173173

174174
An integer option. If there have been no completion requests after this number of seconds, the gocode process will terminate. Defaults to 1800 (30 minutes).
175175

176+
- *unimported-packages*
177+
178+
A boolean option. 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 are supported. Default: **false**.
179+
180+
- *partials*
181+
182+
A boolean option. 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. Default: **true**.
183+
184+
- *ignore-case*
185+
186+
A boolean option. If set to true, gocode will perform case-insensitive matching when doing prefix-based filtering. Default: **false**.
187+
176188
### Debugging
177189

178190
If something went wrong, the first thing you may want to do is manually start the gocode daemon with a debug mode enabled and in a separate terminal window. It will show you all the stack traces, panics if any and additional info about autocompletion requests. Shutdown the daemon if it was already started and run a new one explicitly with a debug mode enabled:

autocompletecontext.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"go/ast"
77
"go/parser"
88
"go/token"
9+
"log"
910
"os"
1011
"path/filepath"
1112
"runtime"
@@ -361,9 +362,21 @@ func (c *auto_complete_context) apropos(file []byte, filename string, cursor int
361362
// And we're ready to Go. ;)
362363

363364
b := new_out_buffers(c)
365+
if g_config.IgnoreCase {
366+
if *g_debug {
367+
log.Printf("ignoring case sensitivity")
368+
}
369+
b.ignorecase = true
370+
}
364371

365-
partial := 0
366372
cc, ok := c.deduce_cursor_context(file, cursor)
373+
partial := len(cc.partial)
374+
if !g_config.Partials {
375+
if *g_debug {
376+
log.Printf("not performing partial prefix matching")
377+
}
378+
cc.partial = ""
379+
}
367380
if !ok {
368381
var d *decl
369382
if ident, ok := cc.expr.(*ast.Ident); ok && g_config.UnimportedPackages {
@@ -415,7 +428,6 @@ func (c *auto_complete_context) apropos(file []byte, filename string, cursor int
415428
c.get_candidates_from_decl(cc, class, b)
416429
}
417430
}
418-
partial = len(cc.partial)
419431

420432
if len(b.candidates) == 0 {
421433
return nil, 0

client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func do_client() int {
5050
cmd_drop_cache(client)
5151
case "set":
5252
cmd_set(client)
53+
case "options":
54+
cmd_options(client)
5355
default:
5456
fmt.Printf("unknown argument: %q, try running \"gocode -h\"\n", flag.Arg(0))
5557
return 1
@@ -180,3 +182,7 @@ func cmd_set(c *rpc.Client) {
180182
fmt.Print(client_set(c, flag.Arg(1), flag.Arg(2)))
181183
}
182184
}
185+
186+
func cmd_options(c *rpc.Client) {
187+
fmt.Print(client_options(c, 0))
188+
}

config.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"os"
1010
"reflect"
11+
"regexp"
1112
"strconv"
1213
)
1314

@@ -28,9 +29,25 @@ type config struct {
2829
PackageLookupMode string `json:"package-lookup-mode"`
2930
CloseTimeout int `json:"close-timeout"`
3031
UnimportedPackages bool `json:"unimported-packages"`
32+
Partials bool `json:"partials"`
33+
IgnoreCase bool `json:"ignore-case"`
3134
}
3235

33-
var g_config = config{
36+
var g_config_desc = map[string]string{
37+
"propose-builtins": "If set to {true}, gocode will add built-in types, functions and constants to autocompletion proposals.",
38+
"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.",
39+
"custom-pkg-prefix": "",
40+
"custom-vendor-dir": "",
41+
"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.",
42+
"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.",
43+
"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.",
44+
"close-timeout": "If there have been no completion requests after this number of seconds, the gocode process will terminate. Default is 30 minutes.",
45+
"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.",
46+
"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.",
47+
"ignore-case": "If set to {true}, gocode will perform case-insensitive matching when doing prefix-based filtering.",
48+
}
49+
50+
var g_default_config = config{
3451
ProposeBuiltins: false,
3552
LibPath: "",
3653
CustomPkgPrefix: "",
@@ -39,7 +56,10 @@ var g_config = config{
3956
PackageLookupMode: "go",
4057
CloseTimeout: 1800,
4158
UnimportedPackages: false,
59+
Partials: true,
60+
IgnoreCase: false,
4261
}
62+
var g_config = g_default_config
4363

4464
var g_string_to_bool = map[string]bool{
4565
"t": true,
@@ -175,3 +195,43 @@ func (this *config) read() error {
175195

176196
return nil
177197
}
198+
199+
func quoted(v interface{}) string {
200+
switch v.(type) {
201+
case string:
202+
return fmt.Sprintf("%q", v)
203+
case int:
204+
return fmt.Sprint(v)
205+
case bool:
206+
return fmt.Sprint(v)
207+
default:
208+
panic("unreachable")
209+
}
210+
}
211+
212+
var descRE = regexp.MustCompile(`{[^}]+}`)
213+
214+
func preprocess_desc(v string) string {
215+
return descRE.ReplaceAllStringFunc(v, func(v string) string {
216+
return color_cyan + v[1:len(v)-1] + color_none
217+
})
218+
}
219+
220+
func (this *config) options() string {
221+
var buf bytes.Buffer
222+
fmt.Fprintf(&buf, "%sConfig file location%s: %s\n", color_white_bold, color_none, config_file())
223+
dv := reflect.ValueOf(g_default_config)
224+
v, t := this.value_and_type()
225+
for i, n := 0, t.NumField(); i < n; i++ {
226+
f := t.Field(i)
227+
index := f.Index
228+
tag := f.Tag.Get("json")
229+
fmt.Fprintf(&buf, "\n%s%s%s\n", color_yellow_bold, tag, color_none)
230+
fmt.Fprintf(&buf, "%stype%s: %s\n", color_yellow, color_none, f.Type)
231+
fmt.Fprintf(&buf, "%svalue%s: %s\n", color_yellow, color_none, quoted(v.FieldByIndex(index).Interface()))
232+
fmt.Fprintf(&buf, "%sdefault%s: %s\n", color_yellow, color_none, quoted(dv.FieldByIndex(index).Interface()))
233+
fmt.Fprintf(&buf, "%sdescription%s: %s\n", color_yellow, color_none, preprocess_desc(g_config_desc[tag]))
234+
}
235+
236+
return buf.String()
237+
}

gocode.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ func show_usage() {
4040
"\nCommands:\n"+
4141
" autocomplete [<path>] <offset> main autocompletion command\n"+
4242
" close close the gocode daemon\n"+
43-
" status gocode daemon status report\n"+
4443
" drop-cache drop gocode daemon's cache\n"+
45-
" set [<name> [<value>]] list or set config options\n")
44+
" options list config options (extended)\n"+
45+
" set [<name> [<value>]] list or set config options\n"+
46+
" status gocode daemon status report\n"+
47+
"")
4648
}
4749

4850
func main() {

rpc.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,27 @@ func client_set(cli *rpc.Client, Arg0, Arg1 string) string {
136136
}
137137
return reply.Arg0
138138
}
139+
140+
// wrapper for: server_options
141+
142+
type Args_options struct {
143+
Arg0 int
144+
}
145+
type Reply_options struct {
146+
Arg0 string
147+
}
148+
149+
func (r *RPC) RPC_options(args *Args_options, reply *Reply_options) error {
150+
reply.Arg0 = server_options(args.Arg0)
151+
return nil
152+
}
153+
func client_options(cli *rpc.Client, Arg0 int) string {
154+
var args Args_options
155+
var reply Reply_options
156+
args.Arg0 = Arg0
157+
err := cli.Call("RPC.RPC_options", &args, &reply)
158+
if err != nil {
159+
panic(err)
160+
}
161+
return reply.Arg0
162+
}

server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,7 @@ func server_set(key, value string) string {
241241
g_daemon.drop_cache()
242242
return g_config.set_option(key, value)
243243
}
244+
245+
func server_options(notused int) string {
246+
return g_config.options()
247+
}

0 commit comments

Comments
 (0)