Skip to content

Commit

Permalink
feat: Improve rg and highlight pattern matching
Browse files Browse the repository at this point in the history
- Use \b to narrow down ripgrep results
- Highlight only keywords inside comments
  • Loading branch information
mike325 committed May 14, 2021
1 parent b5cdae7 commit 7a00efb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
## ⚡️ Requirements

* Neovim >= 0.5.0
* [ripgrep](https://github.com/BurntSushi/ripgrep) is used for searching.
* a [patched font](https://www.nerdfonts.com/) for the icons, or change them to simple ASCII characters
* optional:
- [ripgrep](https://github.com/BurntSushi/ripgrep) is used for searching.
- [Trouble](https://github.com/folke/trouble.nvim)
- [Telescope](https://github.com/nvim-telescope/telescope.nvim)

Expand Down
6 changes: 4 additions & 2 deletions lua/todo-comments/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ local defaults = {
},
-- regex that will be used to match keywords.
-- don't replace the (KEYWORDS) placeholder
pattern = "(KEYWORDS):",
rg_pattern = '\\b(KEYWORDS):', -- rust regex
hl_pattern = '<(KEYWORDS)>:', -- viml regex, this substitudes %s inside commentstring option
-- pattern = "(KEYWORDS)", -- match without the extra colon. You'll likely get false positives
-- pattern = "-- (KEYWORDS):", -- only match in lua comments
}
Expand Down Expand Up @@ -80,7 +81,8 @@ function M._setup()
end
end
local tags = table.concat(vim.tbl_keys(M.keywords), "|")
M.rg_regex = M.options.pattern:gsub("KEYWORDS", tags)
M.rg_regex = M.options.rg_pattern:gsub("KEYWORDS", tags)
M.hl_regex = M.options.hl_pattern:gsub("KEYWORDS", tags)
M.colors()
M.signs()
require("todo-comments.highlight").start()
Expand Down
11 changes: 8 additions & 3 deletions lua/todo-comments/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ M.wins = {}
-- WARNING: ???
-- FIX ddddd

function M.match(str)
local m = vim.fn.matchlist(str, "\\v\\C" .. Config.rg_regex)
function M.match(str, pattern)
pattern = pattern or Config.hl_regex
local m = vim.fn.matchlist(str, [[\v\C]] .. pattern)
if m and m[2] then
local kw = m[2]
local start = str:find(kw)
Expand All @@ -34,9 +35,13 @@ function M.highlight(buf, first, last)
end

local lines = vim.api.nvim_buf_get_lines(buf, first, last + 1, false)
local comment_str = vim.bo.commentstring:gsub('%s+', '')
local wrap = comment_str:sub(#comment_str - 1, #comment_str) ~= '%s'
local pattern = comment_str:gsub('%%s', [[\s*]]..(wrap and Config.hl_regex..'.*' or Config.hl_regex))
pattern = vim.fn.escape(pattern, '%')

for l, line in ipairs(lines) do
local start, finish, kw = M.match(line)
local start, finish, kw = M.match(line, pattern)
local lnum = first + l - 1

if kw then
Expand Down
8 changes: 5 additions & 3 deletions plugin/todo.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

command! TodoQuickFix lua require("todo-comments.search").setqflist()
command! TodoTelescope Telescope todo-comments todo
command! TodoTrouble Trouble todo
if executable('rg')
command! TodoQuickFix lua require("todo-comments.search").setqflist()
command! TodoTelescope Telescope todo-comments todo
command! TodoTrouble Trouble todo
endif

0 comments on commit 7a00efb

Please sign in to comment.