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 ¶
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 ¶
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 ¶
Completer supplies candidates for a matched trigger. Implementations must honor ctx and be safe for concurrent calls if their engine is used concurrently.
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 (*Engine) Complete ¶
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]
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 ¶
Result contains the active completion context and matching candidates. Source is empty when no registered source can resolve the input context.
func (Result) Apply ¶
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.