Minga.Language.Grammar (Minga v0.1.0)

Copy Markdown View Source

Maps filetypes to tree-sitter grammar names and locates highlight queries.

Filetypes (atoms from Minga.Language.Filetype) resolve to tree-sitter grammar names from the registered %Minga.Language{} definitions. The fallback grammar name comes from the language record itself, not a static in-module table. Highlight queries are loaded from priv/queries/{language}/highlights.scm with an optional user override in ~/.config/minga/queries/{language}/highlights.scm.

Summary

Types

A tree-sitter language name.

Functions

Returns the expected path for a dynamically-loaded grammar shared library.

Initializes the dynamic language registry ETS table.

Returns the path to the injection query file for a language.

Returns the tree-sitter language name for a filetype atom.

Returns the path to the highlight query file for a language.

Reads the injection query content for a language.

Reads the highlight query content for a language.

Registers a dynamic filetype-to-grammar mapping.

Returns the full filetype-to-grammar mapping, including dynamic registrations.

Types

language()

@type language() :: String.t()

A tree-sitter language name.

Functions

dynamic_grammar_path(name)

@spec dynamic_grammar_path(String.t()) :: String.t()

Returns the expected path for a dynamically-loaded grammar shared library.

Path: ~/.config/minga/grammars/{name}.so (or .dylib on macOS).

init_registry()

@spec init_registry() :: :ok

Initializes the dynamic language registry ETS table.

Called once at application startup. The table stores runtime-registered filetype-to-grammar mappings from extensions. Lookups check this table first, then fall back to the grammar name on the registered %Minga.Language{} definition.

injection_query_path(language)

@spec injection_query_path(language()) :: String.t() | nil

Returns the path to the injection query file for a language.

Checks user config dir first (~/.config/minga/queries/{lang}/injections.scm), then falls back to priv/queries/{lang}/injections.scm.

Returns nil if no injection query file exists.

language_for_filetype(filetype)

@spec language_for_filetype(atom()) :: {:ok, language()} | :unsupported

Returns the tree-sitter language name for a filetype atom.

Checks the dynamic registry (populated by extensions) first, then falls back to the grammar name on the registered %Minga.Language{} definition.

Examples

iex> Minga.Language.Grammar.language_for_filetype(:elixir)
{:ok, "elixir"}

iex> Minga.Language.Grammar.language_for_filetype(:typescript_react)
{:ok, "tsx"}

iex> Minga.Language.Grammar.language_for_filetype(:text)
:unsupported

query_path(language)

@spec query_path(language()) :: String.t() | nil

Returns the path to the highlight query file for a language.

Checks user config dir first (~/.config/minga/queries/{lang}/highlights.scm), then falls back to priv/queries/{lang}/highlights.scm.

Returns nil if no query file exists.

read_injection_query(language)

@spec read_injection_query(language()) ::
  {:ok, String.t()} | {:error, :no_query | File.posix()}

Reads the injection query content for a language.

Returns {:ok, query_text} or {:error, reason}.

read_query(language)

@spec read_query(language()) :: {:ok, String.t()} | {:error, :no_query | File.posix()}

Reads the highlight query content for a language.

Returns {:ok, query_text} or {:error, reason}.

register_language(filetype, language)

@spec register_language(atom(), String.t()) :: :ok

Registers a dynamic filetype-to-grammar mapping.

Extensions call this to make their grammar available for syntax highlighting. The mapping is checked before the registered-language fallback, so extensions can override bundled grammars.

Examples

Minga.Language.Grammar.register_language(:org, "org")
Minga.Language.Grammar.register_language(:astro, "astro")

supported_languages()

@spec supported_languages() :: %{required(atom()) => language()}

Returns the full filetype-to-grammar mapping, including dynamic registrations.