language

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package language holds the language registry and language configurations (SPEC §8.1). The registry has plain text and nothing else of its own: the bundled languages are the language packs under extensions/ (SPEC §10.15), which the extension host adds and takes away.

A Registry is filled from VS Code extensions: the languages and grammars that the package.json of an extension contributes. It tells the language of a file (Detect, with the user's files.associations first), gives the language configuration of a language (Configuration) and the tokenizer of a language (NewGrammar), and it is the textmate.Registry that grammars find each other in. Extensions are added with AddExtension (for good) and AddNamedExtension (RemoveExtension takes them away again); Version counts the changes.

Index

Constants

View Source
const PlainTextID = "plaintext"

PlainTextID is the id of the language of a file that no language claims.

Variables

This section is empty.

Functions

This section is empty.

Types

type AutoClosingPair

type AutoClosingPair struct {
	Pair
	// NotIn lists the kinds of token the pair is not closed in: string, comment or regex.
	NotIn []string
	// BeforeText, when it is not nil, must match the text before the cursor for the pair to be
	// closed.
	BeforeText *regex.Regexp
}

AutoClosingPair is a pair that is closed automatically when its opening is typed.

type CommentRule

type CommentRule struct {
	// LineComment starts a comment that ends with the line; empty for none.
	LineComment string
	// LineCommentNoIndent makes the line comment start at the first column when it is toggled.
	LineCommentNoIndent bool
	// BlockComment is the start and end of a block comment; nil for none.
	BlockComment *Pair
}

CommentRule says how comments are written.

type Configuration

type Configuration struct {
	// Comments tells how comments are written.
	Comments *CommentRule
	// Brackets are the bracket pairs, for matching and for the indentation.
	Brackets []Pair
	// AutoClosingPairs are the pairs closed when the opening is typed.
	AutoClosingPairs []AutoClosingPair
	// SurroundingPairs are the pairs that can surround a selection.
	SurroundingPairs []Pair
	// ColorizedBracketPairs are the pairs whose levels have colors.
	ColorizedBracketPairs []Pair
	// AutoCloseBefore are the characters before which a pair is closed automatically.
	AutoCloseBefore *string
	// WordPattern matches a word of the language.
	WordPattern *regex.Regexp
	// IndentationRules decide the indentation.
	IndentationRules *IndentationRules
	// OnEnterRules say what Enter does.
	OnEnterRules []OnEnterRule
	// Folding says how the language is folded.
	Folding *FoldingRules
}

Configuration is the language configuration of a language (SPEC §8.1): what the editor needs to know about a language without a language server. A field that is nil (or has no value) is not defined.

func ParseConfiguration

func ParseConfiguration(languageID string, data []byte) (*Configuration, []error)

ParseConfiguration reads a language configuration file (language-configuration.json). What is not valid is left out and listed in the problems: ConfigurationSyntaxError for the errors of the JSON, ConfigurationNotObject for a file that is not an object, ConfigurationFieldInvalid for a field of the wrong type and ConfigurationRegexInvalid for a regular expression that does not compile.

func (*Configuration) Merge

func (c *Configuration) Merge(other *Configuration) *Configuration

Merge gives the configuration with the fields of other that are defined taking the place of the fields of c.

type EnterAction

type EnterAction struct {
	// Indent changes the indentation.
	Indent IndentAction
	// AppendText is typed after the line break and the indentation.
	AppendText string
	// RemoveText is the number of characters taken off the indentation.
	RemoveText int
}

EnterAction is the effect of an on-enter rule.

type FoldingRules

type FoldingRules struct {
	// OffSide makes the language fold by indentation.
	OffSide bool
	// HasOffSide tells whether the configuration says it.
	HasOffSide bool
	// MarkersStart and MarkersEnd, when both are not nil, match the lines that start and end a
	// region.
	MarkersStart, MarkersEnd *regex.Regexp
}

FoldingRules say how a language is folded.

type GrammarContribution

type GrammarContribution struct {
	// Language is the language the grammar tokenizes; empty for a grammar that other grammars
	// include.
	Language string
	// ScopeName is the scope of the grammar.
	ScopeName string
	// Path is the path of the grammar file, relative to the extension.
	Path string
	// EmbeddedLanguages maps scope names in the grammar to the ids of the languages in them.
	EmbeddedLanguages map[string]string
	// TokenTypes maps scope selectors to a token type: string, comment, regex or other.
	TokenTypes map[string]string
	// InjectTo lists the scopes of the grammars this one is injected into.
	InjectTo []string
}

GrammarContribution is an entry of contributes.grammars.

type GrammarSettings

type GrammarSettings struct {
	// Cache compiles the patterns; nil compiles them without a shared cache.
	Cache *regex.Cache
	// MaxLineBytes is editor.maxTokenizationLineLength: a line of this many bytes or more is one
	// token; zero is the default of the tokenizer (20,000).
	MaxLineBytes int
	// ScanTimeout is the longest a search of a pattern may take; zero is regex.ScanTimeout.
	ScanTimeout time.Duration
}

GrammarSettings are what NewGrammarWith adds to a grammar.

type IndentAction

type IndentAction int

IndentAction is what pressing Enter does to the indentation.

const (
	// IndentNone keeps the indentation of the line.
	IndentNone IndentAction = iota
	// IndentIndent indents one level more.
	IndentIndent
	// IndentOutdent puts the text after the cursor on its own line, one level out, and the cursor
	// on an indented line between.
	IndentOutdent
	// IndentOutdentOne indents one level less.
	IndentOutdentOne
)

The indent actions, with the values of VS Code's IndentAction.

type IndentationRules

type IndentationRules struct {
	// IncreaseIndentPattern matches a line after which the next is indented.
	IncreaseIndentPattern *regex.Regexp
	// DecreaseIndentPattern matches a line that is indented less than the line before it.
	DecreaseIndentPattern *regex.Regexp
	// IndentNextLinePattern matches a line after which only the next line is indented.
	IndentNextLinePattern *regex.Regexp
	// UnIndentedLinePattern matches a line whose indentation is not looked at.
	UnIndentedLinePattern *regex.Regexp
}

IndentationRules decide the indentation of the line after a line, and of a line that is typed.

type Language

type Language struct {
	// ID is the language id, such as go.
	ID string
	// Name is the display name: the first alias, or the id.
	Name string
	// Aliases are the names that find the language, ID included.
	Aliases []string
	// Extensions are the file name endings of the language.
	Extensions []string
	// Filenames are the exact file names of the language.
	Filenames []string
	// FilenamePatterns are the file name globs of the language.
	FilenamePatterns []string
	// MIMETypes are the MIME types; the first is the main one.
	MIMETypes []string
}

Language is a language as the registry knows it, merged from every contribution to its id.

type LanguageContribution

type LanguageContribution struct {
	// ID is the language id, such as go.
	ID string
	// Aliases are the names of the language; the first is its display name.
	Aliases []string
	// Extensions are file name endings, such as .go.
	Extensions []string
	// Filenames are exact file names, such as Makefile.
	Filenames []string
	// FilenamePatterns are file name globs.
	FilenamePatterns []string
	// MIMETypes are the MIME types of the language.
	MIMETypes []string
	// FirstLine is a regular expression for the first line of a file of the language.
	FirstLine string
	// Configuration is the path of the language configuration file, relative to the extension.
	Configuration string
}

LanguageContribution is an entry of contributes.languages.

type Manifest

type Manifest struct {
	// Name is the name of the extension.
	Name string
	// Languages are the contributes.languages that are valid.
	Languages []LanguageContribution
	// Grammars are the contributes.grammars that are valid.
	Grammars []GrammarContribution
	// ConfigurationDefaults are the settings that the extension gives to languages
	// (contributes.configurationDefaults, whose "[go]" blocks name languages): by language id.
	// The keys of the file that are not language blocks are not read.
	ConfigurationDefaults map[string]map[string]any
	// Problems are the contributions that were left out, and why.
	Problems []error
}

Manifest is what the registry reads of the package.json of a VS Code extension: the languages and the grammars it contributes.

func ParseManifest

func ParseManifest(data []byte) (*Manifest, error)

ParseManifest reads the languages and grammars of a package.json. A contribution that is not valid is left out and listed in Problems (the tags are LanguageContributionInvalid and GrammarContributionInvalid); a file that is not JSON has the error ManifestInvalid.

type OnEnterRule

type OnEnterRule struct {
	// BeforeText must match the text before the cursor.
	BeforeText *regex.Regexp
	// AfterText, when it is not nil, must match the text after the cursor.
	AfterText *regex.Regexp
	// PreviousLineText, when it is not nil, must match the line above.
	PreviousLineText *regex.Regexp
	// Action is what happens.
	Action EnterAction
}

OnEnterRule is a rule for what pressing Enter does when the text around the cursor matches.

type Pair

type Pair struct {
	Open, Close string
}

Pair is an opening and a closing string, such as ( and ).

type Registry

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

Registry holds the languages and grammars of the extensions it was given, and tells the language of a file (SPEC §8.1). It implements textmate.Registry. It is safe for concurrent use.

func NewRegistry

func NewRegistry() *Registry

NewRegistry makes a registry that has the plain text language.

func (*Registry) AddExtension

func (r *Registry) AddExtension(fsys fs.FS) error

AddExtension adds the languages and grammars that the package.json at the root of fsys contributes. The error has the tag ExtensionUnreadable when there is no readable package.json and ManifestInvalid when it is not JSON; contributions that are not valid are left out and listed in Problems. The extension stays for the life of the registry; AddNamedExtension adds one that can be taken away.

func (*Registry) AddNamedExtension added in v1.0.0

func (r *Registry) AddNamedExtension(key string, fsys fs.FS) (*Manifest, error)

AddNamedExtension is AddExtension for an extension that RemoveExtension can take away again (a language pack of Emetos, SPEC §10.15). It returns what the package.json contributes, so that the caller can tell the languages and the settings defaults that came with it. The error has the tags of AddExtension and ExtensionKeyTaken when an extension with the key is in the registry, ExtensionKeyEmpty when the key is empty.

func (*Registry) ByMIMEType

func (r *Registry) ByMIMEType(mime string) (string, bool)

ByMIMEType gives the id of the language with the MIME type.

func (*Registry) ByName

func (r *Registry) ByName(name string) (string, bool)

ByName gives the id of the language that has the name as an alias or as its id, ignoring case.

func (*Registry) Code

func (r *Registry) Code(id string) int

Code is the number of a language in the tokens of the grammars; 0 for an id that is not registered.

func (*Registry) Configuration

func (r *Registry) Configuration(id string) *Configuration

Configuration gives the language configuration of the language: its configuration files merged in the order they were contributed. A language without any has an empty configuration. What could not be read is listed in Problems.

func (*Registry) ConfigurationDefaults

func (r *Registry) ConfigurationDefaults() map[string]map[string]any

ConfigurationDefaults gives the settings that the extensions contribute to languages (contributes.configurationDefaults), by language id: "[go]": {"editor.insertSpaces": false} is {"go": {"editor.insertSpaces": false}}. The maps are copies.

func (*Registry) Detect

func (r *Registry) Detect(filePath, firstLine string) string

Detect gives the id of the language of a file from its path and the text of its first line (the line may be empty): the associations of the user first, then those of the languages by file name, then glob, then extension, and the first line last. A file no language claims is plain text. For the same kind of match, the language registered last wins, and so does the longest glob or extension.

func (*Registry) Grammar

func (r *Registry) Grammar(scopeName string) *textmate.RawGrammar

Grammar implements textmate.Registry: the grammar with the scope name, read from its file the first time. A grammar that cannot be read is nil, and the reason is in Problems.

func (*Registry) HasGrammar

func (r *Registry) HasGrammar(languageID string) bool

HasGrammar tells whether a grammar is registered for the language.

func (*Registry) IDOfCode

func (r *Registry) IDOfCode(code int) string

IDOfCode is the id of the language with the number, or "" for 0 and for a number that is not one.

func (*Registry) Injections

func (r *Registry) Injections(scopeName string) []string

Injections implements textmate.Registry: the scopes of the grammars that are injected into the grammar with the scope name, or into a grammar whose scope is the start of it (a grammar injected into source.js is injected into source.js.embedded too).

func (*Registry) IsRegistered

func (r *Registry) IsRegistered(id string) bool

IsRegistered tells whether a language has the id.

func (*Registry) Language

func (r *Registry) Language(id string) (Language, bool)

Language gives the language with the id.

func (*Registry) Languages

func (r *Registry) Languages() []Language

Languages gives every language, in the order they were registered.

func (*Registry) NewGrammar

func (r *Registry) NewGrammar(languageID string, cache *regex.Cache) (*textmate.Grammar, error)

NewGrammar makes the tokenizer of a language: the grammar contributed for it, with the embedded languages and token types of its contribution (and of the grammars injected into it). cache, when it is not nil, compiles the patterns. The error has the tag GrammarNotRegistered when no grammar is for the language and GrammarNotFound when the grammar cannot be read.

func (*Registry) NewGrammarWith

func (r *Registry) NewGrammarWith(languageID string, settings GrammarSettings) (*textmate.Grammar, error)

NewGrammarWith is NewGrammar with settings.

func (*Registry) Problems

func (r *Registry) Problems() []error

Problems are the contributions, grammars and configurations that could not be used so far.

func (*Registry) RemoveExtension added in v1.0.0

func (r *Registry) RemoveExtension(key string) bool

RemoveExtension takes away the extension added with the key, with its languages, grammars, associations and configurations, and reports whether it was there. A language that remains in no extension is no longer registered, but its number stays reserved for it, so tokens already made and grammars that name the number stay valid, and an extension that comes back gets the same number.

func (*Registry) SetFileAssociations

func (r *Registry) SetFileAssociations(value any) []error

SetFileAssociations sets the associations of the user, from the value of the setting files.associations: a map from a glob to a language id. They win over the associations of the languages. A pattern whose value is not a string is left out and listed in the returned errors (tag AssociationInvalid). Patterns with equal length are tried in alphabetical order.

func (*Registry) Version added in v1.0.0

func (r *Registry) Version() uint64

Version counts the changes of the set of languages and grammars: it grows whenever an extension is added or removed. A cache of what the registry made (the grammars) is valid while it is the same.

Directories

Path Synopsis
internal
plist2json command
Command plist2json converts a TextMate grammar in the XML property list format to the JSON format that the grammar loader reads (SPEC §8.1).
Command plist2json converts a TextMate grammar in the XML property list format to the JSON format that the grammar loader reads (SPEC §8.1).
Package languagetest makes the registry of the bundled languages for tests.
Package languagetest makes the registry of the bundled languages for tests.
Package packfs gives tests the files of the language packs, which live in the extensions of Emetos (extensions/<name>/assets/, SPEC §8.1, §10.15).
Package packfs gives tests the files of the language packs, which live in the extensions of Emetos (extensions/<name>/assets/, SPEC §8.1, §10.15).

Jump to

Keyboard shortcuts

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