Skip to content

Commit dafe433

Browse files
feat(files): Add method to append file to paths
1 parent 4c6cf0d commit dafe433

File tree

2 files changed

+74
-19
lines changed

2 files changed

+74
-19
lines changed

lua/orgmode/files/init.lua

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ local Listitem = require('orgmode.files.elements.listitem')
1414
---@field all_files table<string, OrgFile> all loaded files, no matter if they are part of paths
1515
---@field load_state 'loading' | 'loaded' | nil
1616
local OrgFiles = {}
17+
OrgFiles.__index = OrgFiles
1718

1819
---@param opts OrgFilesOpts
1920
function OrgFiles:new(opts)
2021
local data = {
21-
paths = opts.paths or {},
2222
files = {},
2323
all_files = {},
2424
load_state = nil,
2525
}
2626
setmetatable(data, self)
27-
self.__index = self
27+
self.paths = self:_setup_paths(opts.paths)
2828
return data
2929
end
3030

@@ -54,6 +54,36 @@ function OrgFiles:load(force)
5454
end)
5555
end
5656

57+
---@param filename string
58+
---@return OrgPromise<OrgFile>
59+
function OrgFiles:add_to_paths(filename)
60+
filename = vim.fs.normalize(filename)
61+
62+
if self.files[filename] then
63+
return self.files[filename]:reload()
64+
end
65+
66+
local promise = self:load_file(filename):next(function(orgfile)
67+
if orgfile then
68+
self.files[filename] = orgfile
69+
local all_paths = self:_files()
70+
if not vim.tbl_contains(all_paths, filename) then
71+
table.insert(self.paths, filename)
72+
end
73+
end
74+
return orgfile
75+
end)
76+
77+
return promise
78+
end
79+
80+
---@param filename string
81+
---@param timeout? number
82+
---@return OrgFile
83+
function OrgFiles:add_to_paths_sync(filename, timeout)
84+
return self:add_to_paths(filename):wait(timeout)
85+
end
86+
5787
function OrgFiles:get_tags()
5888
local tags = {}
5989
for _, orgfile in ipairs(self:all()) do
@@ -295,23 +325,29 @@ function OrgFiles:ensure_loaded()
295325
end
296326

297327
---@private
298-
function OrgFiles:_files()
299-
local all_filenames = {}
300-
local files = self.paths
301-
if not files or files == '' or (type(files) == 'table' and vim.tbl_isempty(files)) then
302-
return all_filenames
328+
---@param paths string | string[] | nil
329+
---@return string[]
330+
function OrgFiles:_setup_paths(paths)
331+
if not paths or paths == '' or (type(paths) == 'table' and vim.tbl_isempty(paths)) then
332+
return {}
303333
end
304-
if type(files) ~= 'table' then
305-
files = { files }
334+
335+
if type(paths) ~= 'table' then
336+
return { paths }
306337
end
307338

339+
return paths
340+
end
341+
342+
---@private
343+
function OrgFiles:_files()
308344
local all_files = vim.tbl_map(function(file)
309345
return vim.tbl_map(function(path)
310346
return vim.fn.resolve(path)
311347
end, vim.fn.glob(vim.fn.fnamemodify(file, ':p'), false, true))
312-
end, files)
348+
end, self.paths)
313349

314-
all_files = utils.concat(vim.tbl_flatten(all_files), all_filenames, true)
350+
all_files = vim.tbl_flatten(all_files)
315351

316352
return vim.tbl_filter(function(file)
317353
local ext = vim.fn.fnamemodify(file, ':e')

tests/plenary/init_spec.lua

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
local helpers = require('tests.plenary.helpers')
12
local orgmode = require('orgmode')
23

34
describe('Init', function()
5+
local org = orgmode.setup({
6+
org_agenda_files = vim.fn.getcwd() .. '/tests/plenary/fixtures/*',
7+
org_default_notes_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org',
8+
})
9+
local todo_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/todo.org'
10+
local todo_archive_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/todo.org_archive'
11+
local refile_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org'
12+
local txt_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/text_notes.txt'
413
it('should load and parse files from folder', function()
5-
local org = orgmode.setup({
6-
org_agenda_files = vim.fn.getcwd() .. '/tests/plenary/fixtures/*',
7-
org_default_notes_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org',
8-
})
9-
local todo_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/todo.org'
10-
local todo_archive_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/todo.org_archive'
11-
local refile_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org'
12-
local txt_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/text_notes.txt'
1314
assert.is.Nil(rawget(org, 'files'))
1415
assert.is.Nil(rawget(org, 'agenda'))
1516
assert.is.Nil(rawget(org, 'capture'))
@@ -34,4 +35,22 @@ describe('Init', function()
3435
assert.are.same(true, org.files:get(todo_archive_file):is_archive_file())
3536
assert.are.same({ 'NESTED', 'OFFICE', 'PRIVATE', 'PROJECT', 'WORK' }, org.files:get_tags())
3637
end)
38+
39+
it('should append files to paths', function()
40+
local fname = vim.fn.tempname() .. '.org'
41+
vim.fn.writefile({ '* Appended' }, fname)
42+
43+
assert.is.Nil(org.files.files[fname])
44+
assert.are.same({}, org.files:find_headlines_by_title('Appended'))
45+
assert.are.same({ vim.fn.getcwd() .. '/tests/plenary/fixtures/*' }, org.files.paths)
46+
47+
org.files:add_to_paths_sync(fname)
48+
assert.is.Not.Nil(org.files.files[fname])
49+
assert.are.same('Appended', org.files:find_headlines_by_title('Appended')[1]:get_title())
50+
assert.are.same({ vim.fn.getcwd() .. '/tests/plenary/fixtures/*', fname }, org.files.paths)
51+
52+
org.files:add_to_paths_sync(todo_file)
53+
-- Existing file in path not appended to paths
54+
assert.are.same({ vim.fn.getcwd() .. '/tests/plenary/fixtures/*', fname }, org.files.paths)
55+
end)
3756
end)

0 commit comments

Comments
 (0)