Documentation
¶
Overview ¶
Package syntax parses Conventional Commits while preserving source bytes.
Index ¶
- type Commit
- func (c Commit) Body() string
- func (c Commit) BodyGap() Gap
- func (c Commit) BodyLines() iter.Seq[Line]
- func (c Commit) BodySpan() Span
- func (c Commit) Description() string
- func (c Commit) DescriptionSpan() Span
- func (c Commit) Footer() string
- func (c Commit) FooterAt(index int) Footer
- func (c Commit) FooterCount() int
- func (c Commit) FooterGap() Gap
- func (c Commit) FooterLines() iter.Seq[Line]
- func (c Commit) FooterSpan() Span
- func (c Commit) Footers() iter.Seq[Footer]
- func (c Commit) HasBody() bool
- func (c Commit) HasBreakingFooter() bool
- func (c Commit) HasFooter() bool
- func (c Commit) HasHeaderBang() bool
- func (c Commit) HasScope() bool
- func (c Commit) Header() string
- func (c Commit) HeaderBangSpan() Span
- func (c Commit) HeaderSeparator() string
- func (c Commit) HeaderSeparatorSpan() Span
- func (c Commit) HeaderSpan() Span
- func (c Commit) Indent() string
- func (c Commit) IndentSpan() Span
- func (c Commit) IsBreakingChange() bool
- func (c Commit) Lines(span Span) LineIterator
- func (c Commit) Message() string
- func (c Commit) Position(offset int) Position
- func (c Commit) Scope() string
- func (c Commit) ScopeSpan() Span
- func (c Commit) Text(span Span) string
- func (c Commit) Trailing() string
- func (c Commit) TrailingSpan() Span
- func (c Commit) Type() string
- func (c Commit) TypeSpan() Span
- type ErrorCode
- type Footer
- type Gap
- type Inspection
- type Line
- type LineIterator
- type ParseError
- type Position
- type Separator
- type Span
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Commit ¶
type Commit struct {
// contains filtered or unexported fields
}
Commit is a read-only parsed message. Copies share the original input and footer storage. Retaining any field may retain the full input allocation. Its zero value is safe to inspect.
func Parse ¶
Parse reads one Conventional Commit. Successful fields reference input without copying. Failure returns a zero Commit and a *ParseError, with no partial result.
Example ¶
package main
import (
"fmt"
"github.com/conventionalcommit/syntax"
)
func main() {
c, err := syntax.Parse("build(cache)!: replace archive index\n\nRefs #84")
if err != nil {
panic(err)
}
fmt.Println(c.Type(), c.Scope(), c.IsBreakingChange())
entry := c.FooterAt(0)
fmt.Println(entry.Token())
fmt.Println(entry.Value())
}
Output: build cache true Refs 84
func (Commit) BodyGap ¶
BodyGap describes the separator between the header and body. Inspection can expose a present body with zero blank lines and a corresponding diagnostic.
func (Commit) Description ¶
func (Commit) DescriptionSpan ¶
func (Commit) FooterCount ¶
func (Commit) FooterGap ¶
FooterGap describes the separator before the footer section, following the body if present and the header otherwise.
func (Commit) FooterLines ¶
FooterLines iterates over lines in Footer(), yielding zero lines when absent.
func (Commit) FooterSpan ¶
func (Commit) HasBreakingFooter ¶
HasBreakingFooter reports an uppercase breaking footer marker. During inspection its value can be invalid; diagnostics describe that separately.
func (Commit) HasHeaderBang ¶
HasHeaderBang reports a bang immediately before the header colon.
func (Commit) HeaderBangSpan ¶
HeaderBangSpan locates the header bang, or returns the zero span if absent.
func (Commit) HeaderSeparator ¶
func (Commit) HeaderSeparatorSpan ¶
func (Commit) HeaderSpan ¶
func (Commit) Indent ¶
Indent returns leading horizontal whitespace before Type, if recovered by Inspect.
func (Commit) IndentSpan ¶
IndentSpan locates leading horizontal whitespace before Type, or returns zero if absent.
func (Commit) IsBreakingChange ¶
func (Commit) Lines ¶
func (c Commit) Lines(span Span) LineIterator
Lines visits a source range with offsets relative to Message(), including when the range is empty. Invalid ranges panic like ordinary slice bounds.
func (Commit) Position ¶
Position converts a zero-based byte offset in Message() into a 1-based line and byte column.
func (Commit) TrailingSpan ¶
TrailingSpan locates terminal line endings after the last content section.
type Footer ¶
type Footer struct {
// contains filtered or unexported fields
}
Footer represents one parsed footer entry. Duplicate tokens are kept.
func (Footer) IsBreaking ¶
IsBreaking reports whether this footer uses an uppercase breaking token.
func (Footer) SeparatorSpan ¶
SeparatorSpan returns the delimiter span between Token and Value, or zero if absent.
type Gap ¶
Gap describes the raw line-ending separator before a present body or footer. Span includes the preceding section's line ending. BlankLines excludes that first ending. An absent section returns the zero gap.
type Inspection ¶
type Inspection struct {
Commit Commit
Diagnostics []ParseError
Structured bool
}
Inspection contains source facts and diagnostics for linting. Structured is true when the header identifies the fields unambiguously, even if invalid. Otherwise only Message(), Header(), HeaderSpan(), and raw Lines are available. Diagnostics are in source order, except byte validation precedes grammar. The caller owns the diagnostic slice; changing it cannot affect Commit.
func Inspect ¶
func Inspect(input string) Inspection
Inspect retains recoverable fields without repairing or normalising input. Parse remains the strict entry point. No diagnostics means Parse would accept the same input and produce the same fields. Invalid bytes or an ambiguous header stop structural recovery; callers can still inspect raw source lines.
Example ¶
package main
import (
"fmt"
"github.com/conventionalcommit/syntax"
)
func main() {
r := syntax.Inspect("feat:reload\nBody text.")
fmt.Println(r.Structured, len(r.Diagnostics))
fmt.Println(r.Commit.Type(), r.Commit.Description())
fmt.Println(r.Commit.BodyGap().BlankLines)
}
Output: true 2 feat reload 0
func (Inspection) Valid ¶
func (i Inspection) Valid() bool
Valid reports whether the message parsed structurally with zero diagnostics.
type LineIterator ¶
type LineIterator struct {
// contains filtered or unexported fields
}
LineIterator visits lines without allocating a collection. Its zero value visits one empty line. Copies advance independently and retain the input.
func Lines ¶
func Lines(input string) LineIterator
Lines iterates raw bytes without validating them. A final newline produces an empty last line, and empty input produces one empty line. Standalone CR is content; CR immediately followed by LF belongs to the line ending.
Example ¶
package main
import (
"fmt"
"github.com/conventionalcommit/syntax"
)
func main() {
it := syntax.Lines("a\r\nb\n")
for line, ok := it.Next(); ok; line, ok = it.Next() {
fmt.Println(line.Content.Len(), line.Ending.Len())
}
}
Output: 1 2 1 1 0 0
func (*LineIterator) Next ¶
func (it *LineIterator) Next() (Line, bool)
Next returns the next line and whether one was available. After exhaustion it returns a zero Line and false on every subsequent call.
type ParseError ¶
ParseError locates a failure in the original input. Offset is a zero-based byte position and may equal the input length for missing content.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type Separator ¶
type Separator uint8
Separator identifies the punctuation separating a footer token and value.