Skip to content

Commit 00cace9

Browse files
chore(refactor): replace util module with env module
## Details Mostly a rename of: - `local util = require('render-markdown.core.util')` - `local Env = require('render-markdown.lib.env')` Changes include: - Use a nested class structure rather than supplying `variant` parameters to functions, i.e. `util.get('win', win, 'diff')` -> `Env.win.get(win, 'diff')` - Consists of `row`, `mode`, `buf`, and `win` nested classes - Move `file_name` method into `log` module directly since it's the only user of it - Add `conceal_lines` validation in `opts` (currently no rendering uses this), update format of associated errors
1 parent c21693f commit 00cace9

File tree

16 files changed

+304
-244
lines changed

16 files changed

+304
-244
lines changed

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2025 March 24
1+
*render-markdown.txt* For 0.10.0 Last change: 2025 March 25
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

lua/render-markdown/api.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
local Env = require('render-markdown.lib.env')
12
local manager = require('render-markdown.manager')
23
local state = require('render-markdown.state')
3-
local util = require('render-markdown.core.util')
44

55
---@class render.md.Api
66
local M = {}
@@ -44,7 +44,7 @@ function M.contract()
4444
end
4545

4646
function M.debug()
47-
local buf, win = util.current('buf'), util.current('win')
47+
local buf, win = Env.buf.current(), Env.win.current()
4848
local row, marks = require('render-markdown.core.ui').get_row_marks(buf, win)
4949
require('render-markdown.debug.marks').debug(row, marks)
5050
end

lua/render-markdown/config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
local Env = require('render-markdown.lib.env')
12
local Range = require('render-markdown.core.range')
2-
local util = require('render-markdown.core.util')
33

44
---@class render.md.component.Config
55
---@field callout table<string, render.md.CustomCallout>
@@ -70,7 +70,7 @@ end
7070
---@param mode string
7171
---@return boolean
7272
function Config:render(mode)
73-
return util.in_modes(self.modes, mode)
73+
return Env.mode.is(mode, self.modes)
7474
end
7575

7676
---@param node render.md.Node

lua/render-markdown/core/buffer.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local Env = require('render-markdown.lib.env')
2+
13
---@class render.md.Buffer
24
---@field private buf integer
35
---@field private empty boolean
@@ -13,7 +15,7 @@ function Buffer.new(buf)
1315
local self = setmetatable({}, Buffer)
1416
self.buf = buf
1517
self.empty = true
16-
self.timer = (vim.uv or vim.loop).new_timer()
18+
self.timer = Env.uv.new_timer()
1719
self.running = false
1820
self.marks = nil
1921
return self

lua/render-markdown/core/conceal.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
local Env = require('render-markdown.lib.env')
12
local Str = require('render-markdown.lib.str')
2-
local util = require('render-markdown.core.util')
33

44
---@class render.md.conceal.Section
55
---@field start_col integer
@@ -82,7 +82,7 @@ end
8282
---@return boolean
8383
function Conceal:hidden(context, node)
8484
-- conceal lines metadata require neovim >= 0.11.0 to function
85-
return util.has_11 and self:line(context, node).hidden
85+
return Env.has_11 and self:line(context, node).hidden
8686
end
8787

8888
---@param context render.md.Context

lua/render-markdown/core/context.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
local Conceal = require('render-markdown.core.conceal')
2+
local Env = require('render-markdown.lib.env')
23
local Node = require('render-markdown.lib.node')
34
local Range = require('render-markdown.core.range')
45
local Str = require('render-markdown.lib.str')
56
local log = require('render-markdown.core.log')
6-
local util = require('render-markdown.core.util')
77

88
---@class render.md.context.Props
99
---@field buf integer
@@ -35,7 +35,7 @@ function Context.new(props, offset)
3535
self.win = props.win
3636

3737
local ranges = {}
38-
for _, window in ipairs(util.windows(self.buf)) do
38+
for _, window in ipairs(Env.buf.windows(self.buf)) do
3939
table.insert(ranges, Context.compute_range(self.buf, window, offset))
4040
end
4141
self.ranges = Range.coalesce(ranges)
@@ -47,7 +47,7 @@ function Context.new(props, offset)
4747

4848
self.mode = props.mode
4949
self.top_level_mode = props.top_level_mode
50-
self.conceal = Conceal.new(self.buf, util.get('win', self.win, 'conceallevel'))
50+
self.conceal = Conceal.new(self.buf, Env.win.get(self.win, 'conceallevel'))
5151
self.last_heading = nil
5252

5353
return self
@@ -59,14 +59,14 @@ end
5959
---@param offset integer
6060
---@return render.md.Range
6161
function Context.compute_range(buf, win, offset)
62-
local top = math.max(util.view(win).topline - 1 - offset, 0)
62+
local top = math.max(Env.win.view(win).topline - 1 - offset, 0)
6363

6464
local bottom = top
6565
local lines = vim.api.nvim_buf_line_count(buf)
6666
local size = vim.api.nvim_win_get_height(win) + (2 * offset)
6767
while bottom < lines and size > 0 do
6868
bottom = bottom + 1
69-
if util.row_visible(win, bottom) then
69+
if Env.row.visible(win, bottom) then
7070
size = size - 1
7171
end
7272
end
@@ -86,7 +86,7 @@ function Context:skip(component)
8686
return false
8787
end
8888
-- Enabled components in component modes should not be skipped
89-
return not util.in_modes(component.render_modes, self.mode)
89+
return not Env.mode.is(self.mode, component.render_modes)
9090
end
9191

9292
---@return integer
@@ -120,7 +120,7 @@ end
120120

121121
---@return integer
122122
function Context:tab_size()
123-
return util.get('buf', self.buf, 'tabstop')
123+
return Env.buf.get(self.buf, 'tabstop')
124124
end
125125

126126
---@param node? render.md.Node
@@ -186,7 +186,7 @@ end
186186
---@return integer
187187
function Context:get_width()
188188
if self.window_width == nil then
189-
self.window_width = vim.api.nvim_win_get_width(self.win) - util.textoff(self.win)
189+
self.window_width = Env.win.width(self.win)
190190
end
191191
return self.window_width
192192
end

lua/render-markdown/core/log.lua

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local util = require('render-markdown.core.util')
1+
local Env = require('render-markdown.lib.env')
22

33
---@class render.md.log.Entry
44
---@field date string
@@ -24,7 +24,7 @@ function M.setup(level)
2424
-- Typically resolves to ~/.local/state/nvim/render-markdown.log
2525
M.file = vim.fn.stdpath('state') .. '/render-markdown.log'
2626
-- Clear the file contents if it is too big
27-
if util.file_size_mb(M.file) > 5 then
27+
if Env.file_size_mb(M.file) > 5 then
2828
assert(io.open(M.file, 'w')):close()
2929
end
3030
end
@@ -58,7 +58,19 @@ end
5858
---@param buf integer
5959
---@param ... any
6060
function M.buf(level, name, buf, ...)
61-
M.add(level, string.format('%s|%s|%d', name, util.file_name(buf), buf), ...)
61+
M.add(level, string.format('%s|%s|%d', name, M.file_name(buf), buf), ...)
62+
end
63+
64+
---@private
65+
---@param buf integer
66+
---@return string
67+
function M.file_name(buf)
68+
if Env.buf.valid(buf) then
69+
local file = vim.api.nvim_buf_get_name(buf)
70+
return vim.fn.fnamemodify(file, ':t')
71+
else
72+
return 'INVALID'
73+
end
6274
end
6375

6476
---@param level render.md.config.LogLevel

lua/render-markdown/core/ui.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
local Buffer = require('render-markdown.core.buffer')
22
local Context = require('render-markdown.core.context')
3+
local Env = require('render-markdown.lib.env')
34
local Extmark = require('render-markdown.core.extmark')
45
local Iter = require('render-markdown.lib.iter')
56
local log = require('render-markdown.core.log')
67
local state = require('render-markdown.state')
7-
local util = require('render-markdown.core.util')
88

99
---@type table<string, render.md.Handler>
1010
local builtin_handlers = {
@@ -48,7 +48,7 @@ end
4848
---@return integer, render.md.Mark[]
4949
function M.get_row_marks(buf, win)
5050
local config, buffer = state.get(buf), Cache.get(buf)
51-
local mode, row = util.mode(), util.row(buf, win)
51+
local mode, row = Env.mode.get(), Env.row.get(buf, win)
5252
local hidden = config:hidden(mode, row)
5353
assert(row ~= nil and hidden ~= nil, 'Row & range must be known to get marks')
5454

@@ -76,7 +76,7 @@ end
7676
---@param change boolean
7777
function M.update(buf, win, event, change)
7878
log.buf('info', 'update', buf, string.format('event %s', event), string.format('change %s', change))
79-
if util.invalid(buf, win) then
79+
if not Env.valid(buf, win) then
8080
return
8181
end
8282

@@ -90,7 +90,7 @@ function M.update(buf, win, event, change)
9090
M.run_update(buf, win, change)
9191
end
9292
if parse and state.log_runtime then
93-
update = util.wrap_runtime(update)
93+
update = Env.runtime(update)
9494
end
9595

9696
if parse and config.debounce > 0 then
@@ -115,19 +115,19 @@ end
115115
---@param win integer
116116
---@param change boolean
117117
function M.run_update(buf, win, change)
118-
if util.invalid(buf, win) then
118+
if not Env.valid(buf, win) then
119119
return
120120
end
121121

122122
local parse = M.parse(buf, win, change)
123123
local config, buffer = state.get(buf), Cache.get(buf)
124-
local mode, row = util.mode(), util.row(buf, win)
124+
local mode, row = Env.mode.get(), Env.row.get(buf, win)
125125
local next_state = M.next_state(config, win, mode)
126126

127127
log.buf('info', 'state', buf, next_state)
128-
for _, window in ipairs(util.windows(buf)) do
128+
for _, window in ipairs(Env.buf.windows(buf)) do
129129
for name, value in pairs(config.win_options) do
130-
util.set('win', window, name, value[next_state])
130+
Env.win.set(window, name, value[next_state])
131131
end
132132
end
133133

@@ -138,7 +138,7 @@ function M.run_update(buf, win, change)
138138
buf = buf,
139139
win = win,
140140
mode = mode,
141-
top_level_mode = util.in_modes(config.render_modes, mode),
141+
top_level_mode = Env.mode.is(mode, config.render_modes),
142142
}))
143143
end
144144
local hidden = config:hidden(mode, row)
@@ -172,10 +172,10 @@ function M.next_state(config, win, mode)
172172
if not config:render(mode) then
173173
return 'default'
174174
end
175-
if util.get('win', win, 'diff') then
175+
if Env.win.get(win, 'diff') then
176176
return 'default'
177177
end
178-
if util.view(win).leftcol ~= 0 then
178+
if Env.win.view(win).leftcol ~= 0 then
179179
return 'default'
180180
end
181181
return 'rendered'

0 commit comments

Comments
 (0)