Skip to content

fix alignment with concealed text #503

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

Closed

Conversation

mageOfstructs
Copy link

This fixes an alignment issue where table cells include concealed text, which doesn't get accounted for. This pr adds some code to the row() fn for extracting all highlight extmarks in the area of the cell and readjusts the col.width field. This also takes virtual_text into account.

@MeanderingProgrammer
Copy link
Owner

In what scenario does this happen?

There's existing logic to account for concealed text from treesitter highlights.

If this is virtual text added by other plugins then that's something I don't want to attempt to support. There's too many potential edge cases to open up this can of worms, including what about all the other places plugins could add virtual text, not just table rows. I can provide some kind of hook to let users modify the width via a function parameter, but not some generic way to handle virtual text from any source.

Issues with the logic as written:

  • hl_group == 'Conceal' doesn't cause the text to be concealed, it's just a highlight group
  • #conceal_highlight[4].virt_text[1] is not measuring text width, there are always 2 entries for each section of virtual text, should iterate over them and add the width of the first element
  • does not account for different conceal level values changing width

@mageOfstructs
Copy link
Author

mageOfstructs commented Aug 17, 2025

If this is virtual text added by other plugins then that's something I don't want to attempt to support.

Ah, apologies I did not know that. That was my main intent when writing the patch 😅

@MeanderingProgrammer
Copy link
Owner

No worries at all, it's not obvious why I would prefer to avoid that behavior, and I appreciate you going through the effort of putting up a PR.

I did add a cell_offset configuration option that you can use in your config to make these adjustments: e5c3c50

Porting your logic to it would be something like:

require('render-markdown').setup({
    pipe_table = {
        cell_offset = function(ctx)
            local offset = 0
            local row, start_col, _, end_col = ctx.node:range()
            local highlights = vim.api.nvim_buf_get_extmarks(
                0,
                -1,
                { row, start_col },
                { row, end_col },
                { details = true, type = 'highlight' }
            )
            for _, conceal_highlight in ipairs(highlights) do
                if conceal_highlight[4].hl_group == 'Conceal' then
                    offset = offfset - (conceal_highlight[4].end_col - conceal_highlight[3])
                    if conceal_highlight[4].virt_text then
                        offset = offset + #conceal_highlight[4].virt_text[1]
                    end
                end
            end
            return offset
        end,
    },
})

@mageOfstructs
Copy link
Author

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants