Skip to content

Commit 59a4977

Browse files
committed
Expose Doc constructors and combinators
1 parent 4725900 commit 59a4977

2 files changed

Lines changed: 177 additions & 5 deletions

File tree

src/Foreign/Lua/Module/DocLayout.hs

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@ module Foreign.Lua.Module.DocLayout (
1919
pushModule
2020
, preloadModule
2121

22+
-- * Doc constructors and combinators
23+
, after_break
24+
, before_non_blank
25+
, blankline
26+
, blanklines
27+
, braces
28+
, brackets
29+
, cblock
30+
, chomp
31+
, cr
32+
, double_quotes
33+
, empty
34+
, flush
35+
, hang
36+
, inside
37+
, lblock
38+
, literal
39+
, nest
40+
, nestle
41+
, nowrap
42+
, parens
43+
, prefixed
44+
, quotes
45+
, rblock
46+
, space
47+
, vfill
48+
2249
-- * Functions
2350
, render
2451

@@ -33,6 +60,7 @@ import Foreign.Lua (Lua, NumResults (..), Optional,
3360
Peekable, Pushable, StackIndex)
3461
import Text.DocLayout (Doc)
3562

63+
import qualified Data.Text as T
3664
import qualified Foreign.Lua as Lua
3765
import qualified Foreign.Lua.Types.Peekable as Lua
3866
import qualified Foreign.Lua.Userdata as Lua
@@ -46,7 +74,33 @@ import qualified Text.DocLayout as Doc
4674
pushModule :: Lua NumResults
4775
pushModule = do
4876
Lua.newtable
49-
Lua.addfield "empty_doc" (Doc.empty :: Doc Text)
77+
-- constructors
78+
Lua.addfield "empty" empty
79+
Lua.addfield "blankline" blankline
80+
Lua.addfield "cr" cr
81+
Lua.addfield "space" space
82+
Lua.addfunction "after_break" after_break
83+
Lua.addfunction "before_non_blank" before_non_blank
84+
Lua.addfunction "blanklines" blanklines
85+
Lua.addfunction "braces" braces
86+
Lua.addfunction "brackets" brackets
87+
Lua.addfunction "cblock" cblock
88+
Lua.addfunction "chomp" chomp
89+
Lua.addfunction "double_quotes" double_quotes
90+
Lua.addfunction "flush" flush
91+
Lua.addfunction "hang" hang
92+
Lua.addfunction "inside" inside
93+
Lua.addfunction "lblock" lblock
94+
Lua.addfunction "literal" literal
95+
Lua.addfunction "nest" nest
96+
Lua.addfunction "nestle" nestle
97+
Lua.addfunction "nowrap" nowrap
98+
Lua.addfunction "parens" parens
99+
Lua.addfunction "quotes" quotes
100+
Lua.addfunction "prefixed" prefixed
101+
Lua.addfunction "rblock" rblock
102+
Lua.addfunction "vfill" vfill
103+
-- renderign
50104
Lua.addfunction "render" render
51105
return 1
52106

@@ -61,6 +115,115 @@ preloadModule = flip Lua.preloadhs pushModule
61115
render :: Doc Text -> Optional Int -> Lua Text
62116
render doc optLength = return $ Doc.render (Lua.fromOptional optLength) doc
63117

118+
--
119+
-- Constructors
120+
--
121+
122+
-- | Creates a @'Doc'@ which is conditionally included only if it
123+
-- comes at the beginning of a line.
124+
after_break :: Text -> Lua (Doc Text)
125+
after_break = return . Doc.afterBreak
126+
127+
-- | Conditionally includes the given @'Doc'@ unless it is
128+
-- followed by a blank space.
129+
before_non_blank :: Doc Text -> Lua (Doc Text)
130+
before_non_blank = return . Doc.beforeNonBlank
131+
132+
-- | Inserts a blank line unless one exists already.
133+
blankline :: Doc Text
134+
blankline = Doc.blankline
135+
136+
-- | Insert blank lines unless they exist already.
137+
blanklines :: Int -> Lua (Doc Text)
138+
blanklines = return . Doc.blanklines
139+
140+
-- | Puts a @'Doc'@ in curly braces.
141+
braces :: Doc Text -> Lua (Doc Text)
142+
braces = return . Doc.braces
143+
144+
-- | Puts a @'Doc'@ in square brackets.
145+
brackets :: Doc Text -> Lua (Doc Text)
146+
brackets = return . Doc.brackets
147+
148+
-- | Like @'lblock'@ but aligned centered.
149+
cblock :: Int -> Doc Text -> Lua (Doc Text)
150+
cblock width = return . Doc.cblock width
151+
152+
-- | Chomps trailing blank space off of a @'Doc'@.
153+
chomp :: Doc Text -> Lua (Doc Text)
154+
chomp = return . Doc.chomp
155+
156+
-- | A carriage return. Does nothing if we're at the beginning of
157+
-- a line; otherwise inserts a newline.
158+
cr :: Doc Text
159+
cr = Doc.cr
160+
161+
-- | Wraps a @'Doc'@ in double quotes
162+
double_quotes :: Doc Text -> Lua (Doc Text)
163+
double_quotes = return . Doc.doubleQuotes
164+
165+
-- | The empty document.
166+
empty :: Doc Text
167+
empty = Doc.empty
168+
169+
-- | Makes a @'Doc'@ flush against the left margin.
170+
flush :: Doc Text -> Lua (Doc Text)
171+
flush = return . Doc.flush
172+
173+
-- | Creates a hanging indent.
174+
hang :: Int -> Doc Text -> Doc Text -> Lua (Doc Text)
175+
hang ind start doc = return $ Doc.hang ind start doc
176+
177+
-- | Encloses a @'Doc'@ inside a start and end @'Doc'@.
178+
inside :: Doc Text -> Doc Text -> Doc Text -> Lua (Doc Text)
179+
inside start end contents = return $ Doc.inside start end contents
180+
181+
-- | Creates a block with the given width and content, aligned to the left.
182+
lblock :: Int -> Doc Text -> Lua (Doc Text)
183+
lblock width = return . Doc.lblock width
184+
185+
-- | Creates a @'Doc'@ from a string.
186+
literal :: Text -> Lua (Doc Text)
187+
literal = return . Doc.literal
188+
189+
-- | Indents a @'Doc' by the specified number of spaces.
190+
nest :: Int -> Doc Text -> Lua (Doc Text)
191+
nest ind = return . Doc.nest ind
192+
193+
-- | Removes leading blank lines from a @'Doc'@.
194+
nestle :: Doc Text -> Lua (Doc Text)
195+
nestle = return . Doc.nestle
196+
197+
-- | Makes a @'Doc'@ non-reflowable.
198+
nowrap :: Doc Text -> Lua (Doc Text)
199+
nowrap = return . Doc.nowrap
200+
201+
-- | Puts a @'Doc'@ in parentheses.
202+
parens :: Doc Text -> Lua (Doc Text)
203+
parens = return . Doc.parens
204+
205+
-- | Uses the specified string as a prefix for every line of the
206+
-- inside document (except the first, if not at the beginning of
207+
-- the line).
208+
prefixed :: Text -> Doc Text -> Lua (Doc Text)
209+
prefixed prefix = return . Doc.prefixed (T.unpack prefix)
210+
211+
-- | Wraps a @'Doc'@ in single quotes.
212+
quotes :: Doc Text -> Lua (Doc Text)
213+
quotes = return . Doc.quotes
214+
215+
-- | Like @'lblock'@ but aligned to the right.
216+
rblock :: Int -> Doc Text -> Lua (Doc Text)
217+
rblock ind = return . Doc.rblock ind
218+
219+
-- | A breaking (reflowable) space.
220+
space :: Doc Text
221+
space = Doc.space
222+
223+
vfill :: Text -> Lua (Doc Text)
224+
vfill = return . Doc.vfill
225+
226+
64227
--
65228
-- Marshaling
66229
--

test/test-doclayout.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,25 @@ local assert = tasty.assert
1010

1111
-- Check existence static fields
1212
return {
13-
group 'static fields' {
14-
test('empty_doc', function ()
15-
assert.are_equal(type(doclayout.empty_doc), 'userdata')
13+
group 'constructors' {
14+
test('empty', function ()
15+
assert.are_equal(type(doclayout.empty), 'userdata')
16+
end),
17+
test('blankline', function ()
18+
assert.are_equal(type(doclayout.blankline), 'userdata')
19+
end),
20+
test('cr', function ()
21+
assert.are_equal(type(doclayout.cr), 'userdata')
22+
end),
23+
test('space', function ()
24+
assert.are_equal(type(doclayout.space), 'userdata')
1625
end),
1726
},
1827

1928
group 'functions' {
2029
group 'render' {
2130
test('empty doc', function ()
22-
assert.are_equal(doclayout.render(doclayout.empty_doc), '')
31+
assert.are_equal(doclayout.render(doclayout.empty), '')
2332
end)
2433
}
2534
},

0 commit comments

Comments
 (0)