Skip to content

Commit ec74afa

Browse files
fix: add option to restart treesitter highlighter to clear invalid state
## Details Issue: #488 Adds top level `restart_highlighter` option that when set to `true` will restart the neovim highlighter by calling `vim.treesitter.stop()` and `vim.treesitter.start()`. This will happen once only, after this plugin attaches to its first buffer for the first time, after modifying treesitter queries. Currently this plugin may modify `injections` to insert markdown and `highlights` to disable specific patterns. In order for this to be a problem the following all need to be true for the user: - They are using a version of neovim >= `0.11.0` - They are lazy loading this plugin using `lazy.nvim` based on filetype. - Their default `conceallevel` is > 0. - They are using the `main` branch of `nvim-treesitter` and their custom `FileType` autocommand to enable highlighting does not handle duplicate events for the same buffer. The `master` branch handles duplicate events by restarting the highlighter already. When all of these are true what can sometimes happen is any `conceal_lines` directives in the highlighter will be activated before this plugin has a chance to disable them. By default this is the case for the delimiters around fenced code blocks. These concealed lines will only reappear after a modification is made to the buffer.
1 parent e5c3c50 commit ec74afa

File tree

9 files changed

+34
-10
lines changed

9 files changed

+34
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
### Features
66

7-
- render list item bullet points in yaml [#496](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/496)
7+
- yaml render list item bullet points [#496](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/496)
88
[d7be79b](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/d7be79b1a006a2afd9945b5d48d3f1f3ce41ce69)
9+
- yaml render wiki style links [#496](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/496)
10+
[146d4ff](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/146d4ff582017f1a1a16ada841aa08175a87aaea)
11+
- add scope_highlight to wiki config [ca86b59](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/ca86b5941a56b04ac140837ee7a366cf3fa5cd88)
12+
- add cell_offset function to pipe_table configuration [e5c3c50](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/e5c3c500d66e9aaf04c116cdfdb0b040d56a1521)
913

1014
### Bug Fixes
1115

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ require('render-markdown').setup({
215215
end,
216216
-- Additional events that will trigger this plugin's render loop.
217217
change_events = {},
218+
-- Whether the treesitter highlighter should be restarted after this plugin attaches to its
219+
-- first buffer for the first time. May be necessary if this plugin is lazy loaded to clear
220+
-- highlights that have been dynamically disabled.
221+
restart_highlighter = false,
218222
injections = {
219223
-- Out of the box language injections for known filetypes that allow markdown to be interpreted
220224
-- in specified locations, see :h treesitter-language-injections.

doc/render-markdown.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For NVIM v0.11.3 Last change: 2025 August 17
1+
*render-markdown.txt* For NVIM v0.11.3 Last change: 2025 August 18
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -281,6 +281,10 @@ Default Configuration ~
281281
end,
282282
-- Additional events that will trigger this plugin's render loop.
283283
change_events = {},
284+
-- Whether the treesitter highlighter should be restarted after this plugin attaches to its
285+
-- first buffer for the first time. May be necessary if this plugin is lazy loaded to clear
286+
-- highlights that have been dynamically disabled.
287+
restart_highlighter = false,
284288
injections = {
285289
-- Out of the box language injections for known filetypes that allow markdown to be interpreted
286290
-- in specified locations, see :h treesitter-language-injections.

lua/render-markdown/core/ts.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local compat = require('render-markdown.lib.compat')
22

33
---@class render.md.ts.Config
44
---@field file_types string[]
5+
---@field restart_highlighter boolean
56
---@field injections table<string, render.md.injection.Config>
67
---@field patterns table<string, render.md.pattern.Config>
78

@@ -35,6 +36,10 @@ function M.init()
3536
for _, language in ipairs(M.config.file_types) do
3637
M.disable(language)
3738
end
39+
if M.config.restart_highlighter then
40+
vim.treesitter.stop()
41+
vim.treesitter.start()
42+
end
3843
end
3944

4045
---@param language string

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
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.7.7'
8+
M.version = '8.7.8'
99

1010
function M.check()
1111
M.start('versions')

lua/render-markdown/init.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local M = {}
88
---@field file_types string[]
99
---@field ignore fun(buf: integer): boolean
1010
---@field change_events string[]
11+
---@field restart_highlighter boolean
1112
---@field injections render.md.injection.Configs
1213
---@field patterns render.md.pattern.Configs
1314
---@field on render.md.on.Config
@@ -77,6 +78,10 @@ M.default = {
7778
end,
7879
-- Additional events that will trigger this plugin's render loop.
7980
change_events = {},
81+
-- Whether the treesitter highlighter should be restarted after this plugin attaches to its
82+
-- first buffer for the first time. May be necessary if this plugin is lazy loaded to clear
83+
-- highlights that have been dynamically disabled.
84+
restart_highlighter = false,
8085
injections = require('render-markdown.config.injections').default,
8186
patterns = require('render-markdown.config.patterns').default,
8287
anti_conceal = require('render-markdown.config.anti_conceal').default,

lua/render-markdown/request/conceal.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ local compat = require('render-markdown.lib.compat')
22
local env = require('render-markdown.lib.env')
33
local str = require('render-markdown.lib.str')
44

5+
---@class render.md.request.conceal.Line
6+
---@field hidden boolean
7+
---@field sections render.md.request.conceal.Section[]
8+
59
---@class render.md.request.conceal.Section
610
---@field start_col integer
711
---@field end_col integer
812
---@field width integer
913
---@field character? string
1014

11-
---@class render.md.request.conceal.Line
12-
---@field hidden boolean
13-
---@field sections render.md.request.conceal.Section[]
14-
1515
---@class render.md.request.Conceal
1616
---@field private buf integer
1717
---@field private level integer
@@ -53,8 +53,7 @@ function Conceal:add(row, entry)
5353
if type(entry) == 'boolean' then
5454
line.hidden = entry
5555
else
56-
-- If the section is covered by an existing one don't add it
57-
if entry.width > 0 and not self:has(line, entry) then
56+
if entry.width > 0 and not Conceal.contains(line, entry) then
5857
line.sections[#line.sections + 1] = entry
5958
end
6059
end
@@ -64,7 +63,7 @@ end
6463
---@param line render.md.request.conceal.Line
6564
---@param entry render.md.request.conceal.Section
6665
---@return boolean
67-
function Conceal:has(line, entry)
66+
function Conceal.contains(line, entry)
6867
for _, section in ipairs(line.sections) do
6968
local starts_before = section.start_col <= entry.start_col
7069
local ends_after = section.end_col >= entry.end_col

lua/render-markdown/state.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function M.setup(config)
3131
})
3232
require('render-markdown.core.ts').setup({
3333
file_types = config.file_types,
34+
restart_highlighter = config.restart_highlighter,
3435
injections = config.injections,
3536
patterns = config.patterns,
3637
})
@@ -89,6 +90,7 @@ function M.validate()
8990
spec:list('file_types', 'string')
9091
spec:type('ignore', 'function')
9192
spec:list('change_events', 'string')
93+
spec:type('restart_highlighter', 'boolean')
9294
spec:nested('injections', require('render-markdown.config.injections').validate)
9395
spec:nested('patterns', require('render-markdown.config.patterns').validate)
9496
spec:nested('on', require('render-markdown.config.on').validate)

lua/render-markdown/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
---@field file_types? string[]
88
---@field ignore? fun(buf: integer): boolean
99
---@field change_events? string[]
10+
---@field restart_highlighter? boolean
1011
---@field injections? render.md.injection.UserConfigs
1112
---@field patterns? render.md.pattern.UserConfigs
1213
---@field on? render.md.on.UserConfig

0 commit comments

Comments
 (0)