From ae2fb11e89078608bbc1722b8b4096e5b20edf8b Mon Sep 17 00:00:00 2001 From: Jaro Date: Thu, 4 May 2023 13:05:13 +0200 Subject: [PATCH] Separate WithSpan for sharing with other modules. --- balkon.cabal | 1 + .../Text/ParagraphLayout/Internal/Layout.hs | 2 +- .../ParagraphLayout/Internal/ResolvedSpan.hs | 24 +------------------ .../Text/ParagraphLayout/Internal/Rich.hs | 2 +- .../Text/ParagraphLayout/Internal/WithSpan.hs | 24 +++++++++++++++++++ 5 files changed, 28 insertions(+), 25 deletions(-) create mode 100644 src/Data/Text/ParagraphLayout/Internal/WithSpan.hs diff --git a/balkon.cabal b/balkon.cabal index c37595f..ef2aebd 100644 --- a/balkon.cabal +++ b/balkon.cabal @@ -127,6 +127,7 @@ library balkon-internal Data.Text.ParagraphLayout.Internal.TextOptions, Data.Text.ParagraphLayout.Internal.Tree, Data.Text.ParagraphLayout.Internal.TreeOfTexts, + Data.Text.ParagraphLayout.Internal.WithSpan, Data.Text.ParagraphLayout.Internal.Zipper -- Modules used purely internally and not in any tests. diff --git a/src/Data/Text/ParagraphLayout/Internal/Layout.hs b/src/Data/Text/ParagraphLayout/Internal/Layout.hs index 8c300cf..1d99b30 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Layout.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Layout.hs @@ -32,11 +32,11 @@ import Data.Text.ParagraphLayout.Internal.LineHeight import Data.Text.ParagraphLayout.Internal.ParagraphExtents import qualified Data.Text.ParagraphLayout.Internal.ProtoFragment as PF import Data.Text.ParagraphLayout.Internal.Rect -import Data.Text.ParagraphLayout.Internal.ResolvedSpan (WithSpan (WithSpan)) import qualified Data.Text.ParagraphLayout.Internal.ResolvedSpan as RS import Data.Text.ParagraphLayout.Internal.Run import Data.Text.ParagraphLayout.Internal.TextContainer import Data.Text.ParagraphLayout.Internal.TextOptions +import Data.Text.ParagraphLayout.Internal.WithSpan -- This is redundant. -- TODO: Consider using `ResolvedSpan` as `fragmentUserData`, then swapping it diff --git a/src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs b/src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs index 4d87ddd..5d39fbc 100644 --- a/src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs +++ b/src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs @@ -1,13 +1,9 @@ -module Data.Text.ParagraphLayout.Internal.ResolvedSpan - ( ResolvedSpan (..) - , WithSpan (WithSpan) - ) +module Data.Text.ParagraphLayout.Internal.ResolvedSpan (ResolvedSpan (..)) where import Data.Text (Text) import qualified Data.Text.ICU as BreakStatus (Line) -import Data.Text.ParagraphLayout.Internal.BiDiReorder import Data.Text.ParagraphLayout.Internal.TextContainer import Data.Text.ParagraphLayout.Internal.TextOptions @@ -29,21 +25,3 @@ instance Eq (ResolvedSpan d) where instance TextContainer (ResolvedSpan d) where getText = spanText - --- | Wrapper for temporarily mapping the relationship to a `ResolvedSpan`. -data WithSpan d a = WithSpan (ResolvedSpan d) a - -instance Functor (WithSpan d) where - fmap f (WithSpan s a) = WithSpan s (f a) - -instance TextContainer a => TextContainer (WithSpan d a) where - getText (WithSpan _ c) = getText c - -instance SeparableTextContainer a => SeparableTextContainer (WithSpan d a) where - splitTextAt8 n (WithSpan rs c) = (WithSpan rs c1, WithSpan rs c2) - where (c1, c2) = splitTextAt8 n c - dropWhileStart p (WithSpan rs c) = WithSpan rs (dropWhileStart p c) - dropWhileEnd p (WithSpan rs c) = WithSpan rs (dropWhileEnd p c) - -instance WithLevel a => WithLevel (WithSpan d a) where - level (WithSpan _ x) = level x diff --git a/src/Data/Text/ParagraphLayout/Internal/Rich.hs b/src/Data/Text/ParagraphLayout/Internal/Rich.hs index 2c3812c..0c89403 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Rich.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Rich.hs @@ -11,13 +11,13 @@ import Data.Text.ParagraphLayout.Internal.Break import Data.Text.ParagraphLayout.Internal.Fragment import Data.Text.ParagraphLayout.Internal.Layout import Data.Text.ParagraphLayout.Internal.ParagraphOptions -import Data.Text.ParagraphLayout.Internal.ResolvedSpan (WithSpan (WithSpan)) import qualified Data.Text.ParagraphLayout.Internal.ResolvedSpan as RS import Data.Text.ParagraphLayout.Internal.Rich.Paragraph import Data.Text.ParagraphLayout.Internal.Rich.ParagraphLayout import Data.Text.ParagraphLayout.Internal.Run import Data.Text.ParagraphLayout.Internal.TextOptions import Data.Text.ParagraphLayout.Internal.Tree +import Data.Text.ParagraphLayout.Internal.WithSpan -- | Lay out a rich text paragraph. layoutRich :: Paragraph d -> ParagraphLayout d diff --git a/src/Data/Text/ParagraphLayout/Internal/WithSpan.hs b/src/Data/Text/ParagraphLayout/Internal/WithSpan.hs new file mode 100644 index 0000000..b946fa4 --- /dev/null +++ b/src/Data/Text/ParagraphLayout/Internal/WithSpan.hs @@ -0,0 +1,24 @@ +module Data.Text.ParagraphLayout.Internal.WithSpan (WithSpan (WithSpan)) +where + +import Data.Text.ParagraphLayout.Internal.BiDiReorder +import Data.Text.ParagraphLayout.Internal.ResolvedSpan +import Data.Text.ParagraphLayout.Internal.TextContainer + +-- | Wrapper for temporarily mapping the relationship to a `ResolvedSpan`. +data WithSpan d a = WithSpan (ResolvedSpan d) a + +instance Functor (WithSpan d) where + fmap f (WithSpan s a) = WithSpan s (f a) + +instance TextContainer a => TextContainer (WithSpan d a) where + getText (WithSpan _ c) = getText c + +instance SeparableTextContainer a => SeparableTextContainer (WithSpan d a) where + splitTextAt8 n (WithSpan rs c) = (WithSpan rs c1, WithSpan rs c2) + where (c1, c2) = splitTextAt8 n c + dropWhileStart p (WithSpan rs c) = WithSpan rs (dropWhileStart p c) + dropWhileEnd p (WithSpan rs c) = WithSpan rs (dropWhileEnd p c) + +instance WithLevel a => WithLevel (WithSpan d a) where + level (WithSpan _ x) = level x -- 2.30.2