Skip to content

Commit 1d825f4

Browse files
committed
Make "adding" Doc's equivalent to concatenating with space
1 parent e1cbed7 commit 1d825f4

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/Foreign/Lua/Module/DocLayout.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ where
5858
import Data.Text (Text)
5959
import Foreign.Lua (Lua, NumResults (..), Optional,
6060
Peekable, Pushable, StackIndex)
61-
import Text.DocLayout (Doc)
61+
import Text.DocLayout ((<+>), Doc)
6262

6363
import qualified Data.Text as T
6464
import qualified Foreign.Lua as Lua
@@ -249,16 +249,17 @@ pushDoc :: Doc Text -> Lua ()
249249
pushDoc = Lua.pushAnyWithMetatable pushDocMT
250250
where
251251
pushDocMT = Lua.ensureUserdataMetatable docTypeName $ do
252+
Lua.addfunction "__add" __add
252253
Lua.addfunction "__concat" __concat
253254
Lua.addfunction "__eq" __eq
254255
Lua.addfunction "__tostring" __tostring
255256

256257
instance Pushable (Doc Text) where
257258
push = pushDoc
258259

259-
-- | Convert to string by rendering without reflowing.
260-
__tostring :: Doc Text -> Lua Text
261-
__tostring d = return $ Doc.render Nothing d
260+
-- | Concatenate two @'Doc'@s, putting breakable spaces between them.
261+
__add :: Doc Text -> Doc Text -> Lua (Doc Text)
262+
__add a b = return (a <+> b)
262263

263264
-- | Concatenate two @'Doc'@.
264265
__concat :: Doc Text -> Doc Text -> Lua (Doc Text)
@@ -267,3 +268,7 @@ __concat a b = return (a <> b)
267268
-- | Test @'Doc'@ equality.
268269
__eq :: Doc Text -> Doc Text -> Lua Bool
269270
__eq a b = return (a == b)
271+
272+
-- | Convert to string by rendering without reflowing.
273+
__tostring :: Doc Text -> Lua Text
274+
__tostring d = return $ Doc.render Nothing d

test/test-doclayout.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ return {
5252
test('has tostring method', function ()
5353
local str = 'just a literal string for now'
5454
assert.are_equal(tostring(str), str)
55+
end),
56+
57+
test('adding concatenates with space', function ()
58+
local helloworld = doclayout.literal "Hello,"
59+
.. doclayout.space
60+
.. doclayout.literal "World"
61+
assert.are_equal(doclayout.literal 'Hello,' + 'World', helloworld)
5562
end)
5663
}
5764
}

0 commit comments

Comments
 (0)