Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mini.comment to be able to beautify comments and texts #1225

Open
2 tasks done
cliarena opened this issue Sep 17, 2024 · 4 comments
Open
2 tasks done

mini.comment to be able to beautify comments and texts #1225

cliarena opened this issue Sep 17, 2024 · 4 comments
Labels
feature-request Request for a feature to existing module mini.comment mini.misc

Comments

@cliarena
Copy link

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 cliarena added the feature-request Request for a feature to existing module label Sep 17, 2024
@echasnovski
Copy link
Owner

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.

@krovuxdev
Copy link

@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
Copy link
Owner

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

@krovuxdev
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for a feature to existing module mini.comment mini.misc
Projects
None yet
Development

No branches or pull requests

3 participants