···3030 },
3131 -- disable any mapping by setting it to `false`
3232 mappings = {
3333- -- close the annotation window
3333+ -- close the annotate window
3434 close = "q",
3535 },
3636}
+50-17
lua/jjannotate.lua
···1616 },
1717 -- disable any mapping by setting it to `false`
1818 mappings = {
1919- -- close the annotation window
1919+ -- close the annotate window
2020 close = "q",
2121 },
2222}
···4444---@field annotate jjannotate.AnnotateResult
4545---@field opts jjannotate.Opts
46464747-local curr ---@type jjannotate.State | nil
4747+---@type table<string, jjannotate.State?>
4848+local states = {}
48494950---@param path string
5051---@return { win: integer, buf: integer} | nil
···218219 end
219220 end
220221221221- -- TODO: support multiple annotate windows
222222- curr = nil
222222+ states[state.path] = nil
223223 end,
224224 })
225225end
···240240 opts = opts,
241241 }
242242243243+ vim.b[state.buf].jjannotate_path = path
243244 vim.bo[state.buf].bufhidden = "wipe"
244245 vim.bo[state.buf].buftype = "nofile"
245246 vim.bo[state.buf].swapfile = false
···285286 return state
286287end
287288289289+---@param state jjannotate.State?
290290+---@return boolean whether the window was closed
291291+local function close(state)
292292+ if state and vim.api.nvim_win_is_valid(state.win) then
293293+ vim.api.nvim_win_close(state.win, true)
294294+ return true
295295+ end
296296+ return false
297297+end
298298+288299local M = {}
289300290301---@param path? string
291302function M.open(path)
292292- -- TODO: support multiple annotate windows
293293- if curr then
294294- update(curr, function() vim.api.nvim_set_current_win(curr.win) end)
303303+ path = path or vim.fn.expand("%")
304304+305305+ local state = states[path]
306306+ if state then
307307+ update(state, function() vim.api.nvim_set_current_win(state.win) end)
295308 return
296309 end
297297-298298- path = path or vim.fn.expand("%")
299310300311 jj.annotate(
301312 path,
302313 vim.schedule_wrap(function(annotate)
303314 local opts = vim.tbl_deep_extend("force", defaults, vim.g.jjannotate_opts or {})
304304- curr = open(path, annotate, opts)
315315+ states[path] = open(path, annotate, opts)
305316 end)
306317 )
307318end
308319320320+--- Checks if any annotate window is open. If `path` is provided, checks if
321321+--- that path's annotate window is open.
322322+---@param path? string
309323---@return boolean
310310-function M.is_open() return curr and vim.api.nvim_win_is_valid(curr.win) or false end
324324+function M.is_open(path)
325325+ path = path or vim.b.jjannotate_path
326326+ if path then
327327+ local state = states[path]
328328+ return state and vim.api.nvim_win_is_valid(state.win) or false
329329+ end
311330312312----@return boolean whether the window closed
313313-function M.close()
314314- if curr and M.is_open() then
315315- vim.api.nvim_win_close(curr.win, true)
316316- return true
331331+ for _, state in pairs(states) do
332332+ if vim.api.nvim_win_is_valid(state.win) then return true end
317333 end
334334+318335 return false
319336end
320337338338+--- Closes all annotate windows. If `path` is provided, closes the annotate
339339+--- window for that path.
340340+---@param path? string
341341+---@return boolean whether any window(s) closed
342342+function M.close(path)
343343+ path = path or vim.b.jjannotate_path
344344+ if path then return close(states[path]) end
345345+346346+ local closed = 0
347347+ for _, p in ipairs(vim.tbl_keys(states)) do
348348+ if close(states[p]) then closed = closed + 1 end
349349+ end
350350+ return closed > 0
351351+end
352352+321353---@param path? string
322354function M.toggle(path)
323323- if not M.close() then M.open(path) end
355355+ path = path or vim.b.jjannotate_path or vim.fn.expand("%")
356356+ if not M.close(path) then M.open(path) end
324357end
325358326359return M