Skip to content

Commit

Permalink
feat: otter.deactivate function (#96)
Browse files Browse the repository at this point in the history
* feat: deactivate method

* fix: nil check otters_attached
  • Loading branch information
benlubas authored Mar 2, 2024
1 parent 6234723 commit 519c777
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions lua/otter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,58 @@ M.activate = function(languages, completion, diagnostics, tsquery)
local ns = api.nvim_create_namespace("otter-lang-" .. lang)
nss[bufnr] = ns
end
keeper._otters_attached[main_nr].nss = nss

local sync_diagnostics = function(_, _)
M.sync_raft(main_nr)
for bufnr, ns in pairs(nss) do
local diag = vim.diagnostic.get(bufnr)
vim.diagnostic.reset(ns, main_nr)
vim.diagnostic.set(ns, main_nr, diag, {})
end
M.sync_raft(main_nr)
for bufnr, ns in pairs(nss) do
local diag = vim.diagnostic.get(bufnr)
vim.diagnostic.reset(ns, main_nr)
vim.diagnostic.set(ns, main_nr, diag, {})
end
end

api.nvim_create_autocmd("BufWritePost", {
buffer = main_nr,
group = api.nvim_create_augroup("OtterDiagnostics", {}),
group = api.nvim_create_augroup("OtterDiagnostics" .. main_nr, {}),
callback = sync_diagnostics,
})
sync_diagnostics(nil, nil)
end
end

---Deactivate the current buffer by removing otter buffers and clearing diagnostics
---@param completion boolean | nil
---@param diagnostics boolean | nil
M.deactivate = function(completion, diagnostics)
completion = completion ~= false
diagnostics = diagnostics ~= false

local main_nr = api.nvim_get_current_buf()
if keeper._otters_attached[main_nr] == nil then
return
end

if diagnostics then
for _, ns in pairs(keeper._otters_attached[main_nr].nss) do
vim.diagnostic.reset(ns, main_nr)
end
end

if completion then
api.nvim_del_augroup_by_name("cmp_otter" .. main_nr)
end

for _, otter_bufnr in pairs(keeper._otters_attached[main_nr].buffers) do
-- Avoid 'textlock' with schedule
vim.schedule(function()
api.nvim_buf_delete(otter_bufnr, { force = true })
end)
end

keeper._otters_attached[main_nr] = nil
end

--- Got to definition of the symbol under the cursor
M.ask_definition = function()
local main_nr = api.nvim_get_current_buf()
Expand Down

0 comments on commit 519c777

Please sign in to comment.