@@ -65,53 +65,11 @@ function M.lsp_setup(server)
6565 end
6666end
6767
68- local function add_buffer_autocmd (augroup , bufnr , autocmds )
69- if not vim .tbl_islist (autocmds ) then autocmds = { autocmds } end
70- local cmds_found , cmds = pcall (vim .api .nvim_get_autocmds , { group = augroup , buffer = bufnr })
71- if not cmds_found or vim .tbl_isempty (cmds ) then
72- vim .api .nvim_create_augroup (augroup , { clear = false })
73- for _ , autocmd in ipairs (autocmds ) do
74- local events = autocmd .events
75- autocmd .events = nil
76- autocmd .group = augroup
77- autocmd .buffer = bufnr
78- vim .api .nvim_create_autocmd (events , autocmd )
79- end
80- end
81- end
82-
83- local function del_buffer_autocmd (augroup , bufnr )
84- local cmds_found , cmds = pcall (vim .api .nvim_get_autocmds , { group = augroup , buffer = bufnr })
85- if cmds_found then vim .tbl_map (function (cmd ) vim .api .nvim_del_autocmd (cmd .id ) end , cmds ) end
86- end
87-
88- local function has_capability (capability , filter )
89- -- TODO: remove check after dropping support for Neovim v0.9
90- --- @diagnostic disable-next-line : deprecated
91- for _ , client in ipairs ((vim .lsp .get_clients or vim .lsp .get_active_clients )(filter )) do
92- if client .supports_method (capability ) then return true end
93- end
94- return false
95- end
96-
9768--- The `on_attach` function used by AstroNvim
9869--- @param client table The LSP client details when attaching
9970--- @param bufnr integer The buffer that the LSP client is attaching to
10071M .on_attach = function (client , bufnr )
101- if client .supports_method " textDocument/codeLens" then
102- add_buffer_autocmd (" lsp_codelens_refresh" , bufnr , {
103- events = { " InsertLeave" , " BufEnter" },
104- desc = " Refresh codelens" ,
105- callback = function ()
106- if not has_capability (" textDocument/codeLens" , { bufnr = bufnr }) then
107- del_buffer_autocmd (" lsp_codelens_refresh" , bufnr )
108- return
109- end
110- if M .config .features .codelens then vim .lsp .codelens .refresh () end
111- end ,
112- })
113- if M .config .features .codelens then vim .lsp .codelens .refresh () end
114- end
72+ if client .supports_method " textDocument/codeLens" and M .config .features .codelens then vim .lsp .codelens .refresh () end
11573
11674 if
11775 client .supports_method " textDocument/formatting"
@@ -131,42 +89,6 @@ M.on_attach = function(client, bufnr)
13189 and (tbl_isempty (autoformat .allow_filetypes or {}) or tbl_contains (autoformat .allow_filetypes , filetype ))
13290 and (tbl_isempty (autoformat .ignore_filetypes or {}) or not tbl_contains (autoformat .ignore_filetypes , filetype ))
13391 end
134- add_buffer_autocmd (" lsp_auto_format" , bufnr , {
135- events = " BufWritePre" ,
136- desc = " autoformat on save" ,
137- callback = function ()
138- if not has_capability (" textDocument/formatting" , { bufnr = bufnr }) then
139- del_buffer_autocmd (" lsp_auto_format" , bufnr )
140- return
141- end
142- local buffer_autoformat = vim .b [bufnr ].autoformat
143- if buffer_autoformat == nil then buffer_autoformat = autoformat .enabled end
144- if buffer_autoformat and ((not autoformat .filter ) or autoformat .filter (bufnr )) then
145- vim .lsp .buf .format (vim .tbl_deep_extend (" force" , M .format_opts , { bufnr = bufnr }))
146- end
147- end ,
148- })
149- end
150-
151- if client .supports_method " textDocument/documentHighlight" then
152- add_buffer_autocmd (" lsp_document_highlight" , bufnr , {
153- {
154- events = { " CursorHold" , " CursorHoldI" },
155- desc = " highlight references when cursor holds" ,
156- callback = function ()
157- if not has_capability (" textDocument/documentHighlight" , { bufnr = bufnr }) then
158- del_buffer_autocmd (" lsp_document_highlight" , bufnr )
159- return
160- end
161- vim .lsp .buf .document_highlight ()
162- end ,
163- },
164- {
165- events = { " CursorMoved" , " CursorMovedI" , " BufLeave" },
166- desc = " clear references when cursor moves" ,
167- callback = function () vim .lsp .buf .clear_references () end ,
168- },
169- })
17092 end
17193
17294 if client .supports_method " textDocument/inlayHint" then
@@ -183,6 +105,43 @@ M.on_attach = function(client, bufnr)
183105 end
184106 end
185107
108+ for augroup , autocmds in pairs (M .config .autocmds ) do
109+ if autocmds then
110+ local cmds_found , cmds = pcall (vim .api .nvim_get_autocmds , { group = augroup , buffer = bufnr })
111+ if not cmds_found or vim .tbl_isempty (cmds ) then
112+ local cond = autocmds .cond
113+ local cond_func = type (cond ) == " string" and function (c ) return c .supports_method (cond ) end or cond
114+ if cond_func == nil or cond_func (client , bufnr ) then
115+ vim .api .nvim_create_augroup (augroup , { clear = false })
116+ for _ , autocmd in ipairs (autocmds ) do
117+ local opts = vim .deepcopy (autocmd )
118+ opts .command , opts .event = nil , nil
119+ opts .group , opts .buffer = augroup , bufnr
120+ local callback = autocmd .callback
121+ if autocmd .command and not callback then callback = function () vim .cmd (autocmd .command ) end end
122+ opts .callback = function (args )
123+ local valid = false
124+ for _ , cb_client in ipairs ((vim .lsp .get_clients or vim .lsp .get_active_clients ) { buffer = bufnr }) do
125+ if cond_func == nil or cond_func (cb_client , bufnr ) then
126+ valid = true
127+ break
128+ end
129+ end
130+ if not valid then
131+ local cb_cmds_found , cb_cmds = pcall (vim .api .nvim_get_autocmds , { group = augroup , buffer = bufnr })
132+ if cb_cmds_found then vim .tbl_map (function (cmd ) vim .api .nvim_del_autocmd (cmd .id ) end , cb_cmds ) end
133+ return
134+ end
135+ --- @diagnostic disable-next-line : redundant-parameter
136+ return callback (args , client , bufnr )
137+ end
138+ vim .api .nvim_create_autocmd (autocmd .event , opts )
139+ end
140+ end
141+ end
142+ end
143+ end
144+
186145 local wk_avail , wk = pcall (require , " which-key" )
187146 for mode , maps in pairs (M .config .mappings ) do
188147 for lhs , map_opts in pairs (maps ) do
0 commit comments