Skip to content

Commit

Permalink
Implement #123: Move to next/prev comment in issue/pr buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
pwntester committed Mar 26, 2021
1 parent 13e9c99 commit 0c9c152
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ Just edit the issue title, description or comments as a regular buffer and use `
| `<space>rr` | add/remove :rocket: reaction |
| `<C-o>` | open issue/pull in browser |
| `<C-r>` | reload current issue/pull |
| `[c` | go to previous comment |
| `]c` | go to next comment |


## Highlight groups
Expand Down
4 changes: 2 additions & 2 deletions lua/octo/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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+)"
Expand Down
66 changes: 66 additions & 0 deletions lua/octo/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,20 @@ function M.apply_buffer_mappings(bufnr, kind)
[[<cmd>lua require'octo.navigation'.go_to_issue()<CR>]],
mapping_opts
)
api.nvim_buf_set_keymap(
bufnr,
"n",
"]c",
[[<cmd>lua require'octo'.next_comment()<CR>]],
mapping_opts
)
api.nvim_buf_set_keymap(
bufnr,
"n",
"[c",
[[<cmd>lua require'octo'.prev_comment()<CR>]],
mapping_opts
)

-- comments
api.nvim_buf_set_keymap(
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions lua/octo/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0c9c152

Please sign in to comment.