code

package
v3.20.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comment

type Comment struct {
	Text   string
	Source string
	Line   int
	Offset int
	Scope  string
	// Strip records, for each line of Text, how many bytes were taken off the
	// front of the matching Source line. An alert's column is meaningful in
	// Text; putting it back in Source means adding this.
	//
	// Empty for a comment that was never dedented, in which case the caller
	// falls back to measuring the source line itself.
	//
	// Not serialised: this is bookkeeping for putting an alert back where it
	// came from, not part of what a comment is.
	Strip []int `json:"-"`
}

Comment represents an in-code comment (line or block).

func GetComments

func GetComments(source []byte, lang *Language) ([]Comment, error)

GetComments returns all comments in the given source code.

func (Comment) StripAt

func (c Comment) StripAt(line int) (int, bool)

StripAt returns what came off the front of a line, 1-based as alerts are.

type Language

type Language struct {
	Delims *regexp.Regexp
	Parser *sitter.Language
	// Prefix matches a block comment's per-line decoration -- the ` *` that
	// starts each line of a JSDoc or Javadoc block.
	//
	// This is not the same thing as Cutset. Decoration is noise of a known
	// width that has to come off for the body to be valid markup; indentation
	// is meaningful and only its common part comes off. Conflating them is why
	// a cutset of " *" cannot work: `*` is both the decoration and Markdown's
	// list and emphasis marker, so a cutset wide enough to remove the noise
	// also eats a list.
	//
	// The match is blanked rather than deleted, which keeps every column where
	// it was and leaves the dedent below to remove the whitespace it becomes.
	Prefix  *regexp.Regexp
	Queries []core.Scope
	Cutset  string
	Padding padding
}

Language represents a supported programming language.

NOTE: What about haskell, less, perl, php, powershell, r, sass, swift?

func C

func C() *Language

func CSS

func CSS() *Language

func Cpp

func Cpp() *Language

func Elixir

func Elixir() *Language

Elixir extracts `#` comments and the prose held by the `@moduledoc`, `@doc`, `@typedoc` and `@shortdoc` attributes.

The attributes are the point. Elixir has no documentation comment syntax: its API documentation lives in module attributes holding a string or a heredoc, and that is what `mix docs` publishes and what a reader of the module reads first. A comment-only pass sees the asides and none of the documentation.

They are given a `doc` meta scope -- `text.comment.doc.line` and `text.comment.doc.block` -- so that published documentation can be held to a different standard than an implementation note, or excluded on its own.

The queries capture `quoted_content`, the body of the string, rather than the string itself. That leaves the delimiters out of the extracted text without a `Delims` pattern having to take them off, which matters for the single-quoted form: stripping its `"` with a regex would also strip any quote written inside the prose. `@doc false` and `@doc since: "1.0"` carry no prose, and neither matches a query that requires a string or sigil.

func GetLanguageFromExt

func GetLanguageFromExt(ext string) (*Language, error)

GetLanguageFromExt returns a Language based on the given file extension.

func Go

func Go() *Language

func Haskell

func Haskell() *Language

Haskell is parsed with the Elm grammar: the two languages share their comment syntax (`--`, `{- -}`), and comments are extras that survive error recovery, which is all comment extraction needs.

func Java

func Java() *Language

func JavaScript

func JavaScript() *Language

func Julia

func Julia() *Language

func Lua

func Lua() *Language

func PHP

func PHP() *Language

func Protobuf

func Protobuf() *Language

func Python

func Python() *Language

func QML

func QML() *Language

QML is parsed with the JavaScript grammar: QML's object syntax isn't valid JavaScript, but comments are extras and survive error recovery, which is all comment extraction needs.

func R

func R() *Language

R (and Perl, which shares the normed extension) is parsed with the Bash grammar: all three languages use `#` line comments, and comments are extras that survive error recovery, which is all comment extraction needs.

func Ruby

func Ruby() *Language

func Rust

func Rust() *Language

func Tsx

func Tsx() *Language

func TypeScript

func TypeScript() *Language

func YAML

func YAML() *Language

type QueryEngine

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

func NewQueryEngine

func NewQueryEngine(tree *sitter.Tree, lang *Language) *QueryEngine

Jump to

Keyboard shortcuts

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