Skip to content

Commit 366f68d

Browse files
author
Sebastian Flügge
committed
fix: adjust refile and link feature to api changes
Add helper methods to retrieve OrgApi objects from picker entries and use them in actions. Fixes bug introduced by 6a53595 and 4ee4502.
1 parent f2b7c77 commit 366f68d

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

lua/telescope-orgmode/actions.lua

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ function M.refile(closest_headline)
5050
local entry = action_state.get_selected_entry()
5151
actions.close(prompt_bufnr)
5252

53-
-- Refile to the file by default
54-
local destination = entry.value.file
53+
local destination = entry.value.headline
54+
and org.get_api_headline(entry.filename, entry.value.headline.line_number)
55+
or org.get_api_file(entry.filename)
5556

56-
-- Refile to a specific heading if is set
57-
if entry.value.headline then
58-
destination = entry.value.headline
57+
if not destination then
58+
error('Could not find destination headline or file')
5959
end
6060

6161
return org.refile({
@@ -72,14 +72,15 @@ function M.insert(_)
7272
---@type MatchEntry
7373
local entry = action_state.get_selected_entry()
7474

75-
local destination = (function()
76-
if entry.value.headline then
77-
-- Link to a specific heading if is set
78-
return entry.value.headline:get_link()
79-
else
80-
return entry.value.file:get_link()
81-
end
82-
end)()
75+
local api_object = entry.value.headline
76+
and org.get_api_headline(entry.filename, entry.value.headline.line_number)
77+
or org.get_api_file(entry.filename)
78+
79+
if not api_object then
80+
error('Could not find ' .. (entry.value.headline and 'headline' or 'file') .. ' for link')
81+
end
82+
83+
local destination = api_object:get_link()
8384

8485
org.insert_link(destination)
8586
return true

lua/telescope-orgmode/org.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,27 @@ function M.get_closest_headline()
9595
return OrgApi.current():get_closest_headline()
9696
end
9797

98+
--- Get the API headline object for a given filename and line number
99+
---@param filename string
100+
---@param line_number number
101+
---@return table|nil
102+
function M.get_api_headline(filename, line_number)
103+
local api_file = OrgApi.load(filename)
104+
if api_file then
105+
for _, headline in ipairs(api_file.headlines) do
106+
if headline.position.start_line == line_number then
107+
return headline
108+
end
109+
end
110+
end
111+
return nil
112+
end
113+
114+
--- Get the API file object for a given filename
115+
---@param filename string
116+
---@return table|nil
117+
function M.get_api_file(filename)
118+
return OrgApi.load(filename)
119+
end
120+
98121
return M

0 commit comments

Comments
 (0)