Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Neovim Integration

Neovim integration has two separate pieces:

  • Tree-sitter for syntax/highlighting/queries
  • LSP for diagnostics and semantic editor features

They should be configured together, but they do different jobs.

LSP Setup

The language server command is:

vim.lsp.config("fol", {
  cmd = { "fol", "tool", "lsp" },
  filetypes = { "fol" },
  root_markers = { "fol.work.yaml", "build.fol", ".git" },
})

vim.lsp.enable("fol")

Also ensure Neovim recognizes .fol files:

vim.filetype.add({
  extension = { fol = "fol" },
})

Tree-sitter Setup

First generate a bundle:

fol tool tree generate /tmp/fol

Then point Neovim’s Tree-sitter parser config at that bundle:

local parser_config = require("nvim-treesitter.parsers").get_parser_configs()

parser_config.fol = {
  install_info = {
    url = "/tmp/fol",
    files = { "src/parser.c" },
    requires_generate_from_grammar = false,
  },
  filetype = "fol",
}

The query files are expected at:

  • /tmp/fol/queries/fol/highlights.scm
  • /tmp/fol/queries/fol/locals.scm
  • /tmp/fol/queries/fol/symbols.scm

Neovim also needs that bundle on runtimepath so it can find the queries.

Practical Model

Tree-sitter provides:

  • highlight captures
  • locals captures
  • symbol-style structure queries

LSP provides:

  • diagnostics
  • hover
  • definitions
  • references
  • rename for same-file local and current-package top-level symbols
  • semantic tokens
  • document symbols
  • proof goals (fol/goals, below), theorem hovers with the statement and the axioms a proof rests on, and tactic and lemma completion inside prf { }

So the normal editor shape is:

  1. Neovim opens a .fol file
  2. Tree-sitter handles syntax/highlighting
  3. Neovim launches fol tool lsp
  4. the server provides semantic editor features

Proof Goals

fol/goals is a server request outside the LSP specification; the server lists it as experimental.folGoals in its capabilities. It takes the usual text-document position and answers with the goals before the tactic under the cursor, the same goals fol tool proof goals prints:

{
  "theorem": "sample::half_sum_le",
  "goals": [
    {
      "hypotheses": [
        { "names": "a, b", "type": "int" },
        { "names": "h", "type": "(0 <= a and a <= b) and b <= 1000000" }
      ],
      "target": "out::holds(sample::half_sum(a, b), fun(r0: int) => r0 <= b)"
    }
  ],
  "rendered": "a, b: int\nh: (0 <= a and a <= b) and b <= 1000000\n|- out::holds(sample::half_sum(a, b), fun(r0: int) => r0 <= b)"
}

That is the goal before unf half_sum; in this package sample:

fun[exp] half_sum(a, b: int): int = {
    return (a + b) / 2;
};

thm half_sum_le(a, b: int): 0 <= a and a <= b and b <= 1000000 -> half_sum(a, b) <= b = prf {
    fix h;
    unf half_sum;
    lia;
};

Outside every theorem the result is null. The request runs the proof pass of the package each time, so it belongs on an idle event rather than on every cursor move. This keeps the goals of the theorem under the cursor in a scratch window to the right, refreshed on CursorHold:

local goals = { buf = nil, win = nil }

local function show_goals()
  local params = vim.lsp.util.make_position_params(0, "utf-16")
  vim.lsp.buf_request(0, "fol/goals", params, function(err, result)
    if err or result == nil then
      return
    end
    if not (goals.buf and vim.api.nvim_buf_is_valid(goals.buf)) then
      goals.buf = vim.api.nvim_create_buf(false, true)
      vim.bo[goals.buf].filetype = "folgoals"
    end
    local lines = vim.split(result.theorem .. "\n\n" .. result.rendered, "\n")
    vim.api.nvim_buf_set_lines(goals.buf, 0, -1, false, lines)
    if not (goals.win and vim.api.nvim_win_is_valid(goals.win)) then
      goals.win = vim.api.nvim_open_win(goals.buf, false, { split = "right", width = 60 })
    end
  end)
end

vim.api.nvim_create_autocmd("CursorHold", {
  pattern = "*.fol",
  callback = show_goals,
})

updatetime sets how long the cursor rests before CursorHold fires. To show the goals in a floating window instead, pass the lines to vim.lsp.util.open_floating_preview(lines, "text").

Debugging A Setup

Useful checks:

:echo &filetype
:lua print(vim.inspect(vim.lsp.get_clients({ bufnr = 0 })))

And from the shell:

fol tool tree generate /tmp/fol
fol tool lsp

If fol tool lsp prints nothing and waits, that is correct.

It is a stdio server, not an interactive shell command.

If the server refuses to start, check that Neovim opened the file inside a directory tree that contains build.fol or fol.work.yaml.