From d17a9969b29a2c08d32bf7346c830b6692b288c3 Mon Sep 17 00:00:00 2001 From: Omar Zeghouani Date: Sun, 7 Aug 2022 22:07:14 +0100 Subject: [PATCH] Add setup function * Removed files key in favor of commands * Add wrapper function for vim.notify * Add presets * Add BufWinEnter to caching --- lua/dev_comments/cache.lua | 2 +- lua/dev_comments/comments.lua | 44 ++++++++++------- lua/dev_comments/cycle.lua | 13 +++-- lua/dev_comments/init.lua | 47 +++++++++++++++++++ lua/dev_comments/presets.lua | 42 +++++++++++++++++ lua/dev_comments/utils.lua | 11 ++++- lua/telescope/_extensions/dev_comments.lua | 6 ++- .../_extensions/dev_comments/picker.lua | 21 +++++++-- 8 files changed, 154 insertions(+), 32 deletions(-) create mode 100644 lua/dev_comments/init.lua create mode 100644 lua/dev_comments/presets.lua diff --git a/lua/dev_comments/cache.lua b/lua/dev_comments/cache.lua index cc15d71..bf9334c 100644 --- a/lua/dev_comments/cache.lua +++ b/lua/dev_comments/cache.lua @@ -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") diff --git a/lua/dev_comments/comments.lua b/lua/dev_comments/comments.lua index b218d8f..4cd517b 100644 --- a/lua/dev_comments/comments.lua +++ b/lua/dev_comments/comments.lua @@ -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 @@ -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, @@ -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 @@ -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 diff --git a/lua/dev_comments/cycle.lua b/lua/dev_comments/cycle.lua index 7e26e18..9ebe023 100644 --- a/lua/dev_comments/cycle.lua +++ b/lua/dev_comments/cycle.lua @@ -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 @@ -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 @@ -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 diff --git a/lua/dev_comments/init.lua b/lua/dev_comments/init.lua new file mode 100644 index 0000000..9ff81aa --- /dev/null +++ b/lua/dev_comments/init.lua @@ -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 diff --git a/lua/dev_comments/presets.lua b/lua/dev_comments/presets.lua new file mode 100644 index 0000000..eb83217 --- /dev/null +++ b/lua/dev_comments/presets.lua @@ -0,0 +1,42 @@ +local P = {} + +local setup = function() + vim.keymap.set( + "n", + "DevCommentsTelescopeCurrent", + "lua require('telescope').extensions.dev_comments.current{}", + { silent = true } + ) + vim.keymap.set( + "n", + "DevCommentsTelescopeOpen", + "lua require('telescope').extensions.dev_comments.open{}", + { silent = true } + ) + vim.keymap.set( + "n", + "DevCommentsTelescopeAll", + "lua require('telescope').extensions.dev_comments.all{}", + { silent = true } + ) + vim.keymap.set( + "n", + "DevCommentsCyclePrev", + "lua require('dev_comments.cycle').goto_prev()", + { silent = true } + ) + vim.keymap.set( + "n", + "DevCommentsCycleNext", + "lua require('dev_comments.cycle').goto_next()", + { silent = true } + ) +end + +P.set_default_mappings = function() + setup() + vim.keymap.set("n", "[c", "DevCommentsCyclePrev") + vim.keymap.set("n", "]c", "DevCommentsCycleNext") +end + +return P diff --git a/lua/dev_comments/utils.lua b/lua/dev_comments/utils.lua index 7fce912..2c4c42b 100644 --- a/lua/dev_comments/utils.lua +++ b/lua/dev_comments/utils.lua @@ -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 @@ -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 diff --git a/lua/telescope/_extensions/dev_comments.lua b/lua/telescope/_extensions/dev_comments.lua index 098298b..1adee87 100644 --- a/lua/telescope/_extensions/dev_comments.lua +++ b/lua/telescope/_extensions/dev_comments.lua @@ -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, + }, }) diff --git a/lua/telescope/_extensions/dev_comments/picker.lua b/lua/telescope/_extensions/dev_comments/picker.lua index fc24ec6..fe3cebd 100644 --- a/lua/telescope/_extensions/dev_comments/picker.lua +++ b/lua/telescope/_extensions/dev_comments/picker.lua @@ -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 @@ -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