Neovim
Warning
The mason package manager can only install the latest tagged release of ZLS which should not be used with Zig nightly/master.
The following config only contains the necessary Zig specific configuration. Please refer to the Neovim LSP docs on how to use various LSP features.
Install the vim-plug plugin manager.
local vim = vim
local Plug = vim.fn['plug#']
vim.call('plug#begin')
Plug('https://codeberg.org/ziglang/zig.vim')
vim.call('plug#end')
-- don't show parse errors in a separate window
vim.g.zig_fmt_parse_errors = 0
-- disable format-on-save from `ziglang/zig.vim`
vim.g.zig_fmt_autosave = 0
-- enable format-on-save from vim.lsp + ZLS
--
-- Formatting with ZLS matches `zig fmt`.
vim.api.nvim_create_autocmd('BufWritePre',{
pattern = {"*.zig", "*.zon"},
callback = function(ev)
vim.lsp.buf.format()
end
})
vim.lsp.config['zls'] = {
-- Set to 'zls' if `zls` is in your PATH
cmd = { '/path/to/zls_executable' },
filetypes = { 'zig' },
root_markers = { 'build.zig' },
-- There are two ways to set config options:
-- - edit your `zls.json` that applies to any editor that uses ZLS
-- - set in-editor config options with the `settings` field below.
--
-- Further information on how to configure ZLS:
-- https://zigtools.org/zls/configure/
settings = {
zls = {
-- Whether to enable build-on-save diagnostics
--
-- Further information about build-on save:
-- https://zigtools.org/zls/guides/build-on-save/
-- enable_build_on_save = true,
-- omit the following line if `zig` is in your PATH
zig_exe_path = '/path/to/zig_executable'
}
},
}
vim.lsp.enable('zls')
Health Check
To make sure that ZLS is set up as expected, open a .zig file and run the following health checks:
:checkhealth vim.lsp
For more information on the health check, refer to Health check.
Code Actions on save
source.fixAll
vim.api.nvim_create_autocmd('BufWritePre',{
pattern = {"*.zig", "*.zon"},
callback = function(ev)
vim.lsp.buf.code_action({
context = { only = { "source.fixAll" } },
apply = true,
})
end
})
source.organizeImports
vim.api.nvim_create_autocmd('BufWritePre',{
pattern = {"*.zig", "*.zon"},
callback = function(ev)
vim.lsp.buf.code_action({
context = { only = { "source.organizeImports" } },
apply = true,
})
end
})
Per-project config
Neovim allows setting LSP settings per project via exrc.
If the use case for per-project config is to set the path to the Zig or ZLS executable, consider using direnv to add it to your $PATH or use a version manager for Zig and ZLS.