languages/clang: refactor lsp.servers to use lsp/presets/<name>

This commit is contained in:
Snoweuph 2026-04-12 15:50:27 +02:00
commit c5a83d3a6a
No known key found for this signature in database
GPG key ID: BEFC41DA223CEC55

View file

@ -6,141 +6,16 @@
}: let
inherit (builtins) attrNames;
inherit (lib.options) mkEnableOption mkOption literalExpression;
inherit (lib.types) bool enum package;
inherit (lib.meta) getExe;
inherit (lib.types) bool enum package listOf;
inherit (lib) genAttrs;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.types) mkGrammarOption deprecatedSingleOrListOf;
inherit (lib.nvim.attrsets) mapListToAttrs;
inherit (lib.nvim.types) mkGrammarOption;
inherit (lib.nvim.dag) entryAfter;
cfg = config.vim.languages.clang;
defaultServers = ["clangd"];
servers = {
ccls = {
cmd = [(getExe pkgs.ccls)];
filetypes = ["c" "cpp" "objc" "objcpp" "cuda"];
offset_encoding = "utf-32";
root_markers = ["compile_commands.json" ".ccls" ".git"];
workspace_required = true;
on_attach = mkLuaInline ''
function(client, bufnr)
local function switch_source_header(bufnr)
local method_name = "textDocument/switchSourceHeader"
local params = vim.lsp.util.make_text_document_params(bufnr)
client:request(method_name, params, function(err, result)
if err then
error(tostring(err))
end
if not result then
vim.notify('corresponding file cannot be determined')
return
end
vim.cmd.edit(vim.uri_to_fname(result))
end, bufnr)
end
vim.api.nvim_buf_create_user_command(
bufnr,
"LspCclsSwitchSourceHeader",
function(arg)
switch_source_header(client, 0)
end,
{desc = "Switch between source/header"}
)
end
'';
};
clangd = {
cmd = ["${pkgs.clang-tools}/bin/clangd"];
filetypes = ["c" "cpp" "objc" "objcpp" "cuda" "proto"];
root_markers = [
".clangd"
".clang-tidy"
".clang-format"
"compile_commands.json"
"compile_flags.txt"
"configure.ac"
".git"
];
capabilities = {
textDocument = {
completion = {
editsNearCursor = true;
};
};
offsetEncoding = ["utf-8" "utf-16"];
};
on_attach = mkLuaInline ''
function(client, bufnr)
local function switch_source_header(bufnr)
local method_name = "textDocument/switchSourceHeader"
local client = vim.lsp.get_clients({ bufnr = bufnr, name = "clangd", })[1]
if not client then
return vim.notify(('method %s is not supported by any servers active on the current buffer'):format(method_name))
end
local params = vim.lsp.util.make_text_document_params(bufnr)
client.request(method_name, params, function(err, result)
if err then
error(tostring(err))
end
if not result then
vim.notify('corresponding file cannot be determined')
return
end
vim.cmd.edit(vim.uri_to_fname(result))
end, bufnr)
end
local function symbol_info()
local bufnr = vim.api.nvim_get_current_buf()
local clangd_client = vim.lsp.get_clients({ bufnr = bufnr, name = "clangd" })[1]
if not clangd_client or not clangd_client:supports_method 'textDocument/symbolInfo' then
return vim.notify('Clangd client not found', vim.log.levels.ERROR)
end
local win = vim.api.nvim_get_current_win()
local params = vim.lsp.util.make_position_params(win, clangd_client.offset_encoding)
clangd_client:request('textDocument/symbolInfo', params, function(err, res)
if err or #res == 0 then
-- Clangd always returns an error, there is not reason to parse it
return
end
local container = string.format('container: %s', res[1].containerName) ---@type string
local name = string.format('name: %s', res[1].name) ---@type string
vim.lsp.util.open_floating_preview({ name, container }, "", {
height = 2,
width = math.max(string.len(name), string.len(container)),
focusable = false,
focus = false,
border = 'single',
title = 'Symbol Info',
})
end, bufnr)
end
vim.api.nvim_buf_create_user_command(
bufnr,
"ClangdSwitchSourceHeader",
function(arg)
switch_source_header(0)
end,
{desc = "Switch between source/header"}
)
vim.api.nvim_buf_create_user_command(
bufnr,
"ClangdShowSymbolInfo",
function(arg)
symbol_info()
end,
{desc = "Show symbol info"}
)
end
'';
};
};
servers = ["ccls" "clangd"];
defaultDebugger = "lldb-vscode";
debuggers = {
@ -204,7 +79,7 @@ in {
servers = mkOption {
description = "The clang LSP server to use";
type = deprecatedSingleOrListOf "vim.language.clang.lsp.servers" (enum (attrNames servers));
type = listOf (enum servers);
default = defaultServers;
};
};
@ -240,12 +115,12 @@ in {
})
(mkIf cfg.lsp.enable {
vim.lsp.servers =
mapListToAttrs (name: {
inherit name;
value = servers.${name};
})
cfg.lsp.servers;
vim.lsp = {
presets = genAttrs cfg.lsp.servers (_: {enable = true;});
servers = genAttrs cfg.lsp.servers (_: {
filetypes = ["c" "cpp" "objc" "objcpp" "cuda" "proto"];
});
};
})
(mkIf cfg.dap.enable {