Skip to content

Commit ae1d2a6

Browse files
committed
Improve document on unsafeTextWithLength
1 parent 225a25e commit ae1d2a6

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

prettyprinter/src/Prettyprinter/Internal.hs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module Prettyprinter.Internal (
2727
emptyDoc, nest, line, line', softline, softline', hardline,
2828

2929
-- ** create doc directly from Text, unsafe
30-
unsafeTextWithLength
30+
unsafeTextWithLength,
3131

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

458-
-- | Convert text to Doc. Must not contain newlines.
459-
-- Useful when dealing with wide characters and emojis
460-
unsafeTextWithLength :: Text -> Int -> Doc ann
461-
unsafeTextWithLength txt l = Text l txt
462-
463458
-- | (lazy 'Text' instance, identical to the strict version)
464459
instance Pretty Lazy.Text where pretty = pretty . Lazy.toStrict
465460
#endif
@@ -484,6 +479,20 @@ unsafeTextWithoutNewlines text = case T.uncons text of
484479
| T.null ext -> Char t
485480
| otherwise -> Text (T.length text) text
486481

482+
-- | @(unsafeTextWithLength t l)@ convert text @t@ of length @l@ into Doc.
483+
--
484+
-- The string must not contain any newline characters.
485+
--
486+
-- The real length can be specified manually when there are some wide character
487+
-- or emojis in the string, so that it can be layed out correctly.
488+
--
489+
-- For example using doclayout to get the real length
490+
-- @
491+
-- unsafeTextWithLength "😃" (realLength "😃")
492+
-- @
493+
unsafeTextWithLength :: Text -> Int -> Doc ann
494+
unsafeTextWithLength txt l = Text l txt
495+
487496
-- | The empty document behaves like @('pretty' "")@, so it has a height of 1.
488497
-- This may lead to surprising behaviour if we expect it to bear no weight
489498
-- inside e.g. 'vcat', where we get an empty line of output from it ('parens'

0 commit comments

Comments
 (0)