Skip to content

Commit

Permalink
Improve document on unsafeTextWithLength
Browse files Browse the repository at this point in the history
  • Loading branch information
reixn committed Jul 19, 2022
1 parent 225a25e commit ae1d2a6
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions prettyprinter/src/Prettyprinter/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Prettyprinter.Internal (
emptyDoc, nest, line, line', softline, softline', hardline,

-- ** create doc directly from Text, unsafe
unsafeTextWithLength
unsafeTextWithLength,

-- ** Primitives for alternative layouts
group, flatAlt,
Expand Down Expand Up @@ -455,11 +455,6 @@ instance Pretty a => Pretty (Maybe a) where
-- Manually use @'hardline'@ if you /definitely/ want newlines.
instance Pretty Text where pretty = vsep . map unsafeTextWithoutNewlines . T.splitOn "\n"

-- | Convert text to Doc. Must not contain newlines.
-- Useful when dealing with wide characters and emojis
unsafeTextWithLength :: Text -> Int -> Doc ann
unsafeTextWithLength txt l = Text l txt

-- | (lazy 'Text' instance, identical to the strict version)
instance Pretty Lazy.Text where pretty = pretty . Lazy.toStrict
#endif
Expand All @@ -484,6 +479,20 @@ unsafeTextWithoutNewlines text = case T.uncons text of
| T.null ext -> Char t
| otherwise -> Text (T.length text) text

-- | @(unsafeTextWithLength t l)@ convert text @t@ of length @l@ into Doc.
--
-- The string must not contain any newline characters.
--
-- The real length can be specified manually when there are some wide character
-- or emojis in the string, so that it can be layed out correctly.
--
-- For example using doclayout to get the real length
-- @
-- unsafeTextWithLength "😃" (realLength "😃")
-- @
unsafeTextWithLength :: Text -> Int -> Doc ann
unsafeTextWithLength txt l = Text l txt

-- | The empty document behaves like @('pretty' "")@, so it has a height of 1.
-- This may lead to surprising behaviour if we expect it to bear no weight
-- inside e.g. 'vcat', where we get an empty line of output from it ('parens'
Expand Down

0 comments on commit ae1d2a6

Please sign in to comment.