Neovim LSP Setup + Code Completion Engine
Tags: neovim, lsp
Published: 2021-10-22 20:48:49

## Overview
One of the cool feature that came with **Neovim 0.5** is **Language Server Protocol (LSP) support** which allows to code more effectively as well as easily.

## What is Language Server Protocol
LSP is a protocol which is used by a language server (eg: `clangd`, `typescript-language-server`) to communicate with client.
Now, the question raises is what is a `language server` too.
So a `language server` is a local server which provides suggestions to your client(in our case neovim) while you are writing the code.
As we go forward, i think the definition will become more clearer.

## Setting up LSP

- I have been using [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) from beginning as it provides the [list of all available LSPs for different languages](https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md) and how exactly to install and configure them.
   <br>But first let's install the `nvim-lspconfig` plugin itself:

```lua
-- Packer.nvim
use 'neovim/nvim-lspconfig'
```
```vim
" vim-plug
Plug 'neovim/nvim-lspconfig
   ```

   Now let's see an example of how to install a `language server`.
   <br>As of now, let's see for `typescript language`. For `.ts` you can install `tsserver` using the below command:
```zsh
npm install -g typescript-language-server
```

- Let's start configuring the server . So if you have gone through [nvim-lspconfig LSP List](https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md) and found **tsserver**, then we already know its quite easy to setup. Below is the snippet you can use to configure.

```lua
require'lspconfig'.tsserver.setup{}
```
   We just need to put this line in either `init.lua` or any `.lua` file which is sourced when neovim starts up.

- Finally, we have our LSP installed and configured, but we don't see any suggestion or any code completions when we open a `.ts` file in a project.
   <br>Now for the code completions, i am currently using [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) which is quite extensible and customizable plugin.
   So, let's first install this plugin:

```lua
-- Packer.nvim
use 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/nvim-cmp'
```
```vimscript
-- vim-plug
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/nvim-cmp'
```

- For configuring up **code completion engine (nvim-cmp)**, you can either refer to the documentation from above link (which is quite extensive) or you can just use the below code snippet to make it work
```lua
cmp.setup {
   -- As currently, i am not using any snippet manager, thus disabled it.
      -- snippet = {
         --   expand = function(args)
            --     require("luasnip").lsp_expand(args.body)
            --   end,
         -- },

      mapping = {
         ["<C-d>"] = cmp.mapping.scroll_docs(-4),
         ["<C-f>"] = cmp.mapping.scroll_docs(4),
         ["<C-e>"] = cmp.mapping.close(),
         ["<c-y>"] = cmp.mapping.confirm {
            behavior = cmp.ConfirmBehavior.Insert,
            select = true,
         },
      },
      formatting = {
         format = lspkind.cmp_format {
            with_text = true,
            menu = {
               buffer   = "[buf]",
               nvim_lsp = "[LSP]",
               path     = "[path]",
            },
         },
      },

      sources = {
         { name = "nvim_lsp"},
         { name = "path" },
         { name = "buffer" , keyword_length = 5},
      },
      experimental = {
         ghost_text = true
      }
}
```
   All the snippets, mapping, formatting and sources part in the above code snippet is expalained nicely in the plugin's documentation.
   <br>For above code to work, you also have to install [lspkind](https://github.com/onsails/lspkind-nvim) which provides awesome icons in the code completions.

- After setting up this much, you would start getting this kind of awesome popups which you can use to make the cool auto completions and much more.
![Code completion popup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6uqq9ukxs0532g0q3ifw.JPG)


**NOTE:**
   1. For LSP setup, you can checkout [primeagen's lsp video](https://www.youtube.com/watch?v=tOjVHXaUrzo&ab_channel=ThePrimeagen).  It's an year old, but its quite good for first time.
   2. For code completion engine `nvim-cmp`, tj has just released a [TakeTuesday video: nvim-cmp](https://www.youtube.com/watch?v=_DnmphIwnjo) which is super informative and it goes quite deep into the customizations too.


   Thank you for reading :)