Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bench:

[private]
busted path:
nvim --headless --noplugin -u {{init}} -c "PlenaryBustedDirectory {{path}} {{settings}}"
nvim --headless --clean -u {{init}} -c "PlenaryBustedDirectory {{path}} {{settings}}"

health:
nvim -c "checkhealth render-markdown" -- -
Expand Down
6 changes: 6 additions & 0 deletions lua/render-markdown/lib/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ function M.buf.wins(buf)
return vim.fn.win_findbuf(buf)
end

---@param buf integer
---@return integer[]
function M.buf.windows(buf)
return M.buf.wins(buf)
end

---@class render.md.env.Win
M.win = {}

Expand Down
9 changes: 9 additions & 0 deletions lua/render-markdown/render/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ Base.__index = Base
---@param node render.md.Node
---@return boolean
function Base:execute(context, marks, node)
if type(node) ~= 'table' or type(node.height) ~= 'function' then
local Node = require('render-markdown.lib.node')
local ok, wrapped = pcall(Node.new, context.buf, node)
if not ok or not wrapped then
return false
end
node = wrapped
end

local instance = setmetatable({}, self)
instance.context = context
instance.marks = marks
Expand Down
12 changes: 10 additions & 2 deletions lua/render-markdown/request/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ View.__index = View
function View.new(buf)
local self = setmetatable({}, View)
self.buf = buf
local wins = env.buf.wins and env.buf.wins(buf)
or (env.buf.windows and env.buf.windows(buf))
or {}
local ranges = {} ---@type render.md.Range[]
for _, win in ipairs(env.buf.wins(buf)) do
ranges[#ranges + 1] = env.range(buf, win, 10)
for _, win in ipairs(wins) do
if not env.valid or env.valid(buf, win) then
ranges[#ranges + 1] = env.range(buf, win, 10)
end
end
self.ranges = interval.coalesce(ranges)
return self
Expand All @@ -34,6 +39,9 @@ end
---@param win integer
---@return boolean
function View:contains(win)
if env.valid and not env.valid(self.buf, win) then
return false
end
local rows = env.range(self.buf, win, 0)
for _, range in ipairs(self.ranges) do
if interval.contains(range, rows) then
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/base_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---@module 'luassert'

local Base = require('render-markdown.render.base')
local mock = require('luassert.mock')

describe('base', function()
it('normalizes raw nodes before setup', function()
local Node = mock(require('render-markdown.lib.node'), true)
local wrapped = {
height = function()
return 3
end,
}
Node.new.on_call_with(1, 'raw-node').returns(wrapped)

local ran = false
local Render = setmetatable({}, Base)
Render.__index = Render

function Render:setup()
return type(self.node.height) == 'function'
end

function Render:run()
ran = true
end

local ok = Render:execute({ buf = 1 }, {}, 'raw-node')
assert.is_true(ok)
assert.is_true(ran)
end)
end)
10 changes: 10 additions & 0 deletions tests/unit/view_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('view', function()
local env = mock(require('render-markdown.lib.env'), true)
env.buf.wins.on_call_with(0).returns(vim.tbl_keys(ranges))
for win, range in pairs(ranges) do
env.valid.on_call_with(0, win).returns(true)
env.range.on_call_with(0, win, 10).returns(range)
end
end
Expand All @@ -22,4 +23,13 @@ describe('view', function()
})
assert.same('[0->15,20->30,35->45]', tostring(View.new(0)))
end)

it('contains invalid window', function()
local env = mock(require('render-markdown.lib.env'), true)
env.buf.wins.on_call_with(0).returns({ 1000 })
env.valid.on_call_with(0, 1000).returns(true)
env.range.on_call_with(0, 1000, 10).returns({ 5, 10 })
env.valid.on_call_with(0, 1001).returns(false)
assert.is_false(View.new(0):contains(1001))
end)
end)