Documentation ¶
Overview ¶
Package fake provides fake implementations of a text editor, LSP client plugin, and Sandbox environment for use in tests.
The Editor type provides a high level API for text editor operations (open/modify/save/close a buffer, jump to definition, etc.), and the Client type exposes an LSP client for the editor that can be connected to a language server. By default, the Editor and Client should be compliant with the LSP spec: their intended use is to verify server compliance with the spec in a variety of environment. Possible future enhancements of these types may allow them to misbehave in configurable ways, but that is not their primary use.
The Sandbox type provides a facility for executing tests with a temporary directory, module proxy, and GOPATH.
Index ¶
- Variables
- func WriteProxy(tmpdir, txt string) (string, error)
- type Client
- func (c *Client) ApplyEdit(ctx context.Context, params *protocol.ApplyWorkspaceEditParams) (*protocol.ApplyWorkspaceEditResponse, error)
- func (c *Client) Configuration(_ context.Context, p *protocol.ParamConfiguration) ([]interface{}, error)
- func (c *Client) Event(ctx context.Context, event *interface{}) error
- func (c *Client) LogMessage(ctx context.Context, params *protocol.LogMessageParams) error
- func (c *Client) Progress(ctx context.Context, params *protocol.ProgressParams) error
- func (c *Client) PublishDiagnostics(ctx context.Context, params *protocol.PublishDiagnosticsParams) error
- func (c *Client) RegisterCapability(ctx context.Context, params *protocol.RegistrationParams) error
- func (c *Client) ShowMessage(ctx context.Context, params *protocol.ShowMessageParams) error
- func (c *Client) ShowMessageRequest(ctx context.Context, params *protocol.ShowMessageRequestParams) (*protocol.MessageActionItem, error)
- func (c *Client) UnregisterCapability(ctx context.Context, params *protocol.UnregistrationParams) error
- func (c *Client) WorkDoneProgressCreate(ctx context.Context, params *protocol.WorkDoneProgressCreateParams) error
- func (c *Client) WorkspaceFolders(context.Context) ([]protocol.WorkspaceFolder, error)
- type ClientHooks
- type Edit
- type Editor
- func (e *Editor) ApplyQuickFixes(ctx context.Context, path string, rng *protocol.Range, ...) error
- func (e *Editor) BufferText(name string) string
- func (e *Editor) BufferVersion(name string) int
- func (e *Editor) Client() *Client
- func (e *Editor) Close(ctx context.Context) error
- func (e *Editor) CloseBuffer(ctx context.Context, path string) error
- func (e *Editor) CodeAction(ctx context.Context, path string, rng *protocol.Range) ([]protocol.CodeAction, error)
- func (e *Editor) CodeLens(ctx context.Context, path string) ([]protocol.CodeLens, error)
- func (e *Editor) Completion(ctx context.Context, path string, pos Pos) (*protocol.CompletionList, error)
- func (e *Editor) Connect(ctx context.Context, conn jsonrpc2.Conn, hooks ClientHooks) (*Editor, error)
- func (e *Editor) CreateBuffer(ctx context.Context, path, content string) error
- func (e *Editor) DocumentLink(ctx context.Context, path string) ([]protocol.DocumentLink, error)
- func (e *Editor) EditBuffer(ctx context.Context, path string, edits []Edit) error
- func (e *Editor) Exit(ctx context.Context) error
- func (e *Editor) FormatBuffer(ctx context.Context, path string) error
- func (e *Editor) GoToDefinition(ctx context.Context, path string, pos Pos) (string, Pos, error)
- func (e *Editor) Hover(ctx context.Context, path string, pos Pos) (*protocol.MarkupContent, Pos, error)
- func (e *Editor) OpenFile(ctx context.Context, path string) error
- func (e *Editor) OpenFileWithContent(ctx context.Context, path, content string) error
- func (e *Editor) OrganizeImports(ctx context.Context, path string) error
- func (e *Editor) RefactorRewrite(ctx context.Context, path string, rng *protocol.Range) error
- func (e *Editor) References(ctx context.Context, path string, pos Pos) ([]protocol.Location, error)
- func (e *Editor) RegexpReplace(ctx context.Context, path, re, replace string) error
- func (e *Editor) RegexpSearch(bufName, re string) (Pos, error)
- func (e *Editor) RunGenerate(ctx context.Context, dir string) error
- func (e *Editor) SaveBuffer(ctx context.Context, path string) error
- func (e *Editor) SaveBufferWithoutActions(ctx context.Context, path string) error
- func (e *Editor) Shutdown(ctx context.Context) error
- func (e *Editor) Symbol(ctx context.Context, query string) ([]SymbolInformation, error)
- type EditorConfig
- type FileEvent
- type Location
- type Pos
- type Range
- type Sandbox
- type SandboxConfig
- type SymbolInformation
- type Workdir
- func (w *Workdir) AddWatcher(watcher func(context.Context, []FileEvent))
- func (w *Workdir) ChangeFilesOnDisk(ctx context.Context, events []FileEvent) error
- func (w *Workdir) CheckForFileChanges(ctx context.Context) error
- func (w *Workdir) ListFiles(dir string) (map[string]time.Time, error)
- func (w *Workdir) ReadFile(path string) (string, error)
- func (w *Workdir) RegexpSearch(path string, re string) (Pos, error)
- func (w *Workdir) RemoveFile(ctx context.Context, path string) error
- func (w *Workdir) RootURI() protocol.DocumentURI
- func (w *Workdir) URI(path string) protocol.DocumentURI
- func (w *Workdir) URIToPath(uri protocol.DocumentURI) string
- func (w *Workdir) WriteFile(ctx context.Context, path, content string) error
- func (w *Workdir) WriteFiles(ctx context.Context, files map[string]string) error
- func (w *Workdir) WriteInitialFiles(txt string) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoMatch = errors.New("no match") ErrUnknownBuffer = errors.New("unknown buffer") )
ErrNoMatch is returned if a regexp search fails.
Functions ¶
func WriteProxy ¶
WriteProxy creates a new proxy file tree using the txtar-encoded content, and returns its URL.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an adapter that converts an *Editor into an LSP Client. It mosly delegates functionality to hooks that can be configured by tests.
func (*Client) ApplyEdit ¶
func (c *Client) ApplyEdit(ctx context.Context, params *protocol.ApplyWorkspaceEditParams) (*protocol.ApplyWorkspaceEditResponse, error)
ApplyEdit applies edits sent from the server. Note that as of writing gopls doesn't use this feature, so it is untested.
func (*Client) Configuration ¶
func (*Client) LogMessage ¶
func (*Client) PublishDiagnostics ¶
func (*Client) RegisterCapability ¶
func (*Client) ShowMessage ¶
func (*Client) ShowMessageRequest ¶
func (c *Client) ShowMessageRequest(ctx context.Context, params *protocol.ShowMessageRequestParams) (*protocol.MessageActionItem, error)
func (*Client) UnregisterCapability ¶
func (*Client) WorkDoneProgressCreate ¶
func (*Client) WorkspaceFolders ¶
type ClientHooks ¶
type ClientHooks struct { OnLogMessage func(context.Context, *protocol.LogMessageParams) error OnDiagnostics func(context.Context, *protocol.PublishDiagnosticsParams) error OnWorkDoneProgressCreate func(context.Context, *protocol.WorkDoneProgressCreateParams) error OnProgress func(context.Context, *protocol.ProgressParams) error OnShowMessage func(context.Context, *protocol.ShowMessageParams) error OnShowMessageRequest func(context.Context, *protocol.ShowMessageRequestParams) error OnRegistration func(context.Context, *protocol.RegistrationParams) error OnUnregistration func(context.Context, *protocol.UnregistrationParams) error }
ClientHooks are called to handle the corresponding client LSP method.
type Editor ¶
type Editor struct { Config EditorConfig // Server, client, and sandbox are concurrency safe and written only // at construction time, so do not require synchronization. Server protocol.Server // contains filtered or unexported fields }
Editor is a fake editor client. It keeps track of client state and can be used for writing LSP tests.
func NewEditor ¶
func NewEditor(sandbox *Sandbox, config EditorConfig) *Editor
NewEditor Creates a new Editor.
func (*Editor) ApplyQuickFixes ¶
func (e *Editor) ApplyQuickFixes(ctx context.Context, path string, rng *protocol.Range, diagnostics []protocol.Diagnostic) error
ApplyQuickFixes requests and performs the quickfix codeAction.
func (*Editor) BufferText ¶
BufferText returns the content of the buffer with the given name.
func (*Editor) BufferVersion ¶
BufferVersion returns the current version of the buffer corresponding to name (or 0 if it is not being edited).
func (*Editor) CloseBuffer ¶
CloseBuffer removes the current buffer (regardless of whether it is saved).
func (*Editor) CodeAction ¶
func (e *Editor) CodeAction(ctx context.Context, path string, rng *protocol.Range) ([]protocol.CodeAction, error)
CodeAction executes a codeAction request on the server.
func (*Editor) Completion ¶
func (e *Editor) Completion(ctx context.Context, path string, pos Pos) (*protocol.CompletionList, error)
Completion executes a completion request on the server.
func (*Editor) Connect ¶
func (e *Editor) Connect(ctx context.Context, conn jsonrpc2.Conn, hooks ClientHooks) (*Editor, error)
Connect configures the editor to communicate with an LSP server on conn. It is not concurrency safe, and should be called at most once, before using the editor.
It returns the editor, so that it may be called as follows:
editor, err := NewEditor(s).Connect(ctx, conn)
func (*Editor) CreateBuffer ¶
CreateBuffer creates a new unsaved buffer corresponding to the workdir path, containing the given textual content.
func (*Editor) DocumentLink ¶
func (*Editor) EditBuffer ¶
EditBuffer applies the given test edits to the buffer identified by path.
func (*Editor) FormatBuffer ¶
FormatBuffer gofmts a Go file.
func (*Editor) GoToDefinition ¶
GoToDefinition jumps to the definition of the symbol at the given position in an open buffer.
func (*Editor) Hover ¶
func (e *Editor) Hover(ctx context.Context, path string, pos Pos) (*protocol.MarkupContent, Pos, error)
Hover triggers a hover at the given position in an open buffer.
func (*Editor) OpenFileWithContent ¶
OpenFileWithContent creates a buffer for the given workdir-relative file with the given contents.
func (*Editor) OrganizeImports ¶
OrganizeImports requests and performs the source.organizeImports codeAction.
func (*Editor) RefactorRewrite ¶
RefactorRewrite requests and performs the source.refactorRewrite codeAction.
func (*Editor) References ¶
References executes a reference request on the server.
func (*Editor) RegexpReplace ¶
RegexpReplace edits the buffer corresponding to path by replacing the first instance of re, or its first subgroup, with the replace text. See RegexpSearch for more explanation of these two modes. It returns an error if re is invalid, has more than one subgroup, or doesn't match the buffer.
func (*Editor) RegexpSearch ¶
RegexpSearch returns the position of the first match for re in the buffer bufName. For convenience, RegexpSearch supports the following two modes:
- If re has no subgroups, return the position of the match for re itself.
- If re has one subgroup, return the position of the first subgroup.
It returns an error re is invalid, has more than one subgroup, or doesn't match the buffer.
func (*Editor) RunGenerate ¶
RunGenerate runs `go generate` non-recursively in the workdir-relative dir path. It does not report any resulting file changes as a watched file change, so must be followed by a call to Workdir.CheckForFileChanges once the generate command has completed.
func (*Editor) SaveBuffer ¶
SaveBuffer writes the content of the buffer specified by the given path to the filesystem.
func (*Editor) SaveBufferWithoutActions ¶
type EditorConfig ¶
type EditorConfig struct { Env map[string]string // CodeLens is a map defining whether codelens are enabled, keyed by the // codeLens command. CodeLens which are not present in this map are left in // their default state. CodeLens map[string]bool // SymbolMatcher is the config associated with the "symbolMatcher" gopls // config option. SymbolMatcher, SymbolStyle *string // LimitWorkspaceScope is true if the user does not want to expand their // workspace scope to the entire module. LimitWorkspaceScope bool // WithoutWorkspaceFolders is used to simulate opening a single file in the // editor, without a workspace root. In that case, the client sends neither // workspace folders nor a root URI. WithoutWorkspaceFolders bool // EditorRootPath specifies the root path of the workspace folder used when // initializing gopls in the sandbox. If empty, the Workdir is used. EditorRootPath string // EnableStaticcheck enables staticcheck analyzers. EnableStaticcheck bool }
EditorConfig configures the editor's LSP session. This is similar to source.UserOptions, but we use a separate type here so that we expose only that configuration which we support.
The zero value for EditorConfig should correspond to its defaults.
type FileEvent ¶
FileEvent wraps the protocol.FileEvent so that it can be associated with a workdir-relative path.
type Pos ¶
type Pos struct {
Line, Column int
}
Pos represents a position in a text buffer. Both Line and Column are 0-indexed.
func (Pos) ToProtocolPosition ¶
type Range ¶
Range corresponds to protocol.Range, but uses the editor friend Pos instead of UTF-16 oriented protocol.Position
type Sandbox ¶
type Sandbox struct { Workdir *Workdir // contains filtered or unexported fields }
Sandbox holds a collection of temporary resources to use for working with Go code in tests.
func NewSandbox ¶
func NewSandbox(config *SandboxConfig) (_ *Sandbox, err error)
NewSandbox creates a collection of named temporary resources, with a working directory populated by the txtar-encoded content in srctxt, and a file-based module proxy populated with the txtar-encoded content in proxytxt.
If rootDir is non-empty, it will be used as the root of temporary directories created for the sandbox. Otherwise, a new temporary directory will be used as root.
type SandboxConfig ¶
type SandboxConfig struct { // RootDir sets the base directory to use when creating temporary // directories. If not specified, defaults to a new temporary directory. RootDir string // Files holds a txtar-encoded archive of files to populate the initial state // of the working directory. Files string // InGoPath specifies that the working directory should be within the // temporary GOPATH. InGoPath bool // Workdir configures the working directory of the Sandbox, for running in a // pre-existing directory. If unset, a new working directory will be created // under RootDir. // // This option is incompatible with InGoPath or Files. Workdir string // ProxyFiles holds a txtar-encoded archive of files to populate a file-based // Go proxy. ProxyFiles string // GOPROXY is the explicit GOPROXY value that should be used for the sandbox. // // This option is incompatible with ProxyFiles. GOPROXY string }
SandboxConfig controls the behavior of a test sandbox. The zero value defines a reasonable default.
type SymbolInformation ¶
type SymbolInformation struct { Name string Kind protocol.SymbolKind Location Location }
SymbolInformation is an editor friendly version of protocol.SymbolInformation, with location information transformed to byte offsets. Field names correspond to the protocol type.
type Workdir ¶
type Workdir struct {
// contains filtered or unexported fields
}
Workdir is a temporary working directory for tests. It exposes file operations in terms of relative paths, and fakes file watching by triggering events on file operations.
func NewWorkdir ¶
NewWorkdir writes the txtar-encoded file data in txt to dir, and returns a Workir for operating on these files using
func (*Workdir) AddWatcher ¶
AddWatcher registers the given func to be called on any file change.
func (*Workdir) ChangeFilesOnDisk ¶
ChangeFilesOnDisk executes the given on-disk file changes in a batch, simulating the action of changing branches outside of an editor.
func (*Workdir) CheckForFileChanges ¶
CheckForFileChanges walks the working directory and checks for any files that have changed since the last poll.
func (*Workdir) ListFiles ¶
ListFiles lists files in the given directory, returning a map of relative path to modification time.
func (*Workdir) RegexpSearch ¶
RegexpSearch searches the file corresponding to path for the first position matching re.
func (*Workdir) RemoveFile ¶
RemoveFile removes a workdir-relative file path.
func (*Workdir) RootURI ¶
func (w *Workdir) RootURI() protocol.DocumentURI
RootURI returns the root URI for this working directory of this scratch environment.
func (*Workdir) URI ¶
func (w *Workdir) URI(path string) protocol.DocumentURI
URI returns the URI to a the workdir-relative path.
func (*Workdir) URIToPath ¶
func (w *Workdir) URIToPath(uri protocol.DocumentURI) string
URIToPath converts a uri to a workdir-relative path (or an absolute path, if the uri is outside of the workdir).
func (*Workdir) WriteFiles ¶
WriteFiles writes the text file content to workdir-relative paths. It batches notifications rather than sending them consecutively.