@@ -14,17 +14,17 @@ local Listitem = require('orgmode.files.elements.listitem')
14
14
--- @field all_files table<string , OrgFile> all loaded files , no matter if they are part of paths
15
15
--- @field load_state ' loading' | ' loaded' | nil
16
16
local OrgFiles = {}
17
+ OrgFiles .__index = OrgFiles
17
18
18
19
--- @param opts OrgFilesOpts
19
20
function OrgFiles :new (opts )
20
21
local data = {
21
- paths = opts .paths or {},
22
22
files = {},
23
23
all_files = {},
24
24
load_state = nil ,
25
25
}
26
26
setmetatable (data , self )
27
- self .__index = self
27
+ self .paths = self : _setup_paths ( opts . paths )
28
28
return data
29
29
end
30
30
@@ -54,6 +54,36 @@ function OrgFiles:load(force)
54
54
end )
55
55
end
56
56
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
+
57
87
function OrgFiles :get_tags ()
58
88
local tags = {}
59
89
for _ , orgfile in ipairs (self :all ()) do
@@ -295,23 +325,29 @@ function OrgFiles:ensure_loaded()
295
325
end
296
326
297
327
--- @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 {}
303
333
end
304
- if type (files ) ~= ' table' then
305
- files = { files }
334
+
335
+ if type (paths ) ~= ' table' then
336
+ return { paths }
306
337
end
307
338
339
+ return paths
340
+ end
341
+
342
+ --- @private
343
+ function OrgFiles :_files ()
308
344
local all_files = vim .tbl_map (function (file )
309
345
return vim .tbl_map (function (path )
310
346
return vim .fn .resolve (path )
311
347
end , vim .fn .glob (vim .fn .fnamemodify (file , ' :p' ), false , true ))
312
- end , files )
348
+ end , self . paths )
313
349
314
- all_files = utils . concat ( vim .tbl_flatten (all_files ), all_filenames , true )
350
+ all_files = vim .tbl_flatten (all_files )
315
351
316
352
return vim .tbl_filter (function (file )
317
353
local ext = vim .fn .fnamemodify (file , ' :e' )
0 commit comments