Skip to content

Commit e1cbed7

Browse files
committed
Make Doc values concatenable
1 parent 59a4977 commit e1cbed7

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/Foreign/Lua/Module/DocLayout.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ instance Peekable (Doc Text) where
248248
pushDoc :: Doc Text -> Lua ()
249249
pushDoc = Lua.pushAnyWithMetatable pushDocMT
250250
where
251-
pushDocMT = Lua.ensureUserdataMetatable docTypeName $
251+
pushDocMT = Lua.ensureUserdataMetatable docTypeName $ do
252+
Lua.addfunction "__concat" __concat
253+
Lua.addfunction "__eq" __eq
252254
Lua.addfunction "__tostring" __tostring
253255

254256
instance Pushable (Doc Text) where
@@ -257,3 +259,11 @@ instance Pushable (Doc Text) where
257259
-- | Convert to string by rendering without reflowing.
258260
__tostring :: Doc Text -> Lua Text
259261
__tostring d = return $ Doc.render Nothing d
262+
263+
-- | Concatenate two @'Doc'@.
264+
__concat :: Doc Text -> Doc Text -> Lua (Doc Text)
265+
__concat a b = return (a <> b)
266+
267+
-- | Test @'Doc'@ equality.
268+
__eq :: Doc Text -> Doc Text -> Lua Bool
269+
__eq a b = return (a == b)

test/test-doclayout.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ return {
3838
assert.are_equal(doclayout.render('hello world'), 'hello world')
3939
end),
4040

41+
test('equality', function ()
42+
assert.is_truthy(doclayout.literal "true", doclayout.literal "true")
43+
end),
44+
45+
test('concatenate docs', function ()
46+
assert.are_equal(
47+
tostring(doclayout.literal 'Rock-' .. doclayout.literal 'Ola'),
48+
'Rock-Ola'
49+
)
50+
end),
51+
4152
test('has tostring method', function ()
4253
local str = 'just a literal string for now'
4354
assert.are_equal(tostring(str), str)

0 commit comments

Comments
 (0)