codemirror-lang-rego
v0.1.0
Published
Rego language support for CodeMirror 6
Maintainers
Readme
codemirror-lang-rego
Rego language support for CodeMirror 6, the policy language of Open Policy Agent (OPA).
Features
- Syntax Highlighting: Full grammar support for Rego, including rules, comprehensions, and every/some quantifiers.
- Autocomplete: Suggestions for all OPA built-in functions and Rego keywords.
- Context-Aware Completions: Pass your
inputanddataJSON schemas to get intelligent completions for paths likeinput.user.roles. - Hover Tooltips: Documentation on hover for built-in functions and data paths.
- Folding & Indentation: Proper code folding for rule bodies, objects, arrays, and sets.
Installation
npm install codemirror-lang-regoUsage
Basic Setup
import { EditorState } from "@codemirror/state";
import { EditorView, basicSetup } from "codemirror";
import { rego } from "codemirror-lang-rego";
const state = EditorState.create({
doc: `package example
allow if {
input.user == "admin"
}`,
extensions: [basicSetup, rego()],
});
new EditorView({ state, parent: document.body });With Data Context (Advanced Autocomplete)
Provide JSON schemas for input and data to enable intelligent path completions.
import { rego, regoDataContext, setRegoDataContext } from "codemirror-lang-rego";
const inputSchema = {
type: "object",
children: {
user: { type: "string", example: "alice" },
roles: {
type: "array",
arrayItemType: { type: "string" },
example: ["admin", "viewer"],
},
},
};
const state = EditorState.create({
extensions: [
rego(),
regoDataContext({ input: inputSchema, data: null }),
],
});Now typing input. will suggest user and roles.
Updating Context Dynamically
If your input/data changes at runtime, dispatch an effect to update the context:
import { setRegoDataContext } from "codemirror-lang-rego";
view.dispatch({
effects: setRegoDataContext.of({
input: newInputSchema,
data: newDataSchema,
}),
});Configuration Options
rego({
autocomplete: true, // Enable keyword/built-in completions (default: true)
hover: true, // Enable hover tooltips (default: true)
dataContext: { // Initial data context (optional)
input: null,
data: null,
},
});API
rego(config?: RegoConfig): LanguageSupport
Creates the Rego language extension.
| Option | Type | Default | Description |
|---------------|-------------------|---------|--------------------------------------------------|
| autocomplete| boolean | true | Enable autocomplete for keywords and built-ins. |
| hover | boolean | true | Enable hover tooltips. |
| dataContext | RegoDataContext | null | Initial schema for input.* and data.* paths.|
regoDataContext(context: RegoDataContext): Extension
Standalone extension to initialize the data context. Use this if you want to separate the context from the main rego() call.
setRegoDataContext: StateEffect<RegoDataContext>
Dispatch this effect to update the data context on a live editor.
regoLanguage: LRLanguage
The raw Lezer-based LRLanguage instance, if you need low-level access.
RegoDataContext
interface RegoDataContext {
input: SchemaNode | null;
data: SchemaNode | null;
}
interface SchemaNode {
type: "object" | "array" | "string" | "number" | "boolean" | "null";
children?: Record<string, SchemaNode>;
arrayItemType?: SchemaNode;
example?: unknown;
source?: string;
}Schema Format
The SchemaNode format is a simple tree structure:
{
"type": "object",
"children": {
"user": { "type": "string", "example": "alice" },
"attributes": {
"type": "object",
"children": {
"department": { "type": "string", "example": "engineering" }
}
}
}
}type: The JSON type of the node.children: For objects, a map of property names to child nodes.arrayItemType: For arrays, the schema of array items.example: An example value shown in tooltips.source: Optional label (e.g., "OIDC Token") for documentation.
Built-in Functions
This package includes OPA's built-in function definitions (from capabilities.json), providing autocomplete and hover docs for functions like count, startswith, json.marshal, etc.
Grammar
The Rego grammar is implemented using Lezer, CodeMirror's incremental parser system. It supports:
- Package and import declarations
- Complete, partial, and function rules
if/elsechainssome/everyquantifiers- Comprehensions (set, array, object)
- All operators and literals
License
MIT
Contributing
Contributions are welcome! Please open an issue or pull request.
Acknowledgments
Created by Maynframe (https://maynframe.xyz). Built on CodeMirror, Lezer, and OPA.
