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 1 commit
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, unsafeLazyTextWithLength,

-- ** Primitives for alternative layouts
group, flatAlt,

Expand Down
12 changes: 12 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, unsafeLazyTextWithLength,

-- ** Primitives for alternative layouts
group, flatAlt,

Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation should clarify why and how this function is useful for dealing with wide characters and emojis.

unsafeTextWithLength :: Text -> Int -> Doc ann
unsafeTextWithLength txt l = Text l txt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this definition is a bit misplaced between these Pretty instances. I suggest placing it below unsafeTextWithoutNewlines.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Text invariants need to be handled in some way too:

-- | Invariants: at least two characters long, does not contain '\n'. For
-- empty documents, there is @Empty@; for singleton documents, there is
-- @Char@; newlines should be replaced by e.g. @Line@.
--
-- Since the frequently used 'T.length' of 'Text' is /O(length)/, we cache
-- it in this constructor.
| Text !Int !Text


-- | identical to the strict version
unsafeLazyTextWithLength :: Lazy.Text -> Int -> Doc ann
unsafeLazyTextWithLength txt l = Text l (Lazy.toStrict txt)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the use case for this function? I don't imagine that wide characters and emojis will typically exist in the form of a lazy Text values?!

If there's no obvious use case, I'd prefer not to add this function.


-- | (lazy 'Text' instance, identical to the strict version)
instance Pretty Lazy.Text where pretty = pretty . Lazy.toStrict
#endif
Expand Down