1
1
local tree_utils = require (' orgmode.utils.treesitter' )
2
+ local dict_watcher = require (' orgmode.utils.dict_watcher' )
2
3
--- @class OrgVirtualIndent
3
4
--- @field private _ns_id number extmarks namespace id
4
5
--- @field private _bufnr integer Buffer VirtualIndent is attached to
5
6
--- @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`
9
9
local VirtualIndent = {
10
10
_ns_id = vim .api .nvim_create_namespace (' orgmode.ui.indent' ),
11
11
_bufnrs = {},
12
+ _watcher_running = false ,
12
13
}
13
14
14
15
--- Creates a new instance of VirtualIndent for a given buffer or returns the existing instance if
@@ -24,14 +25,12 @@ function VirtualIndent:new(bufnr)
24
25
end
25
26
26
27
local new = {}
28
+ VirtualIndent ._bufnrs [bufnr ] = new
27
29
setmetatable (new , self )
28
30
self .__index = self
29
31
30
32
new ._bufnr = bufnr
31
33
new ._attached = false
32
- VirtualIndent ._bufnrs [new ._bufnr ] = new
33
- new ._watcher_running = false
34
- new ._timer = vim .uv .new_timer ()
35
34
return new
36
35
end
37
36
@@ -94,32 +93,28 @@ function VirtualIndent:set_indent(start_line, end_line, ignore_ts)
94
93
end
95
94
end
96
95
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`
99
97
function VirtualIndent :start_watch_org_indent ()
100
98
if not self ._watcher_running then
101
99
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 )
116
111
end
117
112
end
118
113
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`
120
115
function VirtualIndent :stop_watch_org_indent ()
121
116
self ._watcher_running = false
122
- self . _timer : stop ( )
117
+ dict_watcher . unwatch_buffer_variable ( ' org_indent_mode ' )
123
118
end
124
119
125
120
--- Enables virtual indentation in registered buffer
0 commit comments