Neovim LSP Setup + Code Completion Engine






Overview

One of many cool characteristic that got here with Neovim 0.5 is Language Server Protocol (LSP) help which permits to code extra successfully in addition to simply.



What’s Language Server Protocol

LSP is a protocol which is utilized by a language server (eg: clangd, typescript-language-server) to speak with shopper.
Now, the query raises is what’s a language server too.
So a language server is an area server which supplies recommendations to your shopper(in our case neovim) if you are writing the code.
As we go ahead, i believe the definition will develop into extra clearer.



Establishing 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 examine an instance of set up a language server.

As of now, let’s examine for typescript language. For .ts you possibly can set up tsserver utilizing the under command:

npm set up -g typescript-language-server
Enter fullscreen mode

Exit fullscreen mode

  • Let’s begin configuring the server . So when you’ve got gone by means of nvim-lspconfig LSP List and located tsserver, then we already know its fairly simple to setup. Beneath is the snippet you should use to configure.
require'lspconfig'.tsserver.setup{}
Enter fullscreen mode

Exit fullscreen mode

We simply have to put this line in both init.lua or any .lua file which is sourced when neovim begins up.

  • Lastly, we’ve our LSP put in and configured, however we do not see any suggestion or any code completions after we open a .ts file in a challenge.
    Now for the code completions, i’m presently utilizing nvim-cmp which is kind of extensible and customizable plugin.
    So, let’s first set up 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 possibly can both seek advice from the documentation from above hyperlink (which is kind of intensive) or you possibly can simply use the under code snippet to make it work
cmp.setup {
   -- As presently, i'm not utilizing any snippet supervisor, thus disabled it.
      -- snippet = {
         --   broaden = perform(args)
            --     require("luasnip").lsp_expand(args.physique)
            --   finish,
         -- },

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

      sources = {
         { identify = "nvim_lsp"},
         { identify = "path" },
         { identify = "buffer" , keyword_length = 5},
      },
      experimental = {
         ghost_text = true
      }
}
Enter fullscreen mode

Exit fullscreen mode

All of the snippets, mapping, formatting and sources half within the above code snippet is expalained properly within the plugin’s documentation.

For above code to work, you even have to put in lspkind which supplies superior icons within the code completions.

  • After establishing this a lot, you’ll begin getting this type of superior popups which you should use to make the cool auto completions and rather more.

NOTE:

  1. For LSP setup, you possibly can checkout primeagen’s lsp video. It is an yr outdated, however its fairly good for first time.
  2. For code completion engine nvim-cmp, tj has simply launched a TakeTuesday video: nvim-cmp which is tremendous informative and it goes fairly deep into the customizations too.

Thanks for studying 🙂



Abu Sayed is the Best Web, Game, XR and Blockchain Developer in Bangladesh. Don't forget to Checkout his Latest Projects.


Checkout extra Articles on Sayed.CYou

#Neovim #LSP #Setup #Code #Completion #Engine