1 +local icon = require 'std.icon'
2 +local map = require 'std.keymap'
3 +local auto = require 'std.auto'
4 +local project = require 'std.project'
5 +local want = require 'std.want'
7 +-- TODO: setup codelens -> use lsp-signature plugin?
8 +-- TODO: setup custom cmp keybinds
12 + 'nvim-lsp-installer',
17 +}(function(zero, null_ls, installer, neodev, rust, navic, lsp_status)
18 + installer.settings {
20 + --TODO: use some global float window settings
25 + zero.preset 'recommended'
26 + zero.set_preferences {
27 + configure_diagnostics = false,
28 + set_lsp_keymaps = false,
30 + error = icon 'Error',
31 + warn = icon 'Warning',
33 + info = icon 'Information',
37 + -- setup lsp for my neovim config
38 + zero.nvim_workspace {
39 + -- add paths of all plugins and neovim
40 + library = vim.api.nvim_get_runtime_file('', true),
42 + -- Initialize rust_analyzer with rust-tools
44 + server = zero.build_options('rust_analyzer', {}),
47 + -- this will prevent zero from loading this server.
48 + -- Java will be managed by nvim-jdtls, which wants to do things differently.
49 + -- see ftplugin/java.lua
50 + zero.build_options('jdtls', {})
54 + lspconfig = zero.defaults.nvim_workspace(),
57 + -- disable disco mode while typing
58 + zero.setup_nvim_cmp {
60 + autocomplete = false,
64 + local null_opts = zero.build_options 'null-ls'
66 + local formatting = null_ls.builtins.formatting
67 + local diagnostics = null_ls.builtins.diagnostics
68 + local code_actions = null_ls.builtins.code_actions
69 + local hover = null_ls.builtins.hover
70 + local completion = null_ls.builtins.completion
72 + -- run diagnostics on save
73 + local diagnostics_on_save = { method = null_ls.methods.DIAGNOSTICS_ON_SAVE }
75 + on_attach = null_opts.on_attach,
78 + formatting.clang_format,
80 + formatting.protolint,
81 + formatting.shellharden,
87 + diagnostics.actionlint.with(diagnostics_on_save),
88 + diagnostics.checkmake.with(diagnostics_on_save),
89 + diagnostics.gitlint.with(diagnostics_on_save),
90 + diagnostics.protoc_gen_lint.with(diagnostics_on_save),
91 + diagnostics.protolint.with(diagnostics_on_save),
92 + diagnostics.selene.with(diagnostics_on_save).with {
93 + -- null-ls determines the root in a too simplistic manner.
94 + -- since my neovim configuration live inside my dotfiles, the dotfiles root
95 + -- was taken. but i want the neovim config folder as root.
96 + cwd = function(parameters)
97 + local root = project.determine_project_root({ name = 'selene.toml', weight = 10 }, parameters.root)
98 + return root or parameters.root
101 + diagnostics.shellcheck.with(diagnostics_on_save),
102 + diagnostics.tidy.with(diagnostics_on_save),
104 + code_actions.refactoring,
105 + code_actions.shellcheck,
107 + completion.luasnip,
111 + local function setup_keybinds_and_actions(client, buffer_number)
112 + -- TODO: use buffer number in keymaps
113 + -- TODO: make format changed lines command
114 + local capabilities = client.server_capabilities
115 + if capabilities.document_formatting then
117 + name = 'Format buffer using LSP',
118 + category = "editing",
119 + keys = '<leader>' .. 'l' .. 'f',
120 + command = function()
121 + vim.lsp.buf.format { bufnr = buffer_number }
122 + vim.notify('Formatted ' .. vim.api.nvim_buf_get_name(buffer_number))
126 + if capabilities.document_range_formatting then
128 + name = 'Format range using LSP',
129 + category = 'editing',
130 + keys = '<leader>' .. 'l' .. 'f',
131 + command = function()
132 + vim.lsp.buf.range_formatting { bufnr = buffer_number }
133 + vim.notify('Formatted range in ' .. vim.api.nvim_buf_get_name(buffer_number))
137 + --TODO: change some of these commands to telescoped versions
138 + if capabilities.hover then
140 + name = 'Show Hover',
142 + keys = '<leader>' .. 'l' .. 'k',
143 + command = vim.lsp.buf.hover,
146 + if capabilities.goto_definition then
148 + name = 'Go to Definition',
149 + category = 'navigation',
150 + keys = '<leader>' .. 'l' .. 'd',
151 + command = vim.lsp.buf.definition,
154 + if capabilities.declaration then
156 + name = 'Go to Declaration',
157 + category = 'navigation',
158 + keys = '<leader>' .. 'l' .. 'd' .. 'd',
159 + command = vim.lsp.buf.declaration,
162 + if capabilities.implementation then
164 + name = 'Go to Implementation',
165 + category = 'navigation',
166 + keys = '<leader>' .. 'l' .. 'i',
167 + command = vim.lsp.buf.implementation,
170 + if capabilities.type_definition then
172 + name = 'Go to Type Definition',
173 + category = 'navigation',
174 + keys = '<leader>' .. 'l' .. 't',
175 + command = vim.lsp.buf.type_definition,
178 + if capabilities.find_references then
180 + name = 'Go to References',
181 + category = 'navigation',
182 + keys = '<leader>' .. 'l' .. 'r',
183 + command = vim.lsp.buf.references
186 + if capabilities.call_hierarchy then
188 + name = 'Go to Incoming Call',
189 + category = 'navigation',
190 + keys = '<leader>' .. 'l' .. 'c',
191 + command = vim.lsp.buf.incoming_calls,
194 + name = 'Go to Outgoing Call',
195 + category = 'navigation',
196 + keys = '<leader>' .. 'l' .. 'c' .. 'c',
197 + command = vim.lsp.buf.outgoing_calls,
200 + if capabilities.rename then
202 + name = 'Rename symbol',
203 + category = 'editing',
205 + command = vim.lsp.buf.rename,
208 + if capabilities.code_action then
210 + name = 'Show code actions',
213 + command = vim.lsp.buf.code_action,
216 + name = 'Show code actions in range',
219 + command = vim.lsp.buf.range_code_action,
222 + if capabilities.document_highlight then
223 + -- this variable should technically be scoped to the current buffer.
225 + local highlight_timer = nil
226 + auto 'highlight_reference_under_cursor' {
228 + description = 'Automatically highlight a symbol under cursor',
229 + events = 'CursorHold',
230 + pattern = '<buffer>',
231 + command = function()
232 + if not highlight_timer then
233 + highlight_timer = vim.defer_fn(function()
234 + vim.lsp.buf.document_highlight()
235 + highlight_timer = nil
241 + description = 'Clear highlighted symbol under cursor',
242 + events = { 'CursorMoved', 'CursorMovedI', 'InsertEnter' },
243 + pattern = '<buffer>',
244 + command = function()
245 + if highlight_timer then
246 + highlight_timer:close()
247 + highlight_timer = nil
249 + vim.lsp.buf.clear_references()
256 + zero.on_attach(function(client, buffer_number)
257 + if client.name ~= 'null-ls' and not vim.b.lsp_attached then
258 + navic.attach(client, buffer_number)
259 + lsp_status.on_attach(client, buffer_number)
260 + setup_keybinds_and_actions(client, buffer_number)