MeanderingProgrammer / markdown.nvim

Plugin to improve viewing Markdown files in Neovim
MIT License
504 stars 20 forks source link

[BUG]: The `table_style` setting incorrectly calculated the text length inside the table. #26

Closed MasouShizuka closed 1 month ago

MasouShizuka commented 1 month ago

After I tried the plugin, I found that when the cell text length of each column of the table is the same, the plugin will add lines above and below the table. For ascii characters, this calculation is correct.

But for other characters, such as Chinese, this is not so good. Because for Chinese characters, Lua will count the length of the character as 3, such as: . However, with a mono font, actually takes up 2 characters of width.

For example, the following table will add lines, but it actually doesn’t look good:

| 列1 | 列2 | 列3 |
| ---- | ---- | ---- |
| 行1 | 2    | 3    |
| 行2 | 2    | 3    |
| 行3 | 2    | 3    |

image

Although the following table has the same width, lines cannot be added:

| 列1 | 列2 | 列3 |
| --- | --- | --- |
| 行1 | 2   | 3   |
| 行2 | 2   | 3   |
| 行3 | 2   | 3   |

image

Perhaps should use the strwidth function to calculate text length:

vim.fn.strwidth("行") = 2
MeanderingProgrammer commented 1 month ago

Thank you very much for reporting this issue and providing very clear examples!

I had not tested this on any languages outside of English, definitely a miss on my part.

I've corrected it in this commit: https://github.com/MeanderingProgrammer/markdown.nvim/commit/7f90f522750111c32b0515814398514d58f66b23, by using strdisplaywidth which is similar to strwidth, for tables as well as a couple other lingering places that were less likely to be a problem.

I should definitely be uniformly using strdisplaywidth in all string length calculation rather than the Lua #.

I tested it on your example and it seems to work as expected, lmk if you face any issues.

Thanks again!