Documentation ¶
Overview ¶
Package fake provides fake implementations of a text editor, LSP client plugin, and workspace 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 Workspace type provides a facility for executing tests in a clean workspace and GOPATH.
Index ¶
- Variables
- type Client
- func (c *Client) ApplyEdit(ctx context.Context, params *protocol.ApplyWorkspaceEditParams) (*protocol.ApplyWorkspaceEditResponse, error)
- func (c *Client) Configuration(context.Context, *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) OnDiagnostics(hook func(context.Context, *protocol.PublishDiagnosticsParams) error)
- func (c *Client) OnLogMessage(hook func(context.Context, *protocol.LogMessageParams) error)
- func (c *Client) Progress(context.Context, *protocol.ProgressParams) error
- func (c *Client) PublishDiagnostics(ctx context.Context, params *protocol.PublishDiagnosticsParams) error
- func (c *Client) RegisterCapability(context.Context, *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(context.Context, *protocol.UnregistrationParams) error
- func (c *Client) WorkDoneProgressCreate(context.Context, *protocol.WorkDoneProgressCreateParams) error
- func (c *Client) WorkspaceFolders(context.Context) ([]protocol.WorkspaceFolder, error)
- type Edit
- type Editor
- func (e *Editor) BufferText(name string) string
- func (e *Editor) BufferVersion(name string) int
- func (e *Editor) Client() *Client
- func (e *Editor) CloseBuffer(ctx context.Context, path string) error
- func (e *Editor) CreateBuffer(ctx context.Context, path, content string) 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) OpenFile(ctx context.Context, path string) error
- func (e *Editor) OrganizeImports(ctx context.Context, path string) 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) SaveBuffer(ctx context.Context, path string) error
- func (e *Editor) Shutdown(ctx context.Context) error
- type FileEvent
- type Pos
- type Workspace
- func (w *Workspace) AddWatcher(watcher func(context.Context, []FileEvent))
- func (w *Workspace) Close() error
- func (w *Workspace) GOPATH() string
- func (w *Workspace) ReadFile(path string) (string, error)
- func (w *Workspace) RegexpSearch(path string, re string) (Pos, error)
- func (w *Workspace) RemoveFile(ctx context.Context, path string) error
- func (w *Workspace) RootURI() protocol.DocumentURI
- func (w *Workspace) RunGoCommand(ctx context.Context, verb string, args ...string) error
- func (w *Workspace) URI(path string) protocol.DocumentURI
- func (w *Workspace) URIToPath(uri protocol.DocumentURI) string
- func (w *Workspace) WriteFile(ctx context.Context, path, content 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 ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { *Editor // contains filtered or unexported fields }
Client is an adapter that converts a *Client into an LSP Client.
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) OnDiagnostics ¶
func (c *Client) OnDiagnostics(hook func(context.Context, *protocol.PublishDiagnosticsParams) error)
OnDiagnostics sets the hook to run when the editor receives diagnostics published from the language server.
func (*Client) OnLogMessage ¶
OnLogMessage sets the hook to run when the editor receives a log message.
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 Editor ¶
type Editor struct {
// 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 NewConnectedEditor ¶
NewConnectedEditor creates a new editor that dispatches the LSP across the provided jsonrpc2 connection.
The returned editor is initialized and ready to use.
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) CreateBuffer ¶
CreateBuffer creates a new unsaved buffer corresponding to the workspace path, containing the given textual content.
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) OrganizeImports ¶
OrganizeImports requests and performs the source.organizeImports codeAction.
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) SaveBuffer ¶
SaveBuffer writes the content of the buffer specified by the given path to the filesystem.
type FileEvent ¶
FileEvent wraps the protocol.FileEvent so that it can be associated with a workspace-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.
type Workspace ¶
type Workspace struct {
// contains filtered or unexported fields
}
The Workspace type represents a temporary workspace to use for editing Go files in tests.
func NewWorkspace ¶
NewWorkspace creates a named workspace populated by the txtar-encoded content given by txt. It creates temporary directories for the workspace content and for GOPATH.
func (*Workspace) AddWatcher ¶
AddWatcher registers the given func to be called on any file change.
func (*Workspace) GOPATH ¶
GOPATH returns the value that GOPATH should be set to for this workspace.
func (*Workspace) RegexpSearch ¶
RegexpSearch searches the file
func (*Workspace) RemoveFile ¶
RemoveFile removes a workspace-relative file path.
func (*Workspace) RootURI ¶
func (w *Workspace) RootURI() protocol.DocumentURI
RootURI returns the root URI for this workspace.
func (*Workspace) RunGoCommand ¶
RunGoCommand executes a go command in the workspace.
func (*Workspace) URI ¶
func (w *Workspace) URI(path string) protocol.DocumentURI
URI returns the URI to a the workspace-relative path.