Skip to content

Commit 3fb9aff

Browse files
committed
Add function concat to concatenate Doc value
1 parent ec7b1b2 commit 3fb9aff

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/Foreign/Lua/Module/DocLayout.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ module Foreign.Lua.Module.DocLayout (
4848

4949
-- * Functions
5050
, render
51+
, concat
5152

5253
-- * Marshaling
5354
, peekDoc
5455
, pushDoc
5556
)
5657
where
5758

59+
import Prelude hiding (concat)
60+
import Data.List (intersperse)
5861
import Data.Text (Text)
5962
import Foreign.Lua (Lua, NumResults (..), Optional,
6063
Peekable, Pushable, StackIndex)
@@ -101,6 +104,7 @@ pushModule = do
101104
Lua.addfunction "rblock" rblock
102105
Lua.addfunction "vfill" vfill
103106
-- renderign
107+
Lua.addfunction "concat" concat
104108
Lua.addfunction "render" render
105109
return 1
106110

@@ -115,6 +119,13 @@ preloadModule = flip Lua.preloadhs pushModule
115119
render :: Doc Text -> Optional Int -> Lua Text
116120
render doc optLength = return $ Doc.render (Lua.fromOptional optLength) doc
117121

122+
-- | Concatenates a list of @'Doc'@s.
123+
concat :: [Doc Text] -> Optional (Doc Text) -> Lua (Doc Text)
124+
concat docs optSep = return $
125+
case Lua.fromOptional optSep of
126+
Nothing -> mconcat docs
127+
Just sep -> mconcat $ intersperse sep docs
128+
118129
--
119130
-- Constructors
120131
--

test/test-doclayout.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ return {
3030
test('empty doc', function ()
3131
assert.are_equal(doclayout.render(doclayout.empty), '')
3232
end)
33+
},
34+
35+
group 'concat' {
36+
test('without sep', function ()
37+
local list = {
38+
doclayout.literal 'one',
39+
doclayout.literal 'two',
40+
doclayout.literal 'three'
41+
}
42+
local sep = doclayout.cr
43+
assert.are_equal(
44+
doclayout.concat(list, sep),
45+
list[1] .. sep .. list[2] .. sep .. list[3])
46+
end),
47+
test('without sep', function ()
48+
local list = {
49+
doclayout.literal 'one',
50+
doclayout.literal 'two',
51+
doclayout.literal 'three'
52+
}
53+
assert.are_equal(doclayout.concat(list), list[1] .. list[2] .. list[3])
54+
end),
3355
}
3456
},
3557

0 commit comments

Comments
 (0)