sourcecode

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedExtension = errors.New("unsupported file extension")

Functions

func Names

func Names() []string

Names returns the built-in language names, sorted.

Types

type Document

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

func Parse

func Parse(filename string, data []byte, funcs ...OptionFunc) (*Document, error)

Parse builds a document whose section tree mirrors the declarations (functions, methods, types/classes...) found in a source file.

Parsing is best-effort: when the syntax tree cannot be obtained (timeout, unparseable input, oversized file) the document degrades to a single root section covering the whole file. Only unknown extensions and invalid language definitions are reported as errors.

func (*Document) AddCollection

func (d *Document) AddCollection(coll model.Collection)

func (*Document) Chunk

func (d *Document) Chunk(start int, end int) ([]byte, error)

Chunk implements model.Document.

func (*Document) Collections

func (d *Document) Collections() []model.Collection

Collections implements model.Document.

func (*Document) Content

func (d *Document) Content() ([]byte, error)

Content implements model.Document.

func (*Document) ETag

func (d *Document) ETag() string

ETag implements model.Document.

func (*Document) ID

func (d *Document) ID() model.DocumentID

ID implements model.Document.

func (*Document) Metadata

func (d *Document) Metadata() map[string]any

Metadata implements model.WithMetadata.

func (*Document) Sections

func (d *Document) Sections() []model.Section

Sections implements model.Document.

func (*Document) SetETag

func (d *Document) SetETag(etag string)

func (*Document) SetMetadata

func (d *Document) SetMetadata(metadata map[string]any)

SetMetadata attaches arbitrary metadata used for filtering at search time.

func (*Document) SetSource

func (d *Document) SetSource(source *url.URL)

func (*Document) Source

func (d *Document) Source() *url.URL

Source implements model.Document.

type Language

type Language struct {
	// Name identifies the language in document metadata (e.g. "go", "python").
	Name string

	// Grammar lazily returns the tree-sitter grammar.
	Grammar func() *ts.Language

	// Query is a tree-sitter S-expression pattern capturing the declarations
	// to expose as sections, each tagged @def.
	Query string
	// contains filtered or unexported fields
}

Language describes how to parse and chunk one programming language.

func ByName

func ByName(name string) (*Language, bool)

ByName returns the built-in language definition with the given name.

type OptionFunc

type OptionFunc func(opts *Options)

func WithMaxParseBytes

func WithMaxParseBytes(maxBytes int) OptionFunc

func WithMaxWordPerSection

func WithMaxWordPerSection(maxWord int) OptionFunc

func WithRegistry

func WithRegistry(registry *Registry) OptionFunc

type Options

type Options struct {
	MaxWordPerSection int
	Registry          *Registry
	// MaxParseBytes caps structural extraction: larger files are indexed as a
	// single root section (later force-split by word budget).
	MaxParseBytes int
}

func NewOptions

func NewOptions(funcs ...OptionFunc) *Options

type Registry

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

Registry maps file extensions (lowercase, with leading dot) to languages.

func DefaultRegistry

func DefaultRegistry() *Registry

DefaultRegistry returns a registry with the built-in extension mapping.

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Lookup

func (r *Registry) Lookup(ext string) (*Language, bool)

func (*Registry) Register

func (r *Registry) Register(ext string, lang *Language)

func (*Registry) SupportedExtensions

func (r *Registry) SupportedExtensions() []string

SupportedExtensions returns the registered extensions, sorted.

type Section

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

func (*Section) Branch

func (s *Section) Branch() []model.SectionID

Branch implements model.Section.

func (*Section) Content

func (s *Section) Content() ([]byte, error)

Content implements model.Section.

Unlike markdown sections, the chunk is returned as-is: source code must not be re-rendered or trimmed.

func (*Section) Document

func (s *Section) Document() model.Document

Document implements model.Section.

func (*Section) End

func (s *Section) End() int

End implements model.Section.

func (*Section) ID

func (s *Section) ID() model.SectionID

ID implements model.Section.

func (*Section) Level

func (s *Section) Level() uint

Level implements model.Section.

func (*Section) Parent

func (s *Section) Parent() model.Section

Parent implements model.Section.

func (*Section) Sections

func (s *Section) Sections() []model.Section

Sections implements model.Section.

func (*Section) Start

func (s *Section) Start() int

Start implements model.Section.

Jump to

Keyboard shortcuts

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