Working type hints inlay for OCaml with neovim
It took me more than 1 hour to get type hints showing as inlay using OCaml LSP server.
I also got some from this thread: https://discuss.ocaml.org/t/inlay-hints-in-neovim/16547/7.
I am using:
- neovim v0.12.0-dev
- OCaml 5.4.1
- ocamllsp 1.26.0
Here is my working Lua config:
-- LSP config with inlay hints
vim.lsp.config('ocamllsp', {
cmd = { 'ocamllsp' },
filetypes = { 'ocaml', 'ocaml.menhir', 'ocaml.interface', 'ocaml.ocamllex', 'reason', 'dune' },
root_markers = { 'dune-project', 'dune-workspace', '*.opam' },
settings = {
extendedHover = { enable = true },
codelens = { enable = true },
duneDiagnostics = { enable = true },
inlayHints = {
enable = true,
hintFunctionParams = true,
hintLetBindings = true,
hintPatternVariables = true,
},
syntaxDocumentation = { enable = true },
merlinJumpCodeActions = { enable = true },
},
})
-- Force a codelens refresh to get type signatures for top-level definitions
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost', 'InsertLeave' }, {
pattern = { '*.ml', '*.mli' },
callback = function()
vim.lsp.codelens.refresh({ bufnr = 0 })
end,
})
-- Refresh the codelens when opening a file
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('OcamlCodelensOnAttach', { clear = true }),
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client and client.server_capabilities.codeLensProvider then
vim.lsp.codelens.refresh({ bufnr = args.buf })
end
end,
})
-- Enable the LSP server and inlay hints
vim.lsp.enable('ocamllsp')
vim.lsp.inlay_hint.enable(true)
I also learned that I need to keep a running build using a dune build -w process in order to have the LSP server gather the proper type information. Without it, you may see stale type info or no types at all.
