2023-02-01 19:11:37 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
2023-04-17 20:27:27 +00:00
|
|
|
pkgs,
|
2023-02-01 19:11:37 +00:00
|
|
|
...
|
2023-11-08 02:16:46 +00:00
|
|
|
}: let
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit (lib.modules) mkIf;
|
|
|
|
inherit (lib.strings) optionalString;
|
|
|
|
inherit (lib.trivial) boolToString;
|
|
|
|
inherit (lib.nvim.binds) addDescriptionsToMappings;
|
2023-11-08 02:16:46 +00:00
|
|
|
|
2023-02-01 19:11:37 +00:00
|
|
|
cfg = config.vim.lsp;
|
2024-10-09 17:50:34 +00:00
|
|
|
usingNvimCmp = config.vim.autocomplete.nvim-cmp.enable;
|
2023-10-22 19:38:14 +00:00
|
|
|
self = import ./module.nix {inherit config lib pkgs;};
|
|
|
|
|
|
|
|
mappingDefinitions = self.options.vim.lsp.mappings;
|
|
|
|
mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
|
2024-05-06 20:05:43 +00:00
|
|
|
mkBinding = binding: action:
|
|
|
|
if binding.value != null
|
2024-10-27 11:54:53 +00:00
|
|
|
then "vim.keymap.set('n', '${binding.value}', ${action}, {buffer=bufnr, noremap=true, silent=true, desc='${binding.description}'})"
|
2024-05-06 20:05:43 +00:00
|
|
|
else "";
|
2023-02-01 19:11:37 +00:00
|
|
|
in {
|
2023-04-17 20:27:27 +00:00
|
|
|
config = mkIf cfg.enable {
|
2024-03-12 00:46:29 +00:00
|
|
|
vim = {
|
2024-10-25 23:51:58 +00:00
|
|
|
autocomplete.nvim-cmp = {
|
|
|
|
sources = {nvim_lsp = "[LSP]";};
|
|
|
|
sourcePlugins = ["cmp-nvim-lsp"];
|
2024-10-22 13:33:18 +00:00
|
|
|
};
|
2023-04-17 20:27:27 +00:00
|
|
|
|
2024-07-20 08:30:48 +00:00
|
|
|
pluginRC.lsp-setup = ''
|
2024-03-12 00:46:29 +00:00
|
|
|
vim.g.formatsave = ${boolToString cfg.formatOnSave};
|
2023-04-17 20:27:27 +00:00
|
|
|
|
2024-03-12 00:46:29 +00:00
|
|
|
local attach_keymaps = function(client, bufnr)
|
2024-10-27 11:54:53 +00:00
|
|
|
${mkBinding mappings.goToDeclaration "vim.lsp.buf.declaration"}
|
|
|
|
${mkBinding mappings.goToDefinition "vim.lsp.buf.definition"}
|
|
|
|
${mkBinding mappings.goToType "vim.lsp.buf.type_definition"}
|
|
|
|
${mkBinding mappings.listImplementations "vim.lsp.buf.implementation"}
|
|
|
|
${mkBinding mappings.listReferences "vim.lsp.buf.references"}
|
|
|
|
${mkBinding mappings.nextDiagnostic "vim.diagnostic.goto_next"}
|
|
|
|
${mkBinding mappings.previousDiagnostic "vim.diagnostic.goto_prev"}
|
|
|
|
${mkBinding mappings.openDiagnosticFloat "vim.diagnostic.open_float"}
|
|
|
|
${mkBinding mappings.documentHighlight "vim.lsp.buf.document_highlight"}
|
|
|
|
${mkBinding mappings.listDocumentSymbols "vim.lsp.buf.document_symbol"}
|
|
|
|
${mkBinding mappings.addWorkspaceFolder "vim.lsp.buf.add_workspace_folder"}
|
|
|
|
${mkBinding mappings.removeWorkspaceFolder "vim.lsp.buf.remove_workspace_folder"}
|
|
|
|
${mkBinding mappings.listWorkspaceFolders "function() vim.notify(vim.inspect(vim.lsp.buf.list_workspace_folders())) end"}
|
|
|
|
${mkBinding mappings.listWorkspaceSymbols "vim.lsp.buf.workspace_symbol"}
|
|
|
|
${mkBinding mappings.hover "vim.lsp.buf.hover"}
|
|
|
|
${mkBinding mappings.signatureHelp "vim.lsp.buf.signature_help"}
|
|
|
|
${mkBinding mappings.renameSymbol "vim.lsp.buf.rename"}
|
|
|
|
${mkBinding mappings.codeAction "vim.lsp.buf.code_action"}
|
|
|
|
${mkBinding mappings.format "vim.lsp.buf.format"}
|
|
|
|
${mkBinding mappings.toggleFormatOnSave "function() vim.b.disableFormatSave = not vim.b.disableFormatSave end"}
|
2024-03-12 00:46:29 +00:00
|
|
|
end
|
2023-02-01 19:11:37 +00:00
|
|
|
|
2024-03-12 00:46:29 +00:00
|
|
|
-- Enable formatting
|
|
|
|
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
|
2023-10-14 13:01:34 +00:00
|
|
|
|
2024-03-12 00:46:29 +00:00
|
|
|
format_callback = function(client, bufnr)
|
|
|
|
if vim.g.formatsave then
|
|
|
|
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
|
|
|
|
vim.api.nvim_create_autocmd("BufWritePre", {
|
|
|
|
group = augroup,
|
|
|
|
buffer = bufnr,
|
|
|
|
callback = function()
|
|
|
|
${
|
|
|
|
if config.vim.lsp.null-ls.enable
|
|
|
|
then ''
|
|
|
|
if vim.b.disableFormatSave then
|
|
|
|
return
|
|
|
|
end
|
2023-05-23 20:53:18 +00:00
|
|
|
|
2024-03-12 00:46:29 +00:00
|
|
|
local function is_null_ls_formatting_enabled(bufnr)
|
|
|
|
local file_type = vim.api.nvim_buf_get_option(bufnr, "filetype")
|
|
|
|
local generators = require("null-ls.generators").get_available(
|
|
|
|
file_type,
|
|
|
|
require("null-ls.methods").internal.FORMATTING
|
|
|
|
)
|
|
|
|
return #generators > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if is_null_ls_formatting_enabled(bufnr) then
|
|
|
|
vim.lsp.buf.format({
|
|
|
|
bufnr = bufnr,
|
|
|
|
filter = function(client)
|
|
|
|
return client.name == "null-ls"
|
|
|
|
end
|
|
|
|
})
|
|
|
|
else
|
|
|
|
vim.lsp.buf.format({
|
|
|
|
bufnr = bufnr,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
''
|
|
|
|
else "
|
2023-05-12 18:07:44 +00:00
|
|
|
vim.lsp.buf.format({
|
2023-05-23 20:53:18 +00:00
|
|
|
bufnr = bufnr,
|
2023-05-12 18:07:44 +00:00
|
|
|
})
|
2023-05-23 20:53:18 +00:00
|
|
|
"
|
2024-03-12 00:46:29 +00:00
|
|
|
}
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
2023-05-12 18:07:44 +00:00
|
|
|
end
|
2023-02-01 19:11:37 +00:00
|
|
|
|
2024-07-08 21:57:58 +00:00
|
|
|
${optionalString config.vim.ui.breadcrumbs.enable ''local navic = require("nvim-navic")''}
|
2024-03-12 00:46:29 +00:00
|
|
|
default_on_attach = function(client, bufnr)
|
|
|
|
attach_keymaps(client, bufnr)
|
|
|
|
format_callback(client, bufnr)
|
2024-07-08 21:57:58 +00:00
|
|
|
${optionalString config.vim.ui.breadcrumbs.enable ''
|
2024-03-12 00:46:29 +00:00
|
|
|
-- let navic attach to buffers
|
|
|
|
if client.server_capabilities.documentSymbolProvider then
|
|
|
|
navic.attach(client, bufnr)
|
|
|
|
end
|
|
|
|
''}
|
2023-07-19 20:06:06 +00:00
|
|
|
end
|
2023-02-01 19:11:37 +00:00
|
|
|
|
2024-03-12 00:46:29 +00:00
|
|
|
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
2024-11-03 17:34:43 +00:00
|
|
|
${optionalString usingNvimCmp ''
|
2024-11-03 18:26:06 +00:00
|
|
|
-- HACK: copied from cmp-nvim-lsp. If we ever lazy load lspconfig we
|
2024-11-03 17:34:43 +00:00
|
|
|
-- should re-evaluate whether we can just use `default_capabilities`
|
|
|
|
capabilities = {
|
|
|
|
textDocument = {
|
|
|
|
completion = {
|
|
|
|
dynamicRegistration = false,
|
|
|
|
completionItem = {
|
|
|
|
snippetSupport = true,
|
|
|
|
commitCharactersSupport = true,
|
|
|
|
deprecatedSupport = true,
|
|
|
|
preselectSupport = true,
|
|
|
|
tagSupport = {
|
|
|
|
valueSet = {
|
|
|
|
1, -- Deprecated
|
|
|
|
}
|
|
|
|
},
|
|
|
|
insertReplaceSupport = true,
|
|
|
|
resolveSupport = {
|
|
|
|
properties = {
|
|
|
|
"documentation",
|
|
|
|
"detail",
|
|
|
|
"additionalTextEdits",
|
|
|
|
"sortText",
|
|
|
|
"filterText",
|
|
|
|
"insertText",
|
|
|
|
"textEdit",
|
|
|
|
"insertTextFormat",
|
|
|
|
"insertTextMode",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertTextModeSupport = {
|
|
|
|
valueSet = {
|
|
|
|
1, -- asIs
|
|
|
|
2, -- adjustIndentation
|
|
|
|
}
|
|
|
|
},
|
|
|
|
labelDetailsSupport = true,
|
|
|
|
},
|
|
|
|
contextSupport = true,
|
|
|
|
insertTextMode = 1,
|
|
|
|
completionList = {
|
|
|
|
itemDefaults = {
|
|
|
|
'commitCharacters',
|
|
|
|
'editRange',
|
|
|
|
'insertTextFormat',
|
|
|
|
'insertTextMode',
|
|
|
|
'data',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
''}
|
2024-03-12 00:46:29 +00:00
|
|
|
'';
|
|
|
|
};
|
2023-04-17 20:27:27 +00:00
|
|
|
};
|
2023-02-01 19:11:37 +00:00
|
|
|
}
|