text-zipper 0.12 → 0.13
raw patch · 4 files changed
+44/−22 lines, 4 files
Files
- CHANGELOG.md +9/−0
- src/Data/Text/Zipper.hs +14/−19
- tests/WordsSpec.hs +20/−2
- text-zipper.cabal +1/−1
CHANGELOG.md view
@@ -1,4 +1,13 @@ +0.13+----++Bug fixes:+ * The zipper constructors now ignores non-printable characters (see+ also #13)+ * `insertMany` now no longer drops the input following a non-printable+ character (#13)+ 0.12 ----
src/Data/Text/Zipper.hs view
@@ -59,6 +59,7 @@ import Control.Applicative ((<$>)) import Control.DeepSeq import Data.Char (isPrint)+import Data.List (foldl') import Data.Monoid import qualified Data.Text as T import qualified Data.Vector as V@@ -146,7 +147,10 @@ (first, rest) = if null limitedLs then (mempty, mempty) else (head limitedLs, tail limitedLs)- in TZ mempty first [] rest fromCh drp tk lngth lst int nl linesFunc toListF lmt+ numLines = length ls+ insertLine z (i, l) = (if i < numLines - 1 then breakLine else id) $ insertMany l z+ loadInitial z = foldl' insertLine z $ zip [0..] (first:rest)+ in loadInitial $ TZ mempty mempty mempty mempty fromCh drp tk lngth lst int nl linesFunc toListF lmt -- | Get the text contents of the zipper. getText :: (Monoid a) => TextZipper a -> [a]@@ -224,28 +228,19 @@ -- Otherwise insert the character and move the cursor one position to -- the right. insertChar :: (Monoid a) => Char -> TextZipper a -> TextZipper a-insertChar ch tz = maybe tz id $ insertChar_ ch tz--insertChar_ :: (Monoid a) => Char -> TextZipper a -> Maybe (TextZipper a)-insertChar_ ch tz- | ch == '\n' = breakLine_ tz- | isPrint ch = Just $ tz { toLeft = toLeft tz `mappend` (fromChar tz ch) }- | otherwise = Nothing+insertChar ch tz+ | ch == '\n' = breakLine tz+ | isPrint ch = tz { toLeft = toLeft tz `mappend` (fromChar tz ch) }+ | otherwise = tz -- | Insert many characters at the current cursor position. Move the -- cursor to the end of the inserted text. insertMany :: (Monoid a) => a -> TextZipper a -> TextZipper a-insertMany str tz =- let go [] z = z- go (c:cs) z = maybe z (go cs) $ insertChar_ c z- in go (toList_ tz str) tz+insertMany str tz = foldl' (flip insertChar) tz $ toList_ tz str -- | Insert a line break at the current cursor position. breakLine :: (Monoid a) => TextZipper a -> TextZipper a-breakLine tz = maybe tz id $ breakLine_ tz--breakLine_ :: (Monoid a) => TextZipper a -> Maybe (TextZipper a)-breakLine_ tz =+breakLine tz = -- Plus two because we count the current line and the line we are -- about to create; if that number of lines exceeds the limit, -- ignore this operation.@@ -254,9 +249,9 @@ } in case lineLimit tz of Just lim -> if length (above tz) + length (below tz) + 2 > lim- then Nothing- else Just modified- Nothing -> Just modified+ then tz+ else modified+ Nothing -> modified -- | Move the cursor to the end of the current line. gotoEOL :: (Monoid a) => TextZipper a -> TextZipper a
tests/WordsSpec.hs view
@@ -3,8 +3,6 @@ {-# OPTIONS_GHC -fno-warn-orphans #-} module WordsSpec (spec) where --- This test suite is Copyright (c) Hans-Peter Deifel- import Test.Hspec import Test.QuickCheck @@ -17,10 +15,30 @@ spec :: Spec spec = do+ constructorSpec+ insertCharSpec+ insertManySpec moveWordLeftSpec moveWordRightSpec deletePrevWordSpec deleteWordSpec++constructorSpec :: Spec+constructorSpec = describe "constructor" $ do+ it "inserts only printable characters at construction time" $+ (stringZipper ["abc\x1b def"] Nothing) `shouldBe` (stringZipper ["abc def"] Nothing)++insertCharSpec :: Spec+insertCharSpec = describe "insertChar" $ do+ it "ignores an insert of a non-printable character" $+ let z = stringZipper [] Nothing+ in (insertChar '\x1b' z) `shouldBe` z++insertManySpec :: Spec+insertManySpec = describe "insertMany" $ do+ it "ignores an insert of a non-printable character" $+ let z = stringZipper ["abc"] Nothing+ in (insertMany "ghi\x1bjkl" z) `shouldBe` (insertMany "ghijkl" z) moveWordLeftSpec :: Spec moveWordLeftSpec = describe "moveWordLeft" $ do
text-zipper.cabal view
@@ -1,5 +1,5 @@ name: text-zipper-version: 0.12+version: 0.13 synopsis: A text editor zipper library description: This library provides a zipper and API for editing text. license: BSD3