@@ -5,6 +5,7 @@ local tree_utils = require('orgmode.utils.treesitter')
5
5
--- @field private _attached boolean Whether or not VirtualIndent is attached for its buffer
6
6
--- @field private _bufnrs {integer : boolean } Buffers with VirtualIndent attached
7
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`
8
9
local VirtualIndent = {
9
10
_ns_id = vim .api .nvim_create_namespace (' orgmode.ui.indent' ),
10
11
_bufnrs = {},
@@ -29,6 +30,7 @@ function VirtualIndent:new(bufnr)
29
30
new ._bufnr = bufnr
30
31
new ._attached = false
31
32
VirtualIndent ._bufnrs [new ._bufnr ] = new
33
+ new ._watcher_running = false
32
34
new ._timer = vim .uv .new_timer ()
33
35
return new
34
36
end
@@ -92,27 +94,31 @@ function VirtualIndent:set_indent(start_line, end_line, ignore_ts)
92
94
end
93
95
end
94
96
95
- --- Begins a timer to check `vim.b.org_indent_mode` and correctly attach or detatch VirtualIndent as
96
- --- necessary
97
+ --- Begins a timer to check `vim.b.org_indent_mode` if `vim.b.org_indent_mode` is not already being
98
+ --- monitored
97
99
function VirtualIndent :start_watch_org_indent ()
98
- self ._timer :start (
99
- 50 ,
100
- 50 ,
101
- vim .schedule_wrap (function ()
102
- local success , indent_mode_enabled = pcall (vim .api .nvim_buf_get_var , self ._bufnr , ' org_indent_mode' )
103
- if success and indent_mode_enabled then
104
- if not self ._attached then
105
- self :attach ()
100
+ if not self ._watcher_running then
101
+ 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 ()
106
113
end
107
- elseif self ._attached then
108
- self :detach ()
109
- end
110
- end )
111
- )
114
+ end )
115
+ )
116
+ end
112
117
end
113
118
114
119
--- Stops the current VirtualIndent instance from reacting to changes in `vim.b.org_indent_mode`
115
120
function VirtualIndent :stop_watch_org_indent ()
121
+ self ._watcher_running = false
116
122
self ._timer :stop ()
117
123
end
118
124
0 commit comments