Tree-sitter grammar for mtlog message templates, providing syntax highlighting and structural navigation for editors with native tree-sitter support.
- π¨ Syntax Highlighting - Distinguishes properties, literals, and format specifiers
- π Zero-latency - Native tree-sitter performance (<1ms for typical templates)
- π― Smart Navigation - Structural text objects for properties and formats
- π Template Types - Supports
{Property},{{.GoTemplate}},${Builtin} - π Format Specifiers - Recognizes
:F2,:yyyy-MM-dd, etc. - π·οΈ Capture Hints -
@for complex types,$for scalars - π OTEL Properties - Dotted names like
{http.method}
Using nvim-treesitter:
-- Add to your treesitter config
require'nvim-treesitter.configs'.setup {
ensure_installed = { "mtlog", "go" },
highlight = { enable = true },
textobjects = {
select = {
enable = true,
keymaps = {
["ap"] = "@property.outer",
["ip"] = "@property.inner",
["af"] = "@format.outer",
["if"] = "@format.inner",
}
},
move = {
enable = true,
goto_next_start = { ["]p"] = "@property.outer" },
goto_previous_start = { ["[p"] = "@property.outer" },
}
}
}Add to your languages.toml:
[[languages]]
name = "mtlog"
scope = "source.mtlog"
injection-regex = "mtlog"
file-types = []
[[grammars]]
name = "mtlog"
source = { git = "https://github.com/willibrandon/tree-sitter-mtlog" }Install via the runtime directory:
hx --grammar fetch mtlog
hx --grammar build mtloglogger.Info("User {UserId} logged in")
logger.Error("Failed to process {Count} items")logger.Info("Price: {Amount:F2} USD")
logger.Info("Date: {Timestamp:yyyy-MM-dd HH:mm:ss}")logger.Info("Order {@Order} created") // @ = capture complex type
logger.Info("Error: {$Error}") // $ = force scalarlogger.Info("User {{.UserId}} from {{.Location}}")formatter.Format("${Timestamp} [${Level}] ${Message}")logger.Info("HTTP {http.method} to {http.url} returned {http.status_code}")npm install
npm run buildnpm test # Run all tests
npm run test:update # Update test expectations
npm run parse <file> # Parse a file
npm run highlight <file> # Test highlighting
# Test highlight samples
npx tree-sitter parse test/highlight/*.mtlognpm run benchmark # Show parsing speed from test suiteThis grammar focuses exclusively on syntax highlighting and navigation. It deliberately excludes:
- β Validation (handled by mtlog-analyzer)
- β Type checking
- β Cross-references
- β Diagnostics
The grammar is permissive by design, accepting malformed templates to maintain highlighting during active typing.
The parser is optimized for zero-latency editor responsiveness:
| Metric | Target | Actual | Result |
|---|---|---|---|
| Average parsing speed | >100 bytes/ms | 6779 bytes/ms | β 67x faster |
| 100-char template | <1ms | 0.015ms | β 67x faster |
| 1000-char template | <5ms | 0.15ms | β 33x faster |
| Test suite | 100% pass | 24/24 | β All passing |
Contributions are welcome! Please ensure:
- All tests pass:
npm test - Performance benchmarks are maintained
- Changes align with the single-responsibility principle (syntax only)
- mtlog - High-performance structured logging library