Something went wrong. Try again.
Neovim plugin for working with Github pull request comments
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178---@class GhPrUi---@field buf integer? -- buf of the ui menu at startup---@field win integer? -- win of the ui menu at startup---@field pr_comments table<string, Comment[]>? -- 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
---@param pr_data PrData---@return vim.api.keyset.win_configlocal 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
---@param pr_data PrData---@return FloatingWindowGhPrUi.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", "<CR>", 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
---@return string[] files Array of formatted file strings---@return integer count Total number of filesGhPrUi.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, countend
---@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 foundGhPrUi.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 endend
---@param filepath string Relative path to the fileGhPrUi.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)) endend
---@param line_num integer---@param comments Comment[] Array of Comment tablesGhPrUi.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
---Clears the 'ghprcomments' namespace in the current bufferGhPrUi.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) endend
---Handles selection of a file from the PR comments list---Closes the floating window, opens the selected file, and displays commentsGhPrUi.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.curr_rel_fp = relative_fp 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 first_line = c.line 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 - 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", "<C-n>", function() GhPrUi.cycle_next() 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 fileGhPrUi.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