prabirshrestha / vim-lsp

async language server protocol plugin for vim and neovim
MIT License
3.13k stars 305 forks source link

The cursor jump to some where when complete. #619

Closed Caratpine closed 4 years ago

Caratpine commented 4 years ago

Hi, prabirshrestha

I want this effect.

goland

But it's like this

vim

My .vimrc

if executable('gopls')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'gopls',
        \ 'cmd': {server_info->['gopls']},
        \ 'whitelist': ['go'],
        \ })
    autocmd BufWritePre *.go LspDocumentFormatSync
endif
if executable('go-langserver')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'go-langserver',
        \ 'cmd': {server_info->['go-langserver', '-gocodecompletion']},
        \ 'whitelist': ['go'],
        \ })
    autocmd BufWritePre *.go LspDocumentFormatSync
endif
inoremap <expr> <Tab>   pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
inoremap <expr> <cr>    pumvisible() ? "\<C-y>" : "\<cr>"
inoremap <expr> <CR> pumvisible() ? asyncomplete#close_popup() . "\<CR>" : "\<CR>"

How can I do? thanks :-)

prabirshrestha commented 4 years ago

language server needs to support this.

You can try to use :LspCodeAction and see something like this.

image

Also give this a try and it should make it simple to setup language servers. readme contains instructions on how to setup. https://github.com/prabirshrestha/vim-lsp#registering-servers

goland doesn't depend directly on gopls so it has custom features.

I can't repro the cursor moving so please make sure you have the latest master. There were few changes that went in yesterday.

prabirshrestha commented 4 years ago

Also might be good to suggest gopls to support auto imports via textedits.

Caratpine commented 4 years ago

Thanks for your answer :) I'll try it later.

Caratpine commented 4 years ago

Hi, prabirshrestha

The problem I encountered was similar to this issue, but I did not use asyncomplete-omni.vimplugin. I configured the completion shortcut as follows:

inoremap <expr> <CR> pumvisible() ? asyncomplete#close_popup() . "\<CR>" : "\<CR>"
inoremap <expr> <Tab>   pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"

The reproduction process is as follows: I did not import the io package beforehand, I defined a variable i whose type is io.Writer, I pressed . . Wait for the completion prompt, then pressed Tab to the Writer column, and then pressed Enter. At this time, the io package was be imported automatically and the cursor drifted.

123

hrsh7th commented 4 years ago

the asyncomplete-omni seems modify cursor position on purpose. So the problem isn't vim-lsp side , I think. Could you check via <C-x><C-o> manually without asyncomplete-omni.

Caratpine commented 4 years ago

@hrsh7th I didn't use asyncomplete-omni

Here is my .vimrc about completion:

Plug 'prabirshrestha/asyncomplete.vim'
Plug 'prabirshrestha/async.vim'
Plug 'prabirshrestha/vim-lsp', {'branch': 'master'}
Plug 'prabirshrestha/asyncomplete-lsp.vim'

if executable('gopls')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'gopls',
        \ 'cmd': {server_info->['gopls']},
        \ 'whitelist': ['go'],
        \ })
    autocmd BufWritePre *.go LspDocumentFormatSync
endif
if executable('go-langserver')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'go-langserver',
        \ 'cmd': {server_info->['go-langserver', '-gocodecompletion']},
        \ 'whitelist': ['go'],
        \ })
    autocmd BufWritePre *.go LspDocumentFormatSync
endif

inoremap <expr> <CR> pumvisible() ? asyncomplete#close_popup() . "\<CR>" : "\<CR>"
inoremap <expr> <Tab>   pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"

My vim version: Vi IMproved 8.2 macOS version.

hrsh7th commented 4 years ago

Hm... I checked with running gopls and go-langserver both. But has no problem... Could you paste the your vimrc that includes other plugins section.

Caratpine commented 4 years ago

@hrsh7th .vimrc

Sorry to bother you, thanks.

prabirshrestha commented 4 years ago

@Caratpine Can you try using vim-lsp-settings. I just sent a PR that will auto enable go imports. https://github.com/mattn/vim-lsp-settings/pull/68.

I would suggest you to disable Plug 'fatih/vim-go', {'tag': '*'}.

Plug 'prabirshrestha/async.vim'
Plug 'prabirshrestha/asyncomplete.vim'
Plug 'prabirshrestha/asyncomplete-lsp.vim'
Plug 'prabirshrestha/vim-lsp'
Plug 'mattn/vim-lsp-settings'

autocmd BufWritePre *.go LspDocumentFormatSync

I would also suggest you to use autocmd User lsp_buffer_enabled which is documented in README.md

hrsh7th commented 4 years ago

Hm... I checked your vimrc in Vim8.1. But I have no problem, Sorry I can't understood the reason...