feat: Use augroup
I had no idea augroup does all I tried to do with `self.autocmds`. Oh well. Also added sidebar.id, which is one step closer to #25
This commit is contained in:
@@ -131,7 +131,7 @@ function M.open_outline(opts)
|
|||||||
local sidebar = M.sidebars[tab]
|
local sidebar = M.sidebars[tab]
|
||||||
|
|
||||||
if not sidebar then
|
if not sidebar then
|
||||||
sidebar = Sidebar:new()
|
sidebar = Sidebar:new(tab)
|
||||||
M.sidebars[tab] = sidebar
|
M.sidebars[tab] = sidebar
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -17,36 +17,35 @@ local Sidebar = {}
|
|||||||
---@field buf integer
|
---@field buf integer
|
||||||
|
|
||||||
---@class outline.Sidebar
|
---@class outline.Sidebar
|
||||||
|
---@field id integer
|
||||||
---@field view outline.View
|
---@field view outline.View
|
||||||
---@field items outline.Symbol[]
|
---@field items outline.Symbol[]
|
||||||
---@field flats outline.FlatSymbol[]
|
---@field flats outline.FlatSymbol[]
|
||||||
---@field hovered outline.FlatSymbol[]
|
---@field hovered outline.FlatSymbol[]
|
||||||
---@field original_cursor string
|
---@field original_cursor string
|
||||||
---@field code outline.SidebarCodeState
|
---@field code outline.SidebarCodeState
|
||||||
---@field autocmds { [integer]: integer } winnr to autocmd id
|
---@field augroup integer
|
||||||
---@field provider outline.Provider?
|
---@field provider outline.Provider?
|
||||||
---@field preview outline.Preview
|
---@field preview outline.Preview
|
||||||
|
|
||||||
function Sidebar:new()
|
function Sidebar:new(id)
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
|
id = id,
|
||||||
view = View:new(),
|
view = View:new(),
|
||||||
preview = Preview:new(),
|
preview = Preview:new(),
|
||||||
code = { buf = 0, win = 0 },
|
code = { buf = 0, win = 0 },
|
||||||
items = {},
|
items = {},
|
||||||
flats = {},
|
flats = {},
|
||||||
hovered = {},
|
hovered = {},
|
||||||
autocmds = {},
|
|
||||||
original_cursor = vim.o.guicursor,
|
original_cursor = vim.o.guicursor,
|
||||||
}, { __index = Sidebar })
|
}, { __index = Sidebar })
|
||||||
end
|
end
|
||||||
|
|
||||||
function Sidebar:delete_autocmds()
|
function Sidebar:delete_autocmds()
|
||||||
for codewin, au in pairs(self.autocmds) do
|
if self.augroup then
|
||||||
if vim.api.nvim_win_is_valid(codewin) then
|
vim.api.nvim_del_augroup_by_id(self.augroup)
|
||||||
vim.api.nvim_del_autocmd(au)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
self.autocmds = {}
|
self.augroup = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function Sidebar:reset_state()
|
function Sidebar:reset_state()
|
||||||
@@ -214,13 +213,10 @@ function Sidebar:setup_attached_buffer_autocmd()
|
|||||||
local events = cfg.o.outline_items.auto_update_events
|
local events = cfg.o.outline_items.auto_update_events
|
||||||
|
|
||||||
if cfg.o.outline_items.highlight_hovered_item or cfg.o.symbol_folding.auto_unfold_hover then
|
if cfg.o.outline_items.highlight_hovered_item or cfg.o.symbol_folding.auto_unfold_hover then
|
||||||
if self.autocmds[code_win] then
|
|
||||||
vim.api.nvim_del_autocmd(self.autocmds[code_win])
|
|
||||||
self.autocmds[code_win] = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
if utils.str_or_nonempty_table(events.follow) then
|
if utils.str_or_nonempty_table(events.follow) then
|
||||||
self.autocmds[code_win] = vim.api.nvim_create_autocmd(events.follow, {
|
self.augroup = vim.api.nvim_create_augroup("outline_"..self.id, { clear = true })
|
||||||
|
vim.api.nvim_create_autocmd(events.follow, {
|
||||||
|
group = self.augroup,
|
||||||
buffer = code_buf,
|
buffer = code_buf,
|
||||||
callback = function()
|
callback = function()
|
||||||
self:_highlight_current_item(code_win, cfg.o.outline_items.auto_set_cursor)
|
self:_highlight_current_item(code_win, cfg.o.outline_items.auto_set_cursor)
|
||||||
@@ -297,13 +293,6 @@ function Sidebar:refresh_handler(response)
|
|||||||
local curbuf = vim.api.nvim_get_current_buf()
|
local curbuf = vim.api.nvim_get_current_buf()
|
||||||
local newbuf = curbuf ~= self.code.buf
|
local newbuf = curbuf ~= self.code.buf
|
||||||
|
|
||||||
if self.code.win ~= curwin then
|
|
||||||
if self.autocmds[self.code.win] then
|
|
||||||
vim.api.nvim_del_autocmd(self.autocmds[self.code.win])
|
|
||||||
self.autocmds[self.code.win] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self.code.win = curwin
|
self.code.win = curwin
|
||||||
self.code.buf = curbuf
|
self.code.buf = curbuf
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user