diff --git a/prettyprinter/src/Prettyprinter.hs b/prettyprinter/src/Prettyprinter.hs index bf44528e..8f75e9b9 100644 --- a/prettyprinter/src/Prettyprinter.hs +++ b/prettyprinter/src/Prettyprinter.hs @@ -202,6 +202,9 @@ module Prettyprinter ( viaShow, unsafeViaShow, emptyDoc, nest, line, line', softline, softline', hardline, + -- ** Create from Text (unsafe) + unsafeTextWithLength, + -- ** Primitives for alternative layouts group, flatAlt, diff --git a/prettyprinter/src/Prettyprinter/Internal.hs b/prettyprinter/src/Prettyprinter/Internal.hs index ca4c0b10..63602c2d 100755 --- a/prettyprinter/src/Prettyprinter/Internal.hs +++ b/prettyprinter/src/Prettyprinter/Internal.hs @@ -26,6 +26,9 @@ module Prettyprinter.Internal ( viaShow, unsafeViaShow, unsafeTextWithoutNewlines, emptyDoc, nest, line, line', softline, softline', hardline, + -- ** create doc directly from Text, unsafe + unsafeTextWithLength, + -- ** Primitives for alternative layouts group, flatAlt, @@ -476,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'