@@ -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 )
2222end
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
2540M .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