Skip to content

Type to search extensions

Erlang QoL Snippets

v0.1.2

Erlang QoL: module and comment templates as snippets, ported from the erlang-code-generation VSCode extension

Updated 1 month ago
70 downloads 0 stars Repository ↗

Erlang QoL Snippets

中文文档

Erlang code generation templates and everyday code snippets for the Zed editor, ported from the erlang-code-generation VSCode extension.

Features

  • 11 module templates — gen_server, gen_statem, supervisor, header (.hrl with include guard), empty module, Common Test suite, poolboy worker, cowboy websocket handler, cowboy REST handler, lager handler, escript
  • 3 comment templates — header / section / function
  • 14 everyday code snippets — log, receive, case, try, eunit tests, poolboy/cowboy/child-spec snippets

Requirements

  • Zed editor
  • An extension that provides the Erlang language (e.g. the official erlang extension — it also bundles the ELP and erlang_ls language servers). Snippets are scoped to the Erlang language.

Installation (development)

Until this extension is published to the Zed extension registry, install it as a dev extension:

  1. Zed → Extensions panel → Install Dev Extension (or run zed: install dev extension from the command palette)
  2. Select this repository's directory
  3. In Extensions panel, the card shows erlang-qol-snippets as a dev extension

Usage

Via snippet completion

Open an .erl / .hrl file and type a snippet prefix, then select it from the completion menu.

Prefix Snippet
log print to console
rec / reca receive / receive with after
case / if / try / ? case / if / try / inline try-catch
eunit eunit tests section
pools / cows / works / sups poolboy specs / cowboy web supervisor / worker child specs / supervisor child specs
comsec / funsec / comhdr comment: section / function / header
mod-genserver gen_server module template
mod-genstatem gen_statem module template
mod-supervisor supervisor module template
mod-header header (.hrl) template with include guard
mod-empty empty module template
mod-ct Common Test suite template
mod-poolboy-worker poolboy worker template
mod-websocket-handler cowboy websocket handler template
mod-rest-handler cowboy REST handler template
mod-lager-handler lager handler template
mod-escript escript template

Zed extensions cannot register commands, so module templates are triggered by binding the editor::InsertSnippet action in your user keymap (~/.config/zed/keymap.json on macOS and Linux, %APPDATA%\Zed\keymap.json on Windows):

[
  {
    "context": "Editor && !menu && !picker",
    "bindings": {
      "ctrl-shift-p g": ["editor::InsertSnippet", { "language": "erlang", "name": "module: gen_server template" }],
      "ctrl-shift-p s": ["editor::InsertSnippet", { "language": "erlang", "name": "module: gen_statem template" }],
      "ctrl-shift-p u": ["editor::InsertSnippet", { "language": "erlang", "name": "module: supervisor template" }],
      "ctrl-shift-p h": ["editor::InsertSnippet", { "language": "erlang", "name": "module: header template" }],
      "ctrl-shift-p e": ["editor::InsertSnippet", { "language": "erlang", "name": "module: empty template" }],
      "ctrl-shift-p c": ["editor::InsertSnippet", { "language": "erlang", "name": "module: CT template" }],
      "ctrl-shift-p p": ["editor::InsertSnippet", { "language": "erlang", "name": "module: poolboy worker template" }],
      "ctrl-shift-p w": ["editor::InsertSnippet", { "language": "erlang", "name": "module: websocket handler template" }],
      "ctrl-shift-p r": ["editor::InsertSnippet", { "language": "erlang", "name": "module: cowboy rest handler template" }],
      "ctrl-shift-p l": ["editor::InsertSnippet", { "language": "erlang", "name": "module: lager handler template" }],
      "ctrl-shift-p x": ["editor::InsertSnippet", { "language": "erlang", "name": "module: escript template" }],
      "ctrl-shift-p 1": ["editor::InsertSnippet", { "language": "erlang", "name": "comment: header" }],
      "ctrl-shift-p 2": ["editor::InsertSnippet", { "language": "erlang", "name": "comment: section" }],
      "ctrl-shift-p 3": ["editor::InsertSnippet", { "language": "erlang", "name": "comment: function" }]
    }
  }
]

Press ctrl-shift-p then the letter. Note that when a chord is pending, pressing ctrl-shift-p alone opens the command palette after a ~1s delay. $1–$4 tabstops guide you through module name, author, date and template-specific fields (e.g. record fields or exports).

Editing templates / adding new snippets

Zed requires all snippets for one language to live in a single file (snippets/erlang.json), so this repository maintains one JSON file per snippet in snippets-src/ and merges them with a build script.

  1. Edit snippets-src/<nn-name>.json (one snippet per file) or add a new one, e.g. 27-my-snippet.json:

    {
      "My snippet": {
        "prefix": "my",
        "body": ["...", "\t${1:placeholder}"],
        "description": "Optional description"
      }
    }
  2. Run the merge script (PowerShell 7 pwsh, no dependencies):

    .\build.ps1
  3. In Zed: Extensions panel → Rebuild on the dev extension card.

Notes:

  • Snippet names must be unique (the build script fails on duplicates).
  • Tabstops must be numbered continuously ($1, $2, ... $0 for the final position); placeholders with the same number are linked (typing once fills all occurrences).
  • Zed does not support snippet variables ($TM_FILENAME_BASE etc.) — use linked tabstops instead.
  • A literal $ inside the body must be escaped as \\$.
  • Do not edit snippets/erlang.json directly — it is generated by build.ps1. The script requires PowerShell 7 (pwsh): Windows PowerShell 5.1 formats ConvertTo-Json output differently and would rewrite the whole generated file. The build also fails if the prefix tables or keybinding snippet names in the READMEs drift from the snippets, and CI regenerates everything and fails if it is out of date.

Contributors

License

MIT — portions are derived from the MIT-licensed erlang-code-generation VSCode extension (Copyright (c) 2022 BOC Dev).

ZedExt An unofficial community project. Not affiliated with Zed Industries.

Data from Zed’s extension registry and GitHub · updated