syntax

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 8 Imported by: 0

README

Syntax Highlighting Package

A high-performance syntax highlighting package for the AINative-Code TUI, providing language-specific code block highlighting for 30+ programming languages.

Quick Start

Basic Usage
import "github.com/AINative-studio/ainative-code/internal/tui/syntax"

// Create highlighter with default config
h := syntax.NewHighlighter(syntax.DefaultConfig())

// Highlight a code block
code := `func main() {
    fmt.Println("Hello, World!")
}`
result, err := h.HighlightCode(code, "go")

// Or highlight markdown with multiple code blocks
markdown := `Here's some Go code:

` + "```go" + `
func main() {
    fmt.Println("Hello")
}
` + "```" + `

And some Python:

` + "```python" + `
print("Hello")
` + "```" + `
`

highlighted := h.HighlightMarkdown(markdown)
Configuration
// Use AINative branded theme
h := syntax.NewHighlighter(syntax.AINativeConfig())

// Custom configuration
config := syntax.HighlighterConfig{
    Theme:              "dracula",
    Enable:             true,
    FallbackToPlain:    true,
    MaxCodeBlockLines:  1000,
    UseTerminal256:     true,
    UseTrueColor:       false,
}
h := syntax.NewHighlighter(config)

Supported Languages

Core Languages (Required)
  • Go
  • Python
  • JavaScript
  • TypeScript
  • Rust
  • Java
  • C++
  • SQL
  • YAML
  • JSON
Additional Languages
  • Ruby
  • PHP
  • Swift
  • Kotlin
  • Scala
  • Bash/Shell
  • PowerShell
  • HTML
  • CSS/SCSS/Sass
  • Markdown
  • Dockerfile
  • Makefile
  • And 10+ more...

Features

  • Fast: Sub-millisecond highlighting for typical code blocks
  • Memory Efficient: ~26KB allocation for small blocks
  • Comprehensive: 30+ language support via Chroma
  • Configurable: Multiple themes and options
  • Robust: 90.7% test coverage
  • Terminal-Optimized: 256-color and true-color support

API Reference

Functions
NewHighlighter(config HighlighterConfig) *Highlighter

Creates a new syntax highlighter with the given configuration.

ParseCodeBlocks(text string) []CodeBlock

Extracts all code blocks from markdown text.

NormalizeLanguage(lang string) string

Normalizes language identifiers (e.g., "js" → "javascript").

IsLanguageSupported(language string) bool

Checks if a language is supported by the highlighter.

SupportedLanguages() []string

Returns a list of commonly used supported languages.

Methods
(*Highlighter) HighlightCode(code, language string) (string, error)

Applies syntax highlighting to a code snippet.

(*Highlighter) HighlightMarkdown(text string) string

Processes markdown text and highlights all code blocks.

Types
CodeBlock
type CodeBlock struct {
    Language string  // Language identifier
    Code     string  // Code content
    Raw      string  // Original markdown block
}
HighlighterConfig
type HighlighterConfig struct {
    Theme              string
    Enable             bool
    FallbackToPlain    bool
    MaxCodeBlockLines  int
    UseTerminal256     bool
    UseTrueColor       bool
}

Performance

Benchmarks (Apple M3)
Operation Time Memory Throughput
Parse code blocks 3.3 µs 4 KB 300K ops/sec
Highlight small (10 lines) 112 µs 26 KB 8.9K ops/sec
Highlight medium (100 lines) 216 µs 55 KB 4.6K ops/sec
Highlight large (500 lines) 681 µs 1 MB 1.4K ops/sec
Optimization Features
  • Line Limit: Blocks >1000 lines use simplified rendering
  • Lazy Processing: Only highlights visible blocks
  • Efficient Parsing: Single-pass regex matching
  • Memory Pooling: Reuse string builders

Testing

Run Tests
# All tests
go test ./internal/tui/syntax/...

# With coverage
go test -cover ./internal/tui/syntax/...

# Benchmarks
go test -bench=. ./internal/tui/syntax/...

# Verbose
go test -v ./internal/tui/syntax/...
Coverage
coverage: 90.7% of statements
Test Categories
  • Unit tests: Code parsing, normalization, highlighting
  • Integration tests: Full workflow, real-world examples
  • Benchmark tests: Performance validation
  • Edge case tests: Unicode, special characters, large blocks

Examples

Example 1: Highlight Go Code
h := syntax.NewHighlighter(syntax.DefaultConfig())

goCode := `package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
`

result, err := h.HighlightCode(goCode, "go")
if err != nil {
    log.Fatal(err)
}

fmt.Println(result)
Example 2: Process Markdown
h := syntax.NewHighlighter(syntax.AINativeConfig())

markdown := `# My Document

Here's a Python example:

` + "```python" + `
def greet(name):
    print(f"Hello, {name}!")

greet("World")
` + "```" + `
`

highlighted := h.HighlightMarkdown(markdown)
fmt.Println(highlighted)
Example 3: Check Language Support
if syntax.IsLanguageSupported("rust") {
    fmt.Println("Rust is supported!")
}

languages := syntax.SupportedLanguages()
fmt.Printf("Supported: %v\n", languages)
Example 4: Custom Theme
config := syntax.DefaultConfig()
config.Theme = "github"  // Use GitHub theme instead of Monokai

h := syntax.NewHighlighter(config)

Integration with TUI

The syntax highlighting is automatically integrated into the TUI's message rendering:

// In internal/tui/model.go
type Model struct {
    // ...
    syntaxHighlighter *syntax.Highlighter
    syntaxEnabled     bool
}

// In internal/tui/update.go
func (m *Model) renderMessages() string {
    // ...
    if m.syntaxEnabled && m.syntaxHighlighter != nil {
        content = m.syntaxHighlighter.HighlightMarkdown(msg.Content)
    }
    // ...
}
Control Methods
// Enable/disable highlighting
m.EnableSyntaxHighlighting()
m.DisableSyntaxHighlighting()

// Check status
enabled := m.IsSyntaxHighlightingEnabled()

// Get highlighter instance
h := m.GetSyntaxHighlighter()

Dependencies

  • github.com/alecthomas/chroma/v2 - Syntax highlighting engine
  • github.com/charmbracelet/lipgloss - Terminal styling
  • github.com/dlclark/regexp2 - Extended regex (chroma dependency)

License

Part of AINative-Code. See main project license.

Contributing

When adding support for new languages:

  1. Check if Chroma supports it: Chroma Languages
  2. Add language alias to NormalizeLanguage() if needed
  3. Add test case to TestIntegrationLanguageCoverage
  4. Update documentation with new language

Troubleshooting

Issue: Colors don't appear

  • Check terminal supports 256 colors: echo $TERM
  • Try: export TERM=xterm-256color

Issue: Slow performance

  • Check code block size (limit is 1000 lines)
  • Consider disabling for very large blocks

Issue: Language not recognized

  • Check SupportedLanguages() list
  • Try language alias (e.g., "js" for "javascript")
  • Verify code fence syntax: ```language

Resources


Version: 1.0.0 Status: Production Ready Coverage: 90.7%

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsLanguageSupported

func IsLanguageSupported(language string) bool

IsLanguageSupported checks if a language is supported by the highlighter

func NormalizeLanguage

func NormalizeLanguage(lang string) string

NormalizeLanguage normalizes language identifiers to Chroma lexer names

func SupportedLanguages

func SupportedLanguages() []string

SupportedLanguages returns a list of commonly used supported languages

Types

type CodeBlock

type CodeBlock struct {
	Language string
	Code     string
	Raw      string // Original markdown block including backticks
}

CodeBlock represents a parsed code block from markdown

func ParseCodeBlocks

func ParseCodeBlocks(text string) []CodeBlock

ParseCodeBlocks extracts code blocks from markdown text

type Highlighter

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

Highlighter provides syntax highlighting for code blocks

func NewHighlighter

func NewHighlighter(config HighlighterConfig) *Highlighter

NewHighlighter creates a new syntax highlighter with the given configuration

func (*Highlighter) HighlightCode

func (h *Highlighter) HighlightCode(code, language string) (string, error)

HighlightCode applies syntax highlighting to a code snippet

func (*Highlighter) HighlightMarkdown

func (h *Highlighter) HighlightMarkdown(text string) string

HighlightMarkdown processes markdown text and highlights all code blocks

type HighlighterConfig

type HighlighterConfig struct {
	Theme             string // Chroma theme name (e.g., "monokai", "dracula", "github")
	Enable            bool   // Whether syntax highlighting is enabled
	FallbackToPlain   bool   // Use plain text for unsupported languages
	MaxCodeBlockLines int    // Maximum lines before switching to simpler highlighting
	UseTerminal256    bool   // Use 256-color terminal mode
	UseTrueColor      bool   // Use true-color (24-bit) terminal mode
}

HighlighterConfig configures the syntax highlighter

func AINativeConfig

func AINativeConfig() HighlighterConfig

AINativeConfig returns a configuration with AINative branding colors

func DefaultConfig

func DefaultConfig() HighlighterConfig

DefaultConfig returns a default highlighter configuration

Jump to

Keyboard shortcuts

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