11{-# LANGUAGE FlexibleInstances #-}
2+ {-# LANGUAGE LambdaCase #-}
23{-# OPTIONS_GHC -fno-warn-orphans #-}
34{-|
45Module : Foreign.Lua.Module.DocLayout
@@ -20,11 +21,16 @@ module Foreign.Lua.Module.DocLayout (
2021
2122 -- * Functions
2223 , render
24+
25+ -- * Marshaling
26+ , peekDoc
27+ , pushDoc
2328 )
2429where
2530
2631import Data.Text (Text )
27- import Foreign.Lua (Lua , NumResults (.. ), Optional , Peekable , Pushable )
32+ import Foreign.Lua (Lua , NumResults (.. ), Optional ,
33+ Peekable , Pushable , StackIndex )
2834import Text.DocLayout (Doc )
2935
3036import qualified Foreign.Lua as Lua
@@ -48,13 +54,28 @@ preloadModule :: String -> Lua ()
4854preloadModule = flip Lua. preloadhs pushModule
4955
5056-- | Render a @'Doc'@. The text is reflowed on breakable spaces
51- -- to match the line length if a line length is provided . Text is
52- -- not reflowed if the parameter is omitted or nil.
57+ -- to match the given line length. Text is not reflowed if the
58+ -- line length parameter is omitted or nil.
5359render :: Doc Text -> Optional Int -> Lua Text
5460render doc optLength = return $ Doc. render (Lua. fromOptional optLength) doc
5561
62+ --
63+ -- Marshaling
64+ --
65+
66+ -- | Retrieve a @Doc Text@ value from the Lua stack. Strings are
67+ -- converted to plain @'Doc'@ values.
68+ peekDoc :: StackIndex -> Lua (Doc Text )
69+ peekDoc idx = Lua. ltype idx >>= \ case
70+ Lua. TypeString -> Doc. literal <$> Lua. peek idx
71+ _ -> Lua. peekAny idx
72+
5673instance Peekable (Doc Text ) where
57- peek = Lua. peekAny
74+ peek = peekDoc
75+
76+ -- | Push a @Doc Text@ value to the Lua stack.
77+ pushDoc :: Doc Text -> Lua ()
78+ pushDoc = Lua. pushAny
5879
5980instance Pushable (Doc Text ) where
60- push = Lua. pushAny
81+ push = pushDoc
0 commit comments