NeoVim & NVChad & How to setup NVChad/NeoVim to be Zig's best friends ? :)

Hi,

On my Gentoo Linux I am using NeoVim in combination with the NVChad setup. I activated Treesitter for Zig (:TSInstall zig)…but had no luck in setting up for example lsp-config for it.
Any help is very appreciated to make NeoVim/NVChad a totally ziggified :wink: editor…

Cheers!
Tuxic

I’m not familiar with NVChad, but in my init.vim I’m using vim-plug as plugin manager, lsp and nvim-cmp for completion. Relevant bits:

The following Plug lines go between the call plug#begin() and call plug#end() lines.

Zig syntax, formatting, etc.

Plug 'ziglang/zig.vim'

for LSP / nvim-cmp completion:

Plug 'neovim/nvim-lspconfig'                                                                                                                                                                           
Plug 'hrsh7th/nvim-cmp'                                                                                                 
Plug 'hrsh7th/cmp-nvim-lsp'                                                                                             
Plug 'hrsh7th/cmp-buffer'                                                                                               
Plug 'hrsh7th/cmp-path'                                                                                                 
Plug 'hrsh7th/cmp-nvim-lua'                                                                                             
Plug 'hrsh7th/cmp-vsnip'                                                                                                
Plug 'hrsh7th/vim-vsnip'

Do a :PlugInstall and restart nvim for plugins ti kick-in.

Then for LSP to work with ZLS (installed separately):

lua <<EOF
  -- Setup nvim-cmp.
  local cmp = require'cmp'

  cmp.setup({
    snippet = {
      expand = function(args)
        vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
      end,
    },
    mapping = {
      ['<Tab>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
      ['<S-Tab>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
      ['<C-d>'] = cmp.mapping.scroll_docs(-4),
      ['<C-f>'] = cmp.mapping.scroll_docs(4),
      ['<C-Space>'] = cmp.mapping.complete(),
      ['<C-e>'] = cmp.mapping.close(),
      ['<CR>'] = cmp.mapping.confirm({ select = true }),
    },
    sources = cmp.config.sources({
      { name = 'nvim_lsp' },
      { name = 'vsnip' }, -- For vsnip users.
    }, {
        { name = 'path' },
        { name = 'buffer', keyword_length = 3 },
    })
  })

  -- Setup lspconfig.
  local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
  require('lspconfig').zls.setup {
    capabilities = capabilities
  }
EOF

I cloned the ZLS repo and compiled it locally. Then I put a symlink to the zls binary in /usr/local/bin so it’s in the executable PATH. Hope this helps.

PS- I’m on macos by the way. It should also work on Linux.

Hi dude_the_builder,

Thank you VERY much for all the configuration help!!! :slight_smile:
Unfortunately it looks quite different to how “NvChad does it”…and I am quite new
to all the NeoVim-with-lua stuff. NvChad uses some different plugins for the same
task so I need to figure out how to prevent a clash.

Cheers!
Tuxic

1 Like

Hi,

some intermediate results from my side (NO “final solution” … only one step).

The installation of NvChad isn’t customized by me via …/lua/custom/.lua.

I only did a
:TSInstall zig

which installs the Zig-relevant stuff of TreeSitter and I did a
:MasonInstall zls

to install the ZLS languageserver for Zig.

When I will find out more, I will post it here.

Cheers!
Tuxic

1 Like

The problem I found with that is that you have no control over which version of zls is used. For example at the moment, mason installs zls 0.11 (for zig 0.11 / stable release); if you want to use the dev version of zig, zls might not always be correct.

You can use your custom built zls-dev with nvchad.
I followed this setup guide for a golsp first.

When you follow instructions,
you’ll end up having a config file on path ~/.config/nvim/lua/custom/configs/lspconfig.lua
I plugged following lines into there.

lspconfig.zls.setup {
  on_attach = on_attach,
  capabilities = capabilities,
  cmd = {"zls"},
  filetypes = { "zig", "zon" },
  root_dir = util.root_pattern("zls.json", "build.zig", ".git")
  }

You should have now a working zls.

2 Likes

I use lazy-lsp personally GitHub - dundalek/lazy-lsp.nvim: Neovim plugin to auto install LSP servers
nixos-flake/modules/neovim.nix at master · Cloudef/nixos-flake · GitHub

Here is how my zig config looks like:

I use Debian and Nvim+Astronvim (so lazyvim for plugin management).

I had to install llvm and lldb, then I was able to find the lldb-vscode-16.

With dap-ui, I also was able to enter the debugging ui and could do the step by step debugging.