Skip to content

Commit 0c439b8

Browse files
committed
fix: brittle picker state
Manage the state outside of telescope in the opts table.
1 parent b445c09 commit 0c439b8

File tree

5 files changed

+8
-25
lines changed

5 files changed

+8
-25
lines changed

lua/telescope-orgmode/actions.lua

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,20 @@ local org = require('telescope-orgmode.org')
33

44
local actions = require('telescope.actions')
55
local action_state = require('telescope.actions.state')
6-
local state = require('telescope.state')
76

87
local M = {}
98

109
function M.toggle_headlines_orgfiles(opts)
1110
return function(prompt_bufnr)
12-
local status = state.get_status(prompt_bufnr)
13-
14-
-- _ot_ is used as our plugin-specific namespace in the action status
15-
-- (ot - orgmode telescope)
16-
--
17-
-- FIXME: the state get's sometimes nil when the initalization has already been run
18-
-- In this case, a toggle is "dropped" (keypress does not change the state).
19-
-- Can we avoid that by initializing the state in the higher order function?
20-
-- Idea: We can try to do it as before, but pass the prompt_bufnr with the opts.
21-
if status._ot_state == nil then
22-
-- uninitialized state - initialize with orgfiles
23-
-- Because when this function is called the first time, it is triggered
24-
-- by the users and we search over headlines by default, we set the state
25-
-- for the first toggle already here.
26-
status._ot_state = { current = opts.states[2], next = opts.states[1] }
27-
else
28-
status._ot_state.current, status._ot_state.next = status._ot_state.next, status._ot_state.current
29-
end
11+
opts.state.current, opts.state.next = opts.state.next, opts.state.current
3012

31-
if status._ot_state.current == 'headlines' then
13+
if opts.state.current == 'headlines' then
3214
M._find_headlines(opts, prompt_bufnr)
33-
elseif status._ot_state.current == 'orgfiles' then
15+
elseif opts.state.current == 'orgfiles' then
3416
M._find_orgfiles(opts, prompt_bufnr)
3517
else
3618
-- this should not happen
37-
error(string.format('Invalid state %s', status._ot_state.current))
19+
error(string.format('Invalid state %s', opts.state.current))
3820
end
3921
end
4022
end

lua/telescope-orgmode/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function M.init_opts(opts, prompt_titles)
1515
for state, _ in pairs(prompt_titles) do
1616
table.insert(opts.states, state)
1717
end
18+
opts.state = { current = opts.states[1], next = opts.states[2] }
1819
return opts
1920
end
2021

lua/telescope-orgmode/picker/insert_link.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ return function(opts)
1515

1616
pickers
1717
.new(opts, {
18-
prompt_title = opts.prompt_titles[opts.states[1]],
18+
prompt_title = opts.prompt_titles[opts.state.current],
1919
finder = finders.headlines(opts),
2020
sorter = conf.generic_sorter(opts),
2121
previewer = conf.grep_previewer(opts),

lua/telescope-orgmode/picker/refile_heading.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ return function(opts)
1818

1919
pickers
2020
.new(opts, {
21-
prompt_title = opts.prompt_titles[opts.states[1]],
21+
prompt_title = opts.prompt_titles[opts.state.current],
2222
finder = finders.headlines(opts),
2323
sorter = conf.generic_sorter(opts),
2424
previewer = conf.grep_previewer(opts),

lua/telescope-orgmode/picker/search_headings.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ return function(opts)
1313

1414
pickers
1515
.new(opts, {
16-
prompt_title = opts.prompt_titles[opts.states[1]],
16+
prompt_title = opts.prompt_titles[opts.state.current],
1717
finder = finders.headlines(opts),
1818
sorter = conf.generic_sorter(opts),
1919
previewer = conf.grep_previewer(opts),

0 commit comments

Comments
 (0)