fake

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2020 License: BSD-3-Clause Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

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

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 (c *Client) Configuration(context.Context, *protocol.ParamConfiguration) ([]interface{}, error)

func (*Client) Event

func (c *Client) Event(ctx context.Context, event *interface{}) error

func (*Client) LogMessage

func (c *Client) LogMessage(ctx context.Context, params *protocol.LogMessageParams) error

func (*Client) OnDiagnostics added in v1.0.0

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 added in v1.0.0

func (c *Client) OnLogMessage(hook func(context.Context, *protocol.LogMessageParams) error)

OnLogMessage sets the hook to run when the editor receives a log message.

func (*Client) PublishDiagnostics

func (c *Client) PublishDiagnostics(ctx context.Context, params *protocol.PublishDiagnosticsParams) error

func (*Client) RegisterCapability

func (c *Client) RegisterCapability(context.Context, *protocol.RegistrationParams) error

func (*Client) ShowMessage

func (c *Client) ShowMessage(ctx context.Context, params *protocol.ShowMessageParams) error

func (*Client) ShowMessageRequest

func (c *Client) ShowMessageRequest(ctx context.Context, params *protocol.ShowMessageRequestParams) (*protocol.MessageActionItem, error)

func (*Client) UnregisterCapability

func (c *Client) UnregisterCapability(context.Context, *protocol.UnregistrationParams) error

func (*Client) WorkspaceFolders

func (c *Client) WorkspaceFolders(context.Context) ([]protocol.WorkspaceFolder, error)

type Edit

type Edit struct {
	Start, End Pos
	Text       string
}

Edit represents a single (contiguous) buffer edit.

func NewEdit

func NewEdit(startLine, startColumn, endLine, endColumn int, text string) Edit

NewEdit creates an edit replacing all content between (startLine, startColumn) and (endLine, endColumn) with text.

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 added in v1.0.0

func NewConnectedEditor(ctx context.Context, ws *Workspace, conn *jsonrpc2.Conn) (*Editor, error)

NewConnectedEditor creates a new editor that dispatches the LSP across the provided jsonrpc2 connection.

The returned editor is initialized and ready to use.

func NewEditor

func NewEditor(ws *Workspace) *Editor

NewEditor Creates a new Editor.

func (*Editor) BufferText

func (e *Editor) BufferText(name string) string

BufferText returns the content of the buffer with the given name.

func (*Editor) Client

func (e *Editor) Client() *Client

Client returns the LSP client for this editor.

func (*Editor) CloseBuffer

func (e *Editor) CloseBuffer(ctx context.Context, path string) error

CloseBuffer removes the current buffer (regardless of whether it is saved).

func (*Editor) CreateBuffer

func (e *Editor) CreateBuffer(ctx context.Context, path, content string) error

CreateBuffer creates a new unsaved buffer corresponding to the workspace path, containing the given textual content.

func (*Editor) EditBuffer

func (e *Editor) EditBuffer(ctx context.Context, path string, edits []Edit) error

EditBuffer applies the given test edits to the buffer identified by path.

func (*Editor) Exit

func (e *Editor) Exit(ctx context.Context) error

Exit issues the 'exit' LSP notification.

func (*Editor) FormatBuffer

func (e *Editor) FormatBuffer(ctx context.Context, path string) error

FormatBuffer gofmts a Go file.

func (*Editor) GoToDefinition

func (e *Editor) GoToDefinition(ctx context.Context, path string, pos Pos) (string, Pos, error)

GoToDefinition jumps to the definition of the symbol at the given position in an open buffer.

func (*Editor) OpenFile

func (e *Editor) OpenFile(ctx context.Context, path string) error

OpenFile creates a buffer for the given workspace-relative file.

func (*Editor) OrganizeImports

func (e *Editor) OrganizeImports(ctx context.Context, path string) error

OrganizeImports requests and performs the source.organizeImports codeAction.

func (*Editor) SaveBuffer

func (e *Editor) SaveBuffer(ctx context.Context, path string) error

SaveBuffer writes the content of the buffer specified by the given path to the filesystem.

func (*Editor) Shutdown

func (e *Editor) Shutdown(ctx context.Context) error

Shutdown issues the 'shutdown' LSP notification.

type FileEvent

type FileEvent struct {
	Path          string
	ProtocolEvent protocol.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 0-indexed position in a text buffer.

type Workspace added in v1.0.0

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 added in v1.0.0

func NewWorkspace(name string, txt []byte) (_ *Workspace, err error)

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 added in v1.0.0

func (w *Workspace) AddWatcher(watcher func(context.Context, []FileEvent))

AddWatcher registers the given func to be called on any file change.

func (*Workspace) Close added in v1.0.0

func (w *Workspace) Close() error

Close removes all state associated with the workspace.

func (*Workspace) GOPATH added in v1.0.0

func (w *Workspace) GOPATH() string

GOPATH returns the value that GOPATH should be set to for this workspace.

func (*Workspace) ReadFile added in v1.0.0

func (w *Workspace) ReadFile(path string) (string, error)

ReadFile reads a text file specified by a workspace-relative path.

func (*Workspace) RemoveFile added in v1.0.0

func (w *Workspace) RemoveFile(ctx context.Context, path string) error

RemoveFile removes a workspace-relative file path.

func (*Workspace) RootURI added in v1.0.0

func (w *Workspace) RootURI() protocol.DocumentURI

RootURI returns the root URI for this workspace.

func (*Workspace) URI added in v1.0.0

func (w *Workspace) URI(path string) protocol.DocumentURI

URI returns the URI to a the workspace-relative path.

func (*Workspace) URIToPath added in v1.0.0

func (w *Workspace) URIToPath(uri protocol.DocumentURI) string

URIToPath converts a uri to a workspace-relative path (or an absolute path, if the uri is outside of the workspace).

func (*Workspace) WriteFile added in v1.0.0

func (w *Workspace) WriteFile(ctx context.Context, path, content string) error

WriteFile writes text file content to a workspace-relative path.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL