autocomplete

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package autocomplete provides a headless, source-driven completion engine for terminal UIs. It does not render suggestions or execute completed input.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSource     = errors.New("autocomplete: invalid source")
	ErrDuplicateSource   = errors.New("autocomplete: duplicate source")
	ErrInvalidInput      = errors.New("autocomplete: invalid input")
	ErrInvalidCursor     = errors.New("autocomplete: invalid cursor")
	ErrInvalidCandidates = errors.New("autocomplete: invalid candidates")
	ErrInvalidSelection  = errors.New("autocomplete: invalid selection")
	ErrInvalidRange      = errors.New("autocomplete: invalid replacement range")
)

Errors returned for invalid declarations, input, or completion selection.

Functions

func Quote

func Quote(value string) string

Quote wraps value in double quotes (escaping inner quotes and backslashes) when value contains whitespace, quotes, or backslashes.

Types

type Candidate

type Candidate struct {
	Value       string
	Label       string
	Description string
	Aliases     []string
	// InsertText overrides the default quoted Value when nonempty.
	InsertText string
	// Data is opaque, source-owned metadata. The engine preserves it without
	// inspecting or deep-copying its contents.
	Data any
}

Candidate is a selectable value. Label and Description are presentation metadata. Aliases participate in matching but insert the canonical Value.

type Completer

type Completer func(ctx context.Context, req Request) ([]Candidate, error)

Completer supplies candidates for a matched trigger. Implementations must honor ctx and be safe for concurrent calls if their engine is used concurrently.

func Static

func Static(candidates ...Candidate) Completer

Static declares a fixed set of candidates. It snapshots the supplied slice and aliases; each call returns its own copy. Opaque Data payloads are retained as supplied.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine resolves input against registered sources. Its zero value is ready to use. Registration and completion may run concurrently.

func New

func New() *Engine

New returns an engine with no registered sources.

func (*Engine) Complete

func (e *Engine) Complete(ctx context.Context, input string, cursor int) (Result, error)

Complete resolves suggestions at cursor, a UTF-8 byte offset in input. Triggers match unquoted token prefixes. Higher priority wins, then the nearest trigger, then the longest. A whitespace-terminated trigger such as "/mode " activates only after that token's separator. Quotes and backslash escapes group whitespace into tokens; unfinished quotes/escapes are accepted.

Example
engine := New()
for _, source := range []Source{
	{Name: "commands", Trigger: "/", StartOnly: true,
		Complete: Static(Candidate{Value: "mode"}, Candidate{Value: "mcp"})},
	{Name: "modes", Trigger: "/mode ", StartOnly: true,
		Complete: Static(Candidate{Value: "auto"}, Candidate{Value: "ask"})},
	{Name: "mcp", Trigger: "/mcp ", StartOnly: true,
		Complete: Static(Candidate{Value: "list"}, Candidate{Value: "restart"})},
} {
	if err := engine.Register(source); err != nil {
		panic(err)
	}
}
for _, input := range []string{"/mode", "/mode ", "/mcp r"} {
	result, err := engine.Complete(context.Background(), input, len(input))
	if err != nil {
		panic(err)
	}
	fmt.Printf("%q: %v\n", input, candidateValues(result.Candidates))
}
Output:
"/mode": [mode]
"/mode ": [auto ask]
"/mcp r": [restart]

func (*Engine) Match

func (e *Engine) Match(input string, cursor int) (Request, error)

Match resolves the active source without invoking its completer. Source is empty when no trigger matches. Cursor is a UTF-8 byte offset.

func (*Engine) Register

func (e *Engine) Register(source Source) error

Register adds a source. Names and equivalent triggers must be unique. Triggers must be valid UTF-8, begin with a non-whitespace character, and contain no quotes or escape characters.

type Range

type Range struct {
	Start int
	End   int
}

Range identifies a half-open byte range in the original input.

type Request

type Request struct {
	Input   string
	Cursor  int
	Source  string
	Trigger string
	Query   string
	Range   Range
}

Request describes the token being resolved. Cursor and Range use UTF-8 byte offsets. Query is the decoded token prefix up to the cursor, excluding the matched trigger.

type Result

type Result struct {
	Request
	Candidates []Candidate
}

Result contains the active completion context and matching candidates. Source is empty when no registered source can resolve the input context.

func (Result) Apply

func (r Result) Apply(index int) (string, int, error)

Apply replaces the active token with the selected candidate and returns the new input and byte cursor. Text outside Range, including the trigger and later arguments, is preserved. Values containing whitespace, quotes, or backslashes are quoted unless the source supplies InsertText. No separator is added.

type Source

type Source struct {
	Name     string
	Trigger  string
	Complete Completer
	Priority int
	// StartOnly restricts the trigger to the first non-whitespace token.
	StartOnly bool
	// CaseSensitive applies to trigger, value, label, and alias matching.
	CaseSensitive bool
	// Passthrough disables prefix filtering in Engine.Complete, returning
	// candidates in the exact order supplied by Complete.
	Passthrough bool
}

Source associates a trigger with its candidates. For example, "/" can suggest commands while "/mode " suggests mode values. Whitespace in a trigger matches token separators; trailing whitespace begins completion of the next token.

Jump to

Keyboard shortcuts

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