mini.nvim icon indicating copy to clipboard operation
mini.nvim copied to clipboard

mini.comment to be able to beautify comments and texts

Open cliarena opened this issue 1 year ago • 4 comments

Contributing guidelines

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

better-comments

cliarena avatar Sep 17 '24 08:09 cliarena

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 avatar Sep 17 '24 10:09 echasnovski

@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?

krovuxdev avatar Feb 12 '25 20:02 krovuxdev

@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 avatar Feb 12 '25 20:02 echasnovski

@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)

krovuxdev avatar Feb 13 '25 01:02 krovuxdev