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 l…


This content originally appeared on DEV Community and was authored by Nitin Chaudhary

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'
" vim-plug
Plug 'neovim/nvim-lspconfig

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
  • 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{}

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'
-- 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
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.

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 :)


This content originally appeared on DEV Community and was authored by Nitin Chaudhary


Print Share Comment Cite Upload Translate Updates
APA

Nitin Chaudhary | Sciencx (2021-10-22T20:48:49+00:00) Neovim LSP Setup + Code Completion Engine. Retrieved from https://www.scien.cx/2021/10/22/neovim-lsp-setup-code-completion-engine/

MLA
" » Neovim LSP Setup + Code Completion Engine." Nitin Chaudhary | Sciencx - Friday October 22, 2021, https://www.scien.cx/2021/10/22/neovim-lsp-setup-code-completion-engine/
HARVARD
Nitin Chaudhary | Sciencx Friday October 22, 2021 » Neovim LSP Setup + Code Completion Engine., viewed ,<https://www.scien.cx/2021/10/22/neovim-lsp-setup-code-completion-engine/>
VANCOUVER
Nitin Chaudhary | Sciencx - » Neovim LSP Setup + Code Completion Engine. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/22/neovim-lsp-setup-code-completion-engine/
CHICAGO
" » Neovim LSP Setup + Code Completion Engine." Nitin Chaudhary | Sciencx - Accessed . https://www.scien.cx/2021/10/22/neovim-lsp-setup-code-completion-engine/
IEEE
" » Neovim LSP Setup + Code Completion Engine." Nitin Chaudhary | Sciencx [Online]. Available: https://www.scien.cx/2021/10/22/neovim-lsp-setup-code-completion-engine/. [Accessed: ]
rf:citation
» Neovim LSP Setup + Code Completion Engine | Nitin Chaudhary | Sciencx | https://www.scien.cx/2021/10/22/neovim-lsp-setup-code-completion-engine/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.