Sonntag, 19. April 2020

Vim & LSP

I had CoC running for a while. But I couldn't get it working nicely with Dart.
Time for a change…


Vim has some plugins available for interacting with language servers via the language server protocol.
vim-lsp seemed to be a good next candidate to try.
(Why?
It supports many commands for the language server interaction, is written in vimscript, just takes care of the language server part.)

For setting servers up there is another plugin, vim-lsp-settings.

Installing lsp

Installing the following plugins in vim via vim-plug:
Plug 'prabirshrestha/async.vim'
Plug 'prabirshrestha/vim-lsp'
Plug 'mattn/vim-lsp-settings'Plug 'prabirshrestha/asyncomplete.vim'
Plug 'prabirshrestha/asyncomplete-lsp.vim'
is the basic set up.
But still for every language or filetype a server needs to be installed separately. Here the vim-lsp-settings helps with its command
:LspInstallServer
It will install the appropriate server for the filetype. Sometimes there are multiple servers available. You can check via <Ctrl>d or on the website of the plugin and install the server with its name, e.g.
:LspInstallServer pyls-ms
For some servers you need to have some criteria met, like having npm, go or python3 venv installed.

Adjusting Settings

Some languages and there servers are not yet inside the project, so you have to set them up on your own. You can do it inside your .vimrc file, or in a user-global or project-local settings.json file:
~/.local/share/vim-lsp-settings/settings.json
$project/.vim-lsp-settings/settings.json
For python and pyls or python-language-server it could be:
{
    "pyls": {
        "workspace_config": {
            "pyls": {
                "plugins": {
                    "pydocstyle": {"enabled": true},
                    "pycodestyle": {"enabled": true},
                    "pyflakes": {"enabled": true},
                    "jedi": {"enabled": true},
                    "pylint": {"enabled": true}
                }
            }
        }
    }
}
Or multiple, like python, dart, clangd, inside the .vimrc:
 let g:lsp_settings = {
    \ 'analysis-server-dart-snapshot': {
    \     'cmd': [
    \         '/usr/lib/dart/bin/dart',
    \         '/usr/lib/dart/bin/snapshots/analysis_server.dart.snapshot',
    \         '--lsp'
    \     ]
    \ },
    \ 'clangd': {
    \     'cmd': [
    \         'clangd'
    \     ]
    \ },
    \ 'pyls': {
    \     'workspace_config': {
    \         'pyls': {
    \             'plugins': {
    \                 'pydocstyle': {'enabled': v:true},
    \                 'pycodestyle': {'enabled': v:true},
    \                 'pyflakes': {'enabled': v:true},
    \                 'jedi': {'enabled': v:true},
    \                 'pylint': {'enabled': v:true}
    \             }
    \         }
    \     }
    \ }
\ }
Usefull options I enabled are:
let g:lsp_diagnostics_echo_cursor = 1 " enable echo under cursor when in normal mode
let g:lsp_diagnostics_float_cursor = 1 " enable echo under cursor when in normal mode
As an extra I bound <LocalLeader>K (in my case \K) to hover and show a neat little window with information above the current symbole:
nmap <LocalLeader>K <Plug>(lsp-hover)
You can bind more with given mappings or use the commands directly, like showing diagnostics via :LspDocumentDiagnostics, formating the document via :LspDocumentFormat

lsp with python via pyls

Installing the pyls python-language-server via 
:LspInstallServer pyls
installs it in its own environment under
~/.local/share/vim-lsp-settings/servers/pyls/venv
It is basically installing it via pip.
But you might not have the tools, like pydocstyle and pycodestyle, installed. You can just install them inside that virtual environment, e.g.:
$ ~/.local/share/vim-lsp-settings/servers/pyls/venv/bin/pip3 install pyls-black pycodestyle pydocstyle pyflakes pylint autopep8
If you have the feeling, that one of your tools is not working, check the logs provided by lsp via enabling them in your vimrc:
let g:lsp_log_verbose = 1
let g:lsp_log_file = expand('~/vim-lsp.log')
Inside there might be something like this:
Failed to load pyls entry point 'pycodestyle': No module named 'autopep8'",""]]

Keine Kommentare:

Kommentar veröffentlichen

[Review/Critic] UDock X - 13,3" LapDock

The UDock X - 13.3" LapDock is a combination of touch display, keyboard, touch-pad and battery. It can be used as an extension of vari...