Skip to content

Commit 92256e0

Browse files
chore: document limitation with column features and block widths
## Details Issue: #385
1 parent bfbb46a commit 92256e0

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

doc/limitations.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,70 @@
11
# Limitations
22

3+
## `block` Width Removes Column Features
4+
5+
[ISSUE #385](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/385)
6+
7+
This problem impacts both `code` & `heading` rendering when using
8+
`{ width = 'block' }`. Regardless of how wide the actual content is all `colorcolumn`
9+
icons will be hidden on the intersecting lines and `cursorline` will not work.
10+
11+
This occurs because there is no way to create a background highlight that starts
12+
and ends at specific columns. So instead to achieve the effect we combine 2 highlights:
13+
14+
1. `hl_group` + `hl_eol`: results in the entire line being highlighted
15+
2. `virt_text` with many spaces using the background highlight + `virt_text_win_col`:
16+
results in hiding the highlight after our target column
17+
18+
Why does it work like this? To explain that lets see how else we could implement
19+
this. The starting point would be to avoid highlighting the entire line, so we don't
20+
then need to hide the highlight. This part is easy, just remove `hl_eol = true` from
21+
the first mark, done!
22+
23+
Now we have all the inner text highlighted with the background, so the problem is
24+
now to extend each one of these so it reaches our target column.
25+
26+
Your first thought might be to use `virt_text_pos = 'eol'`, and do some basic math
27+
to figure out how long to make each line. Well, unfortunately `eol` does not mean
28+
right at the end of the line, there's actually a space that gets added before the
29+
mark starts that we cannot get rid of, so this one is a non-starter.
30+
31+
Your second thought might be to use `virt_text_win_col`, but set it to be right after
32+
each line, after that it's the same as the previous approach. To make this work we
33+
need to compute the width of each line exactly. If we make it one too large we'll
34+
have an empty space, too small and we'll cut off text in the code block. To do this
35+
correctly we'll need to properly handle concealed ranges for all of the code blocks.
36+
This isn't impossible but it is slow and error prone since we also need to handle
37+
the odd case where another `markdown` block is nested.
38+
39+
To avoid all this additional complexity we take the approach of using 2 highlights
40+
which works because the simple string width calculation is if anything going to be
41+
an over-estimate which is not really a big deal, just adds some extra padding in
42+
the worst case but the block remains contiguous.
43+
44+
Below are a few things to try out to improve the aesthetic:
45+
46+
- Use `win_options` to disable `colorcolumn` when rendering, this is my personal
47+
favorite since `colorcolumn` is really only helpful when editing
48+
49+
```lua
50+
require('render-markdown').setup({
51+
win_options = {
52+
colorcolumn = { default = '80', rendered = '' },
53+
},
54+
})
55+
```
56+
57+
- Set the `min_width` options to the same value as `colorcolumn`
58+
59+
```lua
60+
require('render-markdown').setup({
61+
heading = { width = 'block', min_width = 80 },
62+
code = { width = 'block', min_width = 80 },
63+
})
64+
```
65+
66+
- Do not use `block` width, keep the default value of `full`
67+
368
## `latex` Formula Positioning
469

570
[ISSUE #6](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/6)

lua/render-markdown/core/log.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ end
5858
---@param buf integer
5959
---@param ... any
6060
function M.buf(level, name, buf, ...)
61-
M.add(level, string.format('%s|%s|%d', name, M.file_name(buf), buf), ...)
61+
M.add(level, name, buf, M.file_name(buf), ...)
6262
end
6363

6464
---@private
6565
---@param buf integer
6666
---@return string
6767
function M.file_name(buf)
68-
if Env.buf.valid(buf) then
69-
local file = vim.api.nvim_buf_get_name(buf)
70-
return vim.fn.fnamemodify(file, ':t')
71-
else
68+
if not Env.buf.valid(buf) then
7269
return 'INVALID'
7370
end
71+
local file = vim.api.nvim_buf_get_name(buf)
72+
local name = vim.fn.fnamemodify(file, ':t')
73+
return #name == 0 and 'EMPTY' or name
7474
end
7575

7676
---@param level render.md.config.LogLevel
@@ -81,9 +81,9 @@ function M.add(level, name, ...)
8181
return
8282
end
8383
local messages = {}
84-
local args = vim.F.pack_len(...)
85-
for i = 1, args.n do
86-
local message = type(args[i]) == 'string' and args[i] or vim.inspect(args[i])
84+
for i = 1, select('#', ...) do
85+
local value = select(i, ...)
86+
local message = type(value) == 'string' and value or vim.inspect(value)
8787
table.insert(messages, message)
8888
end
8989
---@type render.md.log.Entry

lua/render-markdown/core/ui.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,15 @@ function M.parse_tree(ctx, language)
231231
local marks = {}
232232
local user = state.custom_handlers[language]
233233
if user ~= nil then
234-
log.buf('debug', 'running handler', ctx.buf, 'user')
234+
log.buf('debug', 'handler', ctx.buf, 'user')
235235
vim.list_extend(marks, user.parse(ctx))
236236
if not user.extends then
237237
return marks
238238
end
239239
end
240240
local builtin = builtin_handlers[language]
241241
if builtin ~= nil then
242-
log.buf('debug', 'running handler', ctx.buf, 'builtin')
242+
log.buf('debug', 'handler', ctx.buf, 'builtin')
243243
vim.list_extend(marks, builtin.parse(ctx))
244244
end
245245
return marks

lua/render-markdown/health.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '8.2.0'
8+
M.version = '8.2.1'
99

1010
function M.check()
1111
M.start('version')
@@ -83,7 +83,7 @@ end
8383
function M.disable_advice(language)
8484
return {
8585
string.format('Disable %s support to avoid this warning', language),
86-
string.format("require('render-markdown').setup({ { %s = { enabled = false } })", language),
86+
string.format("require('render-markdown').setup({ %s = { enabled = false } })", language),
8787
}
8888
end
8989

0 commit comments

Comments
 (0)