GrammarRegistry
Description
This class holds the grammars used for tokenizing.
An instance of this class is always available as the atom.grammars global.
API documentation
All methods
::maintainLanguageMode(buffer)
set a TextBuffer's language mode based on its path and content, and continue to update its language mode as grammars are added or updated, or the buffer's file path changes.
| Argument | Description |
|---|---|
buffer
|
The TextBuffer whose language mode will be maintained. |
Return values
-
Returns a Disposable that can be used to stop updating the buffer's language mode.
::assignLanguageMode(buffer, languageId)
Force a TextBuffer to use a different grammar than the one that would otherwise be selected for it.
| Argument | Description |
|---|---|
buffer
|
The TextBuffer whose grammar will be set. |
languageId
|
The String id of the desired language. |
Return values
-
Returns a Boolean that indicates whether the language was successfully found.
::assignGrammar(buffer, grammar)
Force a TextBuffer to use a different grammar than the one that would otherwise be selected for it.
| Argument | Description |
|---|---|
buffer
|
The TextBuffer whose grammar will be set. |
grammar
|
The desired Grammar. |
Return values
-
Returns a Boolean that indicates whether the assignment was successful
::getAssignedLanguageId()
Get the languageId that has been explicitly assigned to
the given buffer, if any.
Return values
-
Returns a String id of the language
::autoAssignLanguageMode(buffer)
Remove any language mode override that has been set for the given TextBuffer. This will assign to the buffer the best language mode available.
| Argument | Description |
|---|---|
buffer
|
The TextBuffer. |
::selectGrammar(filePath, fileContents)
Select a grammar for the given file path and file contents.
This picks the best match by checking the file path and contents against each grammar.
| Argument | Description |
|---|---|
filePath
|
A String file path. |
fileContents
|
A String of text for the file path. |
Return values
-
Returns a Grammar, never null.
::getGrammarScore()
Return values
-
Returns a Number representing how well the grammar matches the
filePathandcontents.
::onDidAddGrammar(callback)
Invoke the given callback when a grammar is added to the registry.
| Argument | Description |
|---|---|
callback
|
Function to call when a grammar is added. |
grammar
|
Grammar that was added. |
Return values
-
Returns a Disposable on which
.dispose()can be called to unsubscribe.
::onDidUpdateGrammar(callback)
Invoke the given callback when a grammar is updated due to a grammar it depends on being added or removed from the registry.
| Argument | Description |
|---|---|
callback
|
Function to call when a grammar is updated. |
grammar
|
Grammar that was updated. |
Return values
-
Returns a Disposable on which
.dispose()can be called to unsubscribe.
::addInjectionPoint(grammarId, injectionPoint)
Specify a type of syntax node that may embed other languages.
| Argument | Description |
|---|---|
grammarId
|
The String id of the parent language |
injectionPoint
|
An Object with the following keys: |
type
|
The String type of syntax node that may embed other languages |
language
|
A Function that is called with syntax nodes of the specified |
content
|
A Function that is called with syntax nodes of the specified |
includeChildren
|
A Boolean that indicates whether the children (and, in fact, all descendants) of the nodes returned by |
newlinesBetween
|
A Boolean that indicates whether each node returned from |
languageScope
|
A String or Function that returns the desired scope name to apply to each of the injection's buffer ranges. Defaults to the injected grammar's own language scope — e.g., |
coverShallowerScopes
|
A Boolean that indicates whether this injection should prevent shallower layers (including the layer that created this injection) from adding scopes within any of this injection's buffer ranges. Useful for injecting languages into themselves — for instance, injecting Rust into Rust macro definitions. |
includeAdjacentWhitespace
|
A Boolean that indicates whether the injection's buffer range(s) should include whitespace that occurs between two adjacent ranges. Defaults to |
::loadGrammar(grammarPath, callback)
Read a grammar asynchronously and add it to the registry.
| Argument | Description |
|---|---|
grammarPath
|
A String absolute file path to a grammar file. |
callback
|
A Function to call when loaded with the following arguments: |
error
|
An Error, may be null. |
grammar
|
A Grammar or null if an error occurred. |
::loadGrammarSync(grammarPath)
Read a grammar synchronously and add it to this registry.
| Argument | Description |
|---|---|
grammarPath
|
A String absolute file path to a grammar file. |
Return values
-
Returns a Grammar.
::readGrammar(grammarPath, callback)
Read a grammar asynchronously but don't add it to the registry.
| Argument | Description |
|---|---|
grammarPath
|
A String absolute file path to a grammar file. |
callback
|
A Function to call when read with the following arguments: |
error
|
An Error, may be null. |
grammar
|
A Grammar or null if an error occurred. |
Return values
-
Returns undefined.
::readGrammarSync(grammarPath)
Read a grammar synchronously but don't add it to the registry.
| Argument | Description |
|---|---|
grammarPath
|
A String absolute file path to a grammar file. |
Return values
-
Returns a Grammar.
::getGrammars(options)
Get all the grammars in this registry.
| Argument | Description |
|---|---|
options
|
optional |
includeTreeSitter
|
optional
Boolean Set to include Tree-sitter grammars |