Skip to content

Commit

Permalink
Implemented S-Expression formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
phikal authored and stamblerre committed Dec 2, 2019
1 parent eeb2774 commit 939b4a6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/autocomplete_formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ client_set,,func(cli *rpc.Client, Arg0, Arg1 string) string
client_status,,func(cli *rpc.Client, Arg0 int) string
```

## sexp ##
Output in form of S-Expressions. Example:
```
((func "client_auto_complete" "func(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 int, Arg3 gocode_env) (c []candidate, d int)" "gocode")(func "client_close" "func(cli *rpc.Client, Arg0 int) int" "gocode")(func "client_cursor_type_pkg" "func(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 int) (typ, pkg string)" "gocode")(func "client_drop_cache" "func(cli *rpc.Client, Arg0 int) int" "gocode")(func "client_highlight" "func(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 gocode_env) (c []highlight_range, d int)" "gocode")(func "client_set" "func(cli *rpc.Client, Arg0, Arg1 string) string" "gocode")(func "client_status" "func(cli *rpc.Client, Arg0 int) string" "gocode"))
```

## csv ##
Comma-separated values format which has small size. Example:
```csv
Expand Down
2 changes: 1 addition & 1 deletion gocode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var (
g_is_server = flag.Bool("s", false, "run a server instead of a client")
g_cache = flag.Bool("cache", false, "use the cache importer")
g_format = flag.String("f", "nice", "output format (vim | emacs | nice | csv | json)")
g_format = flag.String("f", "nice", "output format (vim | emacs | sexp | nice | csv | json)")
g_input = flag.String("in", "", "use this file instead of stdin input")
g_sock = flag.String("sock", defaultSocketType, "socket type (unix | tcp | none)")
g_addr = flag.String("addr", "127.0.0.1:37373", "address for tcp socket")
Expand Down
9 changes: 9 additions & 0 deletions internal/suggest/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var Formatters = map[string]Formatter{
"csv": csvFormat,
"csv-with-package": csvFormat,
"emacs": emacsFormat,
"sexp": sexpFormat,
"godit": goditFormat,
"json": jsonFormat,
"nice": NiceFormat,
Expand Down Expand Up @@ -71,6 +72,14 @@ func emacsFormat(w io.Writer, candidates []Candidate, num int) {
}
}

func sexpFormat(w io.Writer, candidates []Candidate, num int) {
fmt.Fprint(w, "(")
for _, c := range candidates {
fmt.Fprintf(w, "(%s \"%s\" \"%s\" \"%s\")", c.Class, c.Name, c.Type, c.PkgPath)
}
fmt.Fprint(w, ")")
}

func csvFormat(w io.Writer, candidates []Candidate, num int) {
for _, c := range candidates {
fmt.Fprintf(w, "%s,,%s,,%s,,%s\n", c.Class, c.Name, c.Type, c.PkgPath)
Expand Down
3 changes: 2 additions & 1 deletion internal/suggest/formatters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ client_drop_cache,,func(cli *rpc.Client, Arg0 int) int
client_highlight,,func(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 gocode_env) (c []highlight_range, d int)
client_set,,func(cli *rpc.Client, Arg0, Arg1 string) string
client_status,,func(cli *rpc.Client, Arg0 int) string
`[1:]},
`[1:]}, {"sexp", `
((func "client_auto_complete" "func(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 int, Arg3 gocode_env) (c []candidate, d int)" "gocode")(func "client_close" "func(cli *rpc.Client, Arg0 int) int" "gocode")(func "client_cursor_type_pkg" "func(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 int) (typ, pkg string)" "gocode")(func "client_drop_cache" "func(cli *rpc.Client, Arg0 int) int" "gocode")(func "client_highlight" "func(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 gocode_env) (c []highlight_range, d int)" "gocode")(func "client_set" "func(cli *rpc.Client, Arg0, Arg1 string) string" "gocode")(func "client_status" "func(cli *rpc.Client, Arg0 int) string" "gocode"))`[1:]},
{"csv", `
func,,client_auto_complete,,func(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 int, Arg3 gocode_env) (c []candidate, d int),,gocode
func,,client_close,,func(cli *rpc.Client, Arg0 int) int,,gocode
Expand Down

0 comments on commit 939b4a6

Please sign in to comment.