Skip to content

Commit 1d080c3

Browse files
author
Sebastian Flügge
committed
refactor: split large function
To remove variable shadowing and make the code easier to work with, migrate the local methods into a class and return a callback, which only contains the picker itself.
1 parent 01fa926 commit 1d080c3

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

lua/telescope-orgmode/refile_heading.lua

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,57 @@ local utils = require('telescope-orgmode.utils')
1010

1111
local api = require('orgmode.api')
1212

13-
return function(opts)
14-
opts = opts or {}
13+
local M = {}
1514

16-
local closest_headline = api.current():get_closest_headline()
15+
M.refile = function(prompt_bufnr)
16+
local entry = action_state.get_selected_entry()
17+
actions.close(prompt_bufnr)
1718

18-
local function refile(prompt_bufnr)
19-
local entry = action_state.get_selected_entry()
20-
actions.close(prompt_bufnr)
19+
-- Refile to the file by default
20+
local destination = entry.value.file
2121

22-
-- Refile to the file by default
23-
local destination = entry.value.file
22+
-- Refile to a specific heading if is set
23+
if entry.value.headline then
24+
destination = entry.value.headline
25+
end
2426

25-
-- Refile to a specific heading if is set
26-
if entry.value.headline then
27-
destination = entry.value.headline
28-
end
27+
return api.refile({
28+
source = M.closest_headline,
29+
destination = destination,
30+
})
31+
end
2932

30-
return api.refile({
31-
source = closest_headline,
32-
destination = destination,
33-
})
33+
M.gen_depth_toggle = function(opts, prompt_bufnr)
34+
local status = state.get_status(prompt_bufnr)
35+
status._ot_current_depth = opts.max_depth
36+
status._ot_next_depth = nil
37+
if status._ot_current_depth ~= 0 then
38+
status._ot_next_depth = 0
3439
end
3540

36-
local function gen_depth_toggle(opts, prompt_bufnr)
37-
local status = state.get_status(prompt_bufnr)
38-
status._ot_current_depth = opts.max_depth
39-
status._ot_next_depth = nil
40-
if status._ot_current_depth ~= 0 then
41-
status._ot_next_depth = 0
42-
end
43-
44-
return function()
45-
local current_picker = action_state.get_current_picker(prompt_bufnr)
41+
return function()
42+
local current_picker = action_state.get_current_picker(prompt_bufnr)
4643

47-
local aux = status._ot_current_depth
48-
status._ot_current_depth = status._ot_next_depth
49-
status._ot_next_depth = aux
44+
local aux = status._ot_current_depth
45+
status._ot_current_depth = status._ot_next_depth
46+
status._ot_next_depth = aux
5047

51-
opts.max_depth = status._ot_current_depth
52-
local new_finder = finders.new_table({
53-
results = utils.get_entries(opts),
54-
entry_maker = opts.entry_maker or utils.make_entry(opts),
55-
})
48+
opts.max_depth = status._ot_current_depth
49+
local new_finder = finders.new_table({
50+
results = utils.get_entries(opts),
51+
entry_maker = opts.entry_maker or utils.make_entry(opts),
52+
})
5653

57-
current_picker:refresh(new_finder, opts)
58-
end
54+
current_picker:refresh(new_finder, opts)
5955
end
56+
end
57+
58+
M.closest_headline = nil
59+
60+
return function(opts)
61+
opts = opts or {}
62+
63+
M.closest_headline = api.current():get_closest_headline()
6064

6165
pickers
6266
.new(opts, {
@@ -70,8 +74,8 @@ return function(opts)
7074
sorter = conf.generic_sorter(opts),
7175
previewer = conf.grep_previewer(opts),
7276
attach_mappings = function(prompt_bufnr, map)
73-
action_set.select:replace(refile)
74-
map('i', '<c-space>', gen_depth_toggle(opts, prompt_bufnr))
77+
action_set.select:replace(M.refile)
78+
map('i', '<c-space>', M.gen_depth_toggle(opts, prompt_bufnr))
7579
return true
7680
end,
7781
})

0 commit comments

Comments
 (0)