internal/lsp: a cleaner way of doing overlays

Change-Id: I3ee053cfc4b63f54b9bf43d0ce6a2bae713e3a0d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/172638
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:
Ian Cottrell 2019-04-17 15:45:43 -04:00
Родитель 780da32332
Коммит afc68fbc60
3 изменённых файлов: 40 добавлений и 27 удалений

Просмотреть файл

@ -7,6 +7,7 @@ package cmd_test
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@ -18,7 +19,7 @@ import (
// We hardcode the expected number of test cases to ensure that all tests
// are being executed. If a test is added, this number must be changed.
const (
expectedCompletionsCount = 64
expectedCompletionsCount = 65
expectedDiagnosticsCount = 16
expectedFormatCount = 4
)
@ -31,16 +32,28 @@ func testCommandLine(t *testing.T, exporter packagestest.Exporter) {
const dir = "../testdata"
files := packagestest.MustCopyFileTree(dir)
overlays := map[string][]byte{}
for fragment, operation := range files {
if trimmed := strings.TrimSuffix(fragment, ".in"); trimmed != fragment {
delete(files, fragment)
files[trimmed] = operation
}
const overlay = ".overlay"
if index := strings.Index(fragment, overlay); index >= 0 {
delete(files, fragment)
partial := fragment[:index] + fragment[index+len(overlay):]
contents, err := ioutil.ReadFile(filepath.Join(dir, fragment))
if err != nil {
t.Fatal(err)
}
overlays[partial] = contents
}
}
modules := []packagestest.Module{
{
Name: "golang.org/x/tools/internal/lsp",
Files: files,
Name: "golang.org/x/tools/internal/lsp",
Files: files,
Overlay: overlays,
},
}
exported := packagestest.Export(t, exporter, modules)

Просмотреть файл

@ -11,6 +11,7 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os/exec"
"path/filepath"
"runtime"
@ -18,7 +19,6 @@ import (
"strings"
"testing"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/packages/packagestest"
"golang.org/x/tools/internal/lsp/cache"
"golang.org/x/tools/internal/lsp/diff"
@ -48,24 +48,34 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) {
const expectedSignaturesCount = 19
files := packagestest.MustCopyFileTree(dir)
overlays := map[string][]byte{}
for fragment, operation := range files {
if trimmed := strings.TrimSuffix(fragment, ".in"); trimmed != fragment {
delete(files, fragment)
files[trimmed] = operation
}
const overlay = ".overlay"
if index := strings.Index(fragment, overlay); index >= 0 {
delete(files, fragment)
partial := fragment[:index] + fragment[index+len(overlay):]
contents, err := ioutil.ReadFile(filepath.Join(dir, fragment))
if err != nil {
t.Fatal(err)
}
overlays[partial] = contents
}
}
modules := []packagestest.Module{
{
Name: "golang.org/x/tools/internal/lsp",
Files: files,
Name: "golang.org/x/tools/internal/lsp",
Files: files,
Overlay: overlays,
},
}
exported := packagestest.Export(t, exporter, modules)
defer exported.Cleanup()
// Merge the exported.Config with the view.Config.
addUnsavedFiles(t, exported.Config, exported)
cfg := *exported.Config
cfg.Fset = token.NewFileSet()
@ -190,25 +200,6 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) {
})
}
func addUnsavedFiles(t *testing.T, cfg *packages.Config, exported *packagestest.Exported) {
if cfg.Overlay == nil {
cfg.Overlay = make(map[string][]byte)
}
// For now, we hardcode a file that we know is in the testdata.
// TODO(rstambler): Figure out a way to do this better.
dir := filepath.Dir(filepath.Dir(exported.File("golang.org/x/tools/internal/lsp", filepath.Join("complit", "complit.go"))))
cfg.Overlay[filepath.Join(dir, "nodisk", "nodisk.go")] = []byte(`package nodisk
import (
"golang.org/x/tools/internal/lsp/foo"
)
func _() {
foo.Foo() //@complete("F", Foo, IntFoo, StructFoo)
}
`)
}
type diagnostics map[span.URI][]protocol.Diagnostic
type completionItems map[token.Pos]*protocol.CompletionItem
type completions map[token.Position][]token.Pos

9
internal/lsp/testdata/nodisk/nodisk.overlay.go поставляемый Normal file
Просмотреть файл

@ -0,0 +1,9 @@
package nodisk
import (
"golang.org/x/tools/internal/lsp/foo"
)
func _() {
foo.Foo() //@complete("F", Foo, IntFoo, StructFoo)
}