local GhPrUi = {} vim.api.nvim_set_hl(0, "GhPrCommentBg", { bg = "#144212", fg = "#ffffff" }) -- dark green bg, white text local create_window_configuration = function(pr_data) local width = math.floor((vim.o.columns / 3)) local height = math.floor(vim.o.lines / 2) return { title = "Github PR Comments: " .. pr_data.repo_owner .. "/" .. pr_data.repo_name .. " PR-" .. pr_data.number, 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 GhPrUi.create_floating_window = function(pr_data) local config = create_window_configuration(pr_data) 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 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 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 string[] -- FIX: this type is a table with line/original_line/body keys GhPrUi.write_comments_for_line = function(line_num, comments) local bnr = vim.fn.bufnr('%') local ns_id = vim.api.nvim_create_namespace('ghprcomments') local virt_text = {} for _, c in ipairs(comments) do table.insert(virt_text, { c.body, "GhPrCommentBg" }) end vim.api.nvim_buf_set_extmark(bnr, ns_id, line_num, 0, { virt_text = virt_text, virt_text_pos = 'right_align', }) end GhPrUi.clear_namespace_in_curr_buf = function () local ns_ids = vim.api.nvim_get_namespaces() vim.api.nvim_buf_clear_namespace(0, ns_ids['ghprcomments'], 0, -1) end 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) GhPrUi.open_or_switch_buffer(relative_fp) local comments_by_line = {} local first_line = nil for _, c in pairs(GhPrUi.pr_comments[relative_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 -- lua uses 1 base-index numbering, so add here first_line = c.line + 1 end end for line, comments in pairs(comments_by_line) do -- TODO: currently all lines added together and shown, -- let's just show overall comments info and open popup with keymap GhPrUi.write_comments_for_line(line, comments) end vim.api.nvim_win_set_cursor(0, {first_line, 0}) end return GhPrUi