Permalink
Browse files

Structify the communication between daemon and client.

  • Loading branch information...
1 parent fff16d1 commit 4a058ff276d83d3ada96b435d4a00561c0d30361 @nsf committed Feb 26, 2012
Showing with 74 additions and 73 deletions.
  1. +28 −25 autocompletecontext.go
  2. +36 −37 gocode.go
  3. +5 −5 rpc.go
  4. +5 −6 server.go
View
53 autocompletecontext.go
@@ -21,40 +21,43 @@ import (
// Temporary structure for writing autocomplete response.
//-------------------------------------------------------------------------
+// fields must be exported for RPC
+type candidate struct {
+ Name string
+ Type string
+ Class string
+}
+
type out_buffers struct {
- tmpbuf *bytes.Buffer
- names []string
- types []string
- classes []string
- ctx *auto_complete_context
- tmpns map[string]bool
+ tmpbuf *bytes.Buffer
+ candidates []candidate
+ ctx *auto_complete_context
+ tmpns map[string]bool
}
func new_out_buffers(ctx *auto_complete_context) *out_buffers {
b := new(out_buffers)
b.tmpbuf = bytes.NewBuffer(make([]byte, 0, 1024))
- b.names = make([]string, 0, 1024)
- b.types = make([]string, 0, 1024)
- b.classes = make([]string, 0, 1024)
+ b.candidates = make([]candidate, 0, 64)
b.ctx = ctx
return b
}
func (b *out_buffers) Len() int {
- return len(b.names)
+ return len(b.candidates)
}
func (b *out_buffers) Less(i, j int) bool {
- if b.classes[i][0] == b.classes[j][0] {
- return b.names[i] < b.names[j]
+ x := b.candidates[i]
+ y := b.candidates[j]
+ if x.Class[0] == y.Class[0] {
+ return x.Name < y.Name
}
- return b.classes[i] < b.classes[j]
+ return x.Class < y.Class
}
func (b *out_buffers) Swap(i, j int) {
- b.names[i], b.names[j] = b.names[j], b.names[i]
- b.types[i], b.types[j] = b.types[j], b.types[i]
- b.classes[i], b.classes[j] = b.classes[j], b.classes[i]
+ b.candidates[i], b.candidates[j] = b.candidates[j], b.candidates[i]
}
func (b *out_buffers) append_decl(p, name string, decl *decl, class int) {
@@ -68,13 +71,13 @@ func (b *out_buffers) append_decl(p, name string, decl *decl, class int) {
return
}
- b.names = append(b.names, name)
-
decl.pretty_print_type(b.tmpbuf)
- b.types = append(b.types, b.tmpbuf.String())
+ b.candidates = append(b.candidates, candidate{
+ Name: name,
+ Type: b.tmpbuf.String(),
+ Class: decl.class_name(),
+ })
b.tmpbuf.Reset()
-
- b.classes = append(b.classes, decl.class_name())
}
func (b *out_buffers) append_embedded(p string, decl *decl, class int) {
@@ -191,7 +194,7 @@ func (c *auto_complete_context) make_decl_set(scope *scope) map[string]*decl {
// 2. apropos types (pretty-printed)
// 3. apropos classes
// and length of the part that should be replaced (if any)
-func (c *auto_complete_context) apropos(file []byte, filename string, cursor int) ([]string, []string, []string, int) {
+func (c *auto_complete_context) apropos(file []byte, filename string, cursor int) ([]candidate, int) {
c.current.cursor = cursor
c.current.name = filename
@@ -262,12 +265,12 @@ func (c *auto_complete_context) apropos(file []byte, filename string, cursor int
partial = len(da.partial)
}
- if len(b.names) == 0 || len(b.types) == 0 || len(b.classes) == 0 {
- return nil, nil, nil, 0
+ if len(b.candidates) == 0 {
+ return nil, 0
}
sort.Sort(b)
- return b.names, b.types, b.classes, partial
+ return b.candidates, partial
}
func update_packages(ps map[string]*package_file_cache) {
View
73 gocode.go
@@ -40,7 +40,7 @@ func filter_out_shebang(data []byte) ([]byte, int) {
type formatter interface {
write_empty()
- write_candidates(names, types, classes []string, num int)
+ write_candidates(candidates []candidate, num int)
}
//-------------------------------------------------------------------------
@@ -53,12 +53,12 @@ func (*nice_formatter) write_empty() {
fmt.Printf("Nothing to complete.\n")
}
-func (*nice_formatter) write_candidates(names, types, classes []string, num int) {
- fmt.Printf("Found %d candidates:\n", len(names))
- for i := 0; i < len(names); i++ {
- abbr := fmt.Sprintf("%s %s %s", classes[i], names[i], types[i])
- if classes[i] == "func" {
- abbr = fmt.Sprintf("%s %s%s", classes[i], names[i], types[i][len("func"):])
+func (*nice_formatter) write_candidates(candidates []candidate, num int) {
+ 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 == "func" {
+ abbr = fmt.Sprintf("%s %s%s", c.Class, c.Name, c.Type[len("func"):])
}
fmt.Printf(" %s\n", abbr)
}
@@ -74,26 +74,26 @@ func (*vim_formatter) write_empty() {
fmt.Print("[0, []]")
}
-func (*vim_formatter) write_candidates(names, types, classes []string, num int) {
+func (*vim_formatter) write_candidates(candidates []candidate, num int) {
fmt.Printf("[%d, [", num)
- for i := 0; i < len(names); i++ {
- word := names[i]
- if classes[i] == "func" {
+ for i, c := range candidates {
+ if i != 0 {
+ fmt.Printf(", ")
+ }
+
+ word := c.Name
+ if c.Class == "func" {
word += "("
- if strings.HasPrefix(types[i], "func()") {
+ if strings.HasPrefix(c.Type, "func()") {
word += ")"
}
}
- abbr := fmt.Sprintf("%s %s %s", classes[i], names[i], types[i])
- if classes[i] == "func" {
- abbr = fmt.Sprintf("%s %s%s", classes[i], names[i], types[i][len("func"):])
+ abbr := fmt.Sprintf("%s %s %s", c.Class, c.Name, c.Type)
+ if c.Class == "func" {
+ abbr = fmt.Sprintf("%s %s%s", c.Class, c.Name, c.Type[len("func"):])
}
fmt.Printf("{'word': '%s', 'abbr': '%s'}", word, abbr)
- if i != len(names)-1 {
- fmt.Printf(", ")
- }
-
}
fmt.Printf("]]")
}
@@ -107,14 +107,13 @@ type emacs_formatter struct{}
func (*emacs_formatter) write_empty() {
}
-func (*emacs_formatter) write_candidates(names, types, classes []string, num int) {
- for i := 0; i < len(names); i++ {
- name := names[i]
- hint := classes[i] + " " + types[i]
- if classes[i] == "func" {
- hint = types[i]
+func (*emacs_formatter) write_candidates(candidates []candidate, num int) {
+ for _, c := range candidates {
+ hint := c.Class + " " + c.Type
+ if c.Class == "func" {
+ hint = c.Type
}
- fmt.Printf("%s,,%s\n", name, hint)
+ fmt.Printf("%s,,%s\n", c.Name, hint)
}
}
@@ -127,9 +126,9 @@ type csv_formatter struct{}
func (*csv_formatter) write_empty() {
}
-func (*csv_formatter) write_candidates(names, types, classes []string, num int) {
- for i := 0; i < len(names); i++ {
- fmt.Printf("%s,,%s,,%s\n", classes[i], names[i], types[i])
+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)
}
}
@@ -143,14 +142,14 @@ func (*json_formatter) write_empty() {
fmt.Print("[]")
}
-func (*json_formatter) write_candidates(names, types, classes []string, num int) {
+func (*json_formatter) write_candidates(candidates []candidate, num int) {
fmt.Printf(`[%d, [`, num)
- for i := 0; i < len(names); i++ {
- fmt.Printf(`{"class": "%s", "name": "%s", "type": "%s"}`,
- classes[i], names[i], types[i])
- if i != len(names)-1 {
+ for i, c := range candidates {
+ if i != 0 {
fmt.Printf(", ")
}
+ fmt.Printf(`{"class": "%s", "name": "%s", "type": "%s"}`,
+ c.Class, c.Name, c.Type)
}
fmt.Print("]]")
}
@@ -261,12 +260,12 @@ func cmd_auto_complete(c *rpc.Client) {
}
formatter := get_formatter()
- names, types, classes, partial := client_auto_complete(c, file, filename, cursor)
- if names == nil {
+ candidates, partial := client_auto_complete(c, file, filename, cursor)
+ if candidates == nil {
formatter.write_empty()
return
}
- formatter.write_candidates(names, types, classes, partial)
+ formatter.write_candidates(candidates, partial)
}
func cmd_close(c *rpc.Client) {
View
10 rpc.go
@@ -17,15 +17,15 @@ type Args_auto_complete struct {
Arg2 int
}
type Reply_auto_complete struct {
- Arg0, Arg1, Arg2 []string
- Arg3 int
+ Arg0 []candidate
+ Arg1 int
}
func (r *RPC) RPC_auto_complete(args *Args_auto_complete, reply *Reply_auto_complete) error {
- reply.Arg0, reply.Arg1, reply.Arg2, reply.Arg3 = server_auto_complete(args.Arg0, args.Arg1, args.Arg2)
+ reply.Arg0, reply.Arg1 = server_auto_complete(args.Arg0, args.Arg1, args.Arg2)
return nil
}
-func client_auto_complete(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 int) (a, b, c []string, d int) {
+func client_auto_complete(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 int) (c []candidate, d int) {
var args Args_auto_complete
var reply Reply_auto_complete
args.Arg0 = Arg0
@@ -35,7 +35,7 @@ func client_auto_complete(cli *rpc.Client, Arg0 []byte, Arg1 string, Arg2 int) (
if err != nil {
panic(err)
}
- return reply.Arg0, reply.Arg1, reply.Arg2, reply.Arg3
+ return reply.Arg0, reply.Arg1
}
// wrapper for: server_close
View
11 server.go
@@ -107,20 +107,19 @@ func print_backtrace(err interface{}) {
// Corresponding client_* functions are autogenerated by goremote.
//-------------------------------------------------------------------------
-func server_auto_complete(file []byte, filename string, cursor int) (a, b, c []string, d int) {
+func server_auto_complete(file []byte, filename string, cursor int) (c []candidate, d int) {
defer func() {
if err := recover(); err != nil {
print_backtrace(err)
- a = []string{"PANIC"}
- b = a
- c = a
+ c = []candidate{
+ {"PANIC", "PANIC", "PANIC"},
+ }
// drop cache
g_daemon.drop_cache()
}
}()
- a, b, c, d = g_daemon.autocomplete.apropos(file, filename, cursor)
- return
+ return g_daemon.autocomplete.apropos(file, filename, cursor)
}
func server_close(notused int) int {

0 comments on commit 4a058ff

Please sign in to comment.