diff --git a/README.md b/README.md index 8449145..db2ce26 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ vim.g.jjannotate_opts = { }, -- disable any mapping by setting it to `false` mappings = { - -- close the annotation window + -- close the annotate window close = "q", }, } diff --git a/lua/jjannotate.lua b/lua/jjannotate.lua index b5f322d..996697c 100644 --- a/lua/jjannotate.lua +++ b/lua/jjannotate.lua @@ -16,7 +16,7 @@ local defaults = { }, -- disable any mapping by setting it to `false` mappings = { - -- close the annotation window + -- close the annotate window close = "q", }, } @@ -44,7 +44,8 @@ local hl_cache = {} ---@type table ---@field annotate jjannotate.AnnotateResult ---@field opts jjannotate.Opts -local curr ---@type jjannotate.State | nil +---@type table +local states = {} ---@param path string ---@return { win: integer, buf: integer} | nil @@ -218,8 +219,7 @@ local function setup_autocmds(state) end end - -- TODO: support multiple annotate windows - curr = nil + states[state.path] = nil end, }) end @@ -240,6 +240,7 @@ local function open(path, annotate, opts) opts = opts, } + vim.b[state.buf].jjannotate_path = path vim.bo[state.buf].bufhidden = "wipe" vim.bo[state.buf].buftype = "nofile" vim.bo[state.buf].swapfile = false @@ -285,42 +286,74 @@ local function open(path, annotate, opts) return state end +---@param state jjannotate.State? +---@return boolean whether the window was closed +local function close(state) + if state and vim.api.nvim_win_is_valid(state.win) then + vim.api.nvim_win_close(state.win, true) + return true + end + return false +end + local M = {} ---@param path? string function M.open(path) - -- TODO: support multiple annotate windows - if curr then - update(curr, function() vim.api.nvim_set_current_win(curr.win) end) + path = path or vim.fn.expand("%") + + local state = states[path] + if state then + update(state, function() vim.api.nvim_set_current_win(state.win) end) return end - path = path or vim.fn.expand("%") - jj.annotate( path, vim.schedule_wrap(function(annotate) local opts = vim.tbl_deep_extend("force", defaults, vim.g.jjannotate_opts or {}) - curr = open(path, annotate, opts) + states[path] = open(path, annotate, opts) end) ) end +--- Checks if any annotate window is open. If `path` is provided, checks if +--- that path's annotate window is open. +---@param path? string ---@return boolean -function M.is_open() return curr and vim.api.nvim_win_is_valid(curr.win) or false end +function M.is_open(path) + path = path or vim.b.jjannotate_path + if path then + local state = states[path] + return state and vim.api.nvim_win_is_valid(state.win) or false + end ----@return boolean whether the window closed -function M.close() - if curr and M.is_open() then - vim.api.nvim_win_close(curr.win, true) - return true + for _, state in pairs(states) do + if vim.api.nvim_win_is_valid(state.win) then return true end end + return false end +--- Closes all annotate windows. If `path` is provided, closes the annotate +--- window for that path. +---@param path? string +---@return boolean whether any window(s) closed +function M.close(path) + path = path or vim.b.jjannotate_path + if path then return close(states[path]) end + + local closed = 0 + for _, p in ipairs(vim.tbl_keys(states)) do + if close(states[p]) then closed = closed + 1 end + end + return closed > 0 +end + ---@param path? string function M.toggle(path) - if not M.close() then M.open(path) end + path = path or vim.b.jjannotate_path or vim.fn.expand("%") + if not M.close(path) then M.open(path) end end return M