Skip to content

Commit

Permalink
feat: Add config to wrap jump_next and jump_prev
Browse files Browse the repository at this point in the history
  • Loading branch information
nishantpillai5 committed Jun 21, 2024
1 parent 51e10f8 commit 308a45c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ Todo comes with the following defaults:
pattern = [[\b(KEYWORDS):]], -- ripgrep regex
-- pattern = [[\b(KEYWORDS)\b]], -- match without the extra colon. You'll likely get false positives
},
-- whether to wrap to the start (or end) of the file when searching
wrap = false,
}

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

-- You can jump circularly

vim.keymap.set("n", "]t", function()
require("todo-comments").jump_next({wrap = true})
end, { desc = "Next todo comment" })

-- You can jump to the last todo comment

vim.keymap.set("n", "]T", function()
require("todo-comments").jump_next({last = true})
end, { desc = "Last todo comment" })

```

## 🚀 Usage
Expand Down
2 changes: 2 additions & 0 deletions lua/todo-comments/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ local defaults = {
pattern = [[\b(KEYWORDS):]], -- ripgrep regex
-- pattern = [[\b(KEYWORDS)\b]], -- match without the extra colon. You'll likely get false positives
},
-- whether to wrap to the start (or end) of the file when searching
wrap = false,
}

M._options = nil
Expand Down
45 changes: 38 additions & 7 deletions lua/todo-comments/jump.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ local function jump(up, opts)
local buf = vim.api.nvim_get_current_buf()

local pos = vim.api.nvim_win_get_cursor(win)
local line_count = vim.api.nvim_buf_line_count(buf)

local from = pos[1] + 1
local to = vim.api.nvim_buf_line_count(buf)
local to
local from

if up then
if opts.last and up then
from = 1
to = line_count
up = not up
elseif opts.last and not up then
from = line_count
to = 1
up = not up
elseif up then
from = pos[1] - 1
to = 1
else
from = pos[1] + 1
to = line_count
end

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

if kw then
vim.api.nvim_win_set_cursor(win, { l, start - 1 })
return
return true
end
end
util.warn("No more todo comments to jump to")
return false
end

function M.next(opts)
jump(false, opts)
if jump(false, opts) then
return
end

if config.options.wrap or opts.wrap then
opts.last = true
jump(true, opts)
else
util.warn("No more todo comments to jump to")
end
end

function M.prev(opts)
jump(true, opts)
if jump(true, opts) then
return
end

if config.options.wrap or opts.wrap then
opts.last = true
jump(false, opts)
else
util.warn("No more todo comments to jump to")
end
end

return M

0 comments on commit 308a45c

Please sign in to comment.