Skip to content

Commit 8350a57

Browse files
committed
Add remaining querying functions
1 parent 78bd6c8 commit 8350a57

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,33 @@ If `a` and `b` are `Doc` elements, then `a // b` puts `a` above
332332

333333
Returns `true` iff `doc` is the empty document, `false` otherwise.
334334

335+
#### min_offset
336+
337+
`min_offset (doc)`
338+
339+
Returns the minimal width of a @'Doc'@ when reflowed at breakable
340+
spaces.
341+
342+
#### update_column
343+
344+
`update_column (doc, i)`
345+
346+
Returns the column that would be occupied by the last laid out character.
347+
348+
#### height
349+
350+
`height (doc)`
351+
352+
Returns the height of a block or other Doc.
353+
354+
#### real_length
355+
356+
`real_length (str)`
357+
358+
Returns the real length of a string in a monospace font: 0 for a
359+
combining character, 1, for a regular character, 2 for an East
360+
Asian wide character.
361+
335362
#### offset
336363

337364
`offset (doc)`

src/Foreign/Lua/Module/DocLayout.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ module Foreign.Lua.Module.DocLayout (
5353

5454
-- * Document Querying
5555
, is_empty
56+
, height
57+
, min_offset
5658
, offset
59+
, real_length
60+
, update_column
5761

5862
-- * Marshaling
5963
, peekDoc
@@ -116,6 +120,11 @@ pushModule = do
116120
-- querying
117121
Lua.addfunction "is_empty" is_empty
118122
Lua.addfunction "offset" offset
123+
Lua.addfunction "height" height
124+
Lua.addfunction "min_offset" min_offset
125+
Lua.addfunction "offset" offset
126+
Lua.addfunction "real_length" real_length
127+
Lua.addfunction "update_column" update_column
119128
-- rendering
120129
Lua.addfunction "render" render
121130
return 1
@@ -143,6 +152,26 @@ is_empty = return . Doc.isEmpty
143152
offset :: Doc Text -> Lua Int
144153
offset = return . Doc.offset
145154

155+
-- | Returns the minimal width of a @'Doc'@ when reflowed at
156+
-- breakable spaces.
157+
min_offset :: Doc Text -> Lua Int
158+
min_offset = return . Doc.minOffset
159+
160+
-- | Returns the column that would be occupied by the last laid
161+
-- out character.
162+
update_column :: Doc Text -> Int -> Lua Int
163+
update_column doc = return . Doc.updateColumn doc
164+
165+
-- | Returns the height of a block or other Doc.
166+
height :: Doc Text -> Lua Int
167+
height = return . Doc.height
168+
169+
-- | Returns the real length of a string in a monospace font: 0
170+
-- for a combining character, 1, for a regular character, 2 for
171+
-- an East Asian wide character.
172+
real_length :: Text -> Lua Int
173+
real_length = return . Doc.realLength
174+
146175
--
147176
-- Constructors
148177
--

test/test-doclayout.lua

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,32 @@ return {
6060
assert.is_falsy(doclayout.is_empty('non-empty'))
6161
end),
6262

63+
test('height', function ()
64+
assert.are_equal(doclayout.height(doclayout.empty), 1)
65+
assert.are_equal(doclayout.height('line'), 1)
66+
assert.are_equal(doclayout.height(doclayout.literal 'p' / 'q'), 2)
67+
assert.are_equal(doclayout.height(doclayout.literal 'p' // 'q'), 3)
68+
end),
69+
70+
test('min_offset', function ()
71+
assert.are_equal(doclayout.min_offset 'four', 4)
72+
assert.are_equal(
73+
doclayout.min_offset(doclayout.literal 'four' + 'radio'),
74+
5
75+
)
76+
end),
77+
6378
test('offset', function ()
6479
assert.are_equal(doclayout.offset 'four', 4)
6580
assert.are_equal(doclayout.offset(doclayout.literal 'four' / 'radio'), 5)
66-
end)
81+
end),
82+
83+
test('real_length', function ()
84+
assert.are_equal(doclayout.real_length(''), 0)
85+
assert.are_equal(doclayout.real_length('a'), 1)
86+
assert.are_equal(doclayout.real_length(''), 1)
87+
assert.are_equal(doclayout.real_length(''), 2)
88+
end),
6789
},
6890

6991
group 'Doc type' {

0 commit comments

Comments
 (0)