In neovim I would like to set a keybinding -- via lua
config -- to use LSP to format the visual selection when I type \qf
.
Using this setting, I can format the whole buffer from normal mode:
vim.api.nvim_set_keymap('n', '\\qf', '<cmd>lua vim.lsp.buf.formatting()<CR>', {noremap = true})
In order to format just the visual selection, I believe I need to use the range_formatting()
function. I tried this:
vim.api.nvim_set_keymap('v', '\\qf', '<cmd>lua vim.lsp.buf.range_formatting()<CR>', {noremap = true})
Unfortunately, this gives me the following error:
r_language_server: -3
2603: Error: argument is of length zero
Call: if (!nzchar(line)) return(units)
Stack trace:
1: code_point_from_unit(text,position$character)
2: document$from_lsp_position(params$range$start)
3: dispatch(self, id, params)
How can I modify the keymap setting above to pass the start and end position of the selection, and format the visual selection using LSP?
:h vim.lsp.buf.range_formatting()
seems to say that you need to provide 3 arguments to the function: Some formatting options as well as the range to format. Have you tried that?