diff --git a/README.md b/README.md index afb818e..d222fc1 100644 --- a/README.md +++ b/README.md @@ -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. @@ -114,13 +118,13 @@ the embedded code. Use it as follows: ```lua local cmp = require'cmp' -cmp.setup( +cmp.setup({ -- sources = { { name = "otter" }, -- - } -} + }, +}) ``` diff --git a/lua/otter/config.lua b/lua/otter/config.lua index df77eee..2d3281d 100644 --- a/lua/otter/config.lua +++ b/lua/otter/config.lua @@ -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. diff --git a/lua/otter/init.lua b/lua/otter/init.lua index d6fa5ac..31d6b96 100644 --- a/lua/otter/init.lua +++ b/lua/otter/init.lua @@ -169,11 +169,10 @@ 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) @@ -181,16 +180,23 @@ M.activate = function(languages, completion, diagnostics, tsquery) 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 diff --git a/lua/otter/keeper.lua b/lua/otter/keeper.lua index 8aff907..d2ea39c 100644 --- a/lua/otter/keeper.lua +++ b/lua/otter/keeper.lua @@ -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) @@ -314,6 +314,7 @@ 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 @@ -322,7 +323,7 @@ M.sync_raft = function(main_nr, lang) M._otters_attached[main_nr].code_chunks = all_code_chunks if next(all_code_chunks) == nil then - return {} + return end local langs if lang == nil then @@ -350,9 +351,7 @@ M.sync_raft = function(main_nr, lang) end end - -- clear buffer - api.nvim_buf_set_lines(otter_nr, 0, -1, false, {}) - -- add language lines + -- replace language lines api.nvim_buf_set_lines(otter_nr, 0, nmax, false, ls) end end