Skip to content
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
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ in order to use this plugin you need to set up treesiter

[nvim-treesitter/nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter)

Recommended Packer:
Packer:

```lua
use "Djancyp/better-comments.nvim"
```

Lazy.nvim:
```lua
return {
"Djancyp/better-comments.nvim",
opts = {}
}
```

### Setup
```lua
require('better-comment').Setup()
require('better-comments').setup()
```

## Configs
Expand Down Expand Up @@ -56,7 +65,7 @@ tags = {
```
### Overwrite defaults or add new Config
```lua
require('better-comment').Setup({
require('better-comments').setup({
tags = {
// TODO will overwrite
{
Expand Down
4 changes: 4 additions & 0 deletions lua/better-comment.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local msg =
debug.traceback("`better-comment` module is deprecated. Please use `better-comments` instead.", 2)
vim.notify(msg, vim.log.levels.WARN, { title = "better-comments", once = true })
return require("better-comments")
85 changes: 49 additions & 36 deletions lua/better-comment/init.lua → lua/better-comments/init.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
-- TODO: don't having to resave to remove extmark
-- TODO: see the others TODO

---@class (exact) CommentHighlight
---@field name string
---@field fg string?
---@field bg string?
---@field bold boolean?
---@field underline boolean?
---@field virtual_text string?

local M = {}

local api = vim.api
local cmd = vim.api.nvim_create_autocmd
local treesitter = vim.treesitter
local api, treesitter = vim.api, vim.treesitter
local au = api.nvim_create_autocmd
---@class BetterCommentsConfig
local opts = {
default = true,
---@type CommentHighlight[]
tags = {
{
name = "TODO",
Expand Down Expand Up @@ -36,27 +45,47 @@ local opts = {
}


M.Setup = function(config)
if config and config.default==false then
---@param bufnr integer
---@param filetype string
local function Get_root(bufnr, filetype)
local parser = treesitter.get_parser(bufnr, filetype, {})
local tree = parser:parse()[1]
return tree:root()
end

---@param list CommentHighlight[]
local function Create_hl(list)
for id, hl in ipairs(list) do
api.nvim_set_hl(0, tostring(id), {
fg = hl.fg,
bg = hl.bg,
bold = hl.bold,
underline = hl.underline,
})
end
end

---@param config? BetterCommentsConfig
function M.setup(config)
config = config or {}
if config.default==false then
opts.tags = {}
end
if config and config.tags then
if config.tags then
opts.tags = vim.tbl_deep_extend("force", opts.tags, config.tags or {})
end

local augroup = vim.api.nvim_create_augroup("better-comments", {clear = true})
cmd({ 'BufWinEnter', 'BufFilePost', 'BufWritePost', 'TextChanged', 'TextChangedI' }, {
local augroup = api.nvim_create_augroup("better-comments", {clear = true})
au({ 'BufWinEnter', 'BufFilePost', 'BufWritePost', 'TextChanged', 'TextChangedI' }, {
group = augroup,
callback = function()
local current_buffer = api.nvim_get_current_buf()
local current_buffer_name = api.nvim_buf_get_name(current_buffer)
if current_buffer_name == '' then
return
end
local fileType = api.nvim_buf_get_option(current_buffer, "filetype")
local success, parsed_query = pcall(function()
return treesitter.query.parse(fileType, [[(comment) @all]])
end)
local fileType = vim.bo[current_buffer].filetype
local success = pcall(treesitter.query.parse, fileType, [[(comment) @all]])
if not success then
return
end
Expand All @@ -71,7 +100,7 @@ M.Setup = function(config)
line = range[1],
col_start = range[2],
finish = range[4],
text = vim.treesitter.get_node_text(node, current_buffer)
text = treesitter.get_node_text(node, current_buffer)
})
end

Expand All @@ -83,7 +112,7 @@ M.Setup = function(config)
for id, comment in ipairs(comments) do
for hl_id, hl in ipairs(opts.tags) do
if string.find(comment.text, hl.name) then
local ns_id = vim.api.nvim_create_namespace(hl.name)
local ns_id = api.nvim_create_namespace(hl.name)
if hl.virtual_text and hl.virtual_text ~= "" then
local v_opts = {
id = id,
Expand All @@ -95,19 +124,19 @@ M.Setup = function(config)
-- FIX: comment.line -> 0 in col
api.nvim_buf_set_extmark(current_buffer, ns_id, comment.line, 0, v_opts)
end

-- FIX: using for ns_id ns_id instead of 0
-- so that when we clear the namespace the color also clear
vim.api.nvim_buf_add_highlight(current_buffer, ns_id, tostring(hl_id), comment.line,
api.nvim_buf_add_highlight(current_buffer, ns_id, tostring(hl_id), comment.line,
comment.col_start,
comment.finish)
else
-- FIX: added else to delted extmark

-- TODO: THIS PART IS CALLED A LOT FIND A WAY TO NOT CHECK EVERY TIME
if hl.virtual_text ~= "" then
local ns_id = vim.api.nvim_create_namespace(hl.name)
local ns_id = api.nvim_create_namespace(hl.name)

-- FIX: clearing the namespace to delete the extmark and the color
api.nvim_buf_clear_namespace(current_buffer, ns_id, comment.line, comment.line+1)
end
Expand All @@ -117,22 +146,6 @@ M.Setup = function(config)
end
})
end

Get_root = function(bufnr, filetype)
local parser = vim.treesitter.get_parser(bufnr, filetype, {})
local tree = parser:parse()[1]
return tree:root()
end

function Create_hl(list)
for id, hl in ipairs(list) do
vim.api.nvim_set_hl(0, tostring(id), {
fg = hl.fg,
bg = hl.bg,
bold = hl.bold,
underline = hl.underline,
})
end
end
M.Setup = M.setup -- Preserve backwards compatability!

return M