I don't set a mapping to format code because it is not something I do all the time. So previously, I used to have a command to format or range format as follows:
" format code
command! -range Format if <range> | exec 'lua vim.lsp.buf.range_formatting({ timeout_ms = 2000 })' | else | exec 'lua vim.lsp.buf.format({ timeout_ms = 2000 })' | endif
But Nvim now gives me this message:
vim.lsp.buf.range_formatting is deprecated, use vim.lsp.formatexpr or vim.lsp.buf.format > instead. See :h deprecated This function will be removed in Nvim version 0.9.0 stack traceback: /usr/share/nvim/runtime/lua/vim/lsp/buf.lua:407: in function 'range_formatting' [string ":lua"]:1: in main chunk
How can I set a command (not a key mapping) to format the selected text, when there is any, or the entire buffer if not?
vim.lsp.buf.format
but I couldn't make it work. Also, all examples I could find were for key mappings, not for a command.