From 0c9c1523b160a8e1c7fb92128370f94f1c8548dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvaro=20Mun=CC=83oz?= Date: Fri, 26 Mar 2021 23:07:34 +0100 Subject: [PATCH] Implement #123: Move to next/prev comment in issue/pr buffer --- README.md | 2 ++ lua/octo/constants.lua | 4 +-- lua/octo/init.lua | 66 ++++++++++++++++++++++++++++++++++++++++++ lua/octo/util.lua | 10 +++++++ 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 703a8915..5b2ff65a 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,8 @@ Just edit the issue title, description or comments as a regular buffer and use ` | `rr` | add/remove :rocket: reaction | | `` | open issue/pull in browser | | `` | reload current issue/pull | +| `[c` | go to previous comment | +| `]c` | go to next comment | ## Highlight groups diff --git a/lua/octo/constants.lua b/lua/octo/constants.lua index 85a3b620..da7548da 100644 --- a/lua/octo/constants.lua +++ b/lua/octo/constants.lua @@ -18,8 +18,8 @@ M.OCTO_EVENT_VT_NS = api.nvim_create_namespace("octo_details_vt") M.NO_BODY_MSG = "No description provided." -M.LONG_ISSUE_PATTERN = "%s([^/]+/[^#]+)#(%d+)%s" -M.SHORT_ISSUE_PATTERN = "%s#(%d+)%s" +M.LONG_ISSUE_PATTERN = "([A-Za-z0-9_. -]+/[A-Za-z0-9_. -]+)#(%d+)" +M.SHORT_ISSUE_PATTERN = "%s#(%d+)" M.URL_ISSUE_PATTERN = ("[htps]+://[^/]+/([^/]+/[^/]+)/([pulisue]+)/(%d+)") M.USER_PATTERN = "@(%S+)" diff --git a/lua/octo/init.lua b/lua/octo/init.lua index fdb7e75c..5bece71a 100644 --- a/lua/octo/init.lua +++ b/lua/octo/init.lua @@ -718,6 +718,20 @@ function M.apply_buffer_mappings(bufnr, kind) [[lua require'octo.navigation'.go_to_issue()]], mapping_opts ) + api.nvim_buf_set_keymap( + bufnr, + "n", + "]c", + [[lua require'octo'.next_comment()]], + mapping_opts + ) + api.nvim_buf_set_keymap( + bufnr, + "n", + "[c", + [[lua require'octo'.prev_comment()]], + mapping_opts + ) -- comments api.nvim_buf_set_keymap( @@ -902,4 +916,56 @@ function M.on_cursor_hold() ) end +function M.next_comment() + local bufnr = api.nvim_get_current_buf() + if vim.bo[bufnr].ft == "octo_issue" then + local cursor = api.nvim_win_get_cursor(0) + local current_line = cursor[1] + local lines = util.get_sorted_comment_lines() + lines = util.tbl_slice(lines, 3, #lines) + local target + if current_line < lines[1]+1 then + -- go to first comment + target = lines[1]+1 + elseif current_line > lines[#lines]+1 then + -- do not move + target = current_line - 1 + else + for i=#lines, 1, -1 do + if current_line >= lines[i]+1 then + target = lines[i+1]+1 + break + end + end + end + api.nvim_win_set_cursor(0, {target+1, cursor[2]}) + end +end + +function M.prev_comment() + local bufnr = api.nvim_get_current_buf() + if vim.bo[bufnr].ft == "octo_issue" then + local cursor = api.nvim_win_get_cursor(0) + local current_line = cursor[1] + local lines = util.get_sorted_comment_lines() + lines = util.tbl_slice(lines, 3, #lines) + local target + if current_line > lines[#lines]+2 then + -- go to last comment + target = lines[#lines]+1 + elseif current_line <= lines[1]+2 then + -- do not move + target = current_line - 1 + else + for i=1, #lines, 1 do + if current_line <= lines[i]+2 then + target = lines[i-1]+1 + break + end + end + end + api.nvim_win_set_cursor(0, {target+1, cursor[2]}) + end +end + return M diff --git a/lua/octo/util.lua b/lua/octo/util.lua index b595b3f9..0e1b61c1 100644 --- a/lua/octo/util.lua +++ b/lua/octo/util.lua @@ -689,4 +689,14 @@ function M.count_reactions(reaction_groups) return reactions_count end +function M.get_sorted_comment_lines(bufnr) + local lines = {} + local marks = api.nvim_buf_get_extmarks(bufnr, constants.OCTO_COMMENT_NS, 0, -1, {details = true}) + for _, mark in ipairs(marks) do + table.insert(lines, mark[2]) + end + table.sort(lines) + return lines +end + return M