Convert Figma logo to code with AI

prabirshrestha logovim-lsp

async language server protocol plugin for vim and neovim

3,103
304
3,103
54

Top Related Projects

24,306

Nodejs extension host for vim & neovim, load extensions like VSCode and host language servers.

13,476

Check syntax in Vim/Neovim asynchronously and fix files, with Language Server Protocol (LSP) support

Language Server Protocol (LSP) support for vim and neovim.

Quickstart configs for Nvim LSP

Quick Overview

The vim-lsp project is a Language Server Protocol (LSP) client implementation for the Vim and Neovim text editors. It provides a unified interface for integrating various language servers, allowing developers to leverage advanced code editing features such as code completion, go-to-definition, and diagnostics within their Vim/Neovim workflows.

Pros

  • Language Server Protocol Support: vim-lsp supports the Language Server Protocol, enabling integration with a wide range of language servers, including those for popular programming languages like JavaScript, Python, Go, and more.
  • Extensibility: The project is designed to be extensible, allowing users to easily add support for new language servers or customize the behavior of existing ones.
  • Asynchronous Operation: vim-lsp utilizes asynchronous communication, ensuring a responsive and efficient editing experience.
  • Compatibility: The plugin is compatible with both Vim and Neovim, allowing developers to use it across different Vim-based environments.

Cons

  • Complexity: Integrating and configuring vim-lsp with various language servers can be a complex process, especially for users new to the Vim ecosystem.
  • Dependency Management: The project relies on several external dependencies, which can make installation and setup more involved.
  • Limited Documentation: While the project has a README file, the documentation could be more comprehensive, especially for advanced use cases and customization.
  • Performance Overhead: Depending on the language server and the project's complexity, vim-lsp may introduce some performance overhead, which could be a concern for users with older or less powerful systems.

Code Examples

Example 1: Configuring vim-lsp for Python Development

" Install the Python language server
Plug 'prabirshrestha/vim-lsp'
Plug 'mattn/vim-lsp-settings'

" Configure the Python language server
if executable('pyls')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'pyls',
        \ 'cmd': {server_info->['pyls']},
        \ 'whitelist': ['python'],
        \ })
endif

This example demonstrates how to configure vim-lsp to work with the Python language server (pyls). It uses the vim-lsp-settings plugin to automatically download and configure the language server.

Example 2: Enabling Code Completion

" Enable code completion
inoremap <expr> <Tab>
    \ pumvisible() ? "\<C-n>" :
    \ <SID>check_back_space() ? "\<Tab>" :
    \ coc#refresh()
inoremap <expr><S-Tab> pumvisible() ? "\<C-p>" : "\<C-h>"

function! s:check_back_space() abort
    let col = col('.') - 1
    return !col || getline('.')[col - 1]  =~# '\s'
endfunction

This example configures the code completion functionality provided by vim-lsp. It sets up key mappings for navigating the completion menu and a helper function to check the current cursor position.

Example 3: Jumping to Definition

" Jump to definition
nmap <silent> gd <Plug>(lsp-definition)
nmap <silent> gr <Plug>(lsp-references)

This example demonstrates how to set up key mappings for jumping to the definition and finding references of the symbol under the cursor using vim-lsp.

Getting Started

To get started with vim-lsp, follow these steps:

  1. Install the plugin using your preferred Vim plugin manager, such as Vundle, Plug, or Pathogen. For example, with Plug:

    Plug 'prabirshrestha/vim-lsp'
    Plug 'mattn/vim-lsp-settings'
    
  2. Install the required language servers for the programming languages you want to use. You can either install them manually or use the vim-lsp-settings plugin, which can automatically download and configure the language servers for you.

  3. Configure vim-lsp to work with your language servers. For example, to set up the

Competitor Comparisons

24,306

Nodejs extension host for vim & neovim, load extensions like VSCode and host language servers.

Pros of coc.nvim

  • Supports a wide range of language servers, including LSP, Vim script, and more
  • Provides a rich set of features, such as code completion, code actions, and diagnostics
  • Integrates well with other Vim/Neovim plugins, making it a powerful and extensible solution

Cons of coc.nvim

  • Requires a significant amount of configuration to set up and customize
  • Can be slower than some other LSP clients, especially for large projects
  • Dependency on Node.js may be a concern for some users

Code Comparison

coc.nvim:

" Use tab for trigger completion with characters ahead and navigate.
inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

vim-lsp:

function! s:on_lsp_buffer_enabled() abort
    setlocal omnifunc=lsp#complete
    setlocal signcolumn=yes
    if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif
    nmap <buffer> gd <plug>(lsp-definition)
    nmap <buffer> gs <plug>(lsp-document-symbol-search)
    nmap <buffer> gS <plug>(lsp-workspace-symbol-search)
    nmap <buffer> gr <plug>(lsp-references)
    nmap <buffer> gi <plug>(lsp-implementation)
    nmap <buffer> gt <plug>(lsp-type-definition)
    nmap <buffer> <leader>rn <plug>(lsp-rename)
    nmap <buffer> [g <plug>(lsp-previous-diagnostic)
    nmap <buffer> ]g <plug>(lsp-next-diagnostic)
    nmap <buffer> K <plug>(lsp-hover)
endfunction
13,476

Check syntax in Vim/Neovim asynchronously and fix files, with Language Server Protocol (LSP) support

Pros of ALE

  • ALE supports a wide range of linters and formatters, including language servers, making it a versatile choice for various programming languages.
  • ALE provides a consistent interface for integrating these tools, simplifying the configuration and usage across different projects.
  • ALE offers features like automatic fixing of code issues, which can improve developer productivity.

Cons of ALE

  • ALE may have a higher learning curve compared to Vim LSP, as it requires configuring and integrating multiple linters and formatters.
  • ALE's performance may be slightly slower than Vim LSP, as it needs to manage and coordinate multiple external tools.

Code Comparison

Vim LSP:

" Initialize the language server
if executable('pyls')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'pyls',
        \ 'cmd': {server_info->['pyls']},
        \ 'whitelist': ['python'],
    \ })
endif

ALE:

" Configure ALE to use the Python language server
let g:ale_linters = {
    \ 'python': ['pyls'],
    \ }
let g:ale_fixers = {
    \ 'python': ['black', 'isort'],
    \ }

Language Server Protocol (LSP) support for vim and neovim.

Pros of LanguageClient-neovim

  • Supports a wide range of programming languages and language servers, including popular ones like Python, JavaScript, and Go.
  • Provides a more comprehensive set of features, such as code completion, go-to-definition, and code formatting.
  • Integrates with a variety of Vim/Neovim plugins, allowing for a more seamless development experience.

Cons of LanguageClient-neovim

  • Requires more configuration and setup compared to vim-lsp, which may be a barrier for some users.
  • May have a larger footprint in terms of resource usage, as it includes more functionality.
  • Might not be as actively maintained as vim-lsp, which has a dedicated maintainer.

Code Comparison

Here's a brief comparison of the code for setting up a language server in both projects:

LanguageClient-neovim:

let g:LanguageClient_serverCommands = {
    \ 'python': ['pyls'],
    \ 'javascript': ['javascript-typescript-stdio'],
    \ 'go': ['gopls'],
    \ }

nnoremap <F5> :call LanguageClient_contextMenu()<CR>

vim-lsp:

if executable('pyls')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'pyls',
        \ 'cmd': {server_info->['pyls']},
        \ 'whitelist': ['python'],
        \ })
endif

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

As you can see, LanguageClient-neovim provides a more concise configuration, while vim-lsp requires more explicit registration of language servers.

Quickstart configs for Nvim LSP

Pros of neovim/nvim-lspconfig

  • Easier Configuration: neovim/nvim-lspconfig provides a more streamlined and modular approach to configuring Language Server Protocol (LSP) clients, making it easier to set up and manage multiple LSP servers.
  • Wider Ecosystem: neovim/nvim-lspconfig is part of the larger Neovim ecosystem, which means it benefits from a wider range of community-contributed configurations and integrations.
  • Improved Maintainability: neovim/nvim-lspconfig is actively maintained by the Neovim team, ensuring better long-term support and bug fixes.

Cons of neovim/nvim-lspconfig

  • Limited Vim Support: neovim/nvim-lspconfig is primarily focused on Neovim, and while it can be used with Vim, the integration may not be as seamless as with prabirshrestha/vim-lsp.
  • Steeper Learning Curve: Configuring neovim/nvim-lspconfig may require a deeper understanding of the Neovim ecosystem and its conventions, which can be a barrier for some users.
  • Potential Compatibility Issues: As neovim/nvim-lspconfig is tightly integrated with Neovim, there may be compatibility issues when using it with older versions of Vim or other Vim-based plugins.

Code Comparison

prabirshrestha/vim-lsp:

function! s:on_lsp_buffer_enabled() abort
    setlocal omnifunc=lsp#complete
    setlocal signcolumn=yes
    if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif
    nmap <buffer> gd <plug>(lsp-definition)
    nmap <buffer> gs <plug>(lsp-document-symbol)
    nmap <buffer> gr <plug>(lsp-references)
endfunction

neovim/nvim-lspconfig:

local on_attach = function(client, bufnr)
    vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
    vim.api.nvim_buf_set_option(bufnr, 'signcolumn', 'yes')

    local opts = { noremap = true, silent = true }
    vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gs', '<cmd>lua vim.lsp.buf.document_symbol()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
end

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

vim-lsp Gitter

Async Language Server Protocol plugin for vim8 and neovim.

Installing

Install vim-plug and then:

Plug 'prabirshrestha/vim-lsp'

Performance

Certain bottlenecks in Vim script have been implemented in lua. If you would like to take advantage of these performance gains use vim compiled with lua or neovim v0.4.0+

Registering servers

if executable('pylsp')
    " pip install python-lsp-server
    au User lsp_setup call lsp#register_server({
        \ 'name': 'pylsp',
        \ 'cmd': {server_info->['pylsp']},
        \ 'allowlist': ['python'],
        \ })
endif

function! s:on_lsp_buffer_enabled() abort
    setlocal omnifunc=lsp#complete
    setlocal signcolumn=yes
    if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif
    nmap <buffer> gd <plug>(lsp-definition)
    nmap <buffer> gs <plug>(lsp-document-symbol-search)
    nmap <buffer> gS <plug>(lsp-workspace-symbol-search)
    nmap <buffer> gr <plug>(lsp-references)
    nmap <buffer> gi <plug>(lsp-implementation)
    nmap <buffer> gt <plug>(lsp-type-definition)
    nmap <buffer> <leader>rn <plug>(lsp-rename)
    nmap <buffer> [g <plug>(lsp-previous-diagnostic)
    nmap <buffer> ]g <plug>(lsp-next-diagnostic)
    nmap <buffer> K <plug>(lsp-hover)
    nnoremap <buffer> <expr><c-f> lsp#scroll(+4)
    nnoremap <buffer> <expr><c-d> lsp#scroll(-4)

    let g:lsp_format_sync_timeout = 1000
    autocmd! BufWritePre *.rs,*.go call execute('LspDocumentFormatSync')
    
    " refer to doc to add more commands
endfunction

augroup lsp_install
    au!
    " call s:on_lsp_buffer_enabled only for languages that has the server registered.
    autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END

Refer to vim-lsp-settings on how to easily setup language servers using vim-lsp automatically.

Plug 'prabirshrestha/vim-lsp'
Plug 'mattn/vim-lsp-settings'

auto-complete

Refer to docs on configuring omnifunc or asyncomplete.vim.

Snippets

vim-lsp does not support snippets by default. If you want snippet integration, you will first have to install a third-party snippet plugin and a plugin that integrates it in vim-lsp. At the moment, you have following options:

  1. vim-vsnip together with vim-vsnip-integ
  2. UltiSnips together with vim-lsp-ultisnips
  3. neosnippet.vim together with vim-lsp-neosnippet

For more information, refer to the readme and documentation of the respective plugins.

Folding

You can let the language server automatically handle folding for you. To enable this, you have to set 'foldmethod', 'foldexpr' and (optionally) 'foldtext':

set foldmethod=expr
  \ foldexpr=lsp#ui#vim#folding#foldexpr()
  \ foldtext=lsp#ui#vim#folding#foldtext()

If you would like to disable folding globally, you can add this to your configuration:

let g:lsp_fold_enabled = 0

Also see :h vim-lsp-folding.

Semantic highlighting

vim-lsp supports the unofficial extension to the LSP protocol for semantic highlighting (https://github.com/microsoft/vscode-languageserver-node/pull/367). This feature requires Neovim highlights, or Vim with the textprop feature enabled. You will also need to link language server semantic scopes to Vim highlight groups. Refer to :h vim-lsp-semantic for more info.

Supported commands

Note:

  • Some servers may only support partial commands.
  • While it is possible to register multiple servers for the same filetype, some commands will pick only the first server that supports it. For example, it doesn't make sense for rename and format commands to be sent to multiple servers.
CommandDescription
:LspAddTreeCallHierarchyIncomingFind incoming call hierarchy for the symbol under cursor, but add the result to the current list
:LspCallHierarchyIncomingFind incoming call hierarchy for the symbol under cursor
:LspCallHierarchyOutgoingFind outgoing call hierarchy for the symbol under cursor
:LspCodeActionGets a list of possible commands that can be applied to a file so it can be fixed (quick fix)
:LspCodeLensGets a list of possible commands that can be executed on the current document
:LspDeclarationGo to the declaration of the word under the cursor, and open in the current window
:LspDefinitionGo to the definition of the word under the cursor, and open in the current window
:LspDocumentDiagnosticsGet current document diagnostics information
:LspDocumentFormatFormat entire document
:LspDocumentRangeFormatFormat document selection
:LspDocumentSymbolShow document symbols
:LspHoverShow hover information
:LspImplementationShow implementation of interface in the current window
:LspNextDiagnosticjump to next diagnostic (all of error, warning, information, hint)
:LspNextErrorjump to next error
:LspNextReferencejump to next reference to the symbol under cursor
:LspNextWarningjump to next warning
:LspPeekDeclarationGo to the declaration of the word under the cursor, but open in preview window
:LspPeekDefinitionGo to the definition of the word under the cursor, but open in preview window
:LspPeekImplementationGo to the implementation of an interface, but open in preview window
:LspPeekTypeDefinitionGo to the type definition of the word under the cursor, but open in preview window
:LspPreviousDiagnosticjump to previous diagnostic (all of error, warning, information, hint)
:LspPreviousErrorjump to previous error
:LspPreviousReferencejump to previous reference to the symbol under cursor
:LspPreviousWarningjump to previous warning
:LspReferencesFind references
:LspRenameRename symbol
:LspStatusShow the status of the language server
:LspTypeDefinitionGo to the type definition of the word under the cursor, and open in the current window
:LspTypeHierarchyView type hierarchy of the symbol under the cursor
:LspWorkspaceSymbolSearch/Show workspace symbol

Diagnostics

Document diagnostics (e.g. warnings, errors) are enabled by default, but if you preferred to turn them off and use other plugins instead (like Neomake or ALE, set g:lsp_diagnostics_enabled to 0:

let g:lsp_diagnostics_enabled = 0         " disable diagnostics support

Highlight references

Highlight references to the symbol under the cursor (enabled by default). You can disable it by adding

let g:lsp_document_highlight_enabled = 0

to your configuration.

To change the style of the highlighting, you can set or link the lspReference highlight group, e.g.:

highlight lspReference ctermfg=red guifg=red ctermbg=green guibg=green

Debugging

In order to enable file logging set g:lsp_log_file.

let g:lsp_log_verbose = 1
let g:lsp_log_file = expand('~/vim-lsp.log')

" for asyncomplete.vim log
let g:asyncomplete_log_file = expand('~/asyncomplete.log')

You can get detailed status on your servers using :CheckHealth with a plugin on vim:

if !has('nvim') | Plug 'rhysd/vim-healthcheck' | endif
CheckHealth

Tests

vim-themis is used for testing. To run integration tests gopls executable must be in path.

Maintainers

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer]

Sponsors

Become a sponsor and get your logo on our README on GitHub with a link to your site. [Become a sponsor]