internal/lsp: delay the running of gofmt until the test is running
Change-Id: I222c83313a6366367fbeb7ce7d08058968d3a08e Reviewed-on: https://go-review.googlesource.com/c/tools/+/173340 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
2acd0f4c51
commit
18110c573c
|
@ -9,6 +9,8 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -226,8 +228,24 @@ func summarizeCompletionItems(i int, want []source.CompletionItem, got []protoco
|
||||||
|
|
||||||
func (r *runner) Format(t *testing.T, data tests.Formats) {
|
func (r *runner) Format(t *testing.T, data tests.Formats) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
for filename, gofmted := range data {
|
for _, spn := range data {
|
||||||
uri := span.FileURI(filename)
|
uri := spn.URI()
|
||||||
|
filename, err := uri.Filename()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
gofmted := string(r.data.Golden("gofmt", filename, func(golden string) error {
|
||||||
|
cmd := exec.Command("gofmt", filename)
|
||||||
|
stdout, err := os.Create(golden)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer stdout.Close()
|
||||||
|
cmd.Stdout = stdout
|
||||||
|
cmd.Run() // ignore error, sometimes we have intentionally ungofmt-able files
|
||||||
|
return nil
|
||||||
|
}))
|
||||||
|
|
||||||
edits, err := r.server.Formatting(context.Background(), &protocol.DocumentFormattingParams{
|
edits, err := r.server.Formatting(context.Background(), &protocol.DocumentFormattingParams{
|
||||||
TextDocument: protocol.TextDocumentIdentifier{
|
TextDocument: protocol.TextDocumentIdentifier{
|
||||||
URI: protocol.NewURI(uri),
|
URI: protocol.NewURI(uri),
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -50,7 +49,7 @@ var updateGolden = flag.Bool("golden", false, "Update golden files")
|
||||||
type Diagnostics map[span.URI][]source.Diagnostic
|
type Diagnostics map[span.URI][]source.Diagnostic
|
||||||
type CompletionItems map[token.Pos]*source.CompletionItem
|
type CompletionItems map[token.Pos]*source.CompletionItem
|
||||||
type Completions map[span.Span][]token.Pos
|
type Completions map[span.Span][]token.Pos
|
||||||
type Formats map[string]string
|
type Formats []span.Span
|
||||||
type Definitions map[span.Span]Definition
|
type Definitions map[span.Span]Definition
|
||||||
type Highlights map[string][]span.Span
|
type Highlights map[string][]span.Span
|
||||||
type Symbols map[span.URI][]source.Symbol
|
type Symbols map[span.URI][]source.Symbol
|
||||||
|
@ -100,7 +99,6 @@ func Load(t testing.TB, exporter packagestest.Exporter, dir string) *Data {
|
||||||
Diagnostics: make(Diagnostics),
|
Diagnostics: make(Diagnostics),
|
||||||
CompletionItems: make(CompletionItems),
|
CompletionItems: make(CompletionItems),
|
||||||
Completions: make(Completions),
|
Completions: make(Completions),
|
||||||
Formats: make(Formats),
|
|
||||||
Definitions: make(Definitions),
|
Definitions: make(Definitions),
|
||||||
Highlights: make(Highlights),
|
Highlights: make(Highlights),
|
||||||
Symbols: make(Symbols),
|
Symbols: make(Symbols),
|
||||||
|
@ -317,18 +315,8 @@ func (data *Data) collectCompletionItems(pos token.Pos, label, detail, kind stri
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (data *Data) collectFormats(pos token.Position) {
|
func (data *Data) collectFormats(spn span.Span) {
|
||||||
data.Formats[pos.Filename] = string(data.Golden("gofmt", pos.Filename, func(golden string) error {
|
data.Formats = append(data.Formats, spn)
|
||||||
cmd := exec.Command("gofmt", pos.Filename)
|
|
||||||
stdout, err := os.Create(golden)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer stdout.Close()
|
|
||||||
cmd.Stdout = stdout
|
|
||||||
cmd.Run() // ignore error, sometimes we have intentionally ungofmt-able files
|
|
||||||
return nil
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (data *Data) collectDefinitions(src, target span.Span) {
|
func (data *Data) collectDefinitions(src, target span.Span) {
|
||||||
|
|
Loading…
Reference in New Issue