snippet

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package snippet implements Qt Creator / TextMate / LSP style snippet expansion. A snippet body is a template string containing tab-stop placeholders; Expand turns it into the final text plus a list of stops recording where the editor caret should land.

Placeholder syntax:

$N            a bare tab stop with index N and no default (empty)
${N:default}  a tab stop with index N and a default value
${N}          a tab stop with index N and an empty default
$0 / ${0:..}  the final caret position; always sorted last
\$            an escaped literal dollar sign (not a placeholder)
\\            an escaped literal backslash
\}            a literal brace, useful inside a ${N:default}

Offsets in Stop are BYTE offsets into Expansion.Text. Byte offsets keep slicing (Text[Start:End]) correct for multibyte UTF-8 defaults without any rune/byte conversion at the call site.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBook

func NewBook(ts []Template) map[string]Template

NewBook indexes templates by Trigger for lookup by a typed trigger word. When several templates share a trigger the last one wins.

Types

type Expansion

type Expansion struct {
	Text  string
	Stops []Stop
}

Expansion is the result of expanding a template: the final Text and the Stops sorted by Index ascending with every index-0 stop ($0, the final caret) placed last. Stops sharing an index keep their appearance order.

func Expand

func Expand(template string) (Expansion, error)

Expand parses template, replacing each placeholder with its default (empty for a bare $N) and recording a Stop for every placeholder. It returns an error for a malformed ${...} group: one with no closing brace, a non-numeric index, an out-of-range index, or a stray character where ':' or '}' was expected. A bare '$' not followed by a digit or '{' is a literal dollar, as is an escaped \$.

type Stop

type Stop struct {
	Index   int
	Start   int
	End     int
	Default string
}

Stop is a single tab stop landed in the expanded text. Start and End are byte offsets into Expansion.Text delimiting the region occupied by the placeholder's default (Start == End when the default is empty). A snippet may contain several stops with the same Index (mirrors); each is recorded separately.

type Template

type Template struct {
	Name    string
	Trigger string
	Body    string
}

Template is a named, trigger-keyed snippet: typing Trigger and expanding requests Body. Name is a human label for a picker UI.

Jump to

Keyboard shortcuts

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