Documentation ¶
Overview ¶
internal contains the core parts of vidar that make it work as a text editor. These are the components that make the UI work the way it does.
We are using the 'internal' package here to limit how much plugins can override so that remote pairing plugins (and anything else that would need to sync core operations between multiple instances of vidar) can work without forcing all instances of vidar to have the same set of plugins.
Index ¶
- Constants
- func BinderFor(_ context.Context, elem any) (input.Binder, error)
- type Action
- type Actions
- type Binder
- type Container
- type DisplayOpt
- type EditApplier
- type EditHistory
- type Editor
- func (e *Editor) Bound(context.Context) input.Actions
- func (e *Editor) Close(ctx context.Context) Error
- func (e *Editor) Get(ctx context.Context, a input.Action, key any) (any, bool)
- func (e *Editor) HasChanges(ctx context.Context) bool
- func (e *Editor) Name(context.Context) string
- func (e *Editor) Path(context.Context) (string, bool)
- func (e *Editor) RawText(context.Context) *PieceTable
- func (e *Editor) Run(ctx context.Context, a input.Action) error
- func (e *Editor) Save(ctx context.Context, force bool) error
- func (e *Editor) ScrollPos(ctx context.Context, style ui.ScrollType, sel Selection) error
- func (e *Editor) Selections(context.Context) (primary caret.Selection, secondary []caret.Selection)
- func (e *Editor) Set(ctx context.Context, a input.Action, key, value any) bool
- func (e *Editor) SetActions(_ context.Context, a *Actions)
- func (e *Editor) SetSelections(ctx context.Context, primary Selection, secondary ...Selection)
- func (e *Editor) State(ctx context.Context) text.State
- func (e *Editor) UI(context.Context) Element
- func (e *Editor) UpdateSpans(ctx context.Context, spans ...Span)
- func (e *Editor) Visible(ctx context.Context) ui.Box
- type EditorOpt
- type Element
- type Empty
- type Error
- type Event
- type Focuser
- type InfoBox
- func (i *InfoBox) Bound(context.Context) input.Actions
- func (i *InfoBox) Display(ctx context.Context, opts ...DisplayOpt) error
- func (i *InfoBox) Event(ctx context.Context, ev Event) error
- func (e *InfoBox) Get(ctx context.Context, a input.Action, key any) (any, bool)
- func (i *InfoBox) Input(ctx context.Context) Element
- func (i *InfoBox) Prompt(ctx context.Context, opts ...PromptOpt) error
- func (i *InfoBox) Run(ctx context.Context, a input.Action) error
- func (e *InfoBox) Set(ctx context.Context, a input.Action, key, value any) bool
- func (i *InfoBox) SetActions(_ context.Context, acts *Actions)
- func (i *InfoBox) UI(context.Context) Element
- type Information
- type KeyHandler
- type MultiContainer
- func (p *MultiContainer) Add(ctx context.Context, b Binder) error
- func (p *MultiContainer) Bound(context.Context) input.Actions
- func (p *MultiContainer) Children(context.Context) []Binder
- func (e *MultiContainer) Get(ctx context.Context, a input.Action, key any) (any, bool)
- func (p *MultiContainer) Parent(context.Context) Container
- func (p *MultiContainer) Remove(ctx context.Context, b Binder) (Binder, error)
- func (p *MultiContainer) Run(ctx context.Context, a input.Action) error
- func (e *MultiContainer) Set(ctx context.Context, a input.Action, key, value any) bool
- func (p *MultiContainer) SetActions(_ context.Context, a *Actions)
- func (p *MultiContainer) SetParent(_ context.Context, parent Container)
- func (p *MultiContainer) UI(context.Context) Element
- type PieceTable
- type Placeholder
- func (p *Placeholder) Add(context.Context, Binder) error
- func (p *Placeholder) Bound(context.Context) input.Actions
- func (p *Placeholder) Children(context.Context) []Binder
- func (e *Placeholder) Get(ctx context.Context, a input.Action, key any) (any, bool)
- func (p *Placeholder) Parent(context.Context) Container
- func (p *Placeholder) Remove(context.Context, Binder) (Binder, error)
- func (p *Placeholder) Run(ctx context.Context, a input.Action) error
- func (e *Placeholder) Set(ctx context.Context, a input.Action, key, value any) bool
- func (p *Placeholder) SetActions(_ context.Context, a *Actions)
- func (p *Placeholder) SetParent(_ context.Context, parent Container)
- func (p *Placeholder) UI(context.Context) Element
- type Priority
- type Prompt
- type PromptEventHandler
- type PromptOpt
- type Selection
- type SettableBinder
- type SingleContainer
- func (p *SingleContainer) Add(ctx context.Context, b Binder) error
- func (p *SingleContainer) Bound(context.Context) input.Actions
- func (p *SingleContainer) Children(context.Context) []Binder
- func (e *SingleContainer) Get(ctx context.Context, a input.Action, key any) (any, bool)
- func (p *SingleContainer) Parent(context.Context) Container
- func (p *SingleContainer) Remove(ctx context.Context, b Binder) (Binder, error)
- func (p *SingleContainer) Run(ctx context.Context, a input.Action) error
- func (e *SingleContainer) Set(ctx context.Context, a input.Action, key, value any) bool
- func (p *SingleContainer) SetActions(_ context.Context, a *Actions)
- func (p *SingleContainer) SetParent(_ context.Context, parent Container)
- func (p *SingleContainer) UI(context.Context) Element
- type Span
- type State
- func (s State) Add(_ context.Context, at int, parts [][]rune) State
- func (s State) Equals(ctx context.Context, o State) bool
- func (s State) Len(context.Context) int
- func (s State) LineEnd(ctx context.Context, pos int) int
- func (s State) LineStart(ctx context.Context, pos int) int
- func (s State) Pos(ctx context.Context, line, col int) int
- func (s State) PosTuple(ctx context.Context, pos int) (line, col int)
- func (s State) Remove(_ context.Context, at, length int) State
- func (s State) Text(_ context.Context, start, end int) []rune
- func (s State) Version(context.Context) int
- type UIBinder
- type Window
- func (w *Window) Bound(context.Context) input.Actions
- func (w *Window) Close(ctx context.Context) error
- func (w *Window) Focus(ctx context.Context, b Binder) error
- func (w *Window) Focused(ctx context.Context) (Binder, error)
- func (e *Window) Get(ctx context.Context, a input.Action, key any) (any, bool)
- func (w *Window) Run(ctx context.Context, a input.Action) error
- func (e *Window) Set(ctx context.Context, a input.Action, key, value any) bool
- func (w *Window) SetActions(_ context.Context, acts *Actions)
- func (w *Window) SetChild(ctx context.Context, child Element) error
Constants ¶
const ( ErrPlaceholder = constant.Error("cannot add elements to a placeholder") ErrMustEmpty = constant.Error("container only holds one element and is full") ErrNotFound = constant.Error("container does not contain requested element") ErrNoUI = constant.Error("no UI element found") )
const ErrDone = err.Const("done")
ErrDone is used to signal that something is done. For example, when accepting input from a user prompt, a PromptEventHandler may return ErrDone to signal that the user has finished responding to the prompt.
const ( // ErrModified is returned when saving without force if the file has changed // on disk. ErrModified = constant.Error("file modified on disk") )
const MaxDisplayFor = 10 * time.Second
MaxDisplayFor is the maximum duration allowed in DisplayFor.
const (
PromptMaxExtra = 6
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Action ¶
Action is a copy of the input.Action interface type, to be used as a parameter or return type when we want to prevent plugins from using a method.
type Actions ¶
type Actions struct {
// contains filtered or unexported fields
}
Actions tracks a set of all available actions, an input.Binder, and the subset of Action values that bound successfully to the input.Binder.
func NewActions ¶
NewActions constructs a new Actions type using a binder and a list of all available actions.
type Binder ¶
Binder is a copy of the input.Binder interface type, to be used as a parameter or return type when we want to prevent plugins from using a method.
type DisplayOpt ¶
type DisplayOpt func(Information) Information
DisplayOpt is an option function to change how information is displayed to the user.
func DisplayFor ¶
func DisplayFor(d time.Duration) DisplayOpt
DisplayFor sets a minimum duration to display information to the user. The information may remain on screen longer than the minimum, but no less.
An error will be returned if d is greater than MaxDisplayFor.
func DisplayInfo ¶
func DisplayInfo(spans ...ui.Span) DisplayOpt
DisplayInfo displays information to the user. Most of the time, this should be succinct, single-line information. Info can *only* override other info.
An error will be returned if the information cannot be displayed.
type EditApplier ¶
type EditHistory ¶
type Editor ¶
type Editor struct {
// contains filtered or unexported fields
}
Editor controls all of the important parts of editing text. It is responsible for tracking text changes, reporting updated text to the UI, and keeping track of the currently open file (and any changes on disk).
TODO: really, an Editor is made up of several chunks (functions, type definitions, variables, etc), each of which could have its own actions bound. For example, you wouldn't want word wrap bound to a function body, but you would want it bound to a comment block.
We could in theory provide a mechanism for setting up separate input.Binder types for each chunk of code. This would also make syntax highlighting a little easier, since each individual input.Binder would have its own color and translation rules. When an edit operation applies to that input.Binder, it expands its range, automatically keeping existing syntax highlights relevant.
func (*Editor) HasChanges ¶
HasChanges reports whether or not e has local changes from its on-disk state. If e is not editing a file on disk, this will always return false.
func (*Editor) Selections ¶
func (*Editor) SetSelections ¶
type Empty ¶
type Empty struct{}
Empty is used as an empty binder during editor startup. Since there are no windows yet and the first window needs to be constructed by an Action (specifically: a *WindowManager), this is a sort of placeholder for a nil binder, without the disadvantages of causing panics.
type Event ¶
Event is a wrapper for events (e.g. key strokes). We use it to ensure that third parties can't directly send events to the InfoBox - they must go through an Operation to send events.
type InfoBox ¶
type InfoBox struct {
// contains filtered or unexported fields
}
InfoBox is a type which can be used to display information to the user. It can also be used to get a response from the user, like a standard dialog window. Depending on the ui library in use, this _may_ create standard dialog windows; but vidar is designed to mimic the style of vim and emacs, where the monolog/dialog is in a little pane at the bottom of the editor window.
func NewInfoBox ¶
NewInfoBox creates an InfoBox and sets up its UI element(s).
func (*InfoBox) Display ¶
func (i *InfoBox) Display(ctx context.Context, opts ...DisplayOpt) error
Display displays something to the user.
type Information ¶
type KeyHandler ¶
type KeyHandler struct {
// contains filtered or unexported fields
}
func NewKeyHandler ¶
func NewKeyHandler(ctx context.Context) *KeyHandler
func (*KeyHandler) SetWindow ¶
func (h *KeyHandler) SetWindow(w *Window)
SetWindow tells h which ui.Window it is handling keys for.
type MultiContainer ¶
type MultiContainer struct {
// contains filtered or unexported fields
}
func NewMultiContainer ¶
func (*MultiContainer) SetActions ¶
func (p *MultiContainer) SetActions(_ context.Context, a *Actions)
type PieceTable ¶
type PieceTable struct {
// contains filtered or unexported fields
}
PieceTable implements a piece table, a modern storage structure for text editors, used by the likes of atom and vscode. It relies on a read-only origin buffer, an append-only add buffer, and a table referencing the two buffers to create the full block of text.
Since the text in the origin and add buffers don't change, copies of the table's state are safe to use concurrently with write operations, without making a copy of the full text.
func NewPieceTable ¶
func NewPieceTable(_ context.Context, orig []rune) *PieceTable
NewPieceTable returns a new piece table using orig as the origin buffer.
func (*PieceTable) Apply ¶
func (t *PieceTable) Apply(ctx context.Context, edits ...input.Edit) []EditHistory
Apply applies edits to t, updating its current state.
func (*PieceTable) ApplyLine ¶
func (t *PieceTable) ApplyLine(ctx context.Context, lineEdits ...input.LineEdit) []EditHistory
ApplyLine applies LineEdits to t, updating its current state, and returns
type Placeholder ¶
type Placeholder struct {
// contains filtered or unexported fields
}
func NewPlaceholder ¶
func (*Placeholder) SetActions ¶
func (p *Placeholder) SetActions(_ context.Context, a *Actions)
type Priority ¶
type Priority int
const ( PriorityDebug Priority = iota PriorityInfo PriorityWarning PriorityError // PriorityCritical is for internal use only and will override all // plugin-defined messages. // // NOTE: there is no reason to leave this unexported, since any package that // has access to PriorityError could create PriorityCritical by passing in // (PriorityError + 1). We export it here so that we can document that it is // for internal use only. Packages which can make use of this package // directly *must* block third parties from passing in an arbitrary Priority // value. PriorityCritical )
type Prompt ¶
type Prompt struct { // Prompt is displayed before the user input element. A prompt message is // always required when prompting the user for input, thus this must be // non-empty on first prompt. // // On updates to the prompt, a nil value means to leave the prompt as is. Prompt []ui.Span // Extra is any extra data following the user input element. These could be // things like completion suggestions, fuzzy matches, or just some help to // explain what's going on. // // This may need to be zeroed out at times, so we use a pointer to a slice. // A nil pointer means to leave the current Extra value as-is on updates. // A pointer to an empty slice means to empty the current Extra value. Extra *[][]ui.Span // Input is the input text from the user. It may be altered by the // PromptEventHandler in order to change the text in the input, e.g. in // order to accept multiple values from the prompt or to apply completions. // // If it is non-nil, then it will override whatever is in the input text // box. Input *string // contains filtered or unexported fields }
Prompt is a type to track a dialog-like prompt from an action to the user.
type PromptEventHandler ¶
type PromptEventHandler interface { // Input will be called for all inputs that come from the user. Input(context.Context, input.Binder, Prompt, input.Edit) (Prompt, error) // KeyPress will be called for each key code that comes from the user. KeyPress(context.Context, input.Binder, Prompt, input.KeyEvent) (Prompt, error) // Run will be called when either Input or KeyPress returns ErrDone. The // input.Binder passed to Run will be the input.Binder that focus is being // returned to. Run(context.Context, Prompt, input.Binder) error }
PromptEventHandler is a type which can handle events from a user prompt.
Unless otherwise noted, any input.Binder values passed in to the handler will be the the one which is receiving user input.
type PromptOpt ¶
PromptOpt is an option function to change how information is prompted from the user.
func PromptExtra ¶
PromptExtra sets the initial extra spans that are displayed after the user input box. Each slice represents one line of extra info.
func PromptHandler ¶
func PromptHandler(h PromptEventHandler) PromptOpt
PromptHandler sets the PromptEventHandler that will be called when the user writes input to the InfoBox.
func PromptInput ¶
PromptInput sets the initial user input to fill in the text box with.
func PromptReturnBinder ¶
PromptReturnBinder sets the input.Binder that the InfoBox will return focus to after it finishes getting user input.
func PromptSpans ¶
PromptSpans sets the initial spans that will be displayed before the input.
type Selection ¶
Selection is an alias for (pkg/caret).Selection. We use it in the internal package to ensure that only code that has access to the internal package may access certain methods.
type SettableBinder ¶
type SingleContainer ¶
type SingleContainer struct {
// contains filtered or unexported fields
}
func NewSingleContainer ¶
func (*SingleContainer) SetActions ¶
func (p *SingleContainer) SetActions(_ context.Context, a *Actions)
type State ¶
type State struct {
// contains filtered or unexported fields
}
State represents a state of text in a piece table. Since the elements of a piece table are immutable once referenced, a State value is safe for concurrent access and will never change, even if the piece table has changed.
func (State) Add ¶
Add returns a new State with the addition applied. Add does not change any backing arrays, but simply returns a new state with the references added.
Note: s has no concept of backing arrays, so the parts argument is expected to reference the final backing arrays. Add simply stores those references at the correct point.
func (State) Remove ¶
Remove returns a new State with the removal applied. Remove does not change any backing arrays, but simply returns a new state with the references removed.