Skip to content

Full-featured CodeMirror 6 language support for Rego (Open Policy Agent). Includes syntax highlighting with comprehensive grammar, keyword and built-in function autocomplete, context-aware path completions, hover documentation, and proper code folding for rules, comprehensions, and data structures.

License

Notifications You must be signed in to change notification settings

HZMonama/codemirror-lang-rego

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 input and data JSON schemas to get intelligent completions for paths like input.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-rego

Usage

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/else chains
  • some/every quantifiers
  • 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.

About

Full-featured CodeMirror 6 language support for Rego (Open Policy Agent). Includes syntax highlighting with comprehensive grammar, keyword and built-in function autocomplete, context-aware path completions, hover documentation, and proper code folding for rules, comprehensions, and data structures.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published