Introducing the Rego extension for the Zed editor
Let’s go straight to the TL;DR: we have just released a new Rego extension for the popular Zed editor! Providing both syntax highlighting and all the language server features of Regal that were previously available for editors like VS Code, or Neovim. The extension is available from the Zed extensions view, and you’ll find it by searching for “Rego”.
While it’s a first release, it builds upon two mature open source projects — the tree-sitter grammar for Rego (for syntax highlighting) and the Regal language server for everything else: linting, code completion, go to definition, tooltips on hover, formatting, and much more.
Either head straight to the Extensions for Zed and download the Rego extension, or keep reading if you want to learn more about it first!
How we got here
Much of our recent work in the developer relations team has revolved around the Rego authoring experience. Following our past work on Regal, our open source linter for OPA and Rego, we saw a tool that wasn’t just useful for catching bugs or preventing common mistakes, but one that many users found helpful for learning Rego. For any issue reported by Regal, like an unresolved import, or a messy rule declaration, users are given a link to Regal’s documentation for that issue, where they can learn more about why it’s considered a problem, what they can do to solve it, or if they so prefer, how to ask Regal to ignore it.
While running Regal from the command line is an important use case, we knew from the start that we’d want to bring Regal as close as possible to its users — and that means inside their editors! The existing extensions (VS Code, IntelliJ, others) are all great, but they require different skills and a lot of effort to maintain.
While an integration could call the “regal lint” command and display the results in the editor somewhere, a more modern alternative is to leverage a language server. As the name indicates, this is a continuous process that clients (editors) connect to in order to ask for linting, formatting, document symbols, and a whole bunch of other things the server can provide. A clear benefit of this model is how any compatible editor can use the same server, meaning that we’d be able to avoid having to repeat much of the logic we’d otherwise have to implement, and in different languages and frameworks. Since we didn’t have a language server for Rego, we built one and made it part of Regal.
The first client integration was for the OPA VS Code extension, and during the last couple of months, we’ve ticked off item after item from the long shopping list of language features that the language server protocol defines. Working with OPA and Rego in the VS Code extension is now an experience on par with the most established programming languages, and we intend to make it even better! But what does this all have to do with our new Rego extension for Zed?
Zed
When we first heard of the Zed editor — and there’s been quite a lot said about it this year — we got excited. A new editor from the team that previously built the Atom editor and the Electron framework seemed promising. Its focus on performance and simplicity clearly appealed not just to us, but to the larger developer community, and when the editor was made open source in the beginning of this year, we knew we’d have to take a closer look.
Zed delivered on most of its promises, and we’ve all enjoyed the editing experience provided. However, Zed had a major flaw — we couldn’t use it for Rego development. The first time we checked, there wasn’t any real extension mechanism in Zed, but language integrations were either included in the editor itself, or not at all. A few months ago, that changed. The new extension system allows custom code written in Rust to provide support for any language not supported by Zed itself.
When we say “written in Rust”, that’s a bit of an exaggeration. All that this code (currently 118 lines) does is to initialize a language server for the chosen language, and the language server takes it from there. And wow, did we not just build a language server for Rego? Yes, we did! This means that all the features we’d previously enjoyed having in VS Code were now available in Zed, without almost any additional work. Amazing! Syntax highlighting is as previously mentioned provided by another project — also conveniently available already.
Features
So what’s the developer experience like, working with the Rego extension for Zed? If you’ve used the OPA VS Code extension before, you’ll probably recognize most of the features already. And that’s really the point of using a language server! But for those of you who haven’t yet seen them, or perhaps aren’t aware of them all, let’s take a look at what they look like when used in Zed!
Diagnostics
Diagnostics, or linting, is of course the main feature of Regal, and violations of linter rules are displayed inline as you work with your Rego policies in Zed.
Code completion
We’ve done a lot of work on code completions recently, and users of the latest Regal versions may now benefit from context-aware suggestions while editing. Among the suggestions you’ll see is names of built-in functions, references to rules or packages, keywords and much more.
Code actions
Code actions allow Zed to send commands to Regal that will be executed by the language server. Regal currently supports actions for formatting, making a policy compatible with Rego v1, and fixing a few simple linter warnings, like using =
instead of :=
for assignment.
See available code actions by clicking the lightning symbol in the gutter, or by pressing ctrl/cmd + .
Hover
Hovering over any built-in function call will provide documentation for that function in a tooltip. Big productivity win not having to constantly switch over to the OPA docs for that!
You may notice how the middle section looks a little odd. Zed apparently doesn’t render markdown tables in tooltips yet. We’ll monitor the progress of that issue, but in the meantime we’ll make a special case for Zed in the next Regal release, and present the information in another format.
Go to definition
Ctrl / Cmd + clicking on any reference to a rule or function will take you directly to the definition of that function — wherever in the workspace it may be.
Folding ranges
Folding ranges allows hiding large rule bodies, comments or import blocks, and expanding them only when needed.
Document and workspace symbols
Document and workspace symbols provides a quick way to search your entire Rego project for symbols by their name, and click them to navigate there directly.
Inlay hints
Zed allows Regal to provide inlay hints for built-in functions (and in the future, possibly custom functions too). These hints provide valuable information for what the arguments for any given function mean. Note that you may need to enable inlay hints in the command palette to see them!
Formatting
Using the “opa fmt” command to format your Rego source code is a best practice, and is made simple in Zed through the format command.
Tasks
Finally, users may wish to set up custom tasks in order to run common commands, like “opa eval” or “opa build”. This is easily done by adding a .zed/tasks.json file to the project root directory. An example task.json file for running all the tests in the workspace may look like this:
[
{
"label": "opa test",
"command": "opa",
"args": ["test", "$ZED_WORKTREE_ROOT/policies"]
}
]
The task is then made available via the command palette.
Wrapping up
We believe that developers should be able to work with OPA and Rego in the environment they normally use for development. Familiarity with the environment, its hotkeys, customizations and configuration, is key for a productive developer experience.
With the Rego extension for Zed now made available, we’re excited to bring our favorite policy language to a new and exciting editor. Not just in a basic format, but an experience that is close to the one we’ve got in VS Code and other editors. Future improvements in the Regal language server now mean that all editors benefit, and we can’t wait to show you some of the things we’ve got lined up next. In the meantime, we hope you enjoy working with OPA and Rego in Zed!
For questions, discussions and announcements related to Regal and other Styra products, services and open source projects, please join the Styra community on Slack!