CompMD Developer's Guide. Written for the @compmd packages at 0.14.0, and following them as they change. Download the book as PDF

21. Neovim

Writers edit CompMD in their own editors, and what an editor knows of CompMD comes from one program, the language server compmd-lsp (chapter 24). Neovim gets it, CompMD's grammars and the drawing from one plugin, compmd.nvim. This chapter is that plugin for a developer: setting it up, against a release or your own build of the server, what it does in a project of yours, and its configuration. Writing in Neovim is the Author's Guide's chapter 20.

Quick setup#

Neovim 0.12 or later, with nvim-treesitter's main branch, git and the tree-sitter command.

  1. The server: npm install -g @compmd/lsp (the binary for your platform, through a package of its own), cargo install compmd-lsp, or a binary from the GitHub release, checked against its SHA256SUMS.

  2. The plugin, comp-markdown/compmd.nvim, with any plugin manager; with lazy.nvim:

    return {
      { 'comp-markdown/compmd.nvim', dependencies = { 'nvim-treesitter/nvim-treesitter' } },
    }
    
  3. The grammars: :TSInstall compmd, which builds compmd, compmd_inline and cel from comp-markdown/compmd-grammars with their queries.

Open a .comp.md file and the server attaches; :checkhealth vim.lsp shows its configuration and whether its command runs.

Your own build of the server, to try a change to it: point the plugin's server at the binary, before it starts:

vim.lsp.config('compmd', { cmd = { vim.fn.expand('~/src/compmd/target/debug/compmd-lsp'), '--stdio' } })

cargo build -p compmd-lsp in the repository builds it; its preview needs its page built once (pnpm --filter @compmd/lsp-preview... build), or the preview says so.

How to use#

What the plugin is#

File What it does
ftdetect/compmd.lua .comp.md files are the file type compmd, not Markdown
plugin/compmd.lua adds the three grammars to nvim-treesitter, and enables the server
ftplugin/compmd.lua highlights a compmd buffer by its grammar, once it is installed
lsp/compmd.lua the server's configuration: compmd-lsp --stdio for compmd and markdown buffers, its root markers, :LspCompmdPreview, on-type formatting, and the drawing's capability
lua/compmd/drawing.lua the renderer: the server's plan (compmd/drawing) as extmarks, and a highlight group per style

The plugin draws with every primitive but prefix (chapter 24): Neovim's status column has one width for the whole window, so a container's gutter is an inline insert. It declares concealsMarkdown, since Neovim's Markdown parser hides **, backticks and a link's address at conceallevel 2, and the server measures a table's cells without them.

In a project of yours#

The server reads a document's project as your app does: the nearest compmd.config.json above the document (or package.json's compmd), else the workspace folder Neovim gives it, else the document's folder; the installed packs and their signatures; the components in the folder, whose props it reads (W104). So the scenarios a developer meets:

  • Writing a pack's document: the components of the pack's own folder complete, with their params, and a param the signature doesn't declare is diagnosed as you type.
  • An app's documents: a document that uses a pack the configuration doesn't allow, or one only the app's own components have, shows what the app will show, with the configuration's own diagnostics.
  • A host-language component: on its definition fence, its file's import or its signature fence, the code action Generate the signature (or Update the signature) writes the signature fence from the component's props.
  • A code renderer: a code block in a language a pack renders completes its import; compmd/blocks lists the blocks, for a plugin that hands them to the language's own server.

Each change to the configuration, a package or a lock file reloads the open documents. :LspCompmdPreview opens the page as your host renders it, with the CompMD Inspector behind a tab.

Checking it#

In comp-markdown/compmd, cargo make neovim-check builds the server and the grammars from the checkout and runs the plugin's test/check.lua in a headless Neovim: the file type, the grammars and their highlights, the server and its diagnostics, the preview, and the drawing, each line rebuilt from the text and the extmarks. cargo make nvim-interactive [file] opens Neovim with the plugin in place of your config, on the file or on a copy of the Author's Guide's containers example. The published plugin's own CI runs the check against the released server.

Configuration#

The server's settings, under compmd, sent as workspace/configuration and applied without a restart:

vim.lsp.config('compmd', {
  settings = {
    compmd = {
      packs = { suggest = { '@acme' }, network = true }, -- your own scopes to suggest packs from, and reaching registries
      preview = { browser = 'firefox' }, -- instead of the system's browser
      render = { reveal = 'element', glyphs = 'nerd', modes = { 'n' } },
      markdown = { enabled = true, wikiLinks = true },
      tables = { alignOnType = true, alignOnFormat = true },
      links = { enabled = true, completion = true, diagnostics = true, rename = true, wiki = true },
      trace = 'verbose',
    },
  },
})

Chapter 24 has every setting; render.modes, the modes the plugin draws in, is the plugin's own. The highlight groups (CompmdHeading1 … CompmdParam) link to the Markdown and tree-sitter groups, so a colour scheme restyles them. vim.lsp.log.set_level('debug') and :LspLog show the messages between Neovim and the server.

Upstream. The plugin stands in for entries in nvim-treesitter, nvim-lspconfig, mason-registry and Neovim's file types, drafted in its upstream/ folder and opened as pull requests once the server and the grammars are released.

Next: Emacs.