diff --git a/lua/otter/init.lua b/lua/otter/init.lua index 4f492e4..fac6bad 100644 --- a/lua/otter/init.lua +++ b/lua/otter/init.lua @@ -60,6 +60,7 @@ M.activate = function(languages, completion, diagnostics, tsquery) keeper.rafts[main_nr].code_chunks = nil keeper.rafts[main_nr].last_changetick = nil keeper.rafts[main_nr].otterls = {} + keeper.rafts[main_nr].otterls.clients_by_otternr = {} local all_code_chunks = keeper.extract_code_chunks(main_nr) local found_languages = {} @@ -136,6 +137,29 @@ M.activate = function(languages, completion, diagnostics, tsquery) -- attached to their otter buffers keeper.sync_raft(main_nr) + -- add autocommand for otter to track lsp clients attached + -- to the otter buffers before they are attached + -- to track server capabilities + vim.api.nvim_create_autocmd('LspAttach', { + group = vim.api.nvim_create_augroup('OtterLSPAttach', { clear = true }), + callback = function(event) + local client = vim.lsp.get_client_by_id(event.data.client_id) + + -- check if the event comes from an otter buffer + for _, lang in ipairs(keeper.rafts[main_nr].languages) do + local otter_nr = keeper.rafts[main_nr].buffers[lang] + if keeper.rafts[main_nr].otterls.clients_by_otternr[otter_nr] == nil then + keeper.rafts[main_nr].otterls.clients_by_otternr[otter_nr] = {} + end + if otter_nr == event.buf then + table.insert(keeper.rafts[main_nr].otterls.clients_by_otternr[otter_nr], client) + end + end + end, + }) + + + -- manually attach language server that corresponds to the filetype -- without setting the filetype -- to prevent other plugins we don't need in the otter buffers diff --git a/lua/otter/lsp/init.lua b/lua/otter/lsp/init.lua index 4364915..1008406 100644 --- a/lua/otter/lsp/init.lua +++ b/lua/otter/lsp/init.lua @@ -8,24 +8,11 @@ local capabilities = vim.lsp.protocol.make_client_capabilities() local otterls = {} -otterls.clients = {} - -otterls.check_capabilities = function(main_nr) - local otter_nrs = keeper.rafts[main_nr].buffers - local clients = vim.lsp.get_clients() - for _, client in ipairs(clients) do - otterls.clients[main_nr][client.id] = {} - otterls.clients[main_nr][client.id].capabilities = client.server_capabilities - end - vim.print(otterls.clients) -end - --- @param main_nr integer main buffer --- @param completion boolean should completion be enabled? --- @return integer? client_id otterls.start = function(main_nr, completion) local main_uri = vim.uri_from_bufnr(main_nr) - otterls.clients[main_nr] = {} local client_id = vim.lsp.start({ name = "otter-ls" .. "[" .. main_nr .. "]", capabilities = capabilities, @@ -123,12 +110,17 @@ otterls.start = function(main_nr, completion) return end + local otter_nr = keeper.rafts[main_nr].buffers[lang] local otter_uri = vim.uri_from_bufnr(otter_nr) - -- populate the capabilities of the clients - -- attached to the otter buffers - otterls.check_capabilities(main_nr) + local otterclients = keeper.rafts[main_nr].otterls.clients_by_otternr[otter_nr] + if otterclients == nil then + return + end + for _, client in pairs(otterclients) do + vim.print(client.server_capabilities) + end -- update the otter buffer of that language local success = keeper.sync_raft(main_nr, lang)