Skip to content

Tree-sitter grammar for mtlog message templates - provides syntax highlighting and structural navigation for {Property}, {{.GoTemplate}}, and ${Builtin} syntax

License

Notifications You must be signed in to change notification settings

willibrandon/tree-sitter-mtlog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

tree-sitter-mtlog

Tree-sitter grammar for mtlog message templates, providing syntax highlighting and structural navigation for editors with native tree-sitter support.

Features

  • 🎨 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}

Installation

Neovim

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" },
    }
  }
}

Zed

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" }

Helix

Install via the runtime directory:

hx --grammar fetch mtlog
hx --grammar build mtlog

Template Syntax

Basic Properties

logger.Info("User {UserId} logged in")
logger.Error("Failed to process {Count} items")

Format Specifiers

logger.Info("Price: {Amount:F2} USD")
logger.Info("Date: {Timestamp:yyyy-MM-dd HH:mm:ss}")

Capture Hints

logger.Info("Order {@Order} created")     // @ = capture complex type
logger.Info("Error: {$Error}")            // $ = force scalar

Go Templates

logger.Info("User {{.UserId}} from {{.Location}}")

Built-in Properties

formatter.Format("${Timestamp} [${Level}] ${Message}")

OTEL Properties

logger.Info("HTTP {http.method} to {http.url} returned {http.status_code}")

Development

Setup

npm install
npm run build

Testing

npm 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/*.mtlog

Benchmarking

npm run benchmark          # Show parsing speed from test suite

Design Philosophy

This 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.

Performance

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

Contributing

Contributions are welcome! Please ensure:

  1. All tests pass: npm test
  2. Performance benchmarks are maintained
  3. Changes align with the single-responsibility principle (syntax only)

License

MIT

Related Projects

  • mtlog - High-performance structured logging library

About

Tree-sitter grammar for mtlog message templates - provides syntax highlighting and structural navigation for {Property}, {{.GoTemplate}}, and ${Builtin} syntax

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published