mini.comment to be able to beautify comments and texts
Contributing guidelines
- [X] I have read CONTRIBUTING.md
- [X] I have read CODE_OF_CONDUCT.md
Module(s)
mini.comment
Description
first. thank you so much for all the work you do.
comments are so important to document code. so i think it's a good addition to mini.comment to be able to beautify comments and plain texts this is how comment-box.nvim looks like comment-box.nvim
Thanks for the suggestion!
Yes, I had this idea about a year or so ago, but decided to postpone because there are other more pressing things to implement.
What I had in mind is either a separate module (something like 'mini.box') or a separate function for 'mini.misc' (like "enter comment box"). But maybe a 'mini.comment' can also be a good place for this.
@echasnovski I like that idea, but wouldn't it be better to use mini.snippets? I'm not sure how to handle the letter count to adjust the frame. How would you do it?
@echasnovski I like that idea, but wouldn't it be better to use
mini.snippets? I'm not sure how to handle the letter count to adjust the frame. How would you do it?
Yeah, that's why it is not a good fit for 'mini.snippets' :) There might be a way through transfromations, but 'mini.snippets' doesn't support them.
I don't know yet. The closest thing I have to this kind of feature is pasting a "section" and starting Replace mode to enter the section title. But it is a bit different use case: fixed "box" and text overrides it.
@echasnovski I didn’t know about your code, it’s interesting! I modified it and it worked for me, thank you very much. The number of comment lines when the box resizes is a bit tricky for me, but it doesn’t matter it works! :)
vim.keymap.set("n", "gm", function(symbol, total_width)
local count = vim.v.count1
symbol = symbol or "─"
total_width = total_width or 79
local tl, tr, bl, br = "╭", "╮", "╰", "╯"
local horizontal, vertical = "─", "│"
local comment_string = vim.bo.commentstring:gsub("%%s", " ")
local line_num = vim.fn.line(".")
local border_top = comment_string
.. tl
.. string.rep(horizontal, total_width - #comment_string - 2)
.. tr
local border_bottom = comment_string
.. bl
.. string.rep(horizontal, total_width - #comment_string - 2)
.. br
local text_lines = {}
for _ = 1, count do
local text = " "
local padding =
math.floor((total_width - #comment_string - 2 - #text) / 2)
local text_line = comment_string
.. vertical
.. string.rep(" ", padding)
.. text
.. string.rep(
" ",
total_width - #comment_string - 2 - #text - padding
)
.. vertical
table.insert(text_lines, text_line)
end
local content = { border_top }
for _, line in ipairs(text_lines) do
table.insert(content, line)
end
table.insert(content, border_bottom)
vim.fn.append(line_num, content)
local inner_start = #comment_string + 5
vim.fn.cursor(line_num + 2, inner_start)
vim.cmd([[startreplace]])
end)