Skip to content

Commit 467c135

Browse files
Add basic logging to file to improve debugging during development
1 parent 90637a1 commit 467c135

File tree

9 files changed

+121
-13
lines changed

9 files changed

+121
-13
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ require('render-markdown').setup({
8585
inline_query = [[
8686
(code_span) @code
8787
]],
88+
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
89+
-- Only intended to be used for plugin development / debugging
90+
log_level = 'error',
8891
-- Filetypes this plugin will run on
8992
file_types = { 'markdown' },
9093
-- Vim modes that will show a rendered view of the markdown file

doc/render-markdown.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.9.5 Last change: 2024 April 13
1+
*render-markdown.txt* For 0.9.5 Last change: 2024 May 01
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -112,6 +112,9 @@ modified by the user.
112112
inline_query = [[
113113
(code_span) @code
114114
]],
115+
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
116+
-- Only intended to be used for plugin development / debugging
117+
log_level = 'error',
115118
-- Filetypes this plugin will run on
116119
file_types = { 'markdown' },
117120
-- Vim modes that will show a rendered view of the markdown file

lua/render-markdown/handler/latex.lua

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local logger = require('render-markdown.logger')
12
local state = require('render-markdown.state')
23

34
---@class Cache
@@ -17,19 +18,21 @@ M.render = function(namespace, root)
1718
return
1819
end
1920

20-
local latex = vim.treesitter.get_node_text(root, 0)
21-
local cached_expressions = cache.expressions[latex]
22-
if cached_expressions == nil then
23-
local raw_expression = vim.fn.system('latex2text', latex)
24-
local expressions = vim.split(vim.trim(raw_expression), '\n', { plain = true })
25-
cached_expressions = vim.tbl_map(vim.trim, expressions)
26-
cache.expressions[latex] = cached_expressions
21+
local value = vim.treesitter.get_node_text(root, 0)
22+
local start_row, start_col, end_row, end_col = root:range()
23+
logger.debug_node('latex', root)
24+
25+
local expressions = cache.expressions[value]
26+
if expressions == nil then
27+
local raw_expression = vim.fn.system('latex2text', value)
28+
local parsed_expressions = vim.split(vim.trim(raw_expression), '\n', { plain = true })
29+
expressions = vim.tbl_map(vim.trim, parsed_expressions)
30+
cache.expressions[value] = expressions
2731
end
2832

29-
local start_row, start_col, end_row, end_col = root:range()
3033
local virt_lines = vim.tbl_map(function(expression)
3134
return { { expression, state.config.highlights.latex } }
32-
end, cached_expressions)
35+
end, expressions)
3336
vim.api.nvim_buf_set_extmark(0, namespace, start_row, start_col, {
3437
end_row = end_row,
3538
end_col = end_col,

lua/render-markdown/handler/markdown.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local list = require('render-markdown.list')
2+
local logger = require('render-markdown.logger')
23
local state = require('render-markdown.state')
34

45
local M = {}
@@ -12,6 +13,7 @@ M.render = function(namespace, root)
1213
local capture = state.markdown_query.captures[id]
1314
local value = vim.treesitter.get_node_text(node, 0)
1415
local start_row, start_col, end_row, end_col = node:range()
16+
logger.debug_node(capture, node)
1517

1618
if capture == 'heading' then
1719
local level = #value
@@ -137,7 +139,7 @@ M.render = function(namespace, root)
137139
})
138140
else
139141
-- Should only get here if user provides custom capture, currently unhandled
140-
vim.print('Unhandled markdown capture: ' .. capture)
142+
logger.error('Unhandled markdown capture: ' .. capture)
141143
end
142144
end
143145
end

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local logger = require('render-markdown.logger')
12
local state = require('render-markdown.state')
23

34
local M = {}
@@ -10,6 +11,7 @@ M.render = function(namespace, root)
1011
for id, node in state.inline_query:iter_captures(root, 0) do
1112
local capture = state.inline_query.captures[id]
1213
local start_row, start_col, end_row, end_col = node:range()
14+
logger.debug_node(capture, node)
1315

1416
if capture == 'code' then
1517
vim.api.nvim_buf_set_extmark(0, namespace, start_row, start_col, {
@@ -19,7 +21,7 @@ M.render = function(namespace, root)
1921
})
2022
else
2123
-- Should only get here if user provides custom capture, currently unhandled
22-
vim.print('Unhandled inline capture: ' .. capture)
24+
logger.error('Unhandled inline capture: ' .. capture)
2325
end
2426
end
2527
end

lua/render-markdown/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ local M = {}
3636
---@class UserConfig
3737
---@field public markdown_query? string
3838
---@field public inline_query? string
39+
---@field public log_level? 'debug'|'error'
3940
---@field public file_types? string[]
4041
---@field public render_modes? string[]
4142
---@field public headings? string[]
@@ -47,7 +48,7 @@ local M = {}
4748
---@field public fat_tables? boolean
4849
---@field public highlights? UserHighlights
4950

50-
---@param opts UserConfig|nil
51+
---@param opts? UserConfig
5152
function M.setup(opts)
5253
---@type Config
5354
local default_config = {
@@ -85,6 +86,7 @@ function M.setup(opts)
8586
inline_query = [[
8687
(code_span) @code
8788
]],
89+
log_level = 'error',
8890
file_types = { 'markdown' },
8991
render_modes = { 'n', 'c' },
9092
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },

lua/render-markdown/logger.lua

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
local state = require('render-markdown.state')
2+
3+
-- Typically resolves to ~/.local/state/nvim/render-markdown.log
4+
local log_file = vim.fn.stdpath('state') .. '/render-markdown.log'
5+
6+
---@class LogEntry
7+
---@field date string
8+
---@field level string
9+
---@field message string
10+
11+
---@class Log
12+
---@field entries LogEntry[]
13+
local log = {
14+
entries = {},
15+
}
16+
17+
log.reset = function()
18+
log.entries = {}
19+
end
20+
21+
---@param level string
22+
---@param message any
23+
log.add = function(level, message)
24+
---@type LogEntry
25+
local entry = {
26+
---@diagnostic disable-next-line: assign-type-mismatch
27+
date = os.date('%Y-%m-%d %H:%M:%S'),
28+
level = string.upper(level),
29+
message = vim.inspect(message),
30+
}
31+
table.insert(log.entries, entry)
32+
end
33+
34+
log.flush = function()
35+
if #log.entries == 0 then
36+
return
37+
end
38+
local file = assert(io.open(log_file, 'w'))
39+
for _, entry in ipairs(log.entries) do
40+
local line = string.format('%s - %s - %s', entry.date, entry.level, entry.message)
41+
file:write(line .. '\n')
42+
end
43+
file:close()
44+
log.reset()
45+
end
46+
47+
local M = {}
48+
49+
M.start = function()
50+
log.reset()
51+
end
52+
53+
---@param message any
54+
M.debug = function(message)
55+
if vim.tbl_contains({ 'debug' }, state.config.log_level) then
56+
log.add('debug', message)
57+
end
58+
end
59+
60+
---@param capture string
61+
---@param node TSNode
62+
M.debug_node = function(capture, node)
63+
if vim.tbl_contains({ 'debug' }, state.config.log_level) then
64+
local value = vim.treesitter.get_node_text(node, 0)
65+
local start_row, start_col, end_row, end_col = node:range()
66+
log.add('debug', {
67+
capture = capture,
68+
value = value,
69+
rows = { start_row, end_row },
70+
cols = { start_col, end_col },
71+
})
72+
end
73+
end
74+
75+
---@param message any
76+
M.error = function(message)
77+
if vim.tbl_contains({ 'debug', 'error' }, state.config.log_level) then
78+
log.add('error', message)
79+
end
80+
end
81+
82+
M.flush = function()
83+
log.flush()
84+
end
85+
86+
return M

lua/render-markdown/state.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
---@class Config
3232
---@field public markdown_query string
3333
---@field public inline_query string
34+
---@field public log_level 'debug'|'error'
3435
---@field public file_types string[]
3536
---@field public render_modes string[]
3637
---@field public headings string[]

lua/render-markdown/ui.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local latex = require('render-markdown.handler.latex')
2+
local logger = require('render-markdown.logger')
23
local markdown = require('render-markdown.handler.markdown')
34
local markdown_inline = require('render-markdown.handler.markdown_inline')
45
local state = require('render-markdown.state')
@@ -26,17 +27,22 @@ M.refresh = function()
2627
return
2728
end
2829

30+
logger.start()
2931
vim.opt_local.conceallevel = state.config.conceal.rendered
3032
vim.treesitter.get_parser():for_each_tree(function(tree, language_tree)
3133
local language = language_tree:lang()
34+
logger.debug({ language = language })
3235
if language == 'markdown' then
3336
markdown.render(M.namespace, tree:root())
3437
elseif language == 'markdown_inline' then
3538
markdown_inline.render(M.namespace, tree:root())
3639
elseif language == 'latex' then
3740
latex.render(M.namespace, tree:root())
41+
else
42+
logger.debug('No handler found')
3843
end
3944
end)
45+
logger.flush()
4046
end
4147

4248
return M

0 commit comments

Comments
 (0)