Nvim-R
Nvim-R copied to clipboard
question: equivalent of nvim-R for python use
I am a long time nvim-r user and it is a big reason I use R. But I have to use python for some work but couldn't find a tool as nice as nvim-r. Is there a tool for python that is as good as nvim-r?
I use vimcmdline to send code to the Python interpreter and pyright
as a language server. To use pyright
in Vim/Neovim you will need additional plugins, such as coc.nvim or the built-in Neovim's language server client. I can post my configuration here if you think it could be useful.
I'd like to use your configs. Thanks for sharing!
Option 1: Coc.vim
~/.config/init.vim:
call plug#begin('~/.cache/vim-plug')
Plug 'jalvesaq/R-Vim-runtime'
Plug 'jalvesaq/Nvim-R'
Plug 'jalvesaq/vimcmdline'
Plug 'neoclide/coc.nvim'
call plug#end()
" Coc configuration
source ~/.config/nvim/coc_config.vim
~/.config/nvim/coc_config.vim:
let coc_data_home = '~/.cache/coc'
" "g" (go to or show)
nmap gd <Plug>(coc-definition)
nmap gD <Plug>(coc-declaration)
nmap ge <Plug>(coc-diagnostic-next)
nmap gE <Plug>(coc-diagnostic-next)
nmap gr <Plug>(coc-references)
nmap gh <Plug>(coc-action-doHover)
nmap gi <Plug>(coc-diagnostic-info)
" "s" (server do)
nmap <Leader>sr <Plug>(coc-rename)
nmap <Leader>sa <Plug>(coc-codeaction)
nmap <Leader>sf <Plug>(coc-format-selected)
nmap <Leader>st <Plug>(coc-type-definition)
" Coc configuration is json + comments
autocmd FileType json syntax match Comment +\/\/.\+$+
" Make <tab> used for trigger completion, completion confirm, snippet expand and jump like VSCode.
inoremap <silent><expr> <TAB>
\ pumvisible() ? coc#_select_confirm() :
\ coc#expandableOrJumpable() ? "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>" :
\ <SID>check_back_space() ? "\<TAB>" :
\ coc#refresh()
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
let g:coc_snippet_next = '<tab>'
Do the following commands after installing coc.nvim
and restarting Neovim:
:CocInstall coc-json
:CocInstall coc-pyright
Then do :CocConfig
to open ~/.config/nvim/coc-settings.json
and paste the following lines in it:
{
"diagnostic.infoSign": "∆",
"diagnostic.hintSign": "∆",
"diagnostic.warningSign": "∆",
"diagnostic.errorSign": "▣"
}
NOTES:
-
coc-json
helps to edit~/.config/nvim/coc-settings.json
- I don't remember whether you have to install
pyright
by your own of if Coc.nvim will do it for you.
Option 2: Neovim's LSP
~/.config/init.vim:
call plug#begin('~/.cache/vim-plug')
Plug 'jalvesaq/R-Vim-runtime'
Plug 'jalvesaq/Nvim-R'
Plug 'jalvesaq/vimcmdline'
Plug 'neovim/nvim-lspconfig'
Plug 'kabouzeid/nvim-lspinstall'
Plug 'nvim-lua/completion-nvim'
call plug#end()
" Neovim's LSP configuration
source ~/.config/nvim/lsp_config.vim
~/.config/nvim/lsp_config.vim:
sign define LspDiagnosticsSignError text=▣ texthl=LspDiagnosticsSignError linehl= numhl=
sign define LspDiagnosticsSignWarning text=∆ texthl=LspDiagnosticsSignWarning linehl= numhl=
sign define LspDiagnosticsSignInformation text=∆ texthl=LspDiagnosticsSignInformation linehl= numhl=
sign define LspDiagnosticsSignHint text=∆ texthl=LspDiagnosticsSignHint linehl= numhl=
" Use <tab> to trigger completion without modifying the usage to <tab> keys.
imap <tab> <Plug>(completion_smart_tab)
imap <s-tab> <Plug>(completion_smart_s_tab)
llet g:completion_auto_change_source = 1
let g:completion_chain_complete_list = {
\'default' : [
\ {'complete_items': ['lsp', 'snippet', 'path']},
\ {'mode': '<c-n>'}]}
" Use completion-nvim in every buffer
autocmd BufEnter * lua require'completion'.on_attach()
lua <<EOF
local custom_attach = function(client)
print("LSP started.");
require'completion'.on_attach(client)
end
-- Repeat this for each of your language servers
require'lspconfig'.pyright.setup{on_attach=custom_attach}
EOF
function LspKeymap(lngsvr)
" g: go to or show
nmap <buffer> gd <Cmd>lua vim.lsp.buf.definition()<CR>
nmap <buffer> gD <Cmd>lua vim.lsp.buf.declaration()<CR>
nmap <buffer> gE <Cmd>lua vim.lsp.diagnostic.goto_prev()<CR>
nmap <buffer> ge <Cmd>lua vim.lsp.diagnostic.goto_next()<CR>
nmap <buffer> gr <Cmd>lua vim.lsp.buf.references()<CR>
nmap <buffer> gh <Cmd>lua vim.lsp.buf.hover()<CR>
nmap <buffer> gl <Cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>
nmap <buffer> go <Cmd>lua vim.lsp.diagnostic.set_loclist()<CR>
" s: server do
nmap <buffer> <Leader>sr <Cmd>lua vim.lsp.buf.rename()<CR>
nmap <buffer> <Leader>sa <Cmd>lua vim.lsp.buf.code_action()<CR>
nmap <buffer> <Leader>st <Cmd>lua vim.lsp.buf.type_definition()<CR>
nmap <buffer> gi <cmd>lua vim.lsp.buf.implementation()<CR>
nmap <buffer> gs <cmd>lua vim.lsp.buf.signature_help()<CR>
nmap <buffer> <Leader>wa <Cmd>lua vim.lsp.buf.add_workspace_folder()<CR>
nmap <buffer> <Leader>wr <Cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>
nmap <buffer> <Leader>wl <Cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>
setlocal omnifunc=v:lua.vim.lsp.omnifunc
" Should check server capabilities before these. Add your language servers to the list:
if index(['vimls', 'clangd', 'pyright'], a:lngsvr) > -1
nmap <buffer> <Enter> <Cmd>lua vim.lsp.buf.document_highlight()<CR>
augroup lsp_document_highlight
autocmd! * <buffer>
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
augroup END
nmap <buffer> <Leader>sf <Cmd>lua vim.lsp.buf.formatting()<CR>
vmap <Leader>sf <Cmd>lua vim.lsp.buf.range_formatting()<CR>
endif
endfunction
" Repeat this for each Language Server that you have installed
autocmd FileType python call LspKeymap('pyright')
imap <tab> <Plug>(completion_smart_tab)
imap <s-tab> <Plug>(completion_smart_s_tab)
" Better for auto completion
set completeopt=menuone,noselect
Then do:
:LspInstall python
NOTES:
- The built-in Neovim's LSP might be the best option in the future, but currently, I consider Coc.nvim slightly better.
- Neovim's documentation on its LSP gives a lot of examples written in Lua. I translated what I could into the Vim language.
- You will need a color scheme that supports the LspDiagnostic highlighting groups, such as southernlights, srcery or dracula.
coc.nvim user here. It is first time I hear about Neovim native LSP.
I've tried vimcmdline, very nice, missing \rp and other things from nvim-r though.
Thanks!
coc.nvim user here. It is first time I hear about Neovim native LSP.
I've tried vimcmdline, very nice, missing \rp and other things from nvim-r though.
Thanks!
sorry for bumping
I use this for \rp equivalent
nnoremap <LocalLeader>rp viw :call VimCmdLineSendSelection()<CR>
note sure it's the right thing to do ...