Merge pull request #1156 from horriblename/LspAttach-keybinds

attach LSP keybinds + other setup work in LspAttach
This commit is contained in:
raf 2025-09-30 21:43:36 +03:00 committed by GitHub
commit 5ff51032a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 49 additions and 48 deletions

View file

@ -28,6 +28,10 @@
align with the "hunks" themed mapping and avoid conflict with the new [neogit] align with the "hunks" themed mapping and avoid conflict with the new [neogit]
group. group.
- LSP keybinds and related plugin integrations are now attached in an LspAttach
autocmd event. If you were calling `default_on_attach()` in your LSP setup you
can remove them now.
[NotAShelf](https://github.com/notashelf): [NotAShelf](https://github.com/notashelf):
[typst-preview.nvim]: https://github.com/chomosuke/typst-preview.nvim [typst-preview.nvim]: https://github.com/chomosuke/typst-preview.nvim
@ -135,6 +139,8 @@
- Moved code setting `additionalRuntimePaths` and `enableLuaLoader` out of - Moved code setting `additionalRuntimePaths` and `enableLuaLoader` out of
`luaConfigPre`'s default to prevent being overridden `luaConfigPre`'s default to prevent being overridden
- Use conform over custom autocmds for LSP format on save - Use conform over custom autocmds for LSP format on save
- Move LSP keybinds and other related plugin integrations into an LspAttach
event.
[diniamo](https://github.com/diniamo): [diniamo](https://github.com/diniamo):

View file

@ -8,7 +8,7 @@
# - the addition of the function `entryBefore` indicating a "wanted # - the addition of the function `entryBefore` indicating a "wanted
# by" relationship. # by" relationship.
{lib}: let {lib}: let
inherit (builtins) isAttrs attrValues attrNames elem all head tail length toJSON isString; inherit (builtins) isAttrs attrValues attrNames elem all head tail length toJSON isString removeAttrs;
inherit (lib.attrsets) filterAttrs mapAttrs; inherit (lib.attrsets) filterAttrs mapAttrs;
inherit (lib.lists) toposort; inherit (lib.lists) toposort;
inherit (lib.nvim.dag) empty isEntry entryBetween entryAfter entriesBetween entryAnywhere topoSort; inherit (lib.nvim.dag) empty isEntry entryBetween entryAfter entriesBetween entryAnywhere topoSort;
@ -169,10 +169,11 @@ in {
else value) else value)
dag; dag;
sortedDag = topoSort finalDag; sortedDag = topoSort finalDag;
loopDetail = map (loops: removeAttrs loops ["data"]) sortedDag.loops;
result = result =
if sortedDag ? result if sortedDag ? result
then mapResult sortedDag.result then mapResult sortedDag.result
else abort ("Dependency cycle in ${name}: " + toJSON sortedDag); else abort ("Dependency cycle in ${name}: " + toJSON loopDetail);
in in
result; result;
} }

View file

@ -9,7 +9,7 @@
inherit (lib.types) nullOr submodule listOf str bool; inherit (lib.types) nullOr submodule listOf str bool;
inherit (lib.nvim.types) luaInline; inherit (lib.nvim.types) luaInline;
inherit (lib.nvim.lua) toLuaObject; inherit (lib.nvim.lua) toLuaObject;
inherit (lib.nvim.dag) entryAfter; inherit (lib.nvim.dag) entryAfter entryBetween;
autocommandType = submodule { autocommandType = submodule {
options = { options = {
@ -144,7 +144,7 @@ in {
enabledAutogroups = filter (au: au.enable) cfg.augroups; enabledAutogroups = filter (au: au.enable) cfg.augroups;
in { in {
luaConfigRC = { luaConfigRC = {
augroups = entryAfter ["pluginConfigs"] (optionalString (enabledAutogroups != []) '' augroups = entryBetween ["autocmds"] ["pluginConfigs"] (optionalString (enabledAutogroups != []) ''
local nvf_autogroups = {} local nvf_autogroups = {}
for _, group in ipairs(${toLuaObject enabledAutogroups}) do for _, group in ipairs(${toLuaObject enabledAutogroups}) do
if group.name then if group.name then

View file

@ -77,7 +77,6 @@ in {
{ {
vim.lsp.servers."*" = { vim.lsp.servers."*" = {
capabilities = mkDefault (mkLuaInline "capabilities"); capabilities = mkDefault (mkLuaInline "capabilities");
on_attach = mkDefault (mkLuaInline "default_on_attach");
}; };
} }

View file

@ -26,8 +26,6 @@
workspace_required = true; workspace_required = true;
on_attach = mkLuaInline '' on_attach = mkLuaInline ''
function(client, bufnr) function(client, bufnr)
default_on_attach(client, bufnr)
local function switch_source_header(bufnr) local function switch_source_header(bufnr)
local method_name = "textDocument/switchSourceHeader" local method_name = "textDocument/switchSourceHeader"
local params = vim.lsp.util.make_text_document_params(bufnr) local params = vim.lsp.util.make_text_document_params(bufnr)
@ -77,8 +75,6 @@
}; };
on_attach = mkLuaInline '' on_attach = mkLuaInline ''
function(client, bufnr) function(client, bufnr)
default_on_attach(client, bufnr)
local function switch_source_header(bufnr) local function switch_source_header(bufnr)
local method_name = "textDocument/switchSourceHeader" local method_name = "textDocument/switchSourceHeader"
local client = vim.lsp.get_clients({ bufnr = bufnr, name = "clangd", })[1] local client = vim.lsp.get_clients({ bufnr = bufnr, name = "clangd", })[1]

View file

@ -63,12 +63,11 @@
}; };
on_attach = mkLuaInline '' on_attach = mkLuaInline ''
function(client, bufnr) function(client, bufnr)
default_on_attach(client, bufnr) local oe = require("omnisharp_extended")
local oe = require("omnisharp_extended") ${mkLspBinding "goToDefinition" "oe.lsp_definition"}
${mkLspBinding "goToDefinition" "oe.lsp_definition"} ${mkLspBinding "goToType" "oe.lsp_type_definition"}
${mkLspBinding "goToType" "oe.lsp_type_definition"} ${mkLspBinding "listReferences" "oe.lsp_references"}
${mkLspBinding "listReferences" "oe.lsp_references"} ${mkLspBinding "listImplementations" "oe.lsp_implementation"}
${mkLspBinding "listImplementations" "oe.lsp_implementation"}
end end
''; '';
settings = { settings = {

View file

@ -161,7 +161,6 @@ in {
}, },
capabilities = capabilities, capabilities = capabilities,
on_attach = default_on_attach;
}, },
${optionalString cfg.dap.enable '' ${optionalString cfg.dap.enable ''
debugger = { debugger = {

View file

@ -33,7 +33,6 @@
'' ''
function(client, bufnr) function(client, bufnr)
local ht = require("haskell-tools") local ht = require("haskell-tools")
default_on_attach(client, bufnr, ht)
local opts = { noremap = true, silent = true, buffer = bufnr } local opts = { noremap = true, silent = true, buffer = bufnr }
vim.keymap.set('n', '<localleader>cl', vim.lsp.codelens.run, opts) vim.keymap.set('n', '<localleader>cl', vim.lsp.codelens.run, opts)
vim.keymap.set('n', '<localleader>hs', ht.hoogle.hoogle_signature, opts) vim.keymap.set('n', '<localleader>hs', ht.hoogle.hoogle_signature, opts)

View file

@ -42,7 +42,6 @@
}; };
on_attach = mkLuaInline '' on_attach = mkLuaInline ''
function(client, bufnr) function(client, bufnr)
default_on_attach(client, bufnr);
vim.api.nvim_buf_create_user_command(bufnr, 'LspPyrightOrganizeImports', function() vim.api.nvim_buf_create_user_command(bufnr, 'LspPyrightOrganizeImports', function()
local params = { local params = {
command = 'pyright.organizeimports', command = 'pyright.organizeimports',
@ -89,7 +88,6 @@
}; };
on_attach = mkLuaInline '' on_attach = mkLuaInline ''
function(client, bufnr) function(client, bufnr)
default_on_attach(client, bufnr);
vim.api.nvim_buf_create_user_command(bufnr, 'LspPyrightOrganizeImports', function() vim.api.nvim_buf_create_user_command(bufnr, 'LspPyrightOrganizeImports', function()
local params = { local params = {
command = 'basedpyright.organizeimports', command = 'basedpyright.organizeimports',

View file

@ -160,7 +160,6 @@ in {
${cfg.lsp.opts} ${cfg.lsp.opts}
}, },
on_attach = function(client, bufnr) on_attach = function(client, bufnr)
default_on_attach(client, bufnr)
local opts = { noremap=true, silent=true, buffer = bufnr } local opts = { noremap=true, silent=true, buffer = bufnr }
vim.keymap.set("n", "<localleader>rr", ":RustLsp runnables<CR>", opts) vim.keymap.set("n", "<localleader>rr", ":RustLsp runnables<CR>", opts)
vim.keymap.set("n", "<localleader>rp", ":RustLsp parentModule<CR>", opts) vim.keymap.set("n", "<localleader>rp", ":RustLsp parentModule<CR>", opts)

View file

@ -50,8 +50,6 @@
}; };
on_attach = mkLuaInline '' on_attach = mkLuaInline ''
function(client, bufnr) function(client, bufnr)
default_on_attach(client, bufnr);
-- ts_ls provides `source.*` code actions that apply to the whole file. These only appear in -- ts_ls provides `source.*` code actions that apply to the whole file. These only appear in
-- `vim.lsp.buf.code_action()` if specified in `context.only`. -- `vim.lsp.buf.code_action()` if specified in `context.only`.
vim.api.nvim_buf_create_user_command(0, 'LspTypescriptSourceAction', function() vim.api.nvim_buf_create_user_command(0, 'LspTypescriptSourceAction', function()
@ -106,7 +104,6 @@
}; };
on_attach = mkLuaInline '' on_attach = mkLuaInline ''
function(client, bufnr) function(client, bufnr)
default_on_attach(client, bufnr)
vim.api.nvim_buf_create_user_command(0, 'LspDenolsCache', function() vim.api.nvim_buf_create_user_command(0, 'LspDenolsCache', function()
client:exec_cmd({ client:exec_cmd({
command = 'deno.cache', command = 'deno.cache',

View file

@ -15,18 +15,18 @@
cfg = config.vim.languages.yaml; cfg = config.vim.languages.yaml;
on_attach = mkLuaInline ( on_attach =
if config.vim.languages.helm.lsp.enable && config.vim.languages.helm.enable if config.vim.languages.helm.lsp.enable && config.vim.languages.helm.enable
then '' then
function(client, bufnr) mkLuaInline ''
default_on_attach() function(client, bufnr)
local filetype = vim.bo[bufnr].filetype local filetype = vim.bo[bufnr].filetype
if filetype == "helm" then if filetype == "helm" then
client.stop() client.stop()
end
end end
end'' ''
else "default_on_attach" else null;
);
defaultServers = ["yaml-language-server"]; defaultServers = ["yaml-language-server"];
servers = { servers = {

View file

@ -10,6 +10,8 @@
inherit (lib.strings) optionalString; inherit (lib.strings) optionalString;
inherit (lib.trivial) boolToString; inherit (lib.trivial) boolToString;
inherit (lib.nvim.binds) addDescriptionsToMappings; inherit (lib.nvim.binds) addDescriptionsToMappings;
inherit (lib.nvim.dag) entryBefore;
inherit (lib.nvim.lua) toLuaObject;
cfg = config.vim.lsp; cfg = config.vim.lsp;
usingNvimCmp = config.vim.autocomplete.nvim-cmp.enable; usingNvimCmp = config.vim.autocomplete.nvim-cmp.enable;
@ -22,7 +24,7 @@
mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions; mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
mkBinding = binding: action: mkBinding = binding: action:
if binding.value != null if binding.value != null
then "vim.keymap.set('n', '${binding.value}', ${action}, {buffer=bufnr, noremap=true, silent=true, desc='${binding.description}'})" then "vim.keymap.set('n', ${toLuaObject binding.value}, ${action}, {buffer=bufnr, noremap=true, silent=true, desc=${toLuaObject binding.description}})"
else ""; else "";
in { in {
config = mkIf cfg.enable { config = mkIf cfg.enable {
@ -34,20 +36,26 @@ in {
augroups = [{name = augroup;}]; augroups = [{name = augroup;}];
autocmds = autocmds =
(optional cfg.inlayHints.enable { [
group = augroup; {
event = ["LspAttach"]; group = augroup;
desc = "LSP on-attach enable inlay hints autocmd"; event = ["LspAttach"];
callback = mkLuaInline '' desc = "LSP on-attach add keybinds, enable inlay hints, and other plugin integrations";
function(event) callback = mkLuaInline ''
local bufnr = event.buf function(event)
local client = vim.lsp.get_client_by_id(event.data.client_id) local bufnr = event.buf
if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_inlayHint) then local client = vim.lsp.get_client_by_id(event.data.client_id)
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr }), { bufnr = bufnr }) default_on_attach(client, bufnr)
${optionalString cfg.inlayHints.enable ''
if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_inlayHint) then
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr }), { bufnr = bufnr })
end
''}
end end
end '';
''; }
}) ]
++ (optional (!conformFormatOnSave) { ++ (optional (!conformFormatOnSave) {
group = augroup; group = augroup;
event = ["BufWritePre"]; event = ["BufWritePre"];
@ -86,7 +94,7 @@ in {
''; '';
}); });
pluginRC.lsp-setup = '' pluginRC.lsp-setup = entryBefore ["autocmds"] ''
vim.g.formatsave = ${boolToString cfg.formatOnSave}; vim.g.formatsave = ${boolToString cfg.formatOnSave};
local attach_keymaps = function(client, bufnr) local attach_keymaps = function(client, bufnr)