Skip to content

Commit

Permalink
feat: Use augroup
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hedyhli committed Nov 28, 2023
1 parent 8df3742 commit 3ebf0ff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lua/outline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function M.open_outline(opts)
local sidebar = M.sidebars[tab]

if not sidebar then
sidebar = Sidebar:new()
sidebar = Sidebar:new(tab)
M.sidebars[tab] = sidebar
end

Expand Down
31 changes: 10 additions & 21 deletions lua/outline/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,35 @@ local Sidebar = {}
---@field buf integer

---@class outline.Sidebar
---@field id integer
---@field view outline.View
---@field items outline.Symbol[]
---@field flats outline.FlatSymbol[]
---@field hovered outline.FlatSymbol[]
---@field original_cursor string
---@field code outline.SidebarCodeState
---@field autocmds { [integer]: integer } winnr to autocmd id
---@field augroup integer
---@field provider outline.Provider?
---@field preview outline.Preview

function Sidebar:new()
function Sidebar:new(id)
return setmetatable({
id = id,
view = View:new(),
preview = Preview:new(),
code = { buf = 0, win = 0 },
items = {},
flats = {},
hovered = {},
autocmds = {},
original_cursor = vim.o.guicursor,
}, { __index = Sidebar })
end

function Sidebar:delete_autocmds()
for codewin, au in pairs(self.autocmds) do
if vim.api.nvim_win_is_valid(codewin) then
vim.api.nvim_del_autocmd(au)
end
if self.augroup then
vim.api.nvim_del_augroup_by_id(self.augroup)
end
self.autocmds = {}
self.augroup = nil
end

function Sidebar:reset_state()
Expand Down Expand Up @@ -214,13 +213,10 @@ function Sidebar:setup_attached_buffer_autocmd()
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 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
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,
callback = function()
self:_highlight_current_item(code_win, cfg.o.outline_items.auto_set_cursor)
Expand Down Expand Up @@ -297,13 +293,6 @@ function Sidebar:refresh_handler(response)
local curbuf = vim.api.nvim_get_current_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.buf = curbuf

Expand Down

0 comments on commit 3ebf0ff

Please sign in to comment.