2020-02-27 11:35:46 -07:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package regtest
|
|
|
|
|
2020-02-28 12:56:20 -07:00
|
|
|
import (
|
|
|
|
"golang.org/x/tools/internal/lsp/fake"
|
2020-04-01 19:31:43 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2020-02-28 12:56:20 -07:00
|
|
|
)
|
2020-02-27 11:35:46 -07:00
|
|
|
|
|
|
|
// RemoveFileFromWorkspace deletes a file on disk but does nothing in the
|
|
|
|
// editor. It calls t.Fatal on any error.
|
|
|
|
func (e *Env) RemoveFileFromWorkspace(name string) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
if err := e.W.RemoveFile(e.Ctx, name); err != nil {
|
|
|
|
e.T.Fatal(err)
|
2020-02-27 11:35:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 15:20:53 -07:00
|
|
|
// ReadWorkspaceFile reads a file from the workspace, calling t.Fatal on any
|
|
|
|
// error.
|
|
|
|
func (e *Env) ReadWorkspaceFile(name string) string {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
2020-02-27 15:20:53 -07:00
|
|
|
content, err := e.W.ReadFile(name)
|
|
|
|
if err != nil {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Fatal(err)
|
2020-02-27 15:20:53 -07:00
|
|
|
}
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2020-02-27 11:35:46 -07:00
|
|
|
// OpenFile opens a file in the editor, calling t.Fatal on any error.
|
|
|
|
func (e *Env) OpenFile(name string) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
if err := e.E.OpenFile(e.Ctx, name); err != nil {
|
|
|
|
e.T.Fatal(err)
|
2020-02-27 11:35:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBuffer creates a buffer in the editor, calling t.Fatal on any error.
|
|
|
|
func (e *Env) CreateBuffer(name string, content string) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
if err := e.E.CreateBuffer(e.Ctx, name, content); err != nil {
|
|
|
|
e.T.Fatal(err)
|
2020-02-27 11:35:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseBuffer closes an editor buffer without saving, calling t.Fatal on any
|
|
|
|
// error.
|
|
|
|
func (e *Env) CloseBuffer(name string) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
if err := e.E.CloseBuffer(e.Ctx, name); err != nil {
|
|
|
|
e.T.Fatal(err)
|
2020-02-27 11:35:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditBuffer applies edits to an editor buffer, calling t.Fatal on any error.
|
|
|
|
func (e *Env) EditBuffer(name string, edits ...fake.Edit) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
if err := e.E.EditBuffer(e.Ctx, name, edits); err != nil {
|
|
|
|
e.T.Fatal(err)
|
2020-02-27 11:35:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 12:56:20 -07:00
|
|
|
// RegexpSearch returns the starting position of the first match for re in the
|
|
|
|
// buffer specified by name, calling t.Fatal on any error. It first searches
|
|
|
|
// for the position in open buffers, then in workspace files.
|
|
|
|
func (e *Env) RegexpSearch(name, re string) fake.Pos {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
2020-02-28 12:56:20 -07:00
|
|
|
pos, err := e.E.RegexpSearch(name, re)
|
|
|
|
if err == fake.ErrUnknownBuffer {
|
|
|
|
pos, err = e.W.RegexpSearch(name, re)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-03-24 16:29:27 -06:00
|
|
|
e.T.Fatalf("RegexpSearch: %v, %v", name, err)
|
2020-02-28 12:56:20 -07:00
|
|
|
}
|
|
|
|
return pos
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegexpReplace replaces the first group in the first match of regexpStr with
|
|
|
|
// the replace text, calling t.Fatal on any error.
|
|
|
|
func (e *Env) RegexpReplace(name, regexpStr, replace string) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
if err := e.E.RegexpReplace(e.Ctx, name, regexpStr, replace); err != nil {
|
|
|
|
e.T.Fatalf("RegexpReplace: %v", err)
|
2020-02-28 12:56:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 11:35:46 -07:00
|
|
|
// SaveBuffer saves an editor buffer, calling t.Fatal on any error.
|
|
|
|
func (e *Env) SaveBuffer(name string) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
if err := e.E.SaveBuffer(e.Ctx, name); err != nil {
|
|
|
|
e.T.Fatal(err)
|
2020-02-27 11:35:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GoToDefinition goes to definition in the editor, calling t.Fatal on any
|
|
|
|
// error.
|
|
|
|
func (e *Env) GoToDefinition(name string, pos fake.Pos) (string, fake.Pos) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
n, p, err := e.E.GoToDefinition(e.Ctx, name, pos)
|
2020-02-27 11:35:46 -07:00
|
|
|
if err != nil {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Fatal(err)
|
2020-02-27 11:35:46 -07:00
|
|
|
}
|
|
|
|
return n, p
|
|
|
|
}
|
|
|
|
|
2020-02-27 15:20:53 -07:00
|
|
|
// FormatBuffer formats the editor buffer, calling t.Fatal on any error.
|
|
|
|
func (e *Env) FormatBuffer(name string) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
if err := e.E.FormatBuffer(e.Ctx, name); err != nil {
|
|
|
|
e.T.Fatal(err)
|
2020-02-27 15:20:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OrganizeImports processes the source.organizeImports codeAction, calling
|
|
|
|
// t.Fatal on any error.
|
|
|
|
func (e *Env) OrganizeImports(name string) {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
if err := e.E.OrganizeImports(e.Ctx, name); err != nil {
|
|
|
|
e.T.Fatal(err)
|
2020-02-27 15:20:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:31:43 -06:00
|
|
|
// ApplyQuickFixes processes the quickfix codeAction, calling t.Fatal on any error.
|
|
|
|
func (e *Env) ApplyQuickFixes(path string, diagnostics []protocol.Diagnostic) {
|
|
|
|
e.T.Helper()
|
|
|
|
if err := e.E.ApplyQuickFixes(e.Ctx, path, diagnostics); err != nil {
|
|
|
|
e.T.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 11:35:46 -07:00
|
|
|
// CloseEditor shuts down the editor, calling t.Fatal on any error.
|
|
|
|
func (e *Env) CloseEditor() {
|
2020-03-23 15:26:05 -06:00
|
|
|
e.T.Helper()
|
|
|
|
if err := e.E.Shutdown(e.Ctx); err != nil {
|
|
|
|
e.T.Fatal(err)
|
2020-02-27 11:35:46 -07:00
|
|
|
}
|
2020-03-23 15:26:05 -06:00
|
|
|
if err := e.E.Exit(e.Ctx); err != nil {
|
|
|
|
e.T.Fatal(err)
|
2020-02-27 11:35:46 -07:00
|
|
|
}
|
|
|
|
}
|