mirror of
https://github.com/NotAShelf/nvf.git
synced 2026-04-29 12:55:22 +00:00
languages/clang: refactor lsp.servers to use lsp/presets/<name>
This commit is contained in:
parent
58f73dc76a
commit
c5a83d3a6a
1 changed files with 11 additions and 136 deletions
|
|
@ -6,141 +6,16 @@
|
||||||
}: let
|
}: let
|
||||||
inherit (builtins) attrNames;
|
inherit (builtins) attrNames;
|
||||||
inherit (lib.options) mkEnableOption mkOption literalExpression;
|
inherit (lib.options) mkEnableOption mkOption literalExpression;
|
||||||
inherit (lib.types) bool enum package;
|
inherit (lib.types) bool enum package listOf;
|
||||||
inherit (lib.meta) getExe;
|
inherit (lib) genAttrs;
|
||||||
inherit (lib.modules) mkIf mkMerge;
|
inherit (lib.modules) mkIf mkMerge;
|
||||||
inherit (lib.generators) mkLuaInline;
|
inherit (lib.nvim.types) mkGrammarOption;
|
||||||
inherit (lib.nvim.types) mkGrammarOption deprecatedSingleOrListOf;
|
|
||||||
inherit (lib.nvim.attrsets) mapListToAttrs;
|
|
||||||
inherit (lib.nvim.dag) entryAfter;
|
inherit (lib.nvim.dag) entryAfter;
|
||||||
|
|
||||||
cfg = config.vim.languages.clang;
|
cfg = config.vim.languages.clang;
|
||||||
|
|
||||||
defaultServers = ["clangd"];
|
defaultServers = ["clangd"];
|
||||||
servers = {
|
servers = ["ccls" "clangd"];
|
||||||
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
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
defaultDebugger = "lldb-vscode";
|
defaultDebugger = "lldb-vscode";
|
||||||
debuggers = {
|
debuggers = {
|
||||||
|
|
@ -204,7 +79,7 @@ in {
|
||||||
|
|
||||||
servers = mkOption {
|
servers = mkOption {
|
||||||
description = "The clang LSP server to use";
|
description = "The clang LSP server to use";
|
||||||
type = deprecatedSingleOrListOf "vim.language.clang.lsp.servers" (enum (attrNames servers));
|
type = listOf (enum servers);
|
||||||
default = defaultServers;
|
default = defaultServers;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
@ -240,12 +115,12 @@ in {
|
||||||
})
|
})
|
||||||
|
|
||||||
(mkIf cfg.lsp.enable {
|
(mkIf cfg.lsp.enable {
|
||||||
vim.lsp.servers =
|
vim.lsp = {
|
||||||
mapListToAttrs (name: {
|
presets = genAttrs cfg.lsp.servers (_: {enable = true;});
|
||||||
inherit name;
|
servers = genAttrs cfg.lsp.servers (_: {
|
||||||
value = servers.${name};
|
filetypes = ["c" "cpp" "objc" "objcpp" "cuda" "proto"];
|
||||||
})
|
});
|
||||||
cfg.lsp.servers;
|
};
|
||||||
})
|
})
|
||||||
|
|
||||||
(mkIf cfg.dap.enable {
|
(mkIf cfg.dap.enable {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue