Skip to content

Commit

Permalink
Add setup function
Browse files Browse the repository at this point in the history
* Removed files key in favor of commands
* Add wrapper function for vim.notify
* Add <Plug> presets
* Add BufWinEnter to caching
  • Loading branch information
ram02z committed Aug 7, 2022
1 parent 22f27a6 commit d17a996
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lua/dev_comments/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ C.register = function()
return
end
local id = vim.api.nvim_create_augroup("DevComments", { clear = true })
vim.api.nvim_create_autocmd({ "BufWritePost" }, {
vim.api.nvim_create_autocmd({ "BufWritePost", "BufWinEnter" }, {
callback = function()
C.reset("current")
C.reset("open")
Expand Down
44 changes: 27 additions & 17 deletions lua/dev_comments/comments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local finder = function(bufnr, results, opts)
results = results or {}
local status, root_lang_tree = pcall(vim.treesitter.get_parser, bufnr)
if not status then
vim.notify(root_lang_tree, vim.log.levels.WARN)
utils.notify(root_lang_tree, vim.log.levels.WARN)
return results
end

Expand All @@ -34,13 +34,22 @@ local finder = function(bufnr, results, opts)
local root_node = tree:root()
for child_node in root_node:iter_children() do
if child_node:named() and child_node:type() == "tag" then
local tag = utils.get_node_text(child_node:named_child(0), bufnr)
local user = utils.get_node_text(child_node:named_child(1), bufnr)
local tag = ""
local user = ""
for tag_child_node in child_node:iter_children() do
if tag_child_node:named() then
if tag_child_node:type() == "name" then
tag = utils.get_node_text(tag_child_node, bufnr)
elseif tag_child_node:type() == "user" then
user = utils.get_node_text(tag_child_node, bufnr)
end
end
end
if
(#opts.tags == 0 or vim.tbl_contains(opts.tags, tag))
and (#opts.users == 0 or vim.tbl_contains(opts.users, user))
then
-- FIXME: if a comment node contains two tag nodes, we will get the text of the first node
-- FIXME: if a comment node contains two tag nodes, it will get the text of the comment node
table.insert(results, {
node = root_node,
tag = tag,
Expand All @@ -55,26 +64,27 @@ local finder = function(bufnr, results, opts)
return results
end

C.generate = function(opts)
local set_opts = function(files, opts)
local config = require("dev_comments").config
opts.hidden = vim.F.if_nil(opts.hidden, config.telescope[files].hidden)
opts.depth = vim.F.if_nil(opts.depth, config.telescope[files].depth)
opts.tags = vim.split(opts.tags or config.telescope[files].tags, ",", { trimempty = true })
opts.users = vim.split(opts.users or config.telescope[files].users, ",", { trimempty = true })
end

C.generate = function(files, opts)
local has_comments_parser = vim.treesitter.require_language("comment", nil, true)
if not has_comments_parser then
error("This plugin requires 'comment' parser to be installed. Try running 'TSInstall comment'")
end

cache.register()

opts = opts or {}
-- open, current, all
opts.files = vim.F.if_nil(opts.files, "current") -- TODO: rename this option
opts.cwd = vim.F.if_nil(opts.cwd, vim.loop.cwd())
opts.hidden = vim.F.if_nil(opts.hidden, false)
opts.depth = vim.F.if_nil(opts.depth, 3)
opts.tags = vim.split(opts.tags or "", ",", { trimempty = true })
opts.users = vim.split(opts.users or "", ",", { trimempty = true })
opts.files = files
set_opts(files, opts)

local results = cache.get(opts)
if results then
vim.notify("cache hit", vim.log.levels.DEBUG)
utils.notify("cache hit", vim.log.levels.DEBUG)
return results
end

Expand All @@ -98,14 +108,14 @@ C.generate = function(opts)
if vim.api.nvim_buf_is_loaded(bufnr) then
local hl = vim.treesitter.highlighter.active[bufnr]
if not hl then
vim.notify("Treesitter not active on bufnr: " .. bufnr, vim.log.levels.DEBUG)
utils.notify("Treesitter not active on bufnr: " .. bufnr, vim.log.levels.DEBUG)
end
results = finder(bufnr, results, opts)
end
end

if not cache.add(results, opts) then
vim.notify("couldn't add to cache. Cache is either not registered or files is invalid.")
utils.notify("couldn't add to cache. Cache is either not registered or files is invalid.")
end

return results
Expand Down
13 changes: 6 additions & 7 deletions lua/dev_comments/cycle.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
local C = {}

local comments = require("dev_comments.comments")
local utils = require("dev_comments.utils")

local function next_dev_comment(wrap, opts, forward)
if opts and opts.files ~= "current" then
error("opt.files must be set to current")
end

wrap = vim.F.if_nil(wrap, false)
local config = require("dev_comments").config
wrap = vim.F.if_nil(wrap, config.cycle.wrap)

-- assumes results are in ascending line order
local results = comments.generate(opts)
local results = comments.generate("current", opts)
if #results == 0 then
return
end
Expand Down Expand Up @@ -41,6 +39,7 @@ local function next_dev_comment(wrap, opts, forward)
end

if wrap then
utils.notify("Reached the last node. Wrapping around.", vim.log.levels.INFO)
if forward then
node_row, node_col = results[1].node:range()
else
Expand All @@ -56,7 +55,7 @@ local function moveto_pos(pos)
local win_id = vim.api.nvim_get_current_win()

if not pos then
vim.notify("No dev comment found", vim.log.levels.WARN)
utils.notify("No dev comments to move to", vim.log.levels.WARN)
return
end

Expand Down
47 changes: 47 additions & 0 deletions lua/dev_comments/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local dev_comments = {}

dev_comments.config = {
debug = false,
default_mappings = true,
cache = true,
telescope = {
load = true,
current = {
hidden = false,
depth = 3,
tags = "",
users = "",
},
open = {
hidden = false,
depth = 3,
tags = "",
users = "",
},
all = {
hidden = false,
depth = 3,
tags = "",
users = "",
},
},
cycle = {
wrap = true,
},
}

dev_comments.setup = function(config)
config = config or {}
dev_comments.config = vim.tbl_extend("force", dev_comments.config, config)
if dev_comments.config.default_mappings then
require("dev_comments.presets").set_default_mappings()
end
if dev_comments.config.telescope.load then
require("telescope").load_extension("dev_comments")
end
if dev_comments.config.cache then
require("dev_comments.cache").register()
end
end

return dev_comments
42 changes: 42 additions & 0 deletions lua/dev_comments/presets.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local P = {}

local setup = function()
vim.keymap.set(
"n",
"<Plug>DevCommentsTelescopeCurrent",
"<cmd>lua require('telescope').extensions.dev_comments.current{}<cr>",
{ silent = true }
)
vim.keymap.set(
"n",
"<Plug>DevCommentsTelescopeOpen",
"<cmd>lua require('telescope').extensions.dev_comments.open{}<cr>",
{ silent = true }
)
vim.keymap.set(
"n",
"<Plug>DevCommentsTelescopeAll",
"<cmd>lua require('telescope').extensions.dev_comments.all{}<cr>",
{ silent = true }
)
vim.keymap.set(
"n",
"<Plug>DevCommentsCyclePrev",
"<cmd>lua require('dev_comments.cycle').goto_prev()<cr>",
{ silent = true }
)
vim.keymap.set(
"n",
"<Plug>DevCommentsCycleNext",
"<cmd>lua require('dev_comments.cycle').goto_next()<cr>",
{ silent = true }
)
end

P.set_default_mappings = function()
setup()
vim.keymap.set("n", "[c", "<Plug>DevCommentsCyclePrev")
vim.keymap.set("n", "]c", "<Plug>DevCommentsCycleNext")
end

return P
11 changes: 9 additions & 2 deletions lua/dev_comments/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ local comment_tag_highlight = {
["BUG"] = "TSDanger",
}

U.notify = function(msg, level, opts)
local config = require("dev_comments").config
if config.debug then
vim.notify(msg, level, opts)
end
end

U.get_highlight_by_tag = function(tag)
local hl_name = comment_tag_highlight[tag]
if not hl_name then
Expand Down Expand Up @@ -70,13 +77,13 @@ U.filter_buffers = function(buffer_handles, cwd)
local P = require("plenary.path")
local status, dir = pcall(P.new, cwd)
if not status then
vim.notify("cwd is invalid")
U.notify("cwd is invalid", vim.log.levels.WARN)
return buffer_handles
end

-- TODO: should this be an error?
if not dir:is_dir() then
vim.notify("cwd needs to be a valid directory", vim.log.levels.WARN)
U.notify("cwd needs to be a valid directory", vim.log.levels.WARN)
return buffer_handles
end

Expand Down
6 changes: 5 additions & 1 deletion lua/telescope/_extensions/dev_comments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ if not has_telescope then
end

return telescope.register_extension({
exports = { dev_comments = picker },
exports = {
current = picker.current,
open = picker.open,
all = picker.all,
},
})
21 changes: 17 additions & 4 deletions lua/telescope/_extensions/dev_comments/picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ local dc_picker = {}

local entry_maker = require("telescope._extensions.dev_comments.entry_maker")
local comments = require("dev_comments.comments")
local utils = require("dev_comments.utils")

local create = function(results, opts)
if vim.tbl_isempty(results) then
vim.notify("No dev comments found", vim.log.levels.INFO)
utils.notify("No dev comments found", vim.log.levels.INFO)
end

local conf = require("telescope.config").values
Expand All @@ -27,9 +28,21 @@ local create = function(results, opts)
:find()
end

dc_picker.picker = function(opts)
local results = comments.generate(opts)
local picker = function(files, opts)
local results = comments.generate(files, opts)
create(results, opts)
end

return dc_picker.picker
dc_picker.current = function(opts)
picker("current", opts)
end

dc_picker.open = function(opts)
picker("open", opts)
end

dc_picker.all = function(opts)
picker("all", opts)
end

return dc_picker

0 comments on commit d17a996

Please sign in to comment.