Skip to content

Commit 623591e

Browse files
chore(refactor): Use early returns
1 parent d3ea757 commit 623591e

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

ftplugin/org.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ config:setup_foldlevel()
1414
if config.org_startup_indented then
1515
vim.b.org_indent_mode = true
1616
end
17-
require('orgmode.org.indent').setup()
17+
require('orgmode.org.indent').setup_virtual_indent()
1818

1919
vim.b.org_bufnr = vim.api.nvim_get_current_buf()
2020
vim.bo.modeline = false

lua/orgmode/org/indent.lua

+7-5
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,22 @@ local function foldtext()
313313
return line .. config.org_ellipsis
314314
end
315315

316-
local function setup()
316+
local function setup_virtual_indent()
317317
if not utils.has_version_10() then
318318
return
319319
end
320320

321+
local virtualIndent = VirtualIndent:new()
322+
321323
if config.org_startup_indented then
322-
VirtualIndent:new():attach()
323-
else
324-
VirtualIndent:new():start_watch_org_indent()
324+
return virtualIndent:attach()
325325
end
326+
327+
return virtualIndent:start_watch_org_indent()
326328
end
327329

328330
return {
329-
setup = setup,
331+
setup_virtual_indent = setup_virtual_indent,
330332
foldexpr = foldexpr,
331333
indentexpr = indentexpr,
332334
foldtext = foldtext,

lua/orgmode/ui/virtual_indent.lua

+21-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ local dict_watcher = require('orgmode.utils.dict_watcher')
99
local VirtualIndent = {
1010
_ns_id = vim.api.nvim_create_namespace('orgmode.ui.indent'),
1111
_bufnrs = {},
12-
_watcher_running = false,
1312
}
1413

1514
--- Creates a new instance of VirtualIndent for a given buffer or returns the existing instance if
@@ -31,6 +30,7 @@ function VirtualIndent:new(bufnr)
3130

3231
new._bufnr = bufnr
3332
new._attached = false
33+
new._watcher_running = false
3434
return new
3535
end
3636

@@ -100,31 +100,31 @@ end
100100

101101
--- Make all VirtualIndent instances react to changes in `org_indent_mode`
102102
function VirtualIndent:start_watch_org_indent()
103-
if not self._watcher_running then
104-
self._watcher_running = true
105-
dict_watcher.watch_buffer_variable('org_indent_mode', function(indent_mode, _, buf_vars)
106-
local vindent = VirtualIndent._bufnrs[buf_vars.org_bufnr]
107-
local indent_mode_enabled = indent_mode.new or false
108-
---@diagnostic disable-next-line: invisible
109-
if indent_mode_enabled and not vindent._attached then
110-
vindent:attach()
111-
---@diagnostic disable-next-line: invisible
112-
elseif not indent_mode_enabled and vindent._attached then
113-
vindent:detach()
114-
end
115-
end)
103+
if self._watcher_running then
104+
return
116105
end
106+
dict_watcher.watch_buffer_variable('org_indent_mode', function(indent_mode, _, buf_vars)
107+
local vindent = VirtualIndent._bufnrs[buf_vars.org_bufnr]
108+
local indent_mode_enabled = indent_mode.new or false
109+
if indent_mode_enabled then
110+
return vindent:attach()
111+
end
112+
return vindent:detach()
113+
end)
114+
self._watcher_running = true
117115
end
118116

119117
--- Stops VirtualIndent instances from reacting to changes in `vim.b.org_indent_mode`
120118
function VirtualIndent:stop_watch_org_indent()
121-
self._watcher_running = false
122119
dict_watcher.unwatch_buffer_variable('org_indent_mode')
120+
self._watcher_running = false
123121
end
124122

125123
--- Enables virtual indentation in registered buffer
126124
function VirtualIndent:attach()
127-
self._attached = true
125+
if self._attached then
126+
return
127+
end
128128
self:set_indent(0, vim.api.nvim_buf_line_count(self._bufnr) - 1, true)
129129
self:start_watch_org_indent()
130130

@@ -139,12 +139,15 @@ function VirtualIndent:attach()
139139
end)
140140
end,
141141
})
142+
self._attached = true
142143
end
143144

144145
function VirtualIndent:detach()
145-
self._attached = false
146-
vim.api.nvim_buf_set_var(self._bufnr, 'org_indent_mode', false)
146+
if not self._attached then
147+
return
148+
end
147149
self:_delete_old_extmarks(0, vim.api.nvim_buf_line_count(self._bufnr) - 1)
150+
self._attached = false
148151
end
149152

150153
return VirtualIndent

0 commit comments

Comments
 (0)