|
| 1 | +local render_mod = require("peekstack.ui.render") |
| 2 | +local preview = require("peekstack.ui.stack_view.preview") |
| 3 | +local tree = require("peekstack.ui.stack_view.tree") |
| 4 | +local str = require("peekstack.util.str") |
| 5 | + |
| 6 | +local M = {} |
| 7 | + |
| 8 | +local PREVIEW_LINE_MARKER = "│ " |
| 9 | + |
| 10 | +---@type table<string, string> |
| 11 | +local TITLE_HL_TO_SV = { |
| 12 | + PeekstackTitleProvider = "PeekstackStackViewProvider", |
| 13 | + PeekstackTitlePath = "PeekstackStackViewPath", |
| 14 | + PeekstackTitleIcon = "PeekstackStackViewIcon", |
| 15 | + PeekstackTitleLine = "PeekstackStackViewLine", |
| 16 | +} |
| 17 | + |
| 18 | +---@class PeekstackStackViewPipelineOpts |
| 19 | +---@field items PeekstackPopupModel[] |
| 20 | +---@field focused_id integer? |
| 21 | +---@field filter string? |
| 22 | +---@field win_width integer |
| 23 | +---@field ui_path PeekstackConfigPath? |
| 24 | +---@field location_text fun(popup: PeekstackPopupModel, max_width: integer?): string |
| 25 | +---@field preview_line? fun(source_bufnr: integer?, line: integer, max_width: integer, preview_prefix: string): PeekstackStackViewPreviewLine? |
| 26 | + |
| 27 | +---@param text string |
| 28 | +---@param query string? |
| 29 | +---@return boolean |
| 30 | +local function matches_filter(text, query) |
| 31 | + if not query or query == "" then |
| 32 | + return true |
| 33 | + end |
| 34 | + return text:lower():find(query:lower(), 1, true) ~= nil |
| 35 | +end |
| 36 | + |
| 37 | +---@param popup PeekstackPopupModel |
| 38 | +---@param opts PeekstackStackViewPipelineOpts |
| 39 | +---@return string |
| 40 | +local function filter_label(popup, opts) |
| 41 | + if popup.title then |
| 42 | + return popup.title |
| 43 | + end |
| 44 | + return opts.location_text(popup, nil) |
| 45 | +end |
| 46 | + |
| 47 | +---@param popup PeekstackPopupModel |
| 48 | +---@param prefix string |
| 49 | +---@param idx integer |
| 50 | +---@param is_focused boolean |
| 51 | +---@param ui_path PeekstackConfigPath |
| 52 | +---@param win_width integer |
| 53 | +---@param location_text fun(popup: PeekstackPopupModel, max_width: integer?): string |
| 54 | +---@return string, PeekstackStackViewHighlight[] |
| 55 | +local function build_entry_line(popup, prefix, idx, is_focused, ui_path, win_width, location_text) |
| 56 | + local max_label_width = math.max(win_width - vim.fn.strdisplaywidth(prefix), 0) |
| 57 | + if ui_path.max_width and ui_path.max_width > 0 then |
| 58 | + max_label_width = math.min(max_label_width, ui_path.max_width) |
| 59 | + end |
| 60 | + |
| 61 | + local label = nil |
| 62 | + local label_chunks = nil |
| 63 | + if popup.title_chunks then |
| 64 | + label_chunks = render_mod.truncate_chunks(popup.title_chunks, max_label_width) |
| 65 | + label = render_mod.title_text(label_chunks) |
| 66 | + elseif popup.title then |
| 67 | + label = str.truncate_middle(popup.title, max_label_width) |
| 68 | + else |
| 69 | + label = location_text(popup, max_label_width) |
| 70 | + end |
| 71 | + |
| 72 | + ---@type PeekstackStackViewHighlight[] |
| 73 | + local line_hls = {} |
| 74 | + local focus_marker = is_focused and "▶ " or " " |
| 75 | + local pinned = popup.pinned and "• " or "" |
| 76 | + local index_str = string.format("%d. ", idx) |
| 77 | + local tree_guide = prefix:sub(#focus_marker + #index_str + #pinned + 1) |
| 78 | + |
| 79 | + if is_focused then |
| 80 | + table.insert(line_hls, { col_start = 0, col_end = #focus_marker, hl_group = "PeekstackStackViewFocused" }) |
| 81 | + end |
| 82 | + |
| 83 | + local idx_start = #focus_marker |
| 84 | + table.insert(line_hls, { |
| 85 | + col_start = idx_start, |
| 86 | + col_end = idx_start + #index_str, |
| 87 | + hl_group = "PeekstackStackViewIndex", |
| 88 | + }) |
| 89 | + |
| 90 | + if popup.pinned then |
| 91 | + local pin_start = idx_start + #index_str |
| 92 | + table.insert(line_hls, { |
| 93 | + col_start = pin_start, |
| 94 | + col_end = pin_start + #pinned, |
| 95 | + hl_group = "PeekstackStackViewPinned", |
| 96 | + }) |
| 97 | + end |
| 98 | + |
| 99 | + if tree_guide ~= "" then |
| 100 | + local tree_start = idx_start + #index_str + #pinned |
| 101 | + table.insert(line_hls, { |
| 102 | + col_start = tree_start, |
| 103 | + col_end = tree_start + #tree_guide, |
| 104 | + hl_group = "PeekstackStackViewTree", |
| 105 | + }) |
| 106 | + end |
| 107 | + |
| 108 | + if label_chunks then |
| 109 | + local pos = #prefix |
| 110 | + for _, chunk in ipairs(label_chunks) do |
| 111 | + local text = chunk[1] or "" |
| 112 | + local hl = chunk[2] |
| 113 | + if hl and #text > 0 then |
| 114 | + table.insert(line_hls, { |
| 115 | + col_start = pos, |
| 116 | + col_end = pos + #text, |
| 117 | + hl_group = TITLE_HL_TO_SV[hl] or hl, |
| 118 | + }) |
| 119 | + end |
| 120 | + pos = pos + #text |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + return prefix .. label, line_hls |
| 125 | +end |
| 126 | + |
| 127 | +---@param opts PeekstackStackViewPipelineOpts |
| 128 | +---@return PeekstackStackViewRenderModel |
| 129 | +function M.build(opts) |
| 130 | + local ui_path = opts.ui_path or {} |
| 131 | + local preview_line = opts.preview_line or preview.preview_line |
| 132 | + |
| 133 | + ---@type string[] |
| 134 | + local lines = {} |
| 135 | + ---@type PeekstackStackViewHighlight[][] |
| 136 | + local highlights = {} |
| 137 | + ---@type table<integer, PeekstackStackViewPreviewLine> |
| 138 | + local preview_lines = {} |
| 139 | + ---@type table<integer, integer> |
| 140 | + local line_to_id = {} |
| 141 | + |
| 142 | + ---@type PeekstackPopupModel[] |
| 143 | + local visible = {} |
| 144 | + for _, popup in ipairs(opts.items) do |
| 145 | + if matches_filter(filter_label(popup, opts), opts.filter) then |
| 146 | + table.insert(visible, popup) |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + visible = tree.sort(visible) |
| 151 | + local tree_guides = tree.guide_by_id(visible) |
| 152 | + |
| 153 | + local header = nil |
| 154 | + local header_hl = nil |
| 155 | + if opts.filter and opts.filter ~= "" then |
| 156 | + header = string.format("Filter: %s (%d/%d)", opts.filter, #visible, #opts.items) |
| 157 | + header_hl = "PeekstackStackViewFilter" |
| 158 | + else |
| 159 | + header = string.format("Stack: %d", #visible) |
| 160 | + header_hl = "PeekstackStackViewHeader" |
| 161 | + end |
| 162 | + |
| 163 | + table.insert(lines, header) |
| 164 | + table.insert(highlights, { { col_start = 0, col_end = #header, hl_group = header_hl } }) |
| 165 | + |
| 166 | + if #visible == 0 then |
| 167 | + local empty = opts.filter and opts.filter ~= "" and "No matches" or "No stack entries" |
| 168 | + table.insert(lines, empty) |
| 169 | + table.insert(highlights, { { col_start = 0, col_end = #empty, hl_group = "PeekstackStackViewEmpty" } }) |
| 170 | + end |
| 171 | + |
| 172 | + for idx, popup in ipairs(visible) do |
| 173 | + local is_focused = popup.id == opts.focused_id |
| 174 | + local focus_marker = is_focused and "▶ " or " " |
| 175 | + local pinned = popup.pinned and "• " or "" |
| 176 | + local index_str = string.format("%d. ", idx) |
| 177 | + local tree_guide = tree_guides[popup.id] or "" |
| 178 | + local prefix = focus_marker .. index_str .. pinned .. tree_guide |
| 179 | + |
| 180 | + local line, line_hls = build_entry_line(popup, prefix, idx, is_focused, ui_path, opts.win_width, opts.location_text) |
| 181 | + |
| 182 | + table.insert(lines, line) |
| 183 | + table.insert(highlights, line_hls) |
| 184 | + |
| 185 | + local entry_line_nr = #lines |
| 186 | + line_to_id[entry_line_nr] = popup.id |
| 187 | + |
| 188 | + local source_line = popup.location |
| 189 | + and popup.location.range |
| 190 | + and popup.location.range.start |
| 191 | + and popup.location.range.start.line |
| 192 | + if source_line then |
| 193 | + local preview_prefix = string.rep(" ", vim.fn.strdisplaywidth(prefix)) .. PREVIEW_LINE_MARKER |
| 194 | + local preview_item = preview_line(popup.source_bufnr or popup.bufnr, source_line, opts.win_width, preview_prefix) |
| 195 | + if preview_item then |
| 196 | + table.insert(lines, preview_item.line) |
| 197 | + table.insert(highlights, { |
| 198 | + { col_start = 0, col_end = #preview_item.line, hl_group = "PeekstackStackViewPreview" }, |
| 199 | + }) |
| 200 | + |
| 201 | + local preview_line_nr = #lines |
| 202 | + preview_lines[preview_line_nr] = preview_item |
| 203 | + line_to_id[preview_line_nr] = popup.id |
| 204 | + end |
| 205 | + end |
| 206 | + end |
| 207 | + |
| 208 | + return { |
| 209 | + lines = lines, |
| 210 | + highlights = highlights, |
| 211 | + preview_lines = preview_lines, |
| 212 | + line_to_id = line_to_id, |
| 213 | + header_lines = 1, |
| 214 | + } |
| 215 | +end |
| 216 | + |
| 217 | +return M |
0 commit comments