Skip to content

Commit

Permalink
Simplify the formatters
Browse files Browse the repository at this point in the history
Instead of using reflections, just use empty methods
  • Loading branch information
DanielMorsing committed Feb 21, 2012
1 parent 0420489 commit 11d9417
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions gocode.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ func filterOutShebang(data []byte) ([]byte, int) {
// Formatter interfaces
//-------------------------------------------------------------------------

type FormatterEmpty interface {
type Formatter interface {
WriteEmpty()
}

type FormatterCandidates interface {
WriteCandidates(names, types, classes []string, num int)
}

Expand Down Expand Up @@ -106,6 +103,9 @@ func (*VimFormatter) WriteCandidates(names, types, classes []string, num int) {

type EmacsFormatter struct{}

func (*EmacsFormatter) WriteEmpty() {
}

func (*EmacsFormatter) WriteCandidates(names, types, classes []string, num int) {
for i := 0; i < len(names); i++ {
name := names[i]
Expand All @@ -123,6 +123,9 @@ func (*EmacsFormatter) WriteCandidates(names, types, classes []string, num int)

type CSVFormatter struct{}

func (*CSVFormatter) WriteEmpty() {
}

func (*CSVFormatter) WriteCandidates(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])
Expand Down Expand Up @@ -153,7 +156,7 @@ func (*JSONFormatter) WriteCandidates(names, types, classes []string, num int) {

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

func getFormatter() interface{} {
func getFormatter() Formatter {
switch *format {
case "vim":
return new(VimFormatter)
Expand Down Expand Up @@ -260,15 +263,10 @@ func cmdAutoComplete(c *rpc.Client) {
formatter := getFormatter()
names, types, classes, partial := Client_AutoComplete(c, file, filename, cursor)
if names == nil {
if f, ok := formatter.(FormatterEmpty); ok {
f.WriteEmpty()
}
formatter.WriteEmpty()
return
}

if f, ok := formatter.(FormatterCandidates); ok {
f.WriteCandidates(names, types, classes, partial)
}
formatter.WriteCandidates(names, types, classes, partial)
}

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

0 comments on commit 11d9417

Please sign in to comment.