Testing Parsers and Queries
Thorough testing ensures parsers and queries work correctly. This guide covers the testing tools and workflows for nvim-treesitter.Quick Testing
Run all checks before submitting:- Lua code formatting and checking
- Query formatting, linting, and validation
- Documentation generation
- Test suite
Testing Parsers
Installation Tests
Test that your parser installs correctly:Health Check
Run Neovim’s health check:- Parser is installed
- Parser loads correctly
- ABI version is supported
- Queries are valid
Parser Validation Script
Check specific parsers:- ABI version for each parser
- State count (complexity metric)
- Any errors loading the parser
Manual Parser Testing
Use the tree-sitter CLI to test parsing:- The parsed syntax tree
- Any parse errors
- Parse time
Testing Queries
Query Validation
Run the full query test suite:- Format check - Ensures queries follow formatting standards
- Lint check - Validates captures are correct for Neovim
- Pattern check - Verifies patterns match the installed parser
Individual Query Checks
Run checks separately:Query Validation Script
Check specific languages:- Parse time for each query file
- Any query syntax errors
- Invalid patterns
Interactive Testing
InspectTree
View the syntax tree for the current buffer:- The complete syntax tree
- Node types and ranges
- Highlights the text corresponding to the node under cursor
- Understand parser output
- Find correct node types for queries
- Debug why patterns don’t match
EditQuery
Test query patterns interactively:- Write query patterns
- See which parts of the buffer are captured
- Test different capture groups
- Iterate quickly on query development
Inspect Highlights
See which highlight groups are applied at cursor:- Active highlight groups
- Capture names
- Priority values
- Syntax stack
- Debug highlight conflicts
- Verify captures work correctly
- Understand highlight precedence
Testing Highlights
Visual Testing
Verify highlighting
Check that:
- Keywords are highlighted correctly
- Strings and comments stand out
- Functions and types are distinguished
- No obvious missing or incorrect highlights
Highlight Assertions
The test suite supports highlight assertions for automated testing. Create test files intests/highlights/<language>/:
Testing Injections
Test multi-language support:Testing Folds
Test code folding:Test folding
- Use
zcto close folds - Use
zoto open folds - Use
zMto close all folds - Use
zRto open all folds
Testing Indentation
Test automatic indentation:Test indentation
- Type code and press Enter
- Use
=to re-indent lines - Test various language constructs
Running the Test Suite
The project includes a test suite using plenary.nvim.Run all tests
Run specific tests
Test output
Tests show:- Pass/fail status
- Error messages for failures
- Execution time
CI Testing
Required CI Workflows
These workflows verify:- Parser compiles on all platforms
- Query syntax is valid
- No regressions in parsing
Local CI Simulation
Use the CI install script:Lua Code Testing
Format Lua code
Check Lua code
- Type check Lua code
- Find potential errors
- Validate API usage
Documentation Testing
Generate documentation
SUPPORTED_LANGUAGES.md with:
- List of supported languages
- Query availability per language
- Maintainer information
Common Test Failures
Query linting fails
Error: Invalid captures Solution: Check the valid captures list and use only approved captures for Neovim.Query checking fails
Error: Pattern doesn’t match Solution:- Use
:InspectTreeto see actual node types - Verify parser is installed:
:TSInstall <language> - Check that pattern syntax is correct
Parser loading fails
Error: ABI version mismatch Solution: Parser must support the latest ABI. Update the parser or contact parser maintainers.Highlights don’t work
Diagnosis:- Run
:Inspectat the position - Check
:InspectTreefor node types - Run
make lintqueryfor validation
- Ensure capture names match Neovim’s valid captures
- Verify patterns match actual node types
- Check highlight groups are defined in your colorscheme
Performance Testing
Query performance
The check script shows parse times:- Queries taking > 10ms to parse
- Significant time differences between similar languages
Parser performance
Usetree-sitter parse to measure:
Before Submitting
Run the complete test suite:- All tests pass
- No linting errors
- Documentation is updated
- New parsers install correctly
- Queries are validated
Continuous Testing
As a query maintainer:- Monitor parser updates - Parser changes may break queries
- Test with real code - Use actual projects in your language
- Address issues promptly - Users rely on working queries
- Update queries - Keep pace with language evolution
Next Steps
- Review contributing overview
- Learn about adding parsers
- Study query writing guidelines