Attached A Neovim Buffer To An LSP Based Off File Extension
September 2023
I'm working on an LSP to use with Neopolitan in Neovim. The first hurdle was figuring out how to attach a file to the LSP. This is how I'm doing it.
vim.api.nvim_create_autocmd("BufReadPost", {
pattern = "*.tmp",
group = vim.api.nvim_create_augroup("LSPTestGroup", { clear = true }),
callback = function()
vim.lsp.start({
name = 'neopolitan',
cmd = {'/Users/alan/workshop/neopolitan-lsp/target/debug/nrs-language-server'},
root_dir = vim.fs.dirname(vim.fs.find({'Cargo.toml'}, { upward = true })[1]),
})
end,
})Notes
- "pattern" is for the file pattern. I'm using "*.tmp" for demo purposes.
- The "group" sets an autogroup that clears each time things run to prevent multiple instances of the command getting installed
- The "callback" is what run when the autocmd event triggers
- vim.lsp.start() - starts an LSP client or reuses one with the same name if there's one already running. Then it attaches the current buffer to it
- I'm not sure if "root_dir" is necessary. LSPs do stuff with projects, but I don't need that for my stuff. TBD on that
- I think there are other plugins like "mason" that deal with this stuff for existing LSPs and I'm sure I could figure out how to get what I'm working on set up with them at some point. For now, this gets me started
- It's also possible there are some server connection settings that can get things to work automatically, but it didn't start for me out of the box with tower-lsp and tower-lsp-boilerplate
end of line