Skip to content

Commit 95ade9b

Browse files
author
Sebastian Flügge
committed
fix: sorting by last used file
The cached version of mtime doesn't work reliably. Using uv directly on the filename gives much better results.
1 parent 4ee4502 commit 95ade9b

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lua/telescope-orgmode/org.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ local OrgApi = require('orgmode.api')
33
local M = {}
44

55
function M.load_files(opts)
6-
---@type { filename: string, last_used: number, title: string }[]
6+
---@type { filename: string, title: string }[]
77
local file_results = vim.tbl_map(function(file)
8-
return { filename = file.filename, last_used = file.metadata.mtime, title = file:get_title() }
8+
return { filename = file.filename, title = file:get_title() }
99
end, require('orgmode').files:all())
1010

1111
if not opts.archived then
@@ -15,16 +15,17 @@ function M.load_files(opts)
1515
end
1616

1717
table.sort(file_results, function(a, b)
18-
return a.last_used > b.last_used
18+
local stat_a = vim.uv.fs_stat(a.filename)
19+
local stat_b = vim.uv.fs_stat(b.filename)
20+
local mtime_a = stat_a and stat_a.mtime.sec or 0
21+
local mtime_b = stat_b and stat_b.mtime.sec or 0
22+
return mtime_a > mtime_b
1923
end)
2024

2125
return file_results
2226
end
2327

2428
function M.load_headlines(opts)
25-
---@type { filename: string, title: string, level: number, line_number: number, all_tags: string[], is_archived: boolean }[]
26-
local results = {}
27-
2829
-- Get files sorted by modification time (most recent first)
2930
local files = require('orgmode').files:all()
3031
if not opts.archived then
@@ -34,9 +35,15 @@ function M.load_headlines(opts)
3435
end
3536

3637
table.sort(files, function(a, b)
37-
return a.metadata.mtime < b.metadata.mtime
38+
local stat_a = vim.uv.fs_stat(a.filename)
39+
local stat_b = vim.uv.fs_stat(b.filename)
40+
local mtime_a = stat_a and stat_a.mtime.sec or 0
41+
local mtime_b = stat_b and stat_b.mtime.sec or 0
42+
return mtime_a > mtime_b
3843
end)
3944

45+
---@type { filename: string, title: string, level: number, line_number: number, all_tags: string[], is_archived: boolean }[]
46+
local results = {}
4047
for _, file in ipairs(files) do
4148
-- Skip archive files unless explicitly requested
4249
local headlines = opts.archived and file:get_headlines_including_archived() or file:get_headlines()

0 commit comments

Comments
 (0)