Skip to content

Commit

Permalink
Move formatters out from gocode.go to a separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsf committed Feb 26, 2012
1 parent df7451c commit 7ae0753
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 136 deletions.
140 changes: 140 additions & 0 deletions formatters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
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'}", word, abbr)
}
fmt.Printf("]]")
}

//-------------------------------------------------------------------------
// emacs_formatter
//-------------------------------------------------------------------------

type emacs_formatter struct{}

func (*emacs_formatter) write_candidates(candidates []candidate, num int) {
for _, c := range candidates {
hint := c.Class.String() + " " + c.Type
if c.Class == decl_func {
hint = 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)
}
}

//-------------------------------------------------------------------------
// 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"}`,
c.Class, c.Name, c.Type)
}
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 "json":
return new(json_formatter)
}
return new(vim_formatter)
}
138 changes: 2 additions & 136 deletions gocode.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
"unicode/utf8"
)
Expand All @@ -34,140 +33,6 @@ func filter_out_shebang(data []byte) ([]byte, int) {
return data, 0
}

//-------------------------------------------------------------------------
// 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'}", word, abbr)
}
fmt.Printf("]]")
}

//-------------------------------------------------------------------------
// emacs_formatter
//-------------------------------------------------------------------------

type emacs_formatter struct{}

func (*emacs_formatter) write_candidates(candidates []candidate, num int) {
for _, c := range candidates {
hint := c.Class.String() + " " + c.Type
if c.Class == decl_func {
hint = 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)
}
}

//-------------------------------------------------------------------------
// 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"}`,
c.Class, c.Name, c.Type)
}
fmt.Print("]]")
}

//-------------------------------------------------------------------------

func get_formatter() formatter {
switch *g_format {
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 "json":
return new(json_formatter)
}
return new(vim_formatter)
}

func get_socket_filename() string {
user := os.Getenv("USER")
if user == "" {
Expand Down Expand Up @@ -255,7 +120,8 @@ func cmd_auto_complete(c *rpc.Client) {
filename = filepath.Join(cwd, filename)
}

get_formatter().write_candidates(client_auto_complete(c, file, filename, cursor))
f := get_formatter(*g_format)
f.write_candidates(client_auto_complete(c, file, filename, cursor))
}

func cmd_close(c *rpc.Client) {
Expand Down

0 comments on commit 7ae0753

Please sign in to comment.