Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unsafeTextWithLength #230

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions prettyprinter/src/Prettyprinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
17 changes: 17 additions & 0 deletions prettyprinter/src/Prettyprinter/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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'
Expand Down