tokens

package
v0.0.0-...-b59e95e Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package tokens provides segmentation for multiline YAML tokens and utilities for syntax highlighting.

Token Segments

YAML lexers produce tokens that may span multiple lines (block scalars, multiline strings, folded content).

When rendering YAML line-by-line for display or editing, each line needs its portion of such tokens while still knowing which original token it came from. This package bridges that gap.

A Segment pairs an original source token with a "part" token representing one line's portion.

For a three-line block scalar, you get three Segments that share the same source pointer but have different parts.

This shared pointer enables efficient deduplication: call Segments.SourceTokens to recover the original tokens without duplicates.

Building Line-Based Views

The [line] package uses Segments to represent a single line's worth of tokens and Segments2 for multiple lines.

Position-based queries like Segments2.TokenRangesAt find all ranges a token occupies across lines, which is useful for highlighting all parts of a token (e.g. for errors).

Pointer Safety

Source pointers are shared internally for equality checks and deduplication, but Segment.Source and Segment.Part return clones to prevent accidental modification. Use Segment.SourceEquals for pointer comparisons.

Syntax Highlighting

TypeStyle maps token types to style.Style values for syntax highlighting. It handles context-sensitive styling: a string followed by a colon is styled as a mapping key, not a plain string.

Multi-Document YAML

SplitDocuments splits a token stream at document headers ("---"), returning an iterator over separate token streams for each YAML document.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SplitDocuments

func SplitDocuments(tks token.Tokens) iter.Seq2[int, token.Tokens]

SplitDocuments splits a token stream into multiple token streams, one for each YAML document found (separated by '---' tokens).

The returned slices each contain tokens for a single document, preserving original token order and positions.

Each document header token ('---') is included at the start of its document.

func TypeStyle

func TypeStyle(tk *token.Token) style.Style

TypeStyle returns the style.Style for the given *token.Token's token.Type.

It handles context-sensitive styling: a string followed by a colon is styled as a mapping key, and tokens preceded by anchors or aliases inherit that styling.

func ValueOffset

func ValueOffset(tk *token.Token) int

ValueOffset calculates the byte offset where Value starts within the first non-empty line of the *token.Token's Origin.

This offset is used for string slicing operations.

Types

type Segment

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

Segment pairs an original source *token.Token with a segmented part token.

Multiple Segment values may share the same Segment.Source pointer while having distinct Segment.Part values.

Create instances with NewSegment.

func NewSegment

func NewSegment(source, part *token.Token) Segment

NewSegment creates a new Segment.

func (Segment) Contains

func (s Segment) Contains(tk *token.Token) bool

Contains reports whether the given *token.Token matches the source or part pointer of this Segment.

func (Segment) Part

func (s Segment) Part() *token.Token

Part returns a clone of the Segment's part *token.Token.

func (Segment) PartEquals

func (s Segment) PartEquals(tk *token.Token) bool

PartEquals reports whether the given *token.Token matches the part pointer.

func (Segment) Source

func (s Segment) Source() *token.Token

Source returns a clone of the Segment's source *token.Token.

func (Segment) SourceEquals

func (s Segment) SourceEquals(tk *token.Token) bool

SourceEquals reports whether the given *token.Token matches the source pointer.

func (Segment) Width

func (s Segment) Width() int

Width returns the rune count of this Segment's part, excluding any trailing newline.

type Segments

type Segments []Segment

Segments is a sequence of Segment values, typically representing a single line's tokens.

When constructed from a complete token stream, unique Segment.Source pointers represent the original tokens (deduplicated via Segments.SourceTokens).

For multiline tokens, multiple consecutive Segment values share the same Segment.Source pointer.

func (Segments) Append

func (s Segments) Append(source, part *token.Token) Segments

Append appends a new Segment to the Segments.

func (Segments) Clone

func (s Segments) Clone() Segments

Clone returns a copy of the Segments with cloned [Segment.Part]s but shared Segment.Source pointers.

Sources are intentionally shared since they are immutable references to original tokens.

func (Segments) Merge

func (s Segments) Merge(others ...Segments) Segments

Merge combines this Segments with others, preserving source pointer identity. Returns a new Segments containing all Segment values in order.

func (Segments) NextColumn

func (s Segments) NextColumn() int

NextColumn returns the next available column (0-indexed).

Note that token.Position is 1-indexed, in which case this value + 1 can be used.

Returns 0 if empty or no Segment has a valid position.

func (Segments) PartTokens

func (s Segments) PartTokens() token.Tokens

PartTokens returns clones of all Segment.Part [*token.Token]s in order.

func (Segments) SourceTokenAt

func (s Segments) SourceTokenAt(col int) *token.Token

SourceTokenAt returns a clone of the source *token.Token at the given 0-indexed column.

Returns nil if no token exists at that column.

func (Segments) SourceTokens

func (s Segments) SourceTokens() token.Tokens

SourceTokens returns clones of unique source tokens in order.

This is the inverse of segmentation: Segment values that share a Segment.Source pointer are deduplicated to return a clone of each original *token.Token once.

type Segments2

type Segments2 []Segments

Segments2 represents a slice of Segments, i.e. a 2-dimensional collection of Segment values. Each element typically represents one line's Segments.

func (Segments2) ContentRangesAt

func (s2 Segments2) ContentRangesAt(idx, col int) position.Ranges

ContentRangesAt returns position.Ranges for content at the given position, excluding leading and trailing spaces.

Returns nil if there is no content at the given position or if the token is all whitespace.

func (Segments2) TokenRangesAt

func (s2 Segments2) TokenRangesAt(idx, col int) position.Ranges

TokenRangesAt returns position.Ranges for all Segment values that share the same source token as the Segment at the given 0-indexed idx (typically line) and col.

Positions are calculated based on Segment widths.

Returns nil if the position is out of bounds or no Segment exists there.

Jump to

Keyboard shortcuts

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