Skip to content

Commit 39c2819

Browse files
committed
refactor: simplify pinned-issues view and fix file previewer
1 parent e1b5eb0 commit 39c2819

File tree

2 files changed

+66
-50
lines changed

2 files changed

+66
-50
lines changed

lua/gitforge/issue_actions.lua

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -528,64 +528,76 @@ function IssueActions.list_opened_issues(provider)
528528
:find()
529529
end
530530

531+
---@param plugin_data string
532+
---@param name string
533+
local make_pinned_file = function(plugin_data, name, project, id)
534+
local issue_path = vim.fs.joinpath(plugin_data, name)
535+
local headline = require("gitforge.utility").get_markdown_headline_from_file(issue_path)
536+
local issue_number = string.match(id, "issue_(.*)")
537+
if headline ~= nil and issue_number ~= nil then
538+
return {
539+
project = project,
540+
issue_number = issue_number,
541+
filename = issue_path,
542+
path = issue_path,
543+
headline = headline,
544+
}
545+
end
546+
end
547+
548+
---@return string|nil project
549+
---@return string|nil id
550+
---@return string|nil file_extension
551+
local project_id_from_path = function(name)
552+
local elements = vim.split(name, "/", { plain = true })
553+
if #elements > 2 then
554+
local project = elements[1]
555+
for i = 2, #elements - 1 do
556+
project = project .. "/" .. elements[i]
557+
end
558+
local file_parts = vim.split(elements[#elements], "%.")
559+
if #file_parts == 2 then
560+
return project, file_parts[1], file_parts[2]
561+
else
562+
return nil, nil, nil
563+
end
564+
else
565+
return nil, nil, nil
566+
end
567+
end
568+
531569
function IssueActions.list_pinned_issues()
532570
local util = require("gitforge.utility")
533571
local log = require("gitforge.log")
534572

535-
-- FIXME: This function is the biggest mess of this code-base right now.
536-
-- - Extract good functions for utility and so on
537-
-- - get common functionality for telescope pickers
538-
-- - show the issue content in previewer, I failed to do so...
539-
540573
local plugin_data = util.get_plugin_data_dir()
541574
if not util.dir_exists(plugin_data) then
542575
log.ephemeral_info("Plugin data directory " .. plugin_data .. " does not exist, no pinned issues.")
543576
return
544577
end
578+
--- Array of all issues to show.
545579
local issues = {}
546580
--- Maps a project to its provider.
547581
local providers = {}
548582

549-
local get_headline_from_file = function(path)
550-
local s = util.read_file_to_string(path)
551-
if s == nil then
552-
return nil
553-
end
554-
local idx_first_linebreak = string.find(s, "\n", 1, true)
555-
if idx_first_linebreak == nil or idx_first_linebreak <= 3 then
556-
return nil
557-
end
558-
return vim.trim(string.sub(s, 3, idx_first_linebreak))
559-
end
560-
561583
for name, type in vim.fs.dir(plugin_data, { depth = 4 }) do
562584
if type == "file" then
563-
local el = vim.split(name, "/", { plain = true })
564-
if #el == 4 then
565-
local project = vim.fn.join({ el[1], el[2], el[3] }, "/")
566-
local file_parts = vim.split(el[4], "%.")
567-
if #file_parts == 2 then
568-
local id = file_parts[1]
569-
if id == "issue_provider" then
570-
providers[project] = file_parts[2]
571-
else
572-
local issue_path = vim.fs.joinpath(plugin_data, name)
573-
local headline = get_headline_from_file(issue_path)
574-
if headline ~= nil then
575-
table.insert(issues, {
576-
project = project,
577-
issue_number = id,
578-
filename = issue_path,
579-
headline = headline,
580-
})
581-
end
585+
local project, id, file_extension = project_id_from_path(name)
586+
if project and id and file_extension then
587+
if id == "issue_provider" then
588+
providers[project] = file_extension
589+
elseif string.match(id, "issue_.*") then
590+
local f = make_pinned_file(plugin_data, name, project, id)
591+
if f then
592+
table.insert(issues, f)
582593
end
594+
else
595+
-- Skip
583596
end
584597
end
585598
end
586599
end
587600

588-
589601
local ts = require("telescope")
590602
local pickers = require("telescope.pickers")
591603
local config = require("telescope.config").values
@@ -595,30 +607,21 @@ function IssueActions.list_pinned_issues()
595607
local make_entry = require("telescope.make_entry")
596608
pickers
597609
.new({}, {
598-
prompt_title = "Issue Buffers",
610+
prompt_title = "Pinned Issues",
599611
finder = finders.new_table {
600612
results = issues,
601613
entry_maker = function(entry)
602-
-- local idx_start = string.find(entry.bufname, generic_ui.forge_issue_pattern, 1, true)
603-
-- if idx_start == nil then
604-
-- log.ephemeral_info("Failed to identify cache issue buffer")
605-
-- return nil
606-
-- end
607-
-- local buf_txt = string.sub(entry.bufname, idx_start)
608-
print(vim.inspect(entry))
609614
return make_entry.set_default_entry_mt({
610615
ordinal = entry.project .. ":" .. entry.issue_number .. ":" .. entry.headline,
616+
path = entry.filename,
611617
filename = entry.filename,
612618
issue_number = entry.issue_number,
613619
value = entry,
614620
display = entry.project .. " #" .. entry.issue_number .. " " .. entry.headline,
615621
}, {})
616622
end,
617623
},
618-
-- previewer = require('telescope.config').values.cat_previewer,
619-
-- previewer = previewers.cat({
620-
-- title = "Pinned Issues Preview",
621-
-- }),
624+
previewer = previewers.vim_buffer_cat.new({}),
622625
sorter = config.generic_sorter({}),
623626
attach_mappings = function(prompt_bufnr)
624627
local state = require("telescope.actions.state")
@@ -638,6 +641,7 @@ function IssueActions.list_pinned_issues()
638641
local p = prov:newIssue(selection.value.issue_number, selection.value.project)
639642
IssueActions.view_issue(p)
640643
end)
644+
-- 'return true' means "use default mappings"
641645
return true
642646
end,
643647
})

lua/gitforge/utility.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ function M.read_file_to_string(path)
1010
return content
1111
end
1212

13+
function M.get_markdown_headline_from_file(path)
14+
local s = M.read_file_to_string(path)
15+
if s == nil then
16+
return nil
17+
end
18+
local idx_first_linebreak = string.find(s, "\n", 1, true)
19+
if idx_first_linebreak == nil or idx_first_linebreak <= 3 then
20+
return nil
21+
end
22+
return vim.trim(string.sub(s, 3, idx_first_linebreak))
23+
end
24+
1325
---Creates a string from the buffer content of @p buf
1426
---@param buf integer Buffer Id to get the content from.
1527
---@return string content Concatenation with "\n" and final trimming of the buffer content.
@@ -78,7 +90,7 @@ end
7890
---@return PathlibPath Path
7991
function M.get_issue_data_file(provider)
8092
local proj_dir = M.create_and_get_data_dir(provider)
81-
return require("pathlib").new(vim.fs.joinpath(proj_dir, provider.issue_number .. ".md"))
93+
return require("pathlib").new(vim.fs.joinpath(proj_dir, "issue_" .. provider.issue_number .. ".md"))
8294
end
8395

8496
return M

0 commit comments

Comments
 (0)