From ba7ea40e55acd67893464f4c808d635337279763 Mon Sep 17 00:00:00 2001 From: reixn Date: Tue, 19 Jul 2022 13:41:34 +0800 Subject: [PATCH 1/3] Add unsafeTextWithLength --- prettyprinter/src/Prettyprinter.hs | 3 +++ prettyprinter/src/Prettyprinter/Internal.hs | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/prettyprinter/src/Prettyprinter.hs b/prettyprinter/src/Prettyprinter.hs index bf44528e..24ecf969 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, unsafeLazyTextWithLength, + -- ** Primitives for alternative layouts group, flatAlt, diff --git a/prettyprinter/src/Prettyprinter/Internal.hs b/prettyprinter/src/Prettyprinter/Internal.hs index ca4c0b10..c260d1b2 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, unsafeLazyTextWithLength, + -- ** Primitives for alternative layouts group, flatAlt, @@ -452,6 +455,15 @@ 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 + +-- | identical to the strict version +unsafeLazyTextWithLength :: Lazy.Text -> Int -> Doc ann +unsafeLazyTextWithLength txt l = Text l (Lazy.toStrict txt) + -- | (lazy 'Text' instance, identical to the strict version) instance Pretty Lazy.Text where pretty = pretty . Lazy.toStrict #endif From 225a25e4c79cafc7ff2d762751f786b96975f127 Mon Sep 17 00:00:00 2001 From: reixn Date: Tue, 19 Jul 2022 22:20:48 +0800 Subject: [PATCH 2/3] Remove unsafeLazyTextWithLength --- prettyprinter/src/Prettyprinter.hs | 2 +- prettyprinter/src/Prettyprinter/Internal.hs | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/prettyprinter/src/Prettyprinter.hs b/prettyprinter/src/Prettyprinter.hs index 24ecf969..8f75e9b9 100644 --- a/prettyprinter/src/Prettyprinter.hs +++ b/prettyprinter/src/Prettyprinter.hs @@ -203,7 +203,7 @@ module Prettyprinter ( emptyDoc, nest, line, line', softline, softline', hardline, -- ** Create from Text (unsafe) - unsafeTextWithLength, unsafeLazyTextWithLength, + unsafeTextWithLength, -- ** Primitives for alternative layouts group, flatAlt, diff --git a/prettyprinter/src/Prettyprinter/Internal.hs b/prettyprinter/src/Prettyprinter/Internal.hs index c260d1b2..4fe8b4d3 100755 --- a/prettyprinter/src/Prettyprinter/Internal.hs +++ b/prettyprinter/src/Prettyprinter/Internal.hs @@ -27,7 +27,7 @@ module Prettyprinter.Internal ( emptyDoc, nest, line, line', softline, softline', hardline, -- ** create doc directly from Text, unsafe - unsafeTextWithLength, unsafeLazyTextWithLength, + unsafeTextWithLength -- ** Primitives for alternative layouts group, flatAlt, @@ -460,10 +460,6 @@ instance Pretty Text where pretty = vsep . map unsafeTextWithoutNewlines . T.spl unsafeTextWithLength :: Text -> Int -> Doc ann unsafeTextWithLength txt l = Text l txt --- | identical to the strict version -unsafeLazyTextWithLength :: Lazy.Text -> Int -> Doc ann -unsafeLazyTextWithLength txt l = Text l (Lazy.toStrict txt) - -- | (lazy 'Text' instance, identical to the strict version) instance Pretty Lazy.Text where pretty = pretty . Lazy.toStrict #endif From ae1d2a6a178a5c39aa55648550437d82ca6e2cf1 Mon Sep 17 00:00:00 2001 From: reixn Date: Tue, 19 Jul 2022 23:14:47 +0800 Subject: [PATCH 3/3] Improve document on unsafeTextWithLength --- prettyprinter/src/Prettyprinter/Internal.hs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/prettyprinter/src/Prettyprinter/Internal.hs b/prettyprinter/src/Prettyprinter/Internal.hs index 4fe8b4d3..63602c2d 100755 --- a/prettyprinter/src/Prettyprinter/Internal.hs +++ b/prettyprinter/src/Prettyprinter/Internal.hs @@ -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, @@ -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 @@ -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'