---@class PrUi ---@field win integer? -- win of the ui menu at startup ---@field pr_data PrData? ---@field fp_comments table? -- Map of filepath to array of comments summary ---@field pr_comments table? -- Map of filepath to array of comments ---@field curr_rel_fp string? ---@field curr_line integer? ---@field first_line integer? local PrUi = {} vim.api.nvim_set_hl(0, 'GhPrCommentBg', { bg = '#144212', fg = '#ffffff' }) -- dark green bg, white text local sep = '' ---Takes comments, wraps with useful data and puts it in pretty markdown ---Does not apply the filepath headers ---@param comments Comment[] ---@return string[] bodies local function format_comments(comments) local utils = require('gh-pr-comments.utils') local bodies = {} for _, c in ipairs(comments) do table.insert(bodies, sep) -- add user & datetime here table.insert( bodies, c.user_login .. '(' .. c.updated_at .. ') [L' .. c.original_line .. ']:' ) -- add diff local diff = utils.format_diff_hunk(c.diff_hunk) for _, line in pairs(diff) do table.insert(bodies, line) end -- add comments local comment_msgs = utils.format_comments(c.body) for _, line in pairs(comment_msgs) do table.insert(bodies, line) end end table.insert(bodies, sep) return bodies end ---Generates the contents for comments split local function gen_pr_comments_content() local split_contents = {} for fp, comments in pairs(PrUi.pr_comments) do local fp_header = '# ' .. fp .. ': ' .. #comments .. ' comment(s)' table.insert(split_contents, sep) table.insert(split_contents, fp_header) for _, c in pairs(format_comments(comments)) do table.insert(split_contents, c) end end return split_contents end PrUi.create_split = function() local Split = require('nui.split') local event = require('nui.utils.autocmd').event local split = Split { relative = 'editor', position = 'right', size = '40%', enter = false, } vim.api.nvim_buf_set_lines(split.bufnr, 0, 0, true, gen_pr_comments_content()) -- mount/open the component split:mount() vim.api.nvim_set_option_value('filetype', 'markdown', { buf = split.bufnr }) vim.wo[split.winid].wrap = true vim.wo[split.winid].linebreak = true -- unmount component when cursor leaves buffer split:on(event.BufLeave, function() split:unmount() end) end return PrUi