codemirror-lang-terraform
v0.1.1
Published
Terraform language support for CodeMirror
Readme
CodeMirror Terraform Language Support
This repository provides a CodeMirror 6 language package for Terraform. It includes:
- A Lezer grammar for Terraform.
- Highlighting, indentation, and folding rules specific to Terraform syntax.
- An easy-to-use extension for CodeMirror 6.
Installation
- Clone this repository or install from npm (once published):
npm install codemirror-lang-terraform - Make sure you have the required CodeMirror 6 packages installed (e.g.
@codemirror/language).
Usage
In your CodeMirror setup code, import and include the Terraform language extension:
import { EditorView, basicSetup } from "@codemirror/basic-setup";
import { EditorState } from "@codemirror/state";
import { terraform } from "codemirror-lang-terraform";
const startState = EditorState.create({
doc: `# Example Terraform code
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t2.micro"
}`,
extensions: [basicSetup, terraform()],
});
const view = new EditorView({
state: startState,
parent: document.querySelector("#editor"),
});This will enable Terraform syntax highlighting, indentation, and folding while you edit .tf or .tfvars content.
Development
If you want to develop or modify this language package:
Clone the repository:
git clone https://github.com/enoreyes/lang-terraform cd lang-terraformInstall dependencies:
npm installBuild the project:
npm run prepareThis will use Rollup and the Lezer generator to produce compiled parser files in the
distfolder.Run tests:
npm testTests are kept in the
testdirectory, where parsing behavior is checked.
Grammar
The Terraform grammar file (src/terraform.grammar) uses Lezer’s grammar syntax to define tokens and parse rules, including interpolation, blocks, attributes, and resource definitions.
Key aspects include:
- Local token contexts for strings, to properly detect interpolation (
"${ ... }"). - Precedence rules to handle Terraform operators (
+,-,*,/,&&,||, etc.). - Nodes for
resource,module,provider,data, and other Terraform block types. - Special highlight styling for resource/module labels.
Contributing
- Fork this repo and make a branch for your feature or bugfix.
- Generate the parser by running
npm run prepareand ensure all tests pass withnpm test. - Submit a pull request with your changes.
License
This project is licensed under the MIT License. Please see the license file for more details.
