Skip to content

Commit 536b136

Browse files
authored
feat: Add Experimental Option to Enable Window-Local Extmarks (#163)
* feat(diagnostic): Only show diagnostics on current window Closes #127 * chore(extmarks): Move module-level function to proper location This also adds a luadoc, like the other functions have * fix(extmarks): Don't link window diagnostics on startup This bug occured if the user initially has 2 windows with the same buffer in them when the plugin starts up, resulting in "mirroring" the diagnostics * feat(config): Add flag to for enabling window-local extmarks * docs(readme): Document how to use `use_window_local_extmarks`
1 parent 9b55d36 commit 536b136

5 files changed

Lines changed: 39 additions & 3 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ require("tiny-inline-diagnostic").setup({
252252

253253
-- Automatically disable diagnostics when opening diagnostic float windows
254254
override_open_float = false,
255+
256+
-- Experimental options, subject to misbehave in future NeoVim releases
257+
experimental = {
258+
-- Make diagnostics not mirror across windows containing the same buffer
259+
-- See: https://github.com/rachartier/tiny-inline-diagnostic.nvim/issues/127
260+
use_window_local_extmarks = false,
261+
},
255262
},
256263
})
257264
```

lua/tiny-inline-diagnostic/autocmds.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ end
9191
---@param throttled_apply function
9292
---@param direct_apply function
9393
---@param on_diagnostic_change function
94+
---@param on_window_change function
9495
function M.setup_buffer_autocmds(
9596
autocmd_ns,
9697
opts,
9798
bufnr,
9899
throttled_apply,
99100
direct_apply,
100-
on_diagnostic_change
101+
on_diagnostic_change,
102+
on_window_change
101103
)
102104
if not vim.api.nvim_buf_is_valid(bufnr) or attached_buffers[bufnr] then
103105
return
@@ -156,6 +158,14 @@ function M.setup_buffer_autocmds(
156158
end,
157159
desc = "Update diagnostics on window resize",
158160
})
161+
162+
if opts.options.experimental.use_window_local_extmarks then
163+
vim.api.nvim_create_autocmd("WinEnter", {
164+
group = autocmd_ns,
165+
callback = on_window_change,
166+
desc = "Sync namespace window on window change",
167+
})
168+
end
159169
end
160170

161171
---@return number

lua/tiny-inline-diagnostic/diagnostic.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local M = {}
22

33
local autocmds = require("tiny-inline-diagnostic.autocmds")
44
local cache = require("tiny-inline-diagnostic.cache")
5+
local extmarks = require("tiny-inline-diagnostic.extmarks")
56
local filter = require("tiny-inline-diagnostic.filter")
67
local handlers = require("tiny-inline-diagnostic.handlers")
78
local renderer = require("tiny-inline-diagnostic.renderer")
@@ -50,14 +51,16 @@ function M.set_diagnostic_autocmds(opts)
5051

5152
local on_diagnostic_change = handlers.build_diagnostic_change_handler(cache, opts)
5253
local on_mode_change = handlers.build_mode_change_handler(state, renderer, opts)
54+
local on_window_change = extmarks.update_namespace_window
5355

5456
autocmds.setup_buffer_autocmds(
5557
autocmd_ns,
5658
opts,
5759
event.buf,
5860
throttler.fn,
5961
direct_renderer,
60-
on_diagnostic_change
62+
on_diagnostic_change,
63+
on_window_change
6164
)
6265
autocmds.setup_cursor_autocmds(autocmd_ns, opts, event.buf, throttler.fn, direct_renderer)
6366
autocmds.setup_mode_change_autocmds(autocmd_ns, event.buf, on_mode_change)
@@ -95,7 +98,6 @@ end
9598

9699
function M.disable()
97100
state.user_disable()
98-
local extmarks = require("tiny-inline-diagnostic.extmarks")
99101

100102
vim.schedule(function()
101103
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do

lua/tiny-inline-diagnostic/extmarks.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ function M.count_inlay_hints_characters(buf, linenr)
8080
return count
8181
end
8282

83+
---Update the current namespace used for extmarks to only include the active window.
84+
---Note: this uses a currently experimental API call, and may break in future NeoVim releases.
85+
function M.update_namespace_window()
86+
local current_window = vim.api.nvim_get_current_win()
87+
vim.api.nvim__ns_set(DIAGNOSTIC_NAMESPACE, { wins = { current_window } })
88+
end
89+
8390
-- Public API
8491

8592
---Clear extmarks from buffer

lua/tiny-inline-diagnostic/init.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
local M = {}
88

99
local diag = require("tiny-inline-diagnostic.diagnostic")
10+
local extmarks = require("tiny-inline-diagnostic.extmarks")
1011
local hi = require("tiny-inline-diagnostic.highlights")
1112
local presets = require("tiny-inline-diagnostic.presets")
1213

@@ -71,6 +72,9 @@ local default_config = {
7172
},
7273
override_open_float = false,
7374
overwrite_events = nil,
75+
experimental = {
76+
use_window_local_extmarks = false,
77+
},
7478
},
7579
disabled_ft = {},
7680
}
@@ -135,6 +139,12 @@ function M.setup(opts)
135139

136140
setup_colorscheme_handler(config)
137141
diag.set_diagnostic_autocmds(config)
142+
143+
-- Ensure the diagnostics are only linked to the current window on startup
144+
-- This gets around a bug where having 2 windows with the same buffer on startup causes mirroring
145+
if config.options.experimental.use_window_local_extmarks then
146+
extmarks.update_namespace_window()
147+
end
138148
end
139149

140150
--- Change the blend and highlight settings dynamically.

0 commit comments

Comments
 (0)