Skip to content

Commit 1d256cf

Browse files
committed
refactor: use dict_watcher to monitor vim.b.org_indent_mode
1 parent 9e26d13 commit 1d256cf

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

ftplugin/org.lua

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ if config.org_startup_indented then
1515
end
1616
require('orgmode.org.indent').setup()
1717

18+
vim.b.org_bufnr = vim.api.nvim_get_current_buf()
1819
vim.bo.modeline = false
1920
vim.opt_local.fillchars:append('fold: ')
2021
vim.opt_local.foldmethod = 'expr'

lua/orgmode/ui/virtual_indent.lua

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
local tree_utils = require('orgmode.utils.treesitter')
2+
local dict_watcher = require('orgmode.utils.dict_watcher')
23
---@class OrgVirtualIndent
34
---@field private _ns_id number extmarks namespace id
45
---@field private _bufnr integer Buffer VirtualIndent is attached to
56
---@field private _attached boolean Whether or not VirtualIndent is attached for its buffer
6-
---@field private _bufnrs {integer: boolean} Buffers with VirtualIndent attached
7-
---@field private _timer uv_timer_t Timer used for tracking `org_indent_mode`
8-
---@field private _watcher_running boolean Whether or not VirtualIndent is reacting to `vim.borg_indent_mode`
7+
---@field private _bufnrs table<integer, OrgVirtualIndent> Buffers with VirtualIndent attached
8+
---@field private _watcher_running boolean Whether or not VirtualIndent is reacting to `vim.b.org_indent_mode`
99
local VirtualIndent = {
1010
_ns_id = vim.api.nvim_create_namespace('orgmode.ui.indent'),
1111
_bufnrs = {},
12+
_watcher_running = false,
1213
}
1314

1415
--- Creates a new instance of VirtualIndent for a given buffer or returns the existing instance if
@@ -24,14 +25,12 @@ function VirtualIndent:new(bufnr)
2425
end
2526

2627
local new = {}
28+
VirtualIndent._bufnrs[bufnr] = new
2729
setmetatable(new, self)
2830
self.__index = self
2931

3032
new._bufnr = bufnr
3133
new._attached = false
32-
VirtualIndent._bufnrs[new._bufnr] = new
33-
new._watcher_running = false
34-
new._timer = vim.uv.new_timer()
3534
return new
3635
end
3736

@@ -94,32 +93,28 @@ function VirtualIndent:set_indent(start_line, end_line, ignore_ts)
9493
end
9594
end
9695

97-
--- Begins a timer to check `vim.b.org_indent_mode` if `vim.b.org_indent_mode` is not already being
98-
--- monitored
96+
--- Make all VirtualIndent instances react to changes in `org_indent_mode`
9997
function VirtualIndent:start_watch_org_indent()
10098
if not self._watcher_running then
10199
self._watcher_running = true
102-
self._timer:start(
103-
50,
104-
50,
105-
vim.schedule_wrap(function()
106-
local success, indent_mode_enabled = pcall(vim.api.nvim_buf_get_var, self._bufnr, 'org_indent_mode')
107-
if success and indent_mode_enabled then
108-
if not self._attached then
109-
self:attach()
110-
end
111-
elseif self._attached then
112-
self:detach()
113-
end
114-
end)
115-
)
100+
dict_watcher.watch_buffer_variable('org_indent_mode', function(indent_mode, _, buf_vars)
101+
local vindent = VirtualIndent._bufnrs[buf_vars.org_bufnr]
102+
local indent_mode_enabled = indent_mode.new or false
103+
---@diagnostic disable-next-line: invisible
104+
if indent_mode_enabled and not vindent._attached then
105+
vindent:attach()
106+
---@diagnostic disable-next-line: invisible
107+
elseif not indent_mode_enabled and vindent._attached then
108+
vindent:detach()
109+
end
110+
end)
116111
end
117112
end
118113

119-
--- Stops the current VirtualIndent instance from reacting to changes in `vim.b.org_indent_mode`
114+
--- Stops VirtualIndent instances from reacting to changes in `vim.b.org_indent_mode`
120115
function VirtualIndent:stop_watch_org_indent()
121116
self._watcher_running = false
122-
self._timer:stop()
117+
dict_watcher.unwatch_buffer_variable('org_indent_mode')
123118
end
124119

125120
--- Enables virtual indentation in registered buffer

0 commit comments

Comments
 (0)