Skip to content

Commit 2424693

Browse files
feat: add error log for missing parser, include buffer in logs
1 parent 3e65920 commit 2424693

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

lua/render-markdown/core/log.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ function M.unhandled_type(language, group, value)
5050
M.error('unhandled type', string.format('%s -> %s -> %s', language, group, value))
5151
end
5252

53+
---@param name string
54+
---@param buf integer
55+
---@param ... any
56+
function M.debug_buf(name, buf, ...)
57+
M.debug(string.format('%s %d', name, buf), ...)
58+
end
59+
5360
---@param name string
5461
---@param ... any
5562
function M.debug(name, ...)
@@ -58,6 +65,13 @@ function M.debug(name, ...)
5865
end
5966
end
6067

68+
---@param name string
69+
---@param buf integer
70+
---@param ... any
71+
function M.error_buf(name, buf, ...)
72+
M.error(string.format('%s %d', name, buf), ...)
73+
end
74+
6175
---@param name string
6276
---@param ... any
6377
function M.error(name, ...)

lua/render-markdown/core/ui.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ end
5353

5454
---@param buf integer
5555
---@param win integer
56+
---@param event string
5657
---@param change boolean
57-
function M.debounce_update(buf, win, change)
58+
function M.debounce_update(buf, win, event, change)
59+
log.debug_buf('update', buf, string.format('event %s', event), string.format('change %s', change))
5860
if not util.valid(buf, win) then
5961
return
6062
end
@@ -132,16 +134,15 @@ end
132134
function M.parse_buffer(buf, win)
133135
local has_parser, parser = pcall(vim.treesitter.get_parser, buf)
134136
if not has_parser then
137+
log.error_buf('fail', buf, 'no treesitter parser found')
135138
return {}
136139
end
137140
-- Reset buffer context
138141
Context.reset(buf, win)
139142
-- Make sure injections are processed
140143
parser:parse(Context.get(buf):range())
141-
-- Parse marks
142-
local marks = {}
143144
-- Parse markdown after all other nodes to take advantage of state
144-
local markdown_roots = {}
145+
local marks, markdown_roots = {}, {}
145146
parser:for_each_tree(function(tree, language_tree)
146147
local language = language_tree:lang()
147148
if language == 'markdown' then
@@ -166,23 +167,23 @@ end
166167
---@param root TSNode
167168
---@return render.md.Mark[]
168169
function M.parse_tree(buf, language, root)
169-
log.debug('language', language)
170+
log.debug_buf('language', buf, language)
170171
if not Context.get(buf):contains_node(root) then
171172
return {}
172173
end
173174

174175
local marks = {}
175176
local user = state.custom_handlers[language]
176177
if user ~= nil then
177-
log.debug('running handler', 'user')
178+
log.debug_buf('running handler', buf, 'user')
178179
vim.list_extend(marks, user.parse(root, buf))
179180
if not user.extends then
180181
return marks
181182
end
182183
end
183184
local builtin = builtin_handlers[language]
184185
if builtin ~= nil then
185-
log.debug('running handler', 'builtin')
186+
log.debug_buf('running handler', buf, 'builtin')
186187
vim.list_extend(marks, builtin.parse(root, buf))
187188
end
188189
return marks

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.0.5'
7+
M.version = '7.0.6'
88

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

lua/render-markdown/manager.lua

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ function M.setup()
2424
-- Window resizing is not buffer specific so is managed more globablly
2525
vim.api.nvim_create_autocmd('WinResized', {
2626
group = M.group,
27-
callback = function()
27+
callback = function(args)
2828
for _, win in ipairs(vim.v.event.windows) do
2929
local buf = vim.fn.winbufnr(win)
3030
if vim.tbl_contains(buffers, buf) then
31-
ui.debounce_update(buf, win, true)
31+
ui.debounce_update(buf, win, args.event, true)
3232
end
3333
end
3434
end,
@@ -46,7 +46,7 @@ function M.set_all(enabled)
4646
M.attach(vim.api.nvim_get_current_buf())
4747
state.enabled = enabled
4848
for _, buf in ipairs(buffers) do
49-
ui.debounce_update(buf, vim.fn.bufwinid(buf), true)
49+
ui.debounce_update(buf, vim.fn.bufwinid(buf), 'UserCommand', true)
5050
end
5151
end
5252

@@ -67,9 +67,9 @@ function M.attach(buf)
6767
group = M.group,
6868
buffer = buf,
6969
callback = function(args)
70-
local win = vim.api.nvim_get_current_win()
70+
local event, win = args.event, vim.api.nvim_get_current_win()
7171
if buf == vim.fn.winbufnr(win) then
72-
ui.debounce_update(buf, win, vim.tbl_contains(change_events, args.event))
72+
ui.debounce_update(buf, win, event, vim.tbl_contains(change_events, event))
7373
end
7474
end,
7575
})
@@ -80,33 +80,35 @@ end
8080
---@return boolean
8181
function M.should_attach(buf)
8282
local file = vim.api.nvim_buf_get_name(buf)
83-
local log_name = 'attach ' .. vim.fn.fnamemodify(file, ':t')
84-
log.debug(log_name, 'start')
83+
local log_name = string.format('attach %s', vim.fn.fnamemodify(file, ':t'))
84+
log.debug_buf(log_name, buf, 'start')
8585

8686
if vim.tbl_contains(buffers, buf) then
87-
log.debug(log_name, 'skip', 'already attached')
87+
log.debug_buf(log_name, buf, 'skip', 'already attached')
8888
return false
8989
end
9090

9191
local file_type, file_types = util.get_buf(buf, 'filetype'), state.file_types
9292
if not vim.tbl_contains(file_types, file_type) then
93-
log.debug(log_name, 'skip', 'file type', string.format('%s /∈ %s', file_type, vim.inspect(file_types)))
93+
local reason = string.format('%s /∈ %s', file_type, vim.inspect(file_types))
94+
log.debug_buf(log_name, buf, 'skip', 'file type', reason)
9495
return false
9596
end
9697

9798
local config = state.get_config(buf)
9899
if not config.enabled then
99-
log.debug(log_name, 'skip', 'state disabled')
100+
log.debug_buf(log_name, buf, 'skip', 'state disabled')
100101
return false
101102
end
102103

103104
local file_size, max_file_size = util.file_size_mb(file), config.max_file_size
104105
if file_size > max_file_size then
105-
log.debug(log_name, 'skip', 'file size', string.format('%f > %f', file_size, max_file_size))
106+
local reason = string.format('%f > %f', file_size, max_file_size)
107+
log.debug_buf(log_name, buf, 'skip', 'file size', reason)
106108
return false
107109
end
108110

109-
log.debug(log_name, 'success')
111+
log.debug_buf(log_name, buf, 'success')
110112
table.insert(buffers, buf)
111113
return true
112114
end

0 commit comments

Comments
 (0)