---@class GhPrUi ---@field buf integer? -- buf of the ui menu at startup ---@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 GhPrUi = {} vim.api.nvim_set_hl(0, "GhPrCommentBg", { bg = "#144212", fg = "#ffffff" }) -- dark green bg, white text ---Returns the first line, plus all comments organized by line ---@param rel_fp string ---@return integer, table local function sort_comments_by_line(rel_fp) local comments_by_line = {} local first_line = nil for _, c in pairs(GhPrUi.pr_comments[rel_fp]) do comments_by_line[c.line] = comments_by_line[c.line] or {} table.insert(comments_by_line[c.line], c) if first_line == nil then first_line = c.line end end return first_line, comments_by_line end ---comment ---@param fp string ---@param line_num integer ---@return Comment local function get_comment_for_line_num(fp, line_num) for _, c in pairs(GhPrUi.pr_comments[fp]) do if line_num == c.line then return c end end end ---@param comments Comment[] ---@return string[] bodies local function format_comments(comments) local bodies = {} local sep = string.rep('-', math.floor((vim.o.columns / 3)) - 4) for _, c in ipairs(comments) do table.insert(bodies, sep) table.insert(bodies, c.user_login .. '(' .. c.updated_at .. '):') if string.find(c.body, "\n") then for _, line in pairs(require("gh-pr-comments.utils").split_newlines(c.body)) do table.insert(bodies, line) end else -- or add the whole string if no newlines found table.insert(bodies, c.body) end end table.insert(bodies, sep) return bodies end ---@param title string? ---@return vim.api.keyset.win_config local create_window_configuration = function(title) local width = math.floor((vim.o.columns / 3)) local height = math.floor(vim.o.lines / 2) if not title then title = "Github PR Comments: " .. GhPrUi.pr_data.repo_owner .. "/" .. GhPrUi.pr_data.repo_name .. " PR-" .. GhPrUi.pr_data.number end return { title = title, relative = "editor", width = width, height = height, style = "minimal", border = "rounded", row = math.floor(((vim.o.lines - height) / 2) - 1), col = math.floor((vim.o.columns - width) / 2), } end ---@return FloatingWindow GhPrUi.create_main_window = function() local config = create_window_configuration() local buf = vim.api.nvim_create_buf(false, true) -- no file, scratch buffer local win = vim.api.nvim_open_win(buf, true, config) -- add keymap for selecting file from list vim.keymap.set("n", "", function() GhPrUi.run_select_command() end, { buffer = buf, silent = true }) -- add keymap for closing scratch buffer vim.keymap.set("n", "q", function() vim.api.nvim_win_close(win, true) end, { buffer = buf, }) GhPrUi.buf = buf GhPrUi.win = win return { buf = buf, win = win } end ---Create the buffer with the comments for the line inspected with . ---@param fp string ---@return FloatingWindow GhPrUi.create_comment_window = function(fp) local idx = vim.fn.line(".") local diff_hunk = get_comment_for_line_num(fp, idx).diff_hunk local _, comments_by_line = sort_comments_by_line(fp) local comments = comments_by_line[idx] local title = fp .. ": " .. #comments .. " comment(s)" local config = create_window_configuration(title) local buf = vim.api.nvim_create_buf(false, true) -- no file, scratch buffer vim.api.nvim_set_option_value('filetype', 'markdown', { buf = buf }) local win = vim.api.nvim_open_win(buf, true, config) vim.wo[win].wrap = true vim.wo[win].linebreak = true local buffer_contents = { "```diff" } local diff_hunk_lines = vim.tbl_filter(function(s) return s:match("^[%+%-]") end, require('gh-pr-comments.utils').split_newlines(diff_hunk)) for _, diff_line in pairs(diff_hunk_lines) do table.insert(buffer_contents, diff_line) end table.insert(buffer_contents, "```") for _, c in pairs(format_comments(comments)) do table.insert(buffer_contents, c) end vim.api.nvim_buf_set_lines(buf, 0, 0, true, buffer_contents) -- add keymap for closing scratch buffer vim.keymap.set("n", "q", function() vim.api.nvim_win_close(win, true) end, { buffer = buf, }) GhPrUi.buf = buf GhPrUi.win = win return { buf = buf, win = win } end ---@return string[] files Array of formatted file strings ---@return integer count Total number of files GhPrUi.create_ui_list = function() local files = {} local count = 0 for fp, comments in pairs(GhPrUi.pr_comments) do local fp_ui = fp .. " - " .. #comments .. " comment(s)" table.insert(files, fp_ui) count = count + 1 end return files, count end ---@param n integer The 1-based index of the key to retrieve ---@return string|nil filepath The filepath at index n, or nil if not found GhPrUi.get_n_key = function(n) local i = 0 for fp, _ in pairs(GhPrUi.pr_comments) do i = i + 1 if i == n then return fp end end end ---@param filepath string Relative path to the file GhPrUi.open_or_switch_buffer = function(filepath) local abs_path = vim.fn.fnamemodify(filepath, ":p") local bufnr = nil for _, buf in ipairs(vim.api.nvim_list_bufs()) do if vim.api.nvim_buf_is_loaded(buf) then local buf_path = vim.api.nvim_buf_get_name(buf) if buf_path == abs_path then bufnr = buf break end end end if bufnr then vim.api.nvim_set_current_buf(bufnr) else vim.api.nvim_command("edit " .. vim.fn.fnameescape(filepath)) end end ---@param line_num integer ---@param comments_len integer GhPrUi.write_summary_for_line = function(line_num, comments_len) local bnr = vim.fn.bufnr('%') local ns_id = vim.api.nvim_create_namespace('ghprcomments') local virt_text = { { comments_len .. " comment(s)", "GhPrCommentBg" } } vim.api.nvim_buf_set_extmark(bnr, ns_id, line_num, 0, { virt_text = virt_text, virt_text_pos = 'right_align', }) end ---Clears the 'ghprcomments' namespace in the current buffer GhPrUi.clear_namespace_in_curr_buf = function() local ns_id = vim.api.nvim_get_namespaces()['ghprcomments'] if ns_id ~= nil then vim.api.nvim_buf_clear_namespace(0, ns_id, 0, -1) end end ---Handles selection of a file from the PR comments list ---Closes the floating window, opens the selected file, and displays comments GhPrUi.run_select_command = function() local idx = vim.fn.line(".") vim.api.nvim_win_close(GhPrUi.win, true) -- close the GhPrUi win local relative_fp = GhPrUi.get_n_key(idx) if relative_fp == nil then require('gh-pr-comments.utils').err_notify('Could not find the file in menu') return end GhPrUi.clear_namespace_in_curr_buf() GhPrUi.curr_rel_fp = relative_fp GhPrUi.open_or_switch_buffer(relative_fp) local first_line, comments_by_line = sort_comments_by_line(relative_fp) for line, comments in pairs(comments_by_line) do GhPrUi.write_summary_for_line(line - 1, #comments) end GhPrUi.first_line = first_line GhPrUi.curr_line = first_line vim.api.nvim_win_set_cursor(0, { first_line, 0 }) vim.keymap.set("n", "", function() GhPrUi.cycle_next() end, { buffer = 0, silent = true }) vim.keymap.set("n", "", function() GhPrUi.create_comment_window(relative_fp) end, { buffer = 0, silent = true }) end -- Handles cycling to the next comment when keymap is pressed -- Goes back to the first comment in the same file GhPrUi.cycle_next = function() for _, c in pairs(GhPrUi.pr_comments[GhPrUi.curr_rel_fp]) do if c.line > GhPrUi.curr_line then vim.api.nvim_win_set_cursor(0, { c.line, 0 }) GhPrUi.curr_line = c.line return end end GhPrUi.curr_line = GhPrUi.first_line vim.api.nvim_win_set_cursor(0, { GhPrUi.first_line, 0 }) end return GhPrUi