Skip to content

Commit 87b9fda

Browse files
feat: Improve debug logging
## Details After all the recent changes the current logging approach does not capture all the necessary information at the right time. To get all the necessary information in addition to logging nodes we'll need to also log the marks that we produce. Doing this around the same time as node will make it easier to understand what produced what. There is already a function we call as we encounter new marks our own list.add. Rename this method to add_mark and add a logging statement, now we will be logging marks as they are generated right around the same time as the node producing the marks, sweet. To run at the right time we need to use the fact that most refresh cycles are due to cursor movements so all that would get logged are option value changes. To fix this change where the logger starts and ends to only occur on a parse refresh. Remove logging from setting options since this now happens outside of this cycle. Other minor changes include: - Add a name attribute to all log entries and change the format slightly - Always making the name of parameters optional instead of sometimes doing it to the type and other times the name
1 parent 426b135 commit 87b9fda

File tree

11 files changed

+120
-101
lines changed

11 files changed

+120
-101
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
- Improve lazy.nvim instructions [#80](https://github.com/MeanderingProgrammer/markdown.nvim/pull/80)
4141
- Improve LaTeX compatibility [#90](https://github.com/MeanderingProgrammer/markdown.nvim/pull/90)
4242
[695501b](https://github.com/MeanderingProgrammer/markdown.nvim/commit/695501bd98b1f2ec052889fc4faef24dedd7091b)
43+
- Heading block width: [#94](https://github.com/MeanderingProgrammer/markdown.nvim/pull/94)
44+
[426b135](https://github.com/MeanderingProgrammer/markdown.nvim/commit/426b13574c8264636e5660e5f5a3b4f5e3d5a937)
4345

4446
### Bug Fixes
4547

justfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ update:
4848
--input-file README.md \
4949
--vim-version 0.10.0
5050

51+
cat-log:
52+
cat ~/.local/state/nvim/render-markdown.log
53+
5154
gen-medium:
5255
just gen-file "1000" > medium.md
5356

lua/render-markdown/handler/latex.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function M.parse(root, buf)
2222
return {}
2323
end
2424
if vim.fn.executable(latex.converter) ~= 1 then
25-
logger.debug('Executable not found: ' .. latex.converter)
25+
logger.debug('executable not found', latex.converter)
2626
return {}
2727
end
2828

@@ -58,6 +58,7 @@ function M.parse(root, buf)
5858
virt_lines_above = true,
5959
},
6060
}
61+
logger.debug('mark', latex_mark)
6162
return { latex_mark }
6263
end
6364

lua/render-markdown/handler/markdown.lua

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ function M.parse(root, buf)
2525
if capture == 'heading' then
2626
vim.list_extend(marks, M.render_heading(buf, info))
2727
elseif capture == 'dash' then
28-
list.add(marks, M.render_dash(buf, info))
28+
list.add_mark(marks, M.render_dash(buf, info))
2929
elseif capture == 'code' then
3030
vim.list_extend(marks, M.render_code(buf, info))
3131
elseif capture == 'list_marker' then
3232
vim.list_extend(marks, M.render_list_marker(buf, info))
3333
elseif capture == 'checkbox_unchecked' then
34-
list.add(marks, M.render_checkbox(info, state.config.checkbox.unchecked))
34+
list.add_mark(marks, M.render_checkbox(info, state.config.checkbox.unchecked))
3535
elseif capture == 'checkbox_checked' then
36-
list.add(marks, M.render_checkbox(info, state.config.checkbox.checked))
36+
list.add_mark(marks, M.render_checkbox(info, state.config.checkbox.checked))
3737
elseif capture == 'quote' then
3838
local quote_query = state.markdown_quote_query
3939
for nested_id, nested_node in quote_query:iter_captures(info.node, buf) do
4040
local nested_capture = quote_query.captures[nested_id]
4141
local nested_info = ts.info(nested_node, buf)
4242
logger.debug_node_info(nested_capture, nested_info)
4343
if nested_capture == 'quote_marker' then
44-
list.add(marks, M.render_quote_marker(nested_info, info))
44+
list.add_mark(marks, M.render_quote_marker(nested_info, info))
4545
else
4646
logger.unhandled_capture('markdown quote', nested_capture)
4747
end
@@ -83,10 +83,10 @@ function M.render_heading(buf, info)
8383
hl_eol = heading.width == 'full',
8484
},
8585
}
86-
list.add(marks, background_mark)
86+
list.add_mark(marks, background_mark)
8787

8888
if heading.sign then
89-
list.add(marks, M.render_sign(buf, info, list.cycle(heading.signs, level), foreground))
89+
list.add_mark(marks, M.render_sign(buf, info, list.cycle(heading.signs, level), foreground))
9090
end
9191

9292
if icon == nil then
@@ -112,7 +112,7 @@ function M.render_heading(buf, info)
112112
conceal = '',
113113
},
114114
}
115-
list.add(marks, icon_mark)
115+
list.add_mark(marks, icon_mark)
116116
end
117117
else
118118
---@type render.md.Mark
@@ -127,7 +127,7 @@ function M.render_heading(buf, info)
127127
virt_text_pos = 'overlay',
128128
},
129129
}
130-
list.add(marks, icon_mark)
130+
list.add_mark(marks, icon_mark)
131131
end
132132
return marks
133133
end
@@ -212,7 +212,7 @@ function M.render_code(buf, info)
212212
virt_text_pos = 'overlay',
213213
},
214214
}
215-
list.add(marks, start_mark)
215+
list.add_mark(marks, start_mark)
216216
end
217217
local code_end = ts.child(buf, info, 'fenced_code_block_delimiter', info.end_row - 1)
218218
if ts.hidden(buf, code_end) then
@@ -227,7 +227,7 @@ function M.render_code(buf, info)
227227
virt_text_pos = 'overlay',
228228
},
229229
}
230-
list.add(marks, end_mark)
230+
list.add_mark(marks, end_mark)
231231
end
232232
end
233233

@@ -243,7 +243,7 @@ function M.render_code(buf, info)
243243
hl_eol = true,
244244
},
245245
}
246-
list.add(marks, background_mark)
246+
list.add_mark(marks, background_mark)
247247

248248
if code.width == 'block' then
249249
-- Overwrite anything beyond left_pad + block width + right_pad with Normal
@@ -261,7 +261,7 @@ function M.render_code(buf, info)
261261
virt_text_win_col = width,
262262
},
263263
}
264-
list.add(marks, block_background_mark)
264+
list.add_mark(marks, block_background_mark)
265265
end
266266
end
267267

@@ -283,7 +283,7 @@ function M.render_code(buf, info)
283283
virt_text_pos = 'inline',
284284
},
285285
}
286-
list.add(marks, row_padding_mark)
286+
list.add_mark(marks, row_padding_mark)
287287
end
288288
return marks
289289
end
@@ -304,7 +304,7 @@ function M.render_language(buf, info, code_block)
304304
end
305305
local marks = {}
306306
if code.sign then
307-
list.add(marks, M.render_sign(buf, info, icon, icon_highlight))
307+
list.add_mark(marks, M.render_sign(buf, info, icon, icon_highlight))
308308
end
309309
-- Requires inline extmarks
310310
if not util.has_10 then
@@ -332,7 +332,7 @@ function M.render_language(buf, info, code_block)
332332
virt_text_pos = 'inline',
333333
},
334334
}
335-
list.add(marks, language_marker)
335+
list.add_mark(marks, language_marker)
336336
return marks
337337
end
338338

@@ -399,7 +399,7 @@ function M.render_list_marker(buf, info)
399399
virt_text_pos = 'overlay',
400400
},
401401
}
402-
list.add(marks, bullet_mark)
402+
list.add_mark(marks, bullet_mark)
403403
-- Requires inline extmarks
404404
if util.has_10 and bullet.right_pad > 0 then
405405
---@type render.md.Mark
@@ -412,7 +412,7 @@ function M.render_list_marker(buf, info)
412412
virt_text_pos = 'inline',
413413
},
414414
}
415-
list.add(marks, padding_mark)
415+
list.add_mark(marks, padding_mark)
416416
end
417417
return marks
418418
end
@@ -472,7 +472,7 @@ end
472472
---@private
473473
---@param buf integer
474474
---@param info render.md.NodeInfo
475-
---@param text string?
475+
---@param text? string
476476
---@param highlight string
477477
---@return render.md.Mark?
478478
function M.render_sign(buf, info, text, highlight)
@@ -515,7 +515,7 @@ function M.render_table(buf, info)
515515
local row = ts.info(row_node, buf)
516516
if row.type == 'pipe_table_delimiter_row' then
517517
delim = row
518-
list.add(marks, M.render_table_delimiter(row))
518+
list.add_mark(marks, M.render_table_delimiter(row))
519519
elseif row.type == 'pipe_table_header' then
520520
first = row
521521
vim.list_extend(marks, M.render_table_row(buf, row, pipe_table.head))
@@ -586,7 +586,7 @@ function M.render_table_row(buf, row, highlight)
586586
virt_text_pos = 'overlay',
587587
},
588588
}
589-
list.add(marks, pipe_mark)
589+
list.add_mark(marks, pipe_mark)
590590
elseif cell.type == 'pipe_table_cell' then
591591
-- Requires inline extmarks
592592
if pipe_table.cell == 'padded' and util.has_10 then
@@ -602,7 +602,7 @@ function M.render_table_row(buf, row, highlight)
602602
virt_text_pos = 'inline',
603603
},
604604
}
605-
list.add(marks, padding_mark)
605+
list.add_mark(marks, padding_mark)
606606
end
607607
end
608608
else
@@ -622,7 +622,7 @@ function M.render_table_row(buf, row, highlight)
622622
virt_text_pos = 'overlay',
623623
},
624624
}
625-
list.add(marks, overlay_mark)
625+
list.add_mark(marks, overlay_mark)
626626
end
627627
return marks
628628
end

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ function M.parse(root, buf)
2121
local info = ts.info(node, buf)
2222
logger.debug_node_info(capture, info)
2323
if capture == 'code' then
24-
list.add(marks, M.render_code(info))
24+
list.add_mark(marks, M.render_code(info))
2525
elseif capture == 'callout' then
26-
list.add(marks, M.render_callout(info))
26+
list.add_mark(marks, M.render_callout(info))
2727
elseif capture == 'link' then
28-
list.add(marks, M.render_link(info))
28+
list.add_mark(marks, M.render_link(info))
2929
else
3030
logger.unhandled_capture('inline', capture)
3131
end

lua/render-markdown/health.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function M.version(minimum, recommended)
6666
end
6767

6868
---@param language string
69-
---@param advice string?
69+
---@param advice? string
7070
function M.check_parser(language, advice)
7171
local parsers = require('nvim-treesitter.parsers')
7272
if parsers.has_parser(language) then
@@ -89,7 +89,7 @@ function M.check_highlight(language)
8989
end
9090

9191
---@param name string
92-
---@param advice string?
92+
---@param advice? string
9393
function M.check_executable(name, advice)
9494
if vim.fn.executable(name) == 1 then
9595
vim.health.ok(name .. ': installed')
@@ -101,7 +101,7 @@ function M.check_executable(name, advice)
101101
end
102102

103103
---@param name string
104-
---@param advice string[]?
104+
---@param advice? string[]
105105
function M.check_plugin(name, advice)
106106
local has_plugin = pcall(require, name)
107107
if not has_plugin then

lua/render-markdown/list.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
local logger = require('render-markdown.logger')
2+
13
---@class render.md.ListHelper
24
local M = {}
35

4-
---@param values render.md.Mark[]
5-
---@param value? render.md.Mark
6-
function M.add(values, value)
7-
if value ~= nil then
8-
table.insert(values, value)
6+
---@param marks render.md.Mark[]
7+
---@param new_mark? render.md.Mark
8+
function M.add_mark(marks, new_mark)
9+
if new_mark ~= nil then
10+
logger.debug('mark', new_mark)
11+
table.insert(marks, new_mark)
912
end
1013
end
1114

lua/render-markdown/logger.lua

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ end
1616
---@class render.md.LogEntry
1717
---@field date string
1818
---@field level string
19+
---@field name string
1920
---@field message string
2021

2122
---@class render.md.Log
@@ -29,13 +30,15 @@ function log.reset()
2930
end
3031

3132
---@param level string
33+
---@param name string
3234
---@param message any
33-
function log.add(level, message)
35+
function log.add(level, name, message)
3436
---@type render.md.LogEntry
3537
local entry = {
3638
---@diagnostic disable-next-line: assign-type-mismatch
3739
date = os.date('%Y-%m-%d %H:%M:%S'),
3840
level = string.upper(level),
41+
name = name,
3942
message = convert_message(message),
4043
}
4144
table.insert(log.entries, entry)
@@ -47,7 +50,7 @@ function log.flush()
4750
end
4851
local file = assert(io.open(log_file, 'w'))
4952
for _, entry in ipairs(log.entries) do
50-
local line = string.format('%s - %s - %s', entry.date, entry.level, entry.message)
53+
local line = string.format('%s [%s] %s: %s', entry.date, entry.level, entry.name, entry.message)
5154
file:write(line .. '\n')
5255
end
5356
file:close()
@@ -61,24 +64,27 @@ function M.start()
6164
log.reset()
6265
end
6366

67+
---@param name string
6468
---@param message any
65-
function M.debug(message)
69+
function M.debug(name, message)
6670
if vim.tbl_contains({ 'debug' }, state.config.log_level) then
67-
log.add('debug', message)
71+
log.add('debug', name, message)
6872
end
6973
end
7074

75+
---@private
76+
---@param name string
7177
---@param message any
72-
function M.error(message)
78+
function M.error(name, message)
7379
if vim.tbl_contains({ 'debug', 'error' }, state.config.log_level) then
74-
log.add('error', message)
80+
log.add('error', name, message)
7581
end
7682
end
7783

7884
---@param capture string
7985
---@param info render.md.NodeInfo
8086
function M.debug_node_info(capture, info)
81-
M.debug({
87+
M.debug('node info', {
8288
capture = capture,
8389
text = info.text,
8490
rows = { info.start_row, info.end_row },
@@ -90,15 +96,15 @@ end
9096
---@param group string
9197
---@param capture string
9298
function M.unhandled_capture(group, capture)
93-
M.error(string.format('Unhandled %s capture: %s', group, capture))
99+
M.error('unhandled capture', string.format('%s -> %s', group, capture))
94100
end
95101

96102
---Encountered if new type is seen for a particular group
97103
---@param language string
98104
---@param group string
99105
---@param value string
100106
function M.unhandled_type(language, group, value)
101-
M.error(string.format('Unhandled %s %s type: %s', language, group, value))
107+
M.error('unhandled type', string.format('%s -> %s -> %s', language, group, value))
102108
end
103109

104110
function M.flush()

lua/render-markdown/str.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---@class render.md.StringHelper
22
local M = {}
33

4-
---@param s string?
4+
---@param s? string
55
---@return integer
66
function M.width(s)
77
if s == nil then
@@ -26,7 +26,7 @@ function M.pad_to(value, s)
2626
end
2727

2828
---@param padding integer
29-
---@param s string?
29+
---@param s? string
3030
---@return string
3131
function M.pad(padding, s)
3232
local result = string.rep(' ', padding)

0 commit comments

Comments
 (0)