Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add config to wrap jump_next and jump_prev #280

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 jumping
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 jumping
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