Skip to content

Commit c6b59a2

Browse files
fix: reset options when buffer is hidden
## Details Issue: #209 Currently when a user leaves a buffer we render the buffer. This works as expected when a user is say switching between multiple windows. However if a user re-uses the current window for another buffer it will have the window options applied as if it is rendered markdown, i.e. a conceallevel of 3, which is unepected. This can also occur if a plugin opens a buffer in the window via the `nvim_win_set_buf` API. This occurs because at the time the update is triggered the buffer and window are associated with each other, however due to the use of vim.schedule when the update occurs this may not longer be the case. To fix this add a check that the buffer is the one associated with the window being updated. Include this in the `util.valid` logic so it will be used at the time the update is triggered and at the time it is actually ran. Other minor changes: - Remove `buf` field from `BufferState` - Updated window related utility functions - Remove wrapper functions where they are not needed, i.e. all the renderers will work out of the box no need to add one in each module
1 parent 1871dc4 commit c6b59a2

21 files changed

+33
-152
lines changed

lua/render-markdown/core/buffer_state.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
---@class render.md.BufferState
2-
---@field private buf integer
32
---@field private timer uv_timer_t
43
---@field private running boolean
54
---@field private marks? render.md.Extmark[]
65
local BufferState = {}
76
BufferState.__index = BufferState
87

9-
---@param buf integer
108
---@return render.md.BufferState
11-
function BufferState.new(buf)
9+
function BufferState.new()
1210
local self = setmetatable({}, BufferState)
13-
self.buf = buf
1411
self.timer = (vim.uv or vim.loop).new_timer()
1512
self.running = false
1613
self.marks = nil

lua/render-markdown/core/context.lua

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ end
5252
---@param offset integer
5353
---@return render.md.Range
5454
function Context.compute_range(buf, win, offset)
55-
local top = math.max(util.view(win).topline - 1 - offset, 0)
55+
local top = math.max(util.win_view(win).topline - 1 - offset, 0)
5656

5757
local bottom = top
5858
local lines = vim.api.nvim_buf_line_count(buf)
5959
local size = vim.api.nvim_win_get_height(win) + (2 * offset)
6060
while bottom < lines and size > 0 do
6161
bottom = bottom + 1
62-
if util.visible(win, bottom) then
62+
if util.win_visible(win, bottom) then
6363
size = size - 1
6464
end
6565
end
@@ -142,11 +142,7 @@ end
142142
---@return integer
143143
function Context:get_width()
144144
if self.window_width == nil then
145-
self.window_width = vim.api.nvim_win_get_width(self.win)
146-
local window_info = vim.fn.getwininfo(self.win)
147-
if #window_info == 1 then
148-
self.window_width = self.window_width - window_info[1].textoff
149-
end
145+
self.window_width = vim.api.nvim_win_get_width(self.win) - util.win_textoff(self.win)
150146
end
151147
return self.window_width
152148
end

lua/render-markdown/core/list.lua

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ local util = require('render-markdown.core.util')
88
local Marks = {}
99
Marks.__index = Marks
1010

11-
---@param mode string
12-
---@param ignore render.md.config.conceal.Ignore
11+
---@param mode? string
12+
---@param ignore? render.md.config.conceal.Ignore
1313
---@return render.md.Marks
1414
function Marks.new(mode, ignore)
1515
local self = setmetatable({}, Marks)
16-
self.mode = mode
17-
self.ignore = ignore
16+
self.mode = mode or util.mode()
17+
self.ignore = ignore or {}
1818
self.marks = {}
1919
return self
2020
end
@@ -70,12 +70,7 @@ end
7070
---@class render.md.ListHelper
7171
local M = {}
7272

73-
---@param mode? string
74-
---@param ignore? render.md.config.conceal.Ignore
75-
---@return render.md.Marks
76-
function M.new_marks(mode, ignore)
77-
return Marks.new(mode or util.mode(), ignore or {})
78-
end
73+
M.new_marks = Marks.new
7974

8075
---@generic T
8176
---@param values T[]

lua/render-markdown/core/ui.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ local Cache = {
2424
function Cache.get(buf)
2525
local buffer_state = Cache.states[buf]
2626
if buffer_state == nil then
27-
buffer_state = BufferState.new(buf)
27+
buffer_state = BufferState.new()
2828
Cache.states[buf] = buffer_state
2929
end
3030
return buffer_state
@@ -144,13 +144,13 @@ function M.next_state(config, win, mode)
144144
if not state.enabled then
145145
return 'default'
146146
end
147-
if util.get_win(win, 'diff') then
147+
if not config:render(mode) then
148148
return 'default'
149149
end
150-
if util.view(win).leftcol ~= 0 then
150+
if util.get_win(win, 'diff') then
151151
return 'default'
152152
end
153-
if not config:render(mode) then
153+
if util.win_view(win).leftcol ~= 0 then
154154
return 'default'
155155
end
156156
return 'rendered'

lua/render-markdown/core/util.lua

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ end
3232
---@param win integer
3333
---@return boolean
3434
function M.valid(buf, win)
35-
return vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_win_is_valid(win)
35+
if not vim.api.nvim_buf_is_valid(buf) then
36+
return false
37+
end
38+
if not vim.api.nvim_win_is_valid(win) then
39+
return false
40+
end
41+
return buf == vim.fn.winbufnr(win)
3642
end
3743

3844
---@return string
@@ -75,19 +81,26 @@ end
7581

7682
---@param win integer
7783
---@return vim.fn.winsaveview.ret
78-
function M.view(win)
84+
function M.win_view(win)
7985
return vim.api.nvim_win_call(win, vim.fn.winsaveview)
8086
end
8187

8288
---@param win integer
8389
---@param row integer
8490
---@return boolean
85-
function M.visible(win, row)
91+
function M.win_visible(win, row)
8692
return vim.api.nvim_win_call(win, function()
8793
return vim.fn.foldclosed(row) == -1
8894
end)
8995
end
9096

97+
---@param win integer
98+
---@return integer
99+
function M.win_textoff(win)
100+
local infos = vim.fn.getwininfo(win)
101+
return #infos == 1 and infos[1].textoff or 0
102+
end
103+
91104
---@param file string
92105
---@return number
93106
function M.file_size_mb(file)

lua/render-markdown/debug/validator.lua

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,7 @@ function Validator.new()
171171
return self
172172
end
173173

174-
---@param config table<string, any>
175-
---@param nilable boolean
176-
---@param key? string|string[]
177-
---@param path? string
178-
---@return render.md.debug.ValidatorSpec
179-
function Validator:spec(config, nilable, key, path)
180-
return Spec.new(self, config, nilable, key, path)
181-
end
174+
Validator.spec = Spec.new
182175

183176
---@param suffix string
184177
---@param config table<string, any>

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local state = require('render-markdown.state')
44
local M = {}
55

66
---@private
7-
M.version = '7.4.3'
7+
M.version = '7.4.4'
88

99
function M.check()
1010
M.start('version')

lua/render-markdown/manager.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ function M.attach(buf)
7272
return
7373
end
7474
local event, win = args.event, vim.api.nvim_get_current_win()
75-
if buf == vim.fn.winbufnr(win) then
76-
ui.debounce_update(buf, win, event, vim.tbl_contains(change_events, event))
77-
end
75+
ui.debounce_update(buf, win, event, vim.tbl_contains(change_events, event))
7876
end,
7977
})
8078
end

lua/render-markdown/render/checkbox.lua

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ local str = require('render-markdown.core.str')
77
local Render = setmetatable({}, Base)
88
Render.__index = Render
99

10-
---@param marks render.md.Marks
11-
---@param config render.md.buffer.Config
12-
---@param context render.md.Context
13-
---@param info render.md.NodeInfo
14-
---@return render.md.Renderer
15-
function Render:new(marks, config, context, info)
16-
return Base.new(self, marks, config, context, info)
17-
end
18-
1910
---@return boolean
2011
function Render:setup()
2112
local checkbox = self.config.checkbox

lua/render-markdown/render/code.lua

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ local str = require('render-markdown.core.str')
2424
local Render = setmetatable({}, Base)
2525
Render.__index = Render
2626

27-
---@param marks render.md.Marks
28-
---@param config render.md.buffer.Config
29-
---@param context render.md.Context
30-
---@param info render.md.NodeInfo
31-
---@return render.md.Renderer
32-
function Render:new(marks, config, context, info)
33-
return Base.new(self, marks, config, context, info)
34-
end
35-
3627
---@return boolean
3728
function Render:setup()
3829
self.code = self.config.code

0 commit comments

Comments
 (0)