Claude Skills Guide

Claude Code for Tree-Sitter Syntax Highlighting Guide

Tree-sitter has revolutionized how developers visualize and navigate code. As a robust parsing library, Tree-sitter generates precise syntax trees that power everything from editor highlighting to code intelligence tools. This guide shows you how to use Claude Code to work with Tree-sitter for syntax highlighting in your projects.

Understanding Tree-Sitter Fundamentals

Tree-sitter is a parser generator tool and an incremental parsing library. Unlike traditional regex-based approaches, Tree-sitter builds accurate parse trees by analyzing the grammatical structure of your code. This precision translates directly to superior syntax highlighting that understands code context.

The core concepts you need to grasp are:

  1. Grammars: Define the lexical and syntactic rules for a language
  2. Parse Trees: Hierarchical representations of code structure
  3. Nodes: Individual elements within the parse tree (functions, variables, keywords)
  4. Captures: Pattern matching rules that associate tree nodes with semantic names

Claude Code can help you generate grammars, write capture rules, and debug parsing issues efficiently.

Setting Up Tree-Sitter with Claude Code

Before diving into syntax highlighting, ensure you have the necessary tools installed. Tree-sitter requires a few dependencies:

# Install Tree-sitter CLI
npm install -g tree-sitter-cli

# Verify installation
tree-sitter --version

Once installed, you can use Claude Code to bootstrap new language grammars. Tell Claude about the language you want to support, and it can help generate the initial grammar structure:

// Example: A minimal JavaScript grammar snippet
module.exports = grammar({
  name: 'javascript',
  
  rules: {
    program: $ => repeat($._statement),
    
    _statement: $ => choice(
      $.expression_statement,
      $.variable_declaration,
      $.function_declaration
    ),
    
    expression_statement: $ => $.expression,
    
    variable_declaration: $ => seq(
      'const',
      $.identifier,
      '=',
      $.expression
    ),
    
    // Additional rules...
  }
});

Creating Effective Highlighting Rules

The real power of Tree-sitter lies in its capture system. By defining patterns that match specific node types, you can create sophisticated highlighting that responds to code semantics rather than just patterns.

Understanding Capture Groups

Captures associate node patterns with names that your highlighting theme can style:

// tree-sitter queries syntax
(function_declaration
  name: (identifier) @function.name)

(method_declaration
  name: (property_identifier) @method)

(call_expression
  function: (identifier) @function.call)

Each @capture name maps to a highlight group in your editor. This separation means you can update highlighting themes without touching the query logic.

Highlighting Different Code Elements

Here’s a practical query file for comprehensive JavaScript highlighting:

; Keywords
["const" "let" "var" "function" "return" "if" "else" "for" "while"] @keyword

; Functions
(function_declaration name: @function)
(arrow_function expression: (function_expression))

; Types
(identifier) @type
(type_annotation type: (primitive_type) @type.builtin)

; Strings
(string) @string
(template_string) @string

; Numbers
(number) @number

; Comments
(comment) @comment

; Variables and properties
(property_identifier) @property
(identifier) @variable

; Operators
(binary_expression operator: @operator)
(unary_expression operator: @operator)

Practical Examples with Claude Code

Claude Code excels at helping you write and debug Tree-sitter queries. Here’s how to approach common scenarios:

Example 1: Highlighting Decorators

Modern frameworks use decorators extensively. Here’s how to create queries that catch them:

(decorator
  name: (identifier) @decorator
  arguments: (call_expression arguments: (_) @decorator.args))

This captures both simple decorators like @decorator and those with arguments like @decorator(arg).

Example 2: Context-Aware String Highlighting

Different string types often warrant different visual treatment:

(string 
  (template_string) @string.special)

(string 
  (string_fragment) @string)

Example 3: Function Call vs. Definition

Distinguishing between function calls and definitions helps readers understand code flow:

(function_declaration name: @function.definition)
(call_expression function: (identifier) @function.call)
(call_expression function: (member_expression property: (property_identifier) @method.call))

Debugging Your Queries

When queries don’t match as expected, Tree-sitter provides debugging tools. Use the tree-sitter parse command to see the actual parse tree structure:

tree-sitter parse your-file.js

This output shows node types and their hierarchy. Compare this against your queries to identify mismatches. Claude Code can help interpret parse output and suggest corrections to your queries.

Most modern editors support Tree-sitter highlighting:

Neovim

Neovim has built-in Tree-sitter support:

-- Configuration in init.lua
require('nvim-treesitter.configs').setup({
  highlight = {
    enable = true,
    additional_vim_regex_highlighting = false,
  },
})

VS Code

Install the Tree-sitter extension and add your queries to the extension’s query directory.

Actionable Tips for Better Highlighting

  1. Start Simple: Begin with keywords and basic types before adding complexity
  2. Use Semantic Names: Choose capture names that convey meaning (@function, @variable, @type)
  3. Test Incrementally: Add queries one category at a time and verify they work
  4. use Claude Code: Describe what you want to highlight, and let Claude suggest queries
  5. Consider Performance: Complex queries across large files can slow parsing; optimize patterns

Advanced: Custom Grammars for Domain-Specific Languages

If you’re working with a DSL or custom configuration format, building a dedicated grammar provides the best highlighting experience. Define your language rules in a grammar.js file:

module.exports = grammar({
  name: 'my_config',
  
  extras: $ => [/\s/, $.comment],
  
  rules: {
    config: $ => seq(
      '{',
      repeat($.property),
      '}'
    ),
    
    property: $ => seq(
      $.key,
      ':',
      $.value
    ),
    
    key: $ => /[a-z_]+/,
    
    value: $ => choice(
      $.string,
      $.number,
      $.boolean,
      $.array
    ),
    
    // Define other node types...
  }
})

Generate the parser with tree-sitter generate, then write queries following the patterns shown earlier.

Conclusion

Tree-sitter syntax highlighting transforms code visualization from simple pattern matching to semantic understanding. By using Claude Code’s assistance, you can efficiently create and maintain highlighting rules that make your codebase more navigable and readable. Start with basic queries, iterate based on what you see in your editor, and gradually build comprehensive coverage for all the languages you work with.

The investment in well-crafted Tree-sitter queries pays dividends every time you open your editor and instantly recognize code structure at a glance.

Built by theluckystrike — More at zovo.one