|
1 |
| -local entry_display = require("telescope.pickers.entry_display") |
| 1 | +require('telescope-orgmode.typehints') |
2 | 2 |
|
3 |
| -local orgmode = require("orgmode.api") |
| 3 | +local entry_display = require('telescope.pickers.entry_display') |
| 4 | +local finders = require('telescope.finders') |
| 5 | +local action_state = require('telescope.actions.state') |
| 6 | +local state = require('telescope.state') |
| 7 | + |
| 8 | +local orgmode = require('orgmode.api') |
4 | 9 |
|
5 | 10 | local utils = {}
|
6 | 11 |
|
| 12 | +---@class OrgEntry |
| 13 | +---@field file OrgApiFile |
| 14 | +---@field filename string |
| 15 | +---@field headline? OrgApiHeadline |
| 16 | + |
| 17 | +---Fetches entrys from OrgApi and extracts the relevant information |
| 18 | +---@param opts any |
| 19 | +---@return OrgEntry[] |
7 | 20 | utils.get_entries = function(opts)
|
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 |
| 21 | + ---@type { file: OrgApiFile, filename: string }[] |
| 22 | + local file_results = vim.tbl_map(function(file) |
| 23 | + return { file = file, filename = file.filename } |
| 24 | + end, orgmode.load()) |
| 25 | + |
| 26 | + if not opts.archived then |
| 27 | + file_results = vim.tbl_filter(function(entry) |
| 28 | + return not entry.file.is_archive_file |
| 29 | + end, file_results) |
| 30 | + end |
| 31 | + |
| 32 | + if opts.max_depth == 0 then |
| 33 | + return file_results |
| 34 | + end |
| 35 | + |
| 36 | + local results = {} |
| 37 | + for _, file_entry in ipairs(file_results) do |
| 38 | + for _, headline in ipairs(file_entry.file.headlines) do |
| 39 | + local allowed_depth = opts.max_depth == nil or headline.level <= opts.max_depth |
| 40 | + local allowed_archive = opts.archived or not headline.is_archived |
| 41 | + if allowed_depth and allowed_archive then |
| 42 | + local entry = { |
| 43 | + file = file_entry.file, |
| 44 | + filename = file_entry.filename, |
| 45 | + headline = headline, |
| 46 | + } |
| 47 | + table.insert(results, entry) |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + return results |
39 | 53 | end
|
40 | 54 |
|
| 55 | +---Entry-Maker for Telescope |
| 56 | +---@param opts any |
| 57 | +---@return fun(entry: OrgEntry):MatchEntry |
41 | 58 | utils.make_entry = function(opts)
|
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 |
| 59 | + local displayer = entry_display.create({ |
| 60 | + separator = ' ', |
| 61 | + items = { |
| 62 | + { width = vim.F.if_nil(opts.location_width, 20) }, |
| 63 | + { remaining = true }, |
| 64 | + }, |
| 65 | + }) |
| 66 | + |
| 67 | + local function make_display(entry) |
| 68 | + return displayer({ entry.location, entry.line }) |
| 69 | + end |
| 70 | + |
| 71 | + return function(entry) |
| 72 | + local headline = entry.headline |
| 73 | + |
| 74 | + local lnum = nil |
| 75 | + local location = vim.fn.fnamemodify(entry.filename, ':t') |
| 76 | + local line = '' |
| 77 | + |
| 78 | + if headline then |
| 79 | + lnum = headline.position.start_line |
| 80 | + location = string.format('%s:%i', location, lnum) |
| 81 | + line = string.format('%s %s', string.rep('*', headline.level), headline.title) |
| 82 | + end |
| 83 | + |
| 84 | + return { |
| 85 | + value = entry, |
| 86 | + ordinal = location .. ' ' .. line, |
| 87 | + filename = entry.filename, |
| 88 | + lnum = lnum, |
| 89 | + display = make_display, |
| 90 | + location = location, |
| 91 | + line = line, |
| 92 | + } |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +utils.gen_depth_toggle = function(opts, prompt_bufnr) |
| 97 | + local status = state.get_status(prompt_bufnr) |
| 98 | + status._ot_current_depth = opts.max_depth |
| 99 | + status._ot_next_depth = nil |
| 100 | + if status._ot_current_depth ~= 0 then |
| 101 | + status._ot_next_depth = 0 |
| 102 | + end |
| 103 | + |
| 104 | + return function() |
| 105 | + local current_picker = action_state.get_current_picker(prompt_bufnr) |
| 106 | + |
| 107 | + local aux = status._ot_current_depth |
| 108 | + status._ot_current_depth = status._ot_next_depth |
| 109 | + status._ot_next_depth = aux |
| 110 | + |
| 111 | + opts.max_depth = status._ot_current_depth |
| 112 | + local new_finder = finders.new_table({ |
| 113 | + results = utils.get_entries(opts), |
| 114 | + entry_maker = opts.entry_maker or utils.make_entry(opts), |
| 115 | + }) |
| 116 | + |
| 117 | + current_picker:refresh(new_finder, opts) |
| 118 | + end |
77 | 119 | end
|
78 | 120 |
|
79 | 121 | return utils
|
0 commit comments