Skip to content

Commit ea58ae5

Browse files
refactor: Add "update" method to file instance
1 parent a84a748 commit ea58ae5

File tree

5 files changed

+52
-49
lines changed

5 files changed

+52
-49
lines changed

lua/orgmode/capture/init.lua

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,17 @@ function Capture:_refile_from_capture_buffer(opts)
180180

181181
lines = opts.template:apply_properties_to_lines(lines)
182182

183-
self.files
184-
:update_file(destination_file.filename, function(file)
185-
if not destination_headline and opts.template.regexp then
186-
local line = vim.fn.search(opts.template.regexp, 'ncw')
187-
if line > 0 then
188-
return vim.api.nvim_buf_set_lines(file:bufnr(), line, line, false, lines)
189-
end
183+
destination_file:update_sync(function(file)
184+
if not destination_headline and opts.template.regexp then
185+
local line = vim.fn.search(opts.template.regexp, 'ncw')
186+
if line > 0 then
187+
return vim.api.nvim_buf_set_lines(file:bufnr(), line, line, false, lines)
190188
end
189+
end
191190

192-
local range = self:_get_destination_range_without_empty_lines(Range.from_line(target_line))
193-
vim.api.nvim_buf_set_lines(file:bufnr(), range.start_line, range.end_line, false, lines)
194-
end)
195-
:wait()
191+
local range = self:_get_destination_range_without_empty_lines(Range.from_line(target_line))
192+
vim.api.nvim_buf_set_lines(file:bufnr(), range.start_line, range.end_line, false, lines)
193+
end)
196194

197195
if self.on_post_refile then
198196
self.on_post_refile(self, opts)
@@ -235,18 +233,16 @@ function Capture:_refile_from_org_file(opts)
235233
lines = self:_adapt_headline_level(source_headline, target_level, is_same_file)
236234
end
237235

238-
self.files
239-
:update_file(destination_file.filename, function()
240-
if is_same_file then
241-
local item_range = source_headline:get_range()
242-
return vim.cmd(string.format('silent! %d,%d move %s', item_range.start_line, item_range.end_line, target_line))
243-
end
236+
destination_file:update_sync(function(file)
237+
if is_same_file then
238+
local item_range = source_headline:get_range()
239+
return vim.cmd(string.format('silent! %d,%d move %s', item_range.start_line, item_range.end_line, target_line))
240+
end
244241

245-
local range = self:_get_destination_range_without_empty_lines(Range.from_line(target_line))
246-
target_line = range.start_line
247-
vim.api.nvim_buf_set_lines(0, range.start_line, range.end_line, false, lines)
248-
end)
249-
:wait()
242+
local range = self:_get_destination_range_without_empty_lines(Range.from_line(target_line))
243+
target_line = range.start_line
244+
vim.api.nvim_buf_set_lines(0, range.start_line, range.end_line, false, lines)
245+
end)
250246

251247
if not is_same_file and source_file.filename == utils.current_file_path() then
252248
local item_range = source_headline:get_range()
@@ -287,7 +283,7 @@ function Capture:refile_file_headline_to_archive(headline)
287283
})
288284

289285
destination_file = self.files:get(archive_location)
290-
self.files:update_file(destination_file.filename, function(archive_file)
286+
destination_file:update(function(archive_file)
291287
local archived_headline = archive_file:get_closest_headline({ target_line, 0 })
292288
archived_headline:set_property('ARCHIVE_TIME', Date.now():to_string())
293289
archived_headline:set_property('ARCHIVE_FILE', file.filename)

lua/orgmode/capture/template/datetree.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ function Datetree:create(template)
2020
local result = self:_get_datetree_destination(template)
2121

2222
if result.create then
23-
self.files
24-
:update_file(destination_file.filename, function(file)
25-
vim.api.nvim_buf_set_lines(file:bufnr(), result.target_line, result.target_line, false, result.content)
26-
end)
27-
:wait()
23+
destination_file:update_sync(function(file)
24+
vim.api.nvim_buf_set_lines(file:bufnr(), result.target_line, result.target_line, false, result.content)
25+
end)
2826
destination_file = destination_file:reload_sync()
2927
end
3028

lua/orgmode/clock/init.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ function Clock:org_clock_in()
3939
local promise = Promise.resolve()
4040

4141
if self.clocked_headline and self.clocked_headline:is_clocked_in() then
42-
local filename = self.clocked_headline.file.filename
43-
promise = self.files:update_file(filename, function()
44-
local clocked_item =
45-
self.files:get(filename):get_closest_headline({ self.clocked_headline:get_range().start_line, 0 })
42+
local file = self.clocked_headline.file
43+
promise = file:update(function()
44+
local clocked_item = file:reload_sync():get_closest_headline({ self.clocked_headline:get_range().start_line, 0 })
4645
clocked_item:clock_out()
4746
end)
4847
end

lua/orgmode/files/file.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,32 @@ function OrgFile:reload_sync(timeout)
103103
return self:reload():wait(timeout)
104104
end
105105

106+
---@param action fun(...:OrgFile):any
107+
function OrgFile:update(action)
108+
local is_same_file = self.filename == utils.current_file_path()
109+
if is_same_file then
110+
return Promise.resolve(action(self)):next(function(result)
111+
vim.cmd(':silent! w')
112+
return result
113+
end)
114+
end
115+
116+
local edit_file = utils.edit_file(self.filename)
117+
edit_file.open()
118+
119+
return Promise.resolve(action(self)):next(function(result)
120+
edit_file.close()
121+
return self:reload():next(function()
122+
return result
123+
end)
124+
end)
125+
end
126+
127+
---@param action fun(...:OrgFile):any
128+
function OrgFile:update_sync(action, timeout)
129+
return self:update(action):wait(timeout)
130+
end
131+
106132
---Check if file has been modified via 2 methods:
107133
---1. If file is loaded in a buffer, check the changedtick
108134
---2. If file is not loaded in a buffer, check the mtime

lua/orgmode/files/init.lua

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -296,23 +296,7 @@ function OrgFiles:update_file(filename, action)
296296
if not file then
297297
return Promise.resolve()
298298
end
299-
local is_same_file = filename == utils.current_file_path()
300-
if is_same_file then
301-
return Promise.resolve(action(file)):next(function(result)
302-
vim.cmd(':silent! w')
303-
return result
304-
end)
305-
end
306-
307-
local edit_file = utils.edit_file(filename)
308-
edit_file.open()
309-
310-
return Promise.resolve(action(file)):next(function(result)
311-
edit_file.close()
312-
return file:reload():next(function()
313-
return result
314-
end)
315-
end)
299+
return file:update(action)
316300
end
317301

318302
function OrgFiles:ensure_loaded()

0 commit comments

Comments
 (0)