Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 22, 2021 08:48 pm GMT

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.nvimuse 'neovim/nvim-lspconfig'
" vim-plugPlug '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.nvimuse 'hrsh7th/cmp-nvim-lsp'Plug 'hrsh7th/cmp-buffer'Plug 'hrsh7th/nvim-cmp'
-- vim-plugPlug '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 :)


Original Link: https://dev.to/ntncahay/neovim-lsp-setup-code-completion-engine-3ckm

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To