LudoPinelli / comment-box.nvim

:sparkles: Clarify and beautify your comments and plain text files using boxes and lines.
MIT License
408 stars 15 forks source link

FR: Line comments that incorporate text #10

Closed StephanRoemer closed 9 months ago

StephanRoemer commented 2 years ago

Hey Ludo!

Thanks for this amazing plugin! I wanted to ask, if you would consider implementing another category: lines with text. The result would look like this:

--- This is a header ---------------------------

As a basis, I would suggest to use the templates from the "cbline" category.

Thanks for considering!

LudoPinelli commented 1 year ago

Sorry for the very late answer. It was part of the few ideas I had to add, but as you can see I didn't work on the plugin for quite some time... I'll do my best in the coming few weeks to give some updates.

StephanRoemer commented 1 year ago

No worries, take your time and thanks for getting back to me!

Cheers!

okuma10 commented 10 months ago

Since there was no implementation on this I poke around a bit with the function....

local function create_line(choice, centered_line, right_aligned_line)
    local symbols = set_line(choice)
    comment_string = vim.bo.commentstring
    local line = {}
    local lead_space = " "
    local filetype = vim.bo.filetype

    -- Sanetize current line text
    local line_text = string.gsub(vim.api.nvim_get_current_line(), "^%s*", "")
    -- Add whitespaces if there is text on line
    if string.len(line_text) > 0 then
        line_text = " " .. line_text .. " "
    end

    if centered_line then
        lead_space = string.rep(
            " ",
            math.floor((settings.doc_width - settings.line_width) / 2 - vim.fn.strdisplaywidth(comment_string))
        )
    elseif right_aligned_line then
        lead_space = string.rep(
            " ",
            settings.doc_width - settings.line_width - vim.fn.strdisplaywidth(comment_string) + 2
        )
    end

    if lead_space == "" then
        lead_space = " "
    end

    comment_string, comment_string_end = comment_string:match("^(.*)%%s(.*)")

    if not comment_string or filetype == "markdown" or filetype == "org" then
        comment_string = ""
    end

    if settings.line_blank_line_above then
        table.insert(line, "")
    end
    table.insert(
        line,
        string.format(
            "%s%s%s%s%s%s%s%s",
            comment_string,
            lead_space,
            symbols.line_start,
            string.rep(symbols.line, ( settings.line_width/2 ) - 2),
            line_text,
            string.rep(symbols.line, ( settings.line_width/2 ) - 2),
            symbols.line_end,
            comment_string_end
        )
    )
    if settings.line_blank_line_below then
        table.insert(line, "")
    end
    return line
end

The result

image

It's a bit ugly since it doesn't take into account the string length, but it works for a quick patch.

LudoPinelli commented 9 months ago

Done! I called them "titled lines" and made sure they are as flexible as the boxes. I hope they reach your expectation, but if not please let me know! :)

okuma10 commented 9 months ago

Does that still work with the CBline?

EDIT: Oh, it's CBl[l,c,r]line . A bit more keys to press but the alignment feature is sweet thanks

LudoPinelli commented 9 months ago

Yes, the CBline still works but it's for line without text.