DEV Community

Nitin Chaudhary
Nitin Chaudhary

Posted on

Neovim LSP Setup + Code Completion Engine

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

-- Packer.nvim
use 'neovim/nvim-lspconfig'
Enter fullscreen mode Exit fullscreen mode
" vim-plug
Plug 'neovim/nvim-lspconfig
Enter fullscreen mode Exit fullscreen mode

Now let's see an example of how to install a language server.

As of now, let's see for typescript language. For .ts you can install tsserver using the below command:

npm install -g typescript-language-server
Enter fullscreen mode Exit fullscreen mode
  • Let's start configuring the server . So if you have gone through nvim-lspconfig LSP List and found tsserver, then we already know its quite easy to setup. Below is the snippet you can use to configure.
require'lspconfig'.tsserver.setup{}
Enter fullscreen mode Exit fullscreen mode

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. Now for the code completions, i am currently using nvim-cmp which is quite extensible and customizable plugin. So, let's first install this plugin:
-- Packer.nvim
use 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/nvim-cmp'
Enter fullscreen mode Exit fullscreen mode
-- vim-plug
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/nvim-cmp'
Enter fullscreen mode Exit fullscreen mode
  • 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
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
      }
}
Enter fullscreen mode Exit fullscreen mode

All the snippets, mapping, formatting and sources part in the above code snippet is expalained nicely in the plugin's documentation.

For above code to work, you also have to install lspkind 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

NOTE:

  1. For LSP setup, you can checkout primeagen's lsp video. 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 which is super informative and it goes quite deep into the customizations too.

Thank you for reading :)

Top comments (0)