Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
fix(autocmds): completely clear highlights and autocmds on disable
Browse files Browse the repository at this point in the history
This involves handling all the "rendering" logic inside `autocmds`
module and only exposing two functions (`start_rendering` and
`stop_rendering`) for the main module.

Fix #5
  • Loading branch information
utilyre committed Mar 22, 2023
1 parent 3285cf0 commit 44303dd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
21 changes: 3 additions & 18 deletions lua/sentiment.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local manager = require("sentiment.config.manager")
local autocmds = require("sentiment.autocmds")
local ui = require("sentiment.ui")

local M = {}

Expand All @@ -24,28 +23,14 @@ function M.setup(cfg)
vim.g.loaded_matchparen = 1
manager.apply(cfg or {})

autocmds.renderer:create()
autocmds.start_rendering()
create_user_commands()
end

---Disable the plugin.
function M.disable()
if not autocmds.renderer:exists() then return end

autocmds.renderer:remove()
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
ui.clear(buf)
end
end
function M.disable() autocmds.stop_rendering() end

---Re-enable the plugin.
function M.enable()
if autocmds.renderer:exists() then return end

for _, win in ipairs(vim.api.nvim_list_wins()) do
ui.render(win)
end
autocmds.renderer:create()
end
function M.enable() autocmds.start_rendering() end

return M
32 changes: 29 additions & 3 deletions lua/sentiment/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ local ui = require("sentiment.ui")

local M = {}

---`Pair` renderer.
M.renderer = Autocmd.new({
local renderer_timer = nil

local renderer = Autocmd.new({
name = "renderer",
desc = "Render pair",
events = {
Expand All @@ -14,7 +15,32 @@ M.renderer = Autocmd.new({
"CursorMoved",
"CursorMovedI",
},
callback = function() ui.render() end,
callback = function() renderer_timer = ui.render() end,
})

---Start rendering pairs.
function M.start_rendering()
if renderer:exists() then return end

for _, win in ipairs(vim.api.nvim_list_wins()) do
renderer_timer = ui.render(win)
end

renderer:create()
end

---Stop rendering pairs.
function M.stop_rendering()
if not renderer:exists() then return end

renderer:remove()
if renderer_timer ~= nil and renderer_timer:is_active() then
renderer_timer:close()
end

for _, buf in ipairs(vim.api.nvim_list_bufs()) do
ui.clear(buf)
end
end

return M

0 comments on commit 44303dd

Please sign in to comment.