Home

Awesome

unit-tests

Language Server Protocol (LSP) plugin for Vim. You need Vim version 9.0 or above to use this plugin. This plugin is written using only the Vim9 script.

Installation

You can install this plugin directly from github using the following steps:

$ mkdir -p $HOME/.vim/pack/downloads/opt
$ cd $HOME/.vim/pack/downloads/opt
$ git clone https://github.com/yegappan/lsp
$ vim -u NONE -c "helptags $HOME/.vim/pack/downloads/opt/lsp/doc" -c q

After installing the plugin using the above steps, add the following line to your $HOME/.vimrc file:

packadd lsp

You can also install and manage this plugin using any one of the Vim plugin managers (dein.vim, pathogen, vam, vim-plug, volt, Vundle, etc.).

You will also need to download and install one or more language servers corresponding to the programming languages that you are using. Refer to the https://langserver.org/ page for the list of available language servers. This plugin doesn't install the language servers.

Features

The following language server protocol (LSP) features are supported:

Configuration

To use the plugin features with a particular file type(s), you need to first register a LSP server for that file type(s).

The LSP servers are registered using the LspAddServer() function. This function accepts a list of LSP servers.

To register a LSP server, add the following lines to your .vimrc file (use only the LSP servers that you need from the below list). If you used vim-plug to install the LSP plugin, the steps are described later in this section.


" Clangd language server
call LspAddServer([#{
	\    name: 'clangd',
	\    filetype: ['c', 'cpp'],
	\    path: '/usr/local/bin/clangd',
	\    args: ['--background-index']
	\  }])

" Javascript/Typescript language server
call LspAddServer([#{
	\    name: 'typescriptlang',
	\    filetype: ['javascript', 'typescript'],
	\    path: '/usr/local/bin/typescript-language-server',
	\    args: ['--stdio'],
	\  }])

" Go language server
call LspAddServer([#{
	\    name: 'golang',
	\    filetype: ['go', 'gomod'],
	\    path: '/usr/local/bin/gopls',
	\    args: ['serve'],
	\    syncInit: v:true
	\  }])

" Rust language server
call LspAddServer([#{
	\    name: 'rustlang',
	\    filetype: ['rust'],
	\    path: '/usr/local/bin/rust-analyzer',
	\    args: [],
	\    syncInit: v:true
	\  }])

The above lines register the language servers for C/C++, Javascript/Typescript, Go and Rust file types. Refer to the Wiki page for various language server specific configuration.

To register a LSP server, the following information is needed:

FieldDescription
filetypeOne or more file types supported by the LSP server. This can be a String or a List. To specify multiple multiple file types, use a List.
pathcomplete path to the LSP server executable (without any arguments).
argsa list of command-line arguments passed to the LSP server. Each argument is a separate List item.
initializationOptionsUser provided initialization options. May be of any type. For example the intelephense PHP language server accept several options here with the License Key among others.
customNotificationHandlersA dictionary of notifications and functions that can be specified to add support for custom language server notifications.
customRequestHandlersA dictionary of request handlers and functions that can be specified to add support for custom language server requests replies.
featuresA dictionary of booleans that can be specified to toggle what things a given LSP is providing (folding, goto definition, etc) This is useful when running multiple servers in one buffer.

The LspAddServer() function accepts a list of LSP servers with the above information.

Some of the LSP plugin features can be enabled or disabled by using the LspOptionsSet() function, detailed in :help lsp-options. Here is an example of configuration with default values:

call LspOptionsSet(#{
        \   aleSupport: v:false,
        \   autoComplete: v:true,
        \   autoHighlight: v:false,
        \   autoHighlightDiags: v:true,
        \   autoPopulateDiags: v:false,
        \   completionMatcher: 'case',
        \   completionMatcherValue: 1,
        \   diagSignErrorText: 'E>',
        \   diagSignHintText: 'H>',
        \   diagSignInfoText: 'I>',
        \   diagSignWarningText: 'W>',
        \   echoSignature: v:false,
        \   hideDisabledCodeActions: v:false,
        \   highlightDiagInline: v:true,
        \   hoverInPreview: v:false,
        \   ignoreMissingServer: v:false,
        \   keepFocusInDiags: v:true,
        \   keepFocusInReferences: v:true,
        \   completionTextEdit: v:true,
        \   diagVirtualTextAlign: 'above',
        \   diagVirtualTextWrap: 'default',
        \   noNewlineInCompletion: v:false,
        \   omniComplete: v:null,
        \   outlineOnRight: v:false,
        \   outlineWinSize: 20,
        \   semanticHighlight: v:true,
        \   showDiagInBalloon: v:true,
        \   showDiagInPopup: v:true,
        \   showDiagOnStatusLine: v:false,
        \   showDiagWithSign: v:true,
        \   showDiagWithVirtualText: v:false,
        \   showInlayHints: v:false,
        \   showSignature: v:true,
        \   snippetSupport: v:false,
        \   ultisnipsSupport: v:false,
        \   useBufferCompletion: v:false,
        \   usePopupInCodeAction: v:false,
        \   useQuickfixForLocations: v:false,
        \   vsnipSupport: v:false,
        \   bufferCompletionTimeout: 100,
        \   customCompletionKinds: v:false,
        \   completionKinds: {},
        \   filterCompletionDuplicates: v:false,
	\ })

If you used vim-plug to install the LSP plugin, then you need to use the LspSetup User autocmd to initialize the LSP server and to set the LSP server options. For example:

let lspOpts = #{autoHighlightDiags: v:true}
autocmd User LspSetup call LspOptionsSet(lspOpts)

let lspServers = [#{
	\	  name: 'clang',
	\	  filetype: ['c', 'cpp'],
	\	  path: '/usr/local/bin/clangd',
	\	  args: ['--background-index']
	\ }]
autocmd User LspSetup call LspAddServer(lspServers)

Supported Commands

The following commands are provided to use the LSP features.

CommandDescription
:LspCodeActionApply the code action supplied by the language server to the diagnostic in the current line.
:LspCodeLensDisplay a list of code lens commands and apply a selected code lens command to the current file.
:LspDiag currentDisplay the diagnostic message for the current line.
:LspDiag firstJump to the first diagnostic message for the current buffer.
:LspDiag hereJump to the next diagnostic message in the current line.
:LspDiag highlight disableDisable diagnostic message highlights.
:LspDiag highlight enableEnable diagnostic message highlights.
:LspDiag nextJump to the next diagnostic message after the current position.
:LspDiag nextWrapJump to the next diagnostic message after the current position, wrapping to the first message when the last message is reached.
:LspDiag prevJump to the previous diagnostic message before the current position.
:LspDiag prevWrapJump to the previous diagnostic message before the current position, wrapping to the last message when the first message is reached.
:LspDiag showDisplay the diagnostics messages from the language server for the current buffer in a new location list.
:LspDocumentSymbolDisplay the symbols in the current file in a popup menu and jump to the selected symbol.
:LspFoldFold the current file.
:LspFormatFormat a range of lines in the current file using the language server. The shiftwidth and expandtab values set for the current buffer are used when format is applied. The default range is the entire file.
:LspGotoDeclarationGo to the declaration of the keyword under cursor.
:LspGotoDefinitionGo to the definition of the keyword under cursor.
:LspGotoImplGo to the implementation of the keyword under cursor.
:LspGotoTypeDefGo to the type definition of the keyword under cursor.
:LspHighlightHighlight all the matches for the keyword under cursor.
:LspHighlightClearClear all the matches highlighted by :LspHighlight.
:LspHoverShow the documentation for the symbol under the cursor in a popup window.
:LspIncomingCallsDisplay the list of symbols calling the current symbol.
:LspOutgoingCallsDisplay the list of symbols called by the current symbol.
:LspOutlineShow the list of symbols defined in the current file in a separate window.
:LspPeekDeclarationOpen the declaration of the symbol under cursor in the preview window.
:LspPeekDefinitionOpen the definition of the symbol under cursor in the preview window.
:LspPeekImplOpen the implementation of the symbol under cursor in the preview window.
:LspPeekReferencesDisplay the list of references to the keyword under cursor in a location list associated with the preview window.
:LspPeekTypeDefOpen the type definition of the symbol under cursor in the preview window.
:LspRenameRename the current symbol.
:LspSelectionExpandExpand the current symbol range visual selection.
:LspSelectionShrinkShrink the current symbol range visual selection.
:LspShowAllServersDisplay information about all the registered language servers.
:LspServerDisplay the capabilities or messages or status of the language server for the current buffer or restart the server.
:LspShowReferencesDisplay the list of references to the keyword under cursor in a new location list.
:LspShowSignatureDisplay the signature of the keyword under cursor.
:LspSubTypeHierarchyDisplay the sub type hierarchy in a popup window.
:LspSuperTypeHierarchyDisplay the super type hierarchy in a popup window.
:LspSwitchSourceHeaderSwitch between a source and a header file.
:LspSymbolSearchPerform a workspace wide search for a symbol.
:LspWorkspaceAddFolder {folder}Add a folder to the workspace.
:LspWorkspaceListFoldersShow the list of folders in the workspace.
:LspWorkspaceRemoveFolder {folder}Remove a folder from the workspace.

Similar Vim LSP Plugins

  1. vim-lsp: Async Language Server Protocol
  2. Coc: Conquer of Completion
  3. vim-lsc: Vim Language Server Client
  4. LanguageClient-neovim
  5. ALE: Asynchronous Lint Engine
  6. Neovim built-in LSP client
  7. Omnisharp LSP client