Skip to content

Commit e1a2289

Browse files
chore(refactor): move various modules into new lib directory
## Details Functional changes made even though most of this is a refactor: - Listen to `CmdlineChanged` events for attached buffers to capture any scrolling done while the user is searching Move the following modules into `lib` directory: - `core.node_info` - `core.str` - `core.list` - `core.iter` - `core.icons` Other changes: - Rename `node_info` -> `node` - Replace `info` references with `node` - For modules in `lib` directory import using upper camel case
1 parent c6b59a2 commit e1a2289

31 files changed

+390
-376
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
- skip updates when state is disabled [#208](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/208)
1313
[bea6f20](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/bea6f2078e34abdf5c2632f593651bb13205477f)
14+
- disable rendering when left column is > 0 [1871dc4](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/1871dc4ced6fd775591a63df8e4c343ebaf1a2d2)
15+
- skip updates when buffer in window changes [#209](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/209)
16+
[c6b59a2](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/c6b59a263cffbd6bf463fff03b28a35ad9f1a8e6)
1417

1518
## 7.4.0 (2024-10-16)
1619

lua/render-markdown/api.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local manager = require('render-markdown.manager')
22
local state = require('render-markdown.state')
3+
local util = require('render-markdown.core.util')
34

45
---@class render.md.Api
56
local M = {}
@@ -31,7 +32,7 @@ function M.contract()
3132
end
3233

3334
function M.debug()
34-
local buf, win = vim.api.nvim_get_current_buf(), vim.api.nvim_get_current_win()
35+
local buf, win = util.current('buf'), util.current('win')
3536
local row, marks = require('render-markdown.core.ui').get_row_marks(buf, win)
3637
require('render-markdown.debug.marks').debug(row, marks)
3738
end

lua/render-markdown/core/context.lua

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
local NodeInfo = require('render-markdown.core.node_info')
1+
local Node = require('render-markdown.lib.node')
22
local Range = require('render-markdown.core.range')
3+
local Str = require('render-markdown.lib.str')
34
local log = require('render-markdown.core.log')
4-
local str = require('render-markdown.core.str')
55
local util = require('render-markdown.core.util')
66

77
---@class render.md.Context
@@ -29,9 +29,9 @@ function Context.new(buf, win, mode, offset)
2929
self.win = win
3030

3131
local ranges = { Context.compute_range(self.buf, self.win, offset) }
32-
for _, window_id in ipairs(vim.fn.win_findbuf(buf)) do
33-
if window_id ~= self.win then
34-
table.insert(ranges, Context.compute_range(self.buf, window_id, offset))
32+
for _, buf_win in ipairs(vim.fn.win_findbuf(buf)) do
33+
if buf_win ~= self.win then
34+
table.insert(ranges, Context.compute_range(self.buf, buf_win, offset))
3535
end
3636
end
3737
self.ranges = Range.coalesce(ranges)
@@ -52,74 +52,74 @@ end
5252
---@param offset integer
5353
---@return render.md.Range
5454
function Context.compute_range(buf, win, offset)
55-
local top = math.max(util.win_view(win).topline - 1 - offset, 0)
55+
local top = math.max(util.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.win_visible(win, bottom) then
62+
if util.row_visible(win, bottom) then
6363
size = size - 1
6464
end
6565
end
6666

6767
return Range.new(top, bottom)
6868
end
6969

70-
---@param info render.md.NodeInfo
70+
---@param node render.md.Node
7171
---@return render.md.CustomCallout?
72-
function Context:get_callout(info)
73-
return self.callouts[info.start_row]
72+
function Context:get_callout(node)
73+
return self.callouts[node.start_row]
7474
end
7575

76-
---@param info render.md.NodeInfo
76+
---@param node render.md.Node
7777
---@param callout render.md.CustomCallout
78-
function Context:add_callout(info, callout)
79-
self.callouts[info.start_row] = callout
78+
function Context:add_callout(node, callout)
79+
self.callouts[node.start_row] = callout
8080
end
8181

82-
---@param info render.md.NodeInfo
82+
---@param node render.md.Node
8383
---@return render.md.CustomCheckbox?
84-
function Context:get_checkbox(info)
85-
return self.checkboxes[info.start_row]
84+
function Context:get_checkbox(node)
85+
return self.checkboxes[node.start_row]
8686
end
8787

88-
---@param info render.md.NodeInfo
88+
---@param node render.md.Node
8989
---@param checkbox render.md.CustomCheckbox
90-
function Context:add_checkbox(info, checkbox)
91-
self.checkboxes[info.start_row] = checkbox
90+
function Context:add_checkbox(node, checkbox)
91+
self.checkboxes[node.start_row] = checkbox
9292
end
9393

94-
---@param info? render.md.NodeInfo
94+
---@param node? render.md.Node
9595
---@return integer
96-
function Context:width(info)
97-
if info == nil then
96+
function Context:width(node)
97+
if node == nil then
9898
return 0
9999
end
100-
return str.width(info.text) + self:get_offset(info) - self:concealed(info)
100+
return Str.width(node.text) + self:get_offset(node) - self:concealed(node)
101101
end
102102

103-
---@param info render.md.NodeInfo
103+
---@param node render.md.Node
104104
---@param amount integer
105-
function Context:add_offset(info, amount)
105+
function Context:add_offset(node, amount)
106106
if amount == 0 then
107107
return
108108
end
109-
local row = info.start_row
109+
local row = node.start_row
110110
if self.links[row] == nil then
111111
self.links[row] = {}
112112
end
113-
table.insert(self.links[row], { info.start_col, info.end_col, amount })
113+
table.insert(self.links[row], { node.start_col, node.end_col, amount })
114114
end
115115

116116
---@private
117-
---@param info render.md.NodeInfo
117+
---@param node render.md.Node
118118
---@return integer
119-
function Context:get_offset(info)
119+
function Context:get_offset(node)
120120
local result = 0
121-
for _, offset_range in ipairs(self.links[info.start_row] or {}) do
122-
if info.start_col < offset_range[2] and info.end_col > offset_range[1] then
121+
for _, offset_range in ipairs(self.links[node.start_row] or {}) do
122+
if node.start_col < offset_range[2] and node.end_col > offset_range[1] then
123123
result = result + offset_range[3]
124124
end
125125
end
@@ -142,7 +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) - util.win_textoff(self.win)
145+
self.window_width = vim.api.nvim_win_get_width(self.win) - util.textoff(self.win)
146146
end
147147
return self.window_width
148148
end
@@ -180,38 +180,38 @@ end
180180

181181
---@param root TSNode
182182
---@param query vim.treesitter.Query
183-
---@param callback fun(capture: string, node: render.md.NodeInfo)
183+
---@param callback fun(capture: string, node: render.md.Node)
184184
function Context:query(root, query, callback)
185185
for _, range in ipairs(self.ranges) do
186-
for id, node in query:iter_captures(root, self.buf, range.top, range.bottom) do
186+
for id, ts_node in query:iter_captures(root, self.buf, range.top, range.bottom) do
187187
local capture = query.captures[id]
188-
local info = NodeInfo.new(self.buf, node)
189-
log.node_info(capture, info)
190-
callback(capture, info)
188+
local node = Node.new(self.buf, ts_node)
189+
log.node(capture, node)
190+
callback(capture, node)
191191
end
192192
end
193193
end
194194

195-
---@param info? render.md.NodeInfo
195+
---@param node? render.md.Node
196196
---@return boolean
197-
function Context:hidden(info)
198-
return info == nil or str.width(info.text) == self:concealed(info)
197+
function Context:hidden(node)
198+
return node == nil or Str.width(node.text) == self:concealed(node)
199199
end
200200

201-
---@param info render.md.NodeInfo
201+
---@param node render.md.Node
202202
---@return integer
203-
function Context:concealed(info)
204-
local ranges = self:get_conceal(info.start_row)
203+
function Context:concealed(node)
204+
local ranges = self:get_conceal(node.start_row)
205205
if #ranges == 0 then
206206
return 0
207207
end
208-
local result, col = 0, info.start_col
209-
for _, index in ipairs(vim.fn.str2list(info.text)) do
208+
local result, col = 0, node.start_col
209+
for _, index in ipairs(vim.fn.str2list(node.text)) do
210210
local ch = vim.fn.nr2char(index)
211211
for _, range in ipairs(ranges) do
212212
-- Essentially vim.treesitter.is_in_node_range but only care about column
213213
if col >= range[1] and col + 1 <= range[2] then
214-
result = result + str.width(ch)
214+
result = result + Str.width(ch)
215215
end
216216
end
217217
col = col + #ch
@@ -233,7 +233,7 @@ end
233233
---@private
234234
---@return table<integer, [integer, integer][]>
235235
function Context:compute_conceal()
236-
local conceallevel = util.get_win(self.win, 'conceallevel')
236+
local conceallevel = util.get('win', self.win, 'conceallevel')
237237
if conceallevel == 0 then
238238
return {}
239239
end
@@ -270,15 +270,15 @@ function Context:compute_conceal_ranges(language, root)
270270
end
271271
local result = {}
272272
for _, range in ipairs(self.ranges) do
273-
for id, node, metadata in query:iter_captures(root, self.buf, range.top, range.bottom) do
273+
for id, ts_node, metadata in query:iter_captures(root, self.buf, range.top, range.bottom) do
274274
if metadata.conceal ~= nil then
275275
local node_range = metadata.range
276276
if node_range == nil and metadata[id] ~= nil then
277277
node_range = metadata[id].range
278278
end
279279
if node_range == nil then
280280
---@diagnostic disable-next-line: missing-fields
281-
node_range = { node:range() }
281+
node_range = { ts_node:range() }
282282
end
283283
local row, start_col, _, end_col = unpack(node_range)
284284
table.insert(result, { row, start_col, end_col })
@@ -306,10 +306,7 @@ end
306306
---@return boolean
307307
function M.contains_range(buf, win)
308308
local context = cache[buf]
309-
if context == nil then
310-
return false
311-
end
312-
return context:contains_window(win)
309+
return context ~= nil and context:contains_window(win)
313310
end
314311

315312
---@param buf integer

lua/render-markdown/core/log.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ function M.open()
3535
end
3636

3737
---@param capture string
38-
---@param info render.md.NodeInfo
39-
function M.node_info(capture, info)
40-
M.add('debug', 'node info', {
38+
---@param node render.md.Node
39+
function M.node(capture, node)
40+
M.add('debug', 'node', {
4141
capture = capture,
42-
text = info.text,
43-
rows = { info.start_row, info.end_row },
44-
cols = { info.start_col, info.end_col },
42+
text = node.text,
43+
rows = { node.start_row, node.end_row },
44+
cols = { node.start_col, node.end_col },
4545
})
4646
end
4747

lua/render-markdown/core/ui.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local BufferState = require('render-markdown.core.buffer_state')
22
local Context = require('render-markdown.core.context')
33
local Extmark = require('render-markdown.core.extmark')
4-
local Iter = require('render-markdown.core.iter')
4+
local Iter = require('render-markdown.lib.iter')
55
local log = require('render-markdown.core.log')
66
local state = require('render-markdown.state')
77
local util = require('render-markdown.core.util')
@@ -73,7 +73,7 @@ end
7373
---@param win integer
7474
---@param event string
7575
---@param change boolean
76-
function M.debounce_update(buf, win, event, change)
76+
function M.update(buf, win, event, change)
7777
log.buf('info', 'update', buf, string.format('event %s', event), string.format('change %s', change))
7878
if not util.valid(buf, win) then
7979
return
@@ -85,7 +85,7 @@ function M.debounce_update(buf, win, event, change)
8585
local parse = change or not Context.contains_range(buf, win)
8686

8787
local update = function()
88-
M.update(buf, win, parse)
88+
M.run_update(buf, win, parse)
8989
end
9090
if parse and state.log_runtime then
9191
update = util.wrap_runtime(update)
@@ -102,7 +102,7 @@ end
102102
---@param buf integer
103103
---@param win integer
104104
---@param parse boolean
105-
function M.update(buf, win, parse)
105+
function M.run_update(buf, win, parse)
106106
if not util.valid(buf, win) then
107107
return
108108
end
@@ -113,7 +113,7 @@ function M.update(buf, win, parse)
113113

114114
log.buf('info', 'state', buf, next_state)
115115
for name, value in pairs(config.win_options) do
116-
util.set_win(win, name, value[next_state])
116+
util.set('win', win, name, value[next_state])
117117
end
118118

119119
if next_state == 'rendered' then
@@ -147,10 +147,10 @@ function M.next_state(config, win, mode)
147147
if not config:render(mode) then
148148
return 'default'
149149
end
150-
if util.get_win(win, 'diff') then
150+
if util.get('win', win, 'diff') then
151151
return 'default'
152152
end
153-
if util.win_view(win).leftcol ~= 0 then
153+
if util.view(win).leftcol ~= 0 then
154154
return 'default'
155155
end
156156
return 'rendered'

0 commit comments

Comments
 (0)