-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accepting a motion for vim.lsp.buf.range_formatting
#14680
Comments
Any ideas here? This has probably already been done somewhere? |
It hasn't been for any lsp functionality, given that you can already do this by first going into visual mode (I know that's not what you want) and then format_range, it's not a high priority for me, but happy to review any PR! |
You could easily do something like local function format_range_operator()
local old_func = vim.go.operatorfunc
_G.op_func_formatting = function()
local start = vim.api.nvim_buf_get_mark(0, '[')
local finish = vim.api.nvim_buf_get_mark(0, ']')
vim.lsp.buf.range_formatting({}, start, finish)
vim.go.operatorfunc = old_func
_G.op_func_formatting = nil
end
vim.go.operatorfunc = 'v:lua.op_func_formatting'
vim.api.nvim_feedkeys('g@', 'n', false)
end And map that map that function to what ever key you want to use . I haven't tested it though . I can't find server that does range formatting :P |
To clarify, do you mean that you could add this to
This doesn't seem to work for me. Perhaps I missed something? |
-local function format_range_operator()
+function format_range_operator() Make it a global function you can only map global functions the way you're mapping. Also check if your lasp server has range formatting capability set |
Hmm, I think that helps, but now I get this error when I type function format_range_operator()
local old_func = vim.go.operatorfunc -- ERROR: Error executing lua: <path to init.lua>: attampt to index field 'go' (a nil value)
_G.op_func_formatting = function()
local start = vim.api.nvim_buf_get_mark(0, '[')
local finish = vim.api.nvim_buf_get_mark(0, ']')
vim.lsp.buf.range_formatting({}, start, finish)
vim.go.operatorfunc = old_func
_G.op_func_formatting = nil
end
vim.go.operatorfunc = 'v:lua.op_func_formatting'
vim.api.nvim_feedkeys('g@', 'n', false)
end
vim.api.nvim_set_keymap("n", "gm", "<cmd>lua format_range_operator()<CR>", {noremap = true}) My language server definitely supports range formatting - it works with |
Update nvim. You don't have the You can map this in visual mode too. vim.api.nvim_set_keymap("n", "gm", "<cmd>lua format_range_operator()<CR>", {noremap = true})
+vim.api.nvim_set_keymap("v", "gm", "<cmd>lua format_range_operator()<CR>", {noremap = true}) What lsp server are you using ? |
This works, thanks!! A internal company LSP for python. |
It would be cool if this function was built into https://github.com/neovim/nvim-lspconfig and referenced in the docs : ). @mjlbach do you have an opinion about this? |
It wouldn't go in lspconfig, lspconfig is (supposed) to be just configurations for language servers. It would probably go in |
This issue has been mentioned on Neovim Discourse. There might be relevant details there: https://neovim.discourse.group/t/format-a-text-object-using-lsp/3255/3 |
It looks like the
range_formatting
function takes in line positions that would allow it to format lines that are specified by a motion. I would love to have some kind of normal mode mapping to e.g.gl
that allows me to do something likeglip
to format a paragraph. I feel like this is possible and maybe has already been done, but I'm at a loss for how to do it myself or where to find this functionality. Any pointers?The text was updated successfully, but these errors were encountered: