velocityjson
A Visual Studio Code extension for syntax highlighting, code folding, and formatting of Velocity and JSON mixed code.
Features
This extension provides comprehensive support for files that contain a mix of Velocity template language and JSON.
Syntax Highlighting
- JSON Syntax Highlighting: Properly highlights JSON structures including objects, arrays, strings, numbers, booleans, and null values
- Velocity Syntax Highlighting: Highlights Velocity directives, variables, and comments
- Dual Highlighting Engine: Uses both TextMate grammar (instant) and semantic tokens (context-aware) for best results
- Mixed Code Support: Seamlessly handles Velocity code within JSON structures
- Velocity variables inside JSON strings
- Velocity directives within JSON values
- Velocity code outside JSON structures
- Comprehensive Directive Support: Recognizes all common Velocity directives:
- Control structures:
#if, #else, #elseif, #end
- Loops:
#foreach
- Variable assignment:
#set
- Macros and includes:
#macro, #parse
- Flow control:
#break
Code Folding
- Velocity Block Folding: Fold
#if/#else/#elseif/#end blocks
- Velocity Macro Folding: Fold
#macro/#end blocks
- Block Comment Folding: Fold
#*/*# comment blocks
- JSON Folding: Fold JSON objects
{} and arrays []
- Continuation Folding:
#else and #elseif are treated as folding continuations
- Document Formatting: Format the entire document (Shift+Alt+F)
- Range Formatting: Format a selected range
- Velocity-Aware: Respects Velocity directive nesting during formatting
- JSON-Aware: Properly indents JSON key-value pairs
- Configurable: Uses VS Code's tab size and insert spaces settings
Installation
From VS Code Marketplace
- Open VS Code
- Press
Ctrl+P (Windows/Linux) or Cmd+P (macOS)
- Type
ext install velocityjson and press Enter
- Click Install
From Source
- Clone or download this repository
- Open the folder in VS Code
- Press
F5 to run the extension in development mode
- Or package the extension using
vsce package and install the .vsix file
Usage
- Open a file containing Velocity and JSON mixed code
- Ensure the file is recognized as "Velocity+JSON" language mode:
- Click on the language mode indicator in the bottom right corner
- Select "Velocity+JSON" from the dropdown menu
- The extension will automatically apply syntax highlighting, folding, and formatting
Supported File Extensions
The extension is configured to work with the following file extensions:
.vm (Velocity files)
.json (JSON files)
You can also manually set the language mode to "Velocity+JSON" for any file.
Extension Settings
This extension contributes the following settings:
velocity.highlightLevel: Controls the intensity of syntax highlighting (default: "full")
basic: Basic syntax highlighting
full: Full syntax highlighting with all features
Commands
| Command |
Description |
| Format Document |
Format the entire document (Shift+Alt+F) |
| Format Selection |
Format the selected range |
Example
Here's an example of how the extension highlights mixed Velocity and JSON code:
#set($count = 0)
#foreach($p in $param)
#if($p.sku)
#set($count = $count + 1)
#end
#end
{
"name": "Product",
"count": $count,
"items": [
#foreach($item in $items)
{
"id": "$item.id",
"name": "$item.name"
}#if($foreach.hasNext),#end
#end
]
}
Known Issues
Release Notes
0.0.1
Initial release of velocityjson extension with:
- Basic JSON syntax highlighting support
- Velocity directive and variable highlighting
- Mixed code support for Velocity within JSON structures
- Support for common Velocity directives
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
Enjoy!