Skip to content

Commit 0e224f3

Browse files
committed
feat: align conditionals across mappings, autocmds, and user commands
1 parent 2b594b0 commit 0e224f3

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

lua/astrolsp/config.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77
-- copyright 2023
88
-- license GNU General Public License v3.0
99

10+
---@alias AstroLSPCondition string|boolean|fun(client:lsp.Client,bufnr:integer):boolean conditional for doing something when attaching a language server
11+
1012
---@class AstroLSPMapping: vim.api.keyset.keymap
1113
---@field [1] string|function rhs of keymap
1214
---@field name string? optional which-key mapping name
13-
---@field cond boolean|function|string? condition for the mapping
15+
---@field cond AstroLSPCondition? condition for whether or not to set the mapping during language server attachment
16+
17+
---@class AstroLSPCommand: vim.api.keyset.user_command
18+
---@field [1] string|function the command to execute
19+
---@field cond AstroLSPCondition? condition for whether or not to create the user command during language server attachment
1420

1521
---@class AstroLSPAutocmd: vim.api.keyset.create_autocmd
1622
---@field event string|string[] Event(s) that will trigger the handler
1723

18-
---@class AstroLSPCommand: vim.api.keyset.user_command
19-
---@field [1] string|function the command to execute
20-
---@field cond string|(fun(client,bufnr):boolean)? condition for the user command
24+
---@class AstroLSPAutocmds
25+
---@field cond AstroLSPCondition? condition for whether or not to create the auto commands during language server attachment
26+
---@field [integer] AstroLSPAutocmd an autocommand definition
2127

2228
---@class AstroLSPFeatureOpts
2329
---@field codelens boolean? enable/disable codelens refresh on start (boolean; default = true)
@@ -38,10 +44,6 @@
3844
---@field timeout_ms integer? configure the timeout length for formatting
3945
---@field filter (fun(client):boolean)? fully override the default formatting filter function
4046

41-
---@class AstroLSPAutocmds
42-
---@field cond string|(fun(client,bufnr):boolean)? condition for the auto commands
43-
---@field [integer] AstroLSPAutocmd an autocommand definition
44-
4547
---@class AstroLSPOpts
4648
---Configuration of auto commands
4749
---The key into the table is the group name for the auto commands (`:h augroup`) and the value

lua/astrolsp/init.lua

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ local function lsp_event(name)
2121
vim.schedule(function() vim.api.nvim_exec_autocmds("User", { pattern = "AstroLsp" .. name, modeline = false }) end)
2222
end
2323

24+
---@param cond AstroLSPCondition?
25+
---@param client lsp.Client
26+
---@param bufnr integer
27+
local function check_cond(cond, client, bufnr)
28+
local active = true
29+
if type(cond) == "function" then
30+
active = cond(client, bufnr)
31+
elseif type(cond) == "string" then
32+
active = client.supports_method(cond)
33+
elseif type(cond) == "boolean" then
34+
active = cond
35+
end
36+
return active
37+
end
38+
2439
--- A table of settings for different levels of diagnostics
2540
M.diagnostics = { [0] = {}, {}, {}, {} }
2641

@@ -103,8 +118,7 @@ M.on_attach = function(client, bufnr)
103118
for cmd, spec in pairs(M.config.commands) do
104119
if spec then
105120
local cond = spec.cond
106-
local cond_func = type(cond) == "string" and function(c) return c.supports_method(cond) end or cond
107-
if cond_func == nil or cond_func(client, bufnr) then
121+
if check_cond(cond, client, bufnr) then
108122
local action = spec[1]
109123
spec[1], spec.cond = nil, nil
110124
vim.api.nvim_buf_create_user_command(bufnr, cmd, action, spec)
@@ -118,8 +132,7 @@ M.on_attach = function(client, bufnr)
118132
local cmds_found, cmds = pcall(vim.api.nvim_get_autocmds, { group = augroup, buffer = bufnr })
119133
if not cmds_found or vim.tbl_isempty(cmds) then
120134
local cond = autocmds.cond
121-
local cond_func = type(cond) == "string" and function(c) return c.supports_method(cond) end or cond
122-
if cond_func == nil or cond_func(client, bufnr) then
135+
if check_cond(cond, client, bufnr) then
123136
local group = vim.api.nvim_create_augroup(augroup, { clear = false })
124137
for _, autocmd in ipairs(autocmds) do
125138
local callback, command, event = autocmd.callback, autocmd.command, autocmd.event
@@ -129,7 +142,7 @@ M.on_attach = function(client, bufnr)
129142
autocmd.callback = function(args)
130143
local invalid = true
131144
for _, cb_client in ipairs((vim.lsp.get_clients or vim.lsp.get_active_clients) { buffer = bufnr }) do
132-
if cond_func == nil or cond_func(cb_client, bufnr) then
145+
if check_cond(cond, cb_client, bufnr) then
133146
invalid = false
134147
break
135148
end
@@ -153,28 +166,25 @@ M.on_attach = function(client, bufnr)
153166
local wk_avail, wk = pcall(require, "which-key")
154167
for mode, maps in pairs(M.config.mappings) do
155168
for lhs, map_opts in pairs(maps) do
156-
if
157-
type(map_opts) ~= "table"
158-
or map_opts.cond == nil
159-
or type(map_opts.cond) == "boolean" and map_opts.cond
160-
or type(map_opts.cond) == "function" and map_opts.cond(client, bufnr)
161-
or type(map_opts.cond) == "string" and client.supports_method(map_opts.cond)
162-
then
163-
local rhs
164-
if type(map_opts) == "table" then
165-
rhs = map_opts[1]
166-
map_opts = assert(vim.tbl_deep_extend("force", map_opts, { buffer = bufnr }))
167-
map_opts[1], map_opts.cond = nil, nil
168-
else
169-
---@cast map_opts -AstroLSPMapping
170-
rhs = map_opts
171-
map_opts = { buffer = bufnr }
172-
end
173-
if not rhs or map_opts.name then
174-
if not map_opts.name then map_opts.name = map_opts.desc end
175-
if wk_avail then wk.register({ [lhs] = map_opts }, { mode = mode }) end
176-
else
177-
vim.keymap.set(mode, lhs, rhs, map_opts)
169+
if map_opts then
170+
local active = map_opts ~= false
171+
if type(map_opts) == "table" then active = check_cond(map_opts.cond, client, bufnr) end
172+
if active then
173+
local rhs
174+
if type(map_opts) == "string" then
175+
rhs = map_opts
176+
map_opts = { buffer = bufnr }
177+
else
178+
rhs = map_opts[1]
179+
map_opts = assert(vim.tbl_deep_extend("force", map_opts, { buffer = bufnr }))
180+
map_opts[1], map_opts.cond = nil, nil
181+
end
182+
if not rhs or map_opts.name then
183+
if not map_opts.name then map_opts.name = map_opts.desc end
184+
if wk_avail then wk.register({ [lhs] = map_opts }, { mode = mode }) end
185+
else
186+
vim.keymap.set(mode, lhs, rhs, map_opts)
187+
end
178188
end
179189
end
180190
end

0 commit comments

Comments
 (0)