text-region 0.2.0.0 → 0.3.0.0
raw patch · 4 files changed
+48/−21 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.Text.Region: replaceAction :: EditAction e s => Region -> Contents s -> e s
+ Data.Text.Region: undo :: Editable s => Edit s -> s -> Edit s
+ Data.Text.Region.Types: class Regioned a
+ Data.Text.Region.Types: instance Data.Text.Region.Types.Regioned (Data.Text.Region.Types.Edit s)
+ Data.Text.Region.Types: instance Data.Text.Region.Types.Regioned (Data.Text.Region.Types.Replace s)
+ Data.Text.Region.Types: instance Data.Text.Region.Types.Regioned Data.Text.Region.Types.Point
+ Data.Text.Region.Types: instance Data.Text.Region.Types.Regioned Data.Text.Region.Types.Region
+ Data.Text.Region.Types: regions :: Regioned a => Traversal' a Region
- Data.Text.Region: apply :: Editable s => Edit s -> Contents s -> Contents s
+ Data.Text.Region: apply :: Editable s => Edit s -> s -> s
- Data.Text.Region: overwrite :: EditAction e s => Point -> Contents s -> e s
+ Data.Text.Region: overwrite :: EditAction e s => Point -> s -> e s
- Data.Text.Region: paste :: EditAction e s => Point -> Contents s -> e s
+ Data.Text.Region: paste :: EditAction e s => Point -> s -> e s
- Data.Text.Region: replace :: EditAction e s => Region -> Contents s -> e s
+ Data.Text.Region: replace :: EditAction e s => Region -> s -> e s
- Data.Text.Region: update :: Editable s => Edit s -> Region -> Region
+ Data.Text.Region: update :: (Editable s, Regioned r) => Edit s -> r -> r
- Data.Text.Region.Types: replaceRegion :: forall s_ak05. Lens' (Replace s_ak05) Region
+ Data.Text.Region.Types: replaceRegion :: forall s_amqt. Lens' (Replace s_amqt) Region
- Data.Text.Region.Types: replaceWith :: forall s_ak05 s_alyE. Lens (Replace s_ak05) (Replace s_alyE) (Contents s_ak05) (Contents s_alyE)
+ Data.Text.Region.Types: replaceWith :: forall s_amqt s_ao0Z. Lens (Replace s_amqt) (Replace s_ao0Z) (Contents s_amqt) (Contents s_ao0Z)
- Data.Text.Region.Types: replaces :: forall s_alyS s_amdN. Iso (Edit s_alyS) (Edit s_amdN) [Replace s_alyS] [Replace s_amdN]
+ Data.Text.Region.Types: replaces :: forall s_ao1d s_aoGc. Iso (Edit s_ao1d) (Edit s_aoGc) [Replace s_ao1d] [Replace s_aoGc]
Files
- src/Data/Text/Region.hs +22/−13
- src/Data/Text/Region/Types.hs +16/−0
- tests/Test.hs +9/−7
- text-region.cabal +1/−1
src/Data/Text/Region.hs view
@@ -4,7 +4,7 @@ pt, start, lineStart, regionLength, till, linesSize, regionLines, emptyRegion, line, regionSize, expandLines, atRegion, overlaps, applyMap, cutMap, insertMap, cutRegion, insertRegion, - EditAction(..), cut, paste, overwrite, apply, update, + EditAction(..), replace, cut, paste, overwrite, apply, update, undo, module Data.Text.Region.Types ) where @@ -107,7 +107,7 @@ class Editable s ⇒ EditAction e s where -- | Make replace action over 'Region' and 'Contents' - replace ∷ Region → Contents s → e s + replaceAction ∷ Region → Contents s → e s -- | Make 'Map' from action actionMap ∷ e s → Map -- | Perform action, modifying 'Contents' @@ -115,34 +115,43 @@ -- | Get action undo inversed ∷ e s → Contents s → e s +-- | Replace region with data +replace ∷ EditAction e s ⇒ Region → s → e s +replace r = replaceAction r ∘ view contents + -- | Cuts region cut ∷ EditAction e s ⇒ Region → e s -cut r = replace r emptyContents +cut r = replaceAction r emptyContents -- | Pastes 'Contents' at some 'Point' -paste ∷ EditAction e s ⇒ Point → Contents s → e s -paste p = replace (p `till` p) +paste ∷ EditAction e s ⇒ Point → s → e s +paste p = replaceAction (p `till` p) ∘ view contents -- | Overwrites 'Contents' at some 'Point' -overwrite ∷ EditAction e s ⇒ Point → Contents s → e s -overwrite p c = replace (p `regionSize` measure c) c +overwrite ∷ EditAction e s ⇒ Point → s → e s +overwrite p c = replaceAction (p `regionSize` measure cts) cts where + cts = view contents c -- | 'perform' for 'Edit' -apply ∷ Editable s ⇒ Edit s → Contents s → Contents s -apply = perform +apply ∷ Editable s ⇒ Edit s → s → s +apply = over contents ∘ perform +-- | Get undo +undo ∷ Editable s ⇒ Edit s → s → Edit s +undo e = inversed e ∘ view contents + -- | Update regions -update ∷ Editable s ⇒ Edit s → Region → Region -update = applyMap ∘ actionMap +update ∷ (Editable s, Regioned r) ⇒ Edit s → r → r +update e = over regions (applyMap ∘ actionMap $ e) instance Editable s ⇒ EditAction Replace s where - replace = Replace + replaceAction = Replace actionMap (Replace r w) = insertMap (r & regionLength .~ measure w) `mappend` cutMap r perform (Replace r w) cts = cts & atRegion r .~ w inversed (Replace r w) cts = Replace (r & regionLength .~ measure w) (cts ^. atRegion r) instance Editable s ⇒ EditAction Edit s where - replace rgn txt = Edit [replace rgn txt] + replaceAction rgn txt = Edit [replaceAction rgn txt] actionMap = foldr go mempty ∘ view replaces where go r m = actionMap (over replaceRegion (applyMap m) r) `mappend` m perform = snd ∘ foldr go (mempty, id) ∘ view replaces where
src/Data/Text/Region/Types.hs view
@@ -8,6 +8,7 @@ concatCts, splitCts, splitted, Editable(..), contents, by, measure, Replace(..), replaceRegion, replaceWith, Edit(..), replaces, + Regioned(..), module Data.Group ) where @@ -193,3 +194,18 @@ instance (Editable s, FromJSON s) ⇒ FromJSON (Edit s) where parseJSON = fmap Edit ∘ parseJSON + +class Regioned a where + regions ∷ Traversal' a Region + +instance Regioned Point where + regions = pointRegion + +instance Regioned Region where + regions = id + +instance Regioned (Replace s) where + regions = replaceRegion + +instance Regioned (Edit s) where + regions = replaces . each . replaceRegion
tests/Test.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE FlexibleContexts #-} + module Main ( main ) where @@ -27,18 +29,18 @@ main = hspec $ do describe "regions are updated" $ do it "should delete correctly" $ - apply (cut quux `mappend` cut bar) (by text) ≡ by "foo baz" + apply (cut quux `mappend` cut bar) text ≡ "foo baz" it "should perform undo" $ let - act' = mconcat [cut bar, replace quux (by nums), paste start (by xxx)] - undo' = inversed act' (by text) + act' = mconcat [cut bar, replace quux nums, paste start xxx] + undo' = undo act' text in - (apply undo' ∘ apply act') (by text) ≡ by text + (apply undo' ∘ apply act') text ≡ text it "should reverse text" $ let go 0 _ txt = txt - go n c txt = go (n - 1) (over pointRegion (update act') c) (apply act' txt) where - act' = mconcat [cut first, paste c (txt ^. atRegion first)] + go n c txt = go (n - 1) (update act' c) (apply act' txt) where + act' = mconcat [cut first, paste c (txt ^. contents . atRegion first . from contents)] first = pt 0 0 `till` pt 0 1 in - go (length text) (pt 0 (length text)) (by text) ≡ by (reverse text) + go (length text) (pt 0 (length text)) text ≡ reverse text
text-region.cabal view
@@ -1,5 +1,5 @@ name: text-region -version: 0.2.0.0 +version: 0.3.0.0 synopsis: Marking text regions description: Provides functions to update text region positions according to text edit actions homepage: https://github.com/mvoidex/text-region