2024-06-09

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?

The Language Server Protocol (LSP) establishes a standardized communication framework between text editors, such as VIM, and language servers. This protocol enables essential features like auto-completion, go-to definition, and hover documentation. In the past, integrating these functionalities necessitated individual implementations for each development tool. With LSP, however, the process is streamlined, allowing a single language server to be utilized across various tools, enhancing efficiency and consistency in development environments.

Vim and Neovim: A Divergence

While Vim and Neovim share a common heritage, they have significantly diverged. 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.

Vim does not come with a built-in LSP client, but the advantages of using a specific Vim client are thoroughly outlined in this blog post.

As shown previously, we can utilize yegappan's LSP vim9script plugin, which can be installed in VIM using the Vim-plug plugin manager.

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)