Skip to content

Commit

Permalink
Handle leading spaces for list_items
Browse files Browse the repository at this point in the history
## Details

As reported in the following issues:

* #2
* #5

The list_items we capture from tree-sitter will sometimes have
leading spaces leading to us rendering a bullet point incorrectly.

Add some logic on our side to handle these cases.
  • Loading branch information
MeanderingProgrammer committed Mar 22, 2024
1 parent d7d793b commit df98da8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions demo/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ if __name__ == "__main__":

* List Item 1
* List Item 2
* Nested List Item 1
* Nested List Item 2
* Nested List Item 1
* Nested List Item 2
* List Item 3

| Heading 1 | Heading 2 |
Expand Down
17 changes: 12 additions & 5 deletions lua/render-markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,17 @@ end

M.namespace = vim.api.nvim_create_namespace('render-markdown.nvim')

M.clear = function()
-- Remove existing highlights / virtual text
vim.api.nvim_buf_clear_namespace(0, M.namespace, 0, -1)
end

M.refresh = function()
if not vim.tbl_contains(state.config.file_types, vim.bo.filetype) then
return
end

-- Remove existing highlights / virtual text
vim.api.nvim_buf_clear_namespace(0, M.namespace, 0, -1)

-- Needs to happen after file_type check and before mode check
M.clear()
if not vim.tbl_contains(state.config.render_modes, vim.fn.mode()) then
return
end
Expand Down Expand Up @@ -139,7 +142,11 @@ M.refresh = function()
hl_eol = true,
})
elseif capture == 'item' then
local virt_text = { state.config.bullet, highlights.bullet }
-- List items from tree-sitter should have leading spaces removed, however there are known
-- edge cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
-- As a result we handle leading spaces here, can remove if this gets fixed upstream
local _, leading_spaces = value:find('^%s*')
local virt_text = { string.rep(' ', leading_spaces or 0) .. state.config.bullet, highlights.bullet }
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, start_col, {
end_row = end_row,
end_col = end_col,
Expand Down

0 comments on commit df98da8

Please sign in to comment.