Skip to content

Commit fcce64d

Browse files
feat: Add config to wrap jump_next and jump_prev
1 parent 51e10f8 commit fcce64d

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ Todo comes with the following defaults:
109109
pattern = [[\b(KEYWORDS):]], -- ripgrep regex
110110
-- pattern = [[\b(KEYWORDS)\b]], -- match without the extra colon. You'll likely get false positives
111111
},
112+
-- whether to wrap to the start (or end) of the file when jumping
113+
wrap = false,
112114
}
113115

114116
```
@@ -132,6 +134,18 @@ vim.keymap.set("n", "]t", function()
132134
require("todo-comments").jump_next({keywords = { "ERROR", "WARNING" }})
133135
end, { desc = "Next error/warning todo comment" })
134136

137+
-- You can jump circularly
138+
139+
vim.keymap.set("n", "]t", function()
140+
require("todo-comments").jump_next({wrap = true})
141+
end, { desc = "Next todo comment" })
142+
143+
-- You can jump to the last todo comment
144+
145+
vim.keymap.set("n", "]T", function()
146+
require("todo-comments").jump_next({last = true})
147+
end, { desc = "Last todo comment" })
148+
135149
```
136150

137151
## 🚀 Usage

lua/todo-comments/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ local defaults = {
7878
pattern = [[\b(KEYWORDS):]], -- ripgrep regex
7979
-- pattern = [[\b(KEYWORDS)\b]], -- match without the extra colon. You'll likely get false positives
8080
},
81+
-- whether to wrap to the start (or end) of the file when jumping
82+
wrap = false,
8183
}
8284

8385
M._options = nil

lua/todo-comments/jump.lua

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@ local function jump(up, opts)
1313
local buf = vim.api.nvim_get_current_buf()
1414

1515
local pos = vim.api.nvim_win_get_cursor(win)
16+
local line_count = vim.api.nvim_buf_line_count(buf)
1617

17-
local from = pos[1] + 1
18-
local to = vim.api.nvim_buf_line_count(buf)
18+
local to
19+
local from
1920

20-
if up then
21+
if opts.last and up then
22+
from = 1
23+
to = line_count
24+
up = not up
25+
elseif opts.last and not up then
26+
from = line_count
27+
to = 1
28+
up = not up
29+
elseif up then
2130
from = pos[1] - 1
2231
to = 1
32+
else
33+
from = pos[1] + 1
34+
to = line_count
2335
end
2436

2537
for l = from, to, up and -1 or 1 do
@@ -38,17 +50,36 @@ local function jump(up, opts)
3850

3951
if kw then
4052
vim.api.nvim_win_set_cursor(win, { l, start - 1 })
41-
return
53+
return true
4254
end
4355
end
44-
util.warn("No more todo comments to jump to")
56+
return false
4557
end
4658

4759
function M.next(opts)
48-
jump(false, opts)
60+
if jump(false, opts) then
61+
return
62+
end
63+
64+
if config.options.wrap or opts.wrap then
65+
opts.last = true
66+
jump(true, opts)
67+
else
68+
util.warn("No more todo comments to jump to")
69+
end
4970
end
71+
5072
function M.prev(opts)
51-
jump(true, opts)
73+
if jump(true, opts) then
74+
return
75+
end
76+
77+
if config.options.wrap or opts.wrap then
78+
opts.last = true
79+
jump(false, opts)
80+
else
81+
util.warn("No more todo comments to jump to")
82+
end
5283
end
5384

5485
return M

0 commit comments

Comments
 (0)