Skip to content

Commit

Permalink
fix: diagnostics not updating (#105)
Browse files Browse the repository at this point in the history
* fix: diagnostics not updating

* fix: otter buffer going out of sync
  • Loading branch information
benlubas authored Mar 4, 2024
1 parent 91883c2 commit 8bdc078
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ otter.setup{
hover = {
border = { "", "", "", "", "", "", "", "" },
},
-- `:h events` that cause the diagnostics to update. Set to:
-- { "BufWritePost", "InsertLeave", "TextChanged" } for less performant
-- but more instant diagnostic updates
diagnostic_update_events = { "BufWritePost" },
},
buffers = {
-- if set to true, the filetype of the otterbuffers will be set.
Expand All @@ -114,13 +118,13 @@ the embedded code. Use it as follows:

```lua
local cmp = require'cmp'
cmp.setup(
cmp.setup({
-- <rest of your nvim-cmp config>
sources = {
{ name = "otter" },
-- <other sources>
}
}
},
})
```


Expand Down
1 change: 1 addition & 0 deletions lua/otter/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local default_config = {
hover = {
border = { "", "", "", "", "", "", "", "" },
},
diagnostic_update_events = { "BufWritePost" },
},
buffers = {
-- if set to true, the filetype of the otterbuffers will be set.
Expand Down
26 changes: 16 additions & 10 deletions lua/otter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +169,34 @@ M.activate = function(languages, completion, diagnostics, tsquery)
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 diags = vim.diagnostic.get(bufnr)
vim.diagnostic.reset(ns, main_nr)
local sync_diagnostics = function(args)
if vim.tbl_contains(vim.tbl_values(keeper._otters_attached[main_nr].buffers), args.buf) then
local diags = args.data.diagnostics
vim.diagnostic.reset(nss[args.buf], main_nr)
if config.cfg.handle_leading_whitespace then
for _, diag in ipairs(diags) do
local offset = keeper.get_leading_offset(diag.lnum, main_nr)
diag.col = diag.col + offset
diag.end_col = diag.end_col + offset
end
end
vim.diagnostic.set(ns, main_nr, diags, {})
vim.diagnostic.set(nss[args.buf], main_nr, diags, {})
end
end

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

api.nvim_create_autocmd(config.cfg.lsp.diagnostic_update_events, {
buffer = main_nr,
group = group,
callback = function(_)
M.sync_raft(main_nr)
end,
})
end
end

Expand Down
12 changes: 5 additions & 7 deletions lua/otter/keeper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ M.modify_position = function(obj, main_nr, invert, exclude_end, known_offset)
end
end

--- Syncronize the raft of otters attached to a buffer
--- Synchronize the raft of otters attached to a buffer
---@param main_nr integer bufnr of the parent buffer
---@param lang string|nil only sync one otter buffer matching a language
M.sync_raft = function(main_nr, lang)
Expand All @@ -314,16 +314,14 @@ M.sync_raft = function(main_nr, lang)
local changetick = api.nvim_buf_get_changedtick(main_nr)
if M._otters_attached[main_nr].last_changetick == changetick then
all_code_chunks = M._otters_attached[main_nr].code_chunks
return
else
all_code_chunks = M.extract_code_chunks(main_nr)
end

M._otters_attached[main_nr].last_changetick = changetick
M._otters_attached[main_nr].code_chunks = all_code_chunks

if next(all_code_chunks) == nil then
return {}
end
local langs
if lang == nil then
langs = M._otters_attached[main_nr].languages
Expand All @@ -350,10 +348,10 @@ M.sync_raft = function(main_nr, lang)
end
end

-- clear buffer
-- replace language lines
api.nvim_buf_set_lines(otter_nr, 0, -1, false, ls)
else -- no code chunks so we wipe the otter buffer
api.nvim_buf_set_lines(otter_nr, 0, -1, false, {})
-- add language lines
api.nvim_buf_set_lines(otter_nr, 0, nmax, false, ls)
end
end
end
Expand Down

0 comments on commit 8bdc078

Please sign in to comment.