Something went wrong. Try again.
Neovim plugin for working with Github pull request comments
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266---@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<string, FpComments>? -- Map of filepath to array of comments summary---@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
---Returns the first line, plus all comments organized by line---@param rel_fp string---@return integer, table<integer, Comment[]>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_lineend
---comment---@param fp string---@param line_num integer---@return Commentlocal 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 endend
---@param comments Comment[]---@return string[] bodieslocal 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 bodiesend
---@param title string?---@return vim.api.keyset.win_configlocal 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 FloatingWindowGhPrUi.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", "<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
---Create the buffer with the comments for the line inspected with <C-i>.---@param fp string---@return FloatingWindowGhPrUi.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 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_len integerGhPrUi.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 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.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", "<C-n>", function() GhPrUi.cycle_next() end, { buffer = 0, silent = true }) vim.keymap.set("n", "<C-i>", 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 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