Wire mforth into Neovim¶
Goal: get tree-sitter syntactic highlighting and
mforth lspdiagnostics for.fsfiles in Neovim.Prerequisites:
- Neovim 0.9+ (semantic tokens + built-in LSP client).
nvim-treesitterinstalled via your plugin manager of choice (lazy.nvim, packer, kickstart, etc.).nvim-lspconfiginstalled if you want LSP wiring; tree-sitter highlighting works without it.- The tree-sitter CLI on
$PATH(npm install -g tree-sitter-cliorcargo install tree-sitter-cli) —nvim-treesittershells out to it when installing the parser.- A C compiler (
gccorclang) —nvim-treesittercompiles the generated parser into a shared library.- This repository checked out at a stable absolute path.
mforthon$PATH(e.g.pip install -e .from the repo root) if you want LSP wiring.Verification status: the configuration snippets below match the documented shape of
nvim-treesitter's add-parsers recipe andnvim-lspconfig's custom-server recipe. Exact key names and module paths may vary across plugin-manager distributions (lazy.nvim, packer, kickstart, etc.) — adapt to your setup rather than copying verbatim.
Steps¶
-
Generate the tree-sitter parser. From this repo's root:
cd tree-sitter-mforth tree-sitter generate # emits src/parser.c from grammar.js tree-sitter test # verify the corpus tests passsrc/parser.cis a build artifact (not committed) — regenerate it on each pull whenevergrammar.jschanges. -
Register the parser with
nvim-treesitter. In your Neovim config (e.g.~/.config/nvim/init.luaor a dedicatedlua/plugins/mforth.lua), add the parser toparser_config, register the filetype, and attach aFileTypeautocmd that turns highlighting on:local parser_config = require("nvim-treesitter.parsers").get_parser_configs() parser_config.mforth = { install_info = { url = "/absolute/path/to/tree-sitter-mforth", -- or a git URL files = { "src/parser.c" }, branch = "main", generate_requires_npm = false, requires_generate_from_grammar = false, }, filetype = "mforth", } vim.filetype.add({ extension = { fs = "mforth" } }) vim.api.nvim_create_autocmd("FileType", { pattern = "mforth", callback = function() vim.treesitter.start() end, })Then install the parser from inside Neovim:
-
Copy the highlight queries.
nvim-treesitterreads queries fromqueries/mforth/highlights.scmunder its runtime path. Either symlink or copy the file from this repo:mkdir -p ~/.local/share/nvim/site/queries/mforth cp tree-sitter-mforth/queries/highlights.scm \ ~/.local/share/nvim/site/queries/mforth/highlights.scmRe-run this step whenever
queries/highlights.scmchanges upstream. -
Wire
mforth lspvianvim-lspconfig.nvim-lspconfighas no built-in entry for mforth, so register it as a custom server. In the same Lua config:local lspconfig = require("lspconfig") local configs = require("lspconfig.configs") if not configs.mforth then configs.mforth = { default_config = { cmd = { "mforth", "lsp" }, filetypes = { "mforth" }, root_dir = lspconfig.util.root_pattern(".world.toml", "pyproject.toml", ".git"), settings = {}, }, } end lspconfig.mforth.setup({}) -
Verify the wiring. Open any
.fsfile (e.g.examples/blink.fs) and confirm::set ft?reportsfiletype=mforth.:TSModuleInfolistsmforthunder thehighlightcolumn, with a checkmark.- Colors apply — comments dim, keywords bold, numbers and strings distinct, definition names highlighted as functions.
- If
mforthis on$PATH,:LspInfoshows themforthclient attached and parse / stack-balance / undefined-word diagnostics surface inline.
What the LSP gives you¶
The custom-server block above launches mforth lsp for .fs
buffers. The capabilities the server ships today are catalogued in
the Reference; the surface is:
- parse / stack-balance / undefined-word / sidecar-link diagnostics,
- hover (stack effects for built-ins and inferred effects for user words),
- completion (built-ins and user-defined words in scope),
- semantic tokens that refine tree-sitter's static highlighting.
If mforth is not on $PATH, Neovim logs the spawn failure under
:LspLog and falls back to tree-sitter highlighting only.
Troubleshooting¶
:TSInstall mforthfails. Read the error: missing C compiler or missing tree-sitter CLI are the common causes. Re-run with:TSInstall! mforthto force a rebuild. Theinstall_info.urlmust point at the directory containinggrammar.js, not at the generatedsrc/parser.c.- No highlighting in a
.fsbuffer. Check:set ft?— if it saystextor empty, thevim.filetype.addcall above didn't run. If filetype is right but colors are missing, check:TSModuleInfo highlight— an unchecked row means the parser installed but the highlight module isn't enabled formforth. highlights.scmnot found. Confirm the file lives at~/.local/share/nvim/site/queries/mforth/highlights.scm(or wherever yourruntimepathputs user queries — check:set rtp?).- LSP fails to attach. Run
mforth lsp --helpfrom your shell. If the command isn't found, install mforth (pip install -e .from the repo root) and restart Neovim.:LspLogshows the spawn error verbatim. - Re-link the grammar after a pull. Re-run
tree-sitter generateand:TSUpdate mforthwhenevergrammar.jschanges; re-copyhighlights.scmwhenever the query file changes.
What to read next¶
- Tutorials — a guided walkthrough now that your editor is wired up.
- Use with Helix — the same wiring for Helix's native tree-sitter + LSP support.
- Reference — the catalogue of every mforth surface, including the LSP capabilities listed above.