Extract function doesn't extract visual region
hanspinckaers opened this issue · 10 comments
Hi @pappasam,
Somehow code actions with visual selections do not work for me anymore. I've noticed a pattern: whenever my cursor is on a word during a visual selection, I can select the extract function code action. However it only extracts the word under the cursor into a new function.
Strange behaviour? Will try a stable version of neovim now, I'm on nightlies.
Thanks,
Hans
Would you be able to provide a code snippet / gif so I can try it out on my machine? Based on some cursory testing, things seem to work fine for me, but I also haven't ever really actively used code actions in my workflow.
Here is a minimal vimrc:
call plug#begin('~/.vim/plugged')
Plug 'neovim/nvim-lspconfig'
call plug#end()
lua << EOF
-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
--Enable completion triggered by <c-x><c-o>
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
local opts = { noremap=true, silent=true }
buf_set_keymap('n', '<space>a', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
buf_set_keymap('v', '<space>a', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
end
require'lspconfig'.jedi_language_server.setup {
init_options = {
codeAction = {
nameExtractVariable = "new_var",
nameExtractFunction = "new_function",
},
completion = {
resolve_eagerly = false
},
},
on_attach = on_attach,
}
EOF
It happens for me on 0.6.1 (release) and 0.7.0 (master).
You see it only extracts a very small part of the selection and depending on where the cursor is inside the selection it doesn't offer the code action at all.
Your example is a bit strange / may be an edge case (and not an indicator that the code action no longer "works"). There's nothing obvious being returned here that Jedi could extract into a function.
Please try the following example:
x = 12
y = x + 13
z = y + 14
Highlight all text, extract into a function, and let me know if you get something like this:
def jls_extract_def():
x = 12
y = x + 13
z = y + 14
return z
z = jls_extract_def()
If this works for you, it's a jedi issue.
I just noticed another edge case here without a leading / trailing newline. That's a separate issue...
Hmm, strange result, if I select the whole block and put my cursor on the 'y' I get:
x = 12
y = x + 13
def new_function():
return y
z = new_function() + 14
Edit: if i put my cursor on whitespace i have no code action available.
Relevant line in my vimrc for coc:
vmap <silent> <leader>sa <Plug>(coc-codeaction-selected)
Is it possible that you need vim.lsp.buf.range_code_action()
? https://neovim.io/doc/user/lsp.html
Yes, that's probably it. Can't try it now, will do it tomorrow. Thanks!
Yes it works again! Sorry about that.