Skip to content

Commit c66faa0

Browse files
authored
Merge pull request #6 from lyz-code/style/lint-errors
style: format code
2 parents 483b401 + 5efafb5 commit c66faa0

File tree

4 files changed

+163
-163
lines changed

4 files changed

+163
-163
lines changed

lua/telescope-orgmode/utils.lua

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,79 @@
11
local entry_display = require("telescope.pickers.entry_display")
22

3-
local orgmode = require('orgmode.api')
3+
local orgmode = require("orgmode.api")
44

55
local utils = {}
66

77
utils.get_entries = function(opts)
8-
9-
local file_results = vim.tbl_map(function(file)
10-
return { file = file, filename = file.filename }
11-
end, orgmode.load())
12-
13-
if not opts.archived then
14-
file_results = vim.tbl_filter(function(entry)
15-
return not entry.file.is_archive_file
16-
end, file_results)
17-
end
18-
19-
if opts.max_depth == 0 then
20-
return file_results
21-
end
22-
23-
local results = {}
24-
for _, file_entry in ipairs(file_results) do
25-
for _, headline in ipairs(file_entry.file.headlines) do
26-
27-
local allowed_depth = opts.max_depth == nil or headline.level <= opts.max_depth
28-
local allowed_archive = opts.archived or not headline.is_archived
29-
if allowed_depth and allowed_archive then
30-
local entry = {
31-
file = file_entry.file,
32-
filename = file_entry.filename,
33-
headline = headline
34-
}
35-
table.insert(results, entry)
36-
end
37-
end
38-
end
39-
40-
return results
8+
local file_results = vim.tbl_map(function(file)
9+
return { file = file, filename = file.filename }
10+
end, orgmode.load())
11+
12+
if not opts.archived then
13+
file_results = vim.tbl_filter(function(entry)
14+
return not entry.file.is_archive_file
15+
end, file_results)
16+
end
17+
18+
if opts.max_depth == 0 then
19+
return file_results
20+
end
21+
22+
local results = {}
23+
for _, file_entry in ipairs(file_results) do
24+
for _, headline in ipairs(file_entry.file.headlines) do
25+
local allowed_depth = opts.max_depth == nil or headline.level <= opts.max_depth
26+
local allowed_archive = opts.archived or not headline.is_archived
27+
if allowed_depth and allowed_archive then
28+
local entry = {
29+
file = file_entry.file,
30+
filename = file_entry.filename,
31+
headline = headline,
32+
}
33+
table.insert(results, entry)
34+
end
35+
end
36+
end
37+
38+
return results
4139
end
4240

4341
utils.make_entry = function(opts)
44-
45-
local displayer = entry_display.create({
46-
separator = ' ',
47-
items = {
48-
{ width = vim.F.if_nil(opts.location_width, 20) },
49-
{ remaining = true }
50-
}
51-
})
52-
53-
local function make_display(entry)
54-
return displayer({ entry.location, entry.line })
55-
end
56-
57-
return function(entry)
58-
local headline = entry.headline
59-
60-
local lnum = nil
61-
local location = vim.fn.fnamemodify(entry.filename, ':t')
62-
local line = ""
63-
64-
if headline then
65-
lnum = headline.position.start_line
66-
location = string.format('%s:%i', location, lnum)
67-
line = string.format('%s %s', string.rep('*', headline.level), headline.title)
68-
end
69-
70-
return {
71-
value = entry,
72-
ordinal = location .. ' ' .. line,
73-
filename = entry.filename,
74-
lnum = lnum,
75-
display = make_display,
76-
location = location,
77-
line = line
78-
}
79-
end
42+
local displayer = entry_display.create({
43+
separator = " ",
44+
items = {
45+
{ width = vim.F.if_nil(opts.location_width, 20) },
46+
{ remaining = true },
47+
},
48+
})
49+
50+
local function make_display(entry)
51+
return displayer({ entry.location, entry.line })
52+
end
53+
54+
return function(entry)
55+
local headline = entry.headline
56+
57+
local lnum = nil
58+
local location = vim.fn.fnamemodify(entry.filename, ":t")
59+
local line = ""
60+
61+
if headline then
62+
lnum = headline.position.start_line
63+
location = string.format("%s:%i", location, lnum)
64+
line = string.format("%s %s", string.rep("*", headline.level), headline.title)
65+
end
66+
67+
return {
68+
value = entry,
69+
ordinal = location .. " " .. line,
70+
filename = entry.filename,
71+
lnum = lnum,
72+
display = make_display,
73+
location = location,
74+
line = line,
75+
}
76+
end
8077
end
8178

8279
return utils

lua/telescope/_extensions/orgmode/init.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
-- public orgmode api
33
-- TODO: add highlight groups
44

5-
return require("telescope").register_extension {
6-
exports = {
7-
search_headings = require("telescope._extensions.orgmode.search_headings"),
8-
refile_heading = require("telescope._extensions.orgmode.refile_heading")
9-
},
10-
}
5+
return require("telescope").register_extension({
6+
exports = {
7+
search_headings = require("telescope._extensions.orgmode.search_headings"),
8+
refile_heading = require("telescope._extensions.orgmode.refile_heading"),
9+
},
10+
})

lua/telescope/_extensions/orgmode/refile_heading.lua

Lines changed: 77 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,94 +6,95 @@ local actions = require("telescope.actions")
66
local action_state = require("telescope.actions.state")
77
local state = require("telescope.state")
88

9-
local utils = require('telescope-orgmode.utils')
9+
local utils = require("telescope-orgmode.utils")
1010

11-
local orgmode = require('orgmode')
12-
local Files = require('orgmode.parser.files')
13-
local Capture = require('orgmode.capture')
14-
local Range = require('orgmode.parser.range')
11+
local orgmode = require("orgmode")
12+
local Files = require("orgmode.parser.files")
13+
local Capture = require("orgmode.capture")
14+
local Range = require("orgmode.parser.range")
1515

1616
return function(opts)
17-
opts = opts or {}
17+
opts = opts or {}
1818

19-
-- TODO: this should be included in return from Files.get_current_file
20-
local is_capture = vim.F.npcall(vim.api.nvim_buf_get_var, 0, 'org_capture')
19+
-- TODO: this should be included in return from Files.get_current_file
20+
local is_capture = vim.F.npcall(vim.api.nvim_buf_get_var, 0, "org_capture")
2121

22-
local src_file = Files.get_current_file()
23-
-- In capture, refile top level heading even if cursor closer to a subheading
24-
local src_item = is_capture and src_file:get_headlines()[1] or src_file:get_closest_headline()
25-
local src_lines = src_file:get_headline_lines(src_item)
22+
local src_file = Files.get_current_file()
23+
-- In capture, refile top level heading even if cursor closer to a subheading
24+
local src_item = is_capture and src_file:get_headlines()[1] or src_file:get_closest_headline()
25+
local src_lines = src_file:get_headline_lines(src_item)
2626

27-
local function refile(prompt_bufnr)
28-
local entry = action_state.get_selected_entry()
29-
actions.close(prompt_bufnr)
27+
local function refile(prompt_bufnr)
28+
local entry = action_state.get_selected_entry()
29+
actions.close(prompt_bufnr)
3030

31-
local dst_file = entry.value.file
32-
local dst_headline = entry.value.headline
33-
if dst_headline then
34-
-- NOTE: adapted from Capture:refile_to_headline
35-
local is_same_file = dst_file.filename == src_item.root.filename
36-
src_lines = Capture:_adapt_headline_level(src_item, dst_headline.level, is_same_file)
37-
local refile_opts = {
38-
file = dst_file.filename,
39-
lines = src_lines,
40-
item = src_item,
41-
range = Range.from_line(dst_headline.position.end_line),
42-
headline = dst_headline.title,
43-
}
44-
local refiled = Capture:_refile_to(refile_opts)
45-
if not refiled then
46-
return false
47-
end
48-
else
49-
Capture:_refile_to_end(dst_file.filename, src_lines, src_item)
50-
end
31+
local dst_file = entry.value.file
32+
local dst_headline = entry.value.headline
33+
if dst_headline then
34+
-- NOTE: adapted from Capture:refile_to_headline
35+
local is_same_file = dst_file.filename == src_item.root.filename
36+
src_lines = Capture:_adapt_headline_level(src_item, dst_headline.level, is_same_file)
37+
local refile_opts = {
38+
file = dst_file.filename,
39+
lines = src_lines,
40+
item = src_item,
41+
range = Range.from_line(dst_headline.position.end_line),
42+
headline = dst_headline.title,
43+
}
44+
local refiled = Capture:_refile_to(refile_opts)
45+
if not refiled then
46+
return false
47+
end
48+
else
49+
Capture:_refile_to_end(dst_file.filename, src_lines, src_item)
50+
end
5151

52-
if is_capture then
53-
orgmode.action('capture.kill')
54-
end
55-
end
52+
if is_capture then
53+
orgmode.action("capture.kill")
54+
end
55+
end
5656

57-
local function gen_depth_toggle(opts, prompt_bufnr)
57+
local function gen_depth_toggle(opts, prompt_bufnr)
58+
local status = state.get_status(prompt_bufnr)
59+
status._ot_current_depth = opts.max_depth
60+
status._ot_next_depth = nil
61+
if status._ot_current_depth ~= 0 then
62+
status._ot_next_depth = 0
63+
end
5864

59-
local status = state.get_status(prompt_bufnr)
60-
status._ot_current_depth = opts.max_depth
61-
status._ot_next_depth = nil
62-
if status._ot_current_depth ~= 0 then
63-
status._ot_next_depth = 0
64-
end
65+
return function()
66+
local current_picker = action_state.get_current_picker(prompt_bufnr)
6567

66-
return function()
67-
local current_picker = action_state.get_current_picker(prompt_bufnr)
68+
local aux = status._ot_current_depth
69+
status._ot_current_depth = status._ot_next_depth
70+
status._ot_next_depth = aux
6871

69-
local aux = status._ot_current_depth
70-
status._ot_current_depth = status._ot_next_depth
71-
status._ot_next_depth = aux
72+
opts.max_depth = status._ot_current_depth
73+
local new_finder = finders.new_table({
74+
results = utils.get_entries(opts),
75+
entry_maker = opts.entry_maker or utils.make_entry(opts),
76+
})
7277

73-
opts.max_depth = status._ot_current_depth
74-
local new_finder = finders.new_table {
75-
results = utils.get_entries(opts),
76-
entry_maker = opts.entry_maker or utils.make_entry(opts),
77-
}
78+
current_picker:refresh(new_finder, opts)
79+
end
80+
end
7881

79-
current_picker:refresh(new_finder, opts)
80-
end
81-
end
82-
83-
pickers.new(opts, {
84-
-- TODO: alter prompt title when depth is 0: Refile under file, Refile
85-
-- under Headline
86-
prompt_title = "Refile Destination",
87-
finder = finders.new_table {
88-
results = utils.get_entries(opts),
89-
entry_maker = opts.entry_maker or utils.make_entry(opts),
90-
},
91-
sorter = conf.generic_sorter(opts),
92-
previewer = conf.grep_previewer(opts),
93-
attach_mappings = function(prompt_bufnr, map)
94-
action_set.select:replace(refile)
95-
map("i", "<c-space>", gen_depth_toggle(opts, prompt_bufnr))
96-
return true
97-
end,
98-
}):find()
82+
pickers
83+
.new(opts, {
84+
-- TODO: alter prompt title when depth is 0: Refile under file, Refile
85+
-- under Headline
86+
prompt_title = "Refile Destination",
87+
finder = finders.new_table({
88+
results = utils.get_entries(opts),
89+
entry_maker = opts.entry_maker or utils.make_entry(opts),
90+
}),
91+
sorter = conf.generic_sorter(opts),
92+
previewer = conf.grep_previewer(opts),
93+
attach_mappings = function(prompt_bufnr, map)
94+
action_set.select:replace(refile)
95+
map("i", "<c-space>", gen_depth_toggle(opts, prompt_bufnr))
96+
return true
97+
end,
98+
})
99+
:find()
99100
end

lua/telescope/_extensions/orgmode/search_headings.lua

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ local pickers = require("telescope.pickers")
22
local finders = require("telescope.finders")
33
local conf = require("telescope.config").values
44

5-
local utils = require('telescope-orgmode.utils')
5+
local utils = require("telescope-orgmode.utils")
66

77
return function(opts)
8-
opts = opts or {}
8+
opts = opts or {}
99

10-
pickers.new(opts, {
11-
prompt_title = "Search Headings",
12-
finder = finders.new_table {
13-
results = utils.get_entries(opts),
14-
entry_maker = opts.entry_maker or utils.make_entry(opts),
15-
},
16-
sorter = conf.generic_sorter(opts),
17-
previewer = conf.grep_previewer(opts),
18-
}):find()
10+
pickers
11+
.new(opts, {
12+
prompt_title = "Search Headings",
13+
finder = finders.new_table({
14+
results = utils.get_entries(opts),
15+
entry_maker = opts.entry_maker or utils.make_entry(opts),
16+
}),
17+
sorter = conf.generic_sorter(opts),
18+
previewer = conf.grep_previewer(opts),
19+
})
20+
:find()
1921
end

0 commit comments

Comments
 (0)