From fcce64dc302cb77d0f3efe5420e5d6332982b965 Mon Sep 17 00:00:00 2001 From: Nishant Pillai Date: Fri, 21 Jun 2024 13:23:07 +0200 Subject: [PATCH] feat: Add config to wrap jump_next and jump_prev --- README.md | 14 +++++++++++ lua/todo-comments/config.lua | 2 ++ lua/todo-comments/jump.lua | 45 ++++++++++++++++++++++++++++++------ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 81f82d6..46cb579 100644 --- a/README.md +++ b/README.md @@ -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, } ``` @@ -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 diff --git a/lua/todo-comments/config.lua b/lua/todo-comments/config.lua index 7ea43d9..b9059ad 100644 --- a/lua/todo-comments/config.lua +++ b/lua/todo-comments/config.lua @@ -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 diff --git a/lua/todo-comments/jump.lua b/lua/todo-comments/jump.lua index 4d4fe40..f729c7c 100644 --- a/lua/todo-comments/jump.lua +++ b/lua/todo-comments/jump.lua @@ -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 @@ -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