Enhancing Vim Productivity with the Language Server Protocol (LSP) Plugin
The Language Server Protocol (LSP) has revolutionized the way we interact with code editors. By providing a standardized interface for language-specific features, LSP enables a more efficient and integrated development environment. Let’s explore how LSP can augment productivity and efficiency.
What is LSP?
LSP defines the protocol used between an editor and a language server. It facilitates features like auto-completion, go-to definition, and documentation on hover. Traditionally, implementing these features required separate efforts for each development tool. However, LSP standardizes the communication between language servers and development tools, allowing a single language server to be reused across multiple tools.
Vim and Neovim: A Divergence
While Vim and Neovim share a common heritage, they have significantly diverged. While many Language Server Protocols (LSPs) offer detailed instructions for configuring Neovim, similar guidance for Vim is often lacking. Given the distinctions between Vim and Neovim, it’s both logical and practical to treat them as separate projects, each with unique requirements and challenges. This perspective enables a more focused approach to problem-solving and feature development within each project.
In this blog post, I hope to partially bridge the information gap by providing an expanded overview of the V language (vlang) LSP implementation, which I believe is particularly noteworthy.
As previously described for Ruff, which is a linter for Python, we can utilize the same vim9script plugin.
This plugin can be installed by adding:
Plug 'yegappan/lsp'
to your .vimrc file and then running the command
:PlugInstall
in Vim.
V is a simple, fast, and safe language. Its LSP V-Analyzer is both written in and designed specifically for the V programming language. To create a local configuration, use the command:
v-analyzer init
Run this command at the root of your project. The configuration file will be located at ./.v-analyzer/config.toml. Each setting in the config has a detailed description.
The video below demonstrates how to initialize a directory and create a simple V executable with assistance from V-Analyzer. It covers the creation of a mutable structure, which is then compiled in Vim using the VLANG make compiler and the Dispatch plugin.
The importance of mutability for modifying structure elements is highlighted. In the video, the language server protocol notifies the user of any errors before compiling the source code.
The configuration for several Language Server Protocols (LSPs), deno-lsp, typst-lsp v-analyzer, are outlined below. Please adjust the paths of the executables, according to their location on your system. Executables can be downloaded from the releases page of their respective GitHub repositories.
let lspOpts = #{
\ autoHighlightDiags: v:true,
\ snippetSupport: v:true,
\ ultisnipsSupport: v:false,
\ noNewlineInCompletion: v:true,
\}
silent! autocmd User LspSetup call LspOptionsSet(lspOpts)
"" Ruff Lsp Start/AddServer
let lspServers = [#{
\ name: 'pythonlinter',
\ filetype: ['python'],
\ path: 'C:\\Users\Seve\\workspace\\toolbox\\bin\\ruff.exe',
\ args: ['server','--preview'],
\ },
\#{
\ name: 'v-analyzer',
\ filetype: ['vlang'],
\ path: 'C:\\Users\Seve\\workspace\\toolbox\\bin\\v-analyzer.exe',
\ args: ['','--stdio'],
\ },
\#{
\ name: 'deno',
\ filetype: ['javascript','typescript'],
\ path: 'C:\\Users\Seve\\bin\\deno.exe',
\ initializationOptions: { "enable": v:true, "lint": v:true, "unstable": v:false },
\ args: ['lsp'],
\ },
\#{
\ name: 'typst-lsp',
\ filetype: ['typst'],
\ path: 'C:\\Users\Seve\\workspace\\toolbox\\bin\\typst-lsp.exe',
\ args: [''],
\ }]
silent! autocmd User LspSetup call LspAddServer(lspServers)