Skip to content

Commit

Permalink
Implement -format=json (for vim-go)
Browse files Browse the repository at this point in the history
related to #1
  • Loading branch information
stapelberg committed Sep 2, 2017
1 parent 6389883 commit ed86514
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
35 changes: 32 additions & 3 deletions expanderr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main

import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -561,6 +562,7 @@ func logic(w io.Writer, buildctx *build.Context, posn string) error {
if _, err := src.Write(b[:subject.Pos()-1]); err != nil {
return err
}
var newEnd int
// Print the replacement
for _, node := range repl {
// Hack: inline comments (e.g. os.Remove("/foo" /* path */)) get lost
Expand All @@ -574,6 +576,8 @@ func logic(w io.Writer, buildctx *build.Context, posn string) error {
return fmt.Errorf("formatting replacement: %v", err)
}

newEnd += len(strings.Split(stmtFmt.String(), "\n"))

ceOrig := string(b[e.ce.Pos()-1 : e.ce.End()-1])
if _, err := src.Write([]byte(strings.Replace(stmtFmt.String(), ceFmt.String(), ceOrig, 1))); err != nil {
return err
Expand All @@ -595,14 +599,39 @@ func logic(w io.Writer, buildctx *build.Context, posn string) error {
return fmt.Errorf("formatting source: %v.\nsource:\n%s", err, src.String())
}

if _, err := w.Write(formatted); err != nil {
return err
if *formatFlag == "json" {
start := e.fset.Position(subject.Pos()).Line
end := e.fset.Position(subject.End()).Line
var lines []string
for idx, line := range strings.Split(string(formatted), "\n") {
if idx >= start-1 && idx < start-1+newEnd {
lines = append(lines, line)
}
}
if err := json.NewEncoder(w).Encode(struct {
Start int `json:"start"`
End int `json:"end"`
Lines []string `json:"lines"`
}{
Start: start,
End: end,
Lines: lines,
}); err != nil {
return err
}
} else {
if _, err := w.Write(formatted); err != nil {
return err
}
}

return nil
}

var wFlag = flag.String("w", "", "write")
var (
wFlag = flag.String("w", "", "write")
formatFlag = flag.String("format", "", "output format (source, json). defaults to 'source'")
)

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`")

Expand Down
38 changes: 38 additions & 0 deletions expanderr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"bytes"
"encoding/json"
"flag"
"go/build"
"io/ioutil"
"path/filepath"
Expand Down Expand Up @@ -42,6 +44,8 @@ func TestExpand(t *testing.T) {
// Cannot be safely run in parallel as long as build.Default is overridden
// t.Parallel()

flag.Set("format", "source")

wantContents, err := ioutil.ReadFile(strings.Replace(entry.fn, ".got", ".want", 1))
if err != nil {
t.Fatal(err)
Expand All @@ -67,6 +71,40 @@ func TestExpand(t *testing.T) {
if got, want := buf.String(), string(wantContents); got != want {
t.Fatalf("unexpected result: have:\n%s\nwant:\n%s", got, want)
}

// Test the JSON output format as well.

buf.Reset()
flag.Set("format", "json")

gotContents, err := ioutil.ReadFile(entry.fn)
if err != nil {
t.Fatal(err)
}

if err := logic(&buf, &buildctx, entry.fn+entry.posn); err != nil {
t.Fatal(err)
}

var change struct {
Start int `json:"start"`
End int `json:"end"`
Lines []string `json:"lines"`
}
if err := json.Unmarshal(buf.Bytes(), &change); err != nil {
t.Fatal(err)
}

lines := strings.Split(string(gotContents), "\n")

replaced := make([]string, len(lines[:change.Start-1]))
copy(replaced, lines)
replaced = append(replaced, change.Lines...)
replaced = append(replaced, lines[change.End:]...)

if got, want := strings.Join(replaced, "\n"), string(wantContents); got != want {
t.Fatalf("unexpected result: have:\n%s\nwant:\n%s", got, want)
}
})
}
}

0 comments on commit ed86514

Please sign in to comment.