From 32a0e6359c77b0170af48a9884e808ec03b538cb Mon Sep 17 00:00:00 2001 From: pdavidow Date: Tue, 15 Aug 2023 22:46:16 -0400 Subject: [PATCH 01/16] FINAL need to uncomment tests --- simplex-chat.cabal | 8 + src/Simplex/Chat/MarkdownEditing.hs | 167 ++++++++++++++ tests/MarkdownEditingTests.hs | 324 ++++++++++++++++++++++++++++ tests/Test.hs | 28 +-- 4 files changed, 514 insertions(+), 13 deletions(-) create mode 100644 src/Simplex/Chat/MarkdownEditing.hs create mode 100644 tests/MarkdownEditingTests.hs diff --git a/simplex-chat.cabal b/simplex-chat.cabal index 526051edb6..dfaf53e9c3 100644 --- a/simplex-chat.cabal +++ b/simplex-chat.cabal @@ -33,6 +33,7 @@ library Simplex.Chat.Core Simplex.Chat.Help Simplex.Chat.Markdown + Simplex.Chat.MarkdownEditing Simplex.Chat.Messages Simplex.Chat.Messages.CIContent Simplex.Chat.Migrations.M20220101_initial @@ -152,6 +153,7 @@ library , http-types ==0.12.* , memory ==0.15.* , mtl ==2.2.* + , myers-diff >= 0.2.0.0 , network >=3.1.2.7 && <3.2 , optparse-applicative >=0.15 && <0.17 , process ==1.6.* @@ -200,6 +202,7 @@ executable simplex-bot , http-types ==0.12.* , memory ==0.15.* , mtl ==2.2.* + , myers-diff >= 0.2.0.0 , network >=3.1.2.7 && <3.2 , optparse-applicative >=0.15 && <0.17 , process ==1.6.* @@ -249,6 +252,7 @@ executable simplex-bot-advanced , http-types ==0.12.* , memory ==0.15.* , mtl ==2.2.* + , myers-diff >= 0.2.0.0 , network >=3.1.2.7 && <3.2 , optparse-applicative >=0.15 && <0.17 , process ==1.6.* @@ -299,6 +303,7 @@ executable simplex-broadcast-bot , http-types ==0.12.* , memory ==0.15.* , mtl ==2.2.* + , myers-diff >= 0.2.0.0 , network >=3.1.2.7 && <3.2 , optparse-applicative >=0.15 && <0.17 , process ==1.6.* @@ -349,6 +354,7 @@ executable simplex-chat , http-types ==0.12.* , memory ==0.15.* , mtl ==2.2.* + , myers-diff >= 0.2.0.0 , network ==3.1.* , optparse-applicative >=0.15 && <0.17 , process ==1.6.* @@ -384,6 +390,7 @@ test-suite simplex-chat-test ChatTests.Profiles ChatTests.Utils MarkdownTests + MarkdownEditingTests MobileTests ProtocolTests SchemaDump @@ -415,6 +422,7 @@ test-suite simplex-chat-test , http-types ==0.12.* , memory ==0.15.* , mtl ==2.2.* + , myers-diff >= 0.2.0.0 , network ==3.1.* , optparse-applicative >=0.15 && <0.17 , process ==1.6.* diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs new file mode 100644 index 0000000000..65c840f760 --- /dev/null +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -0,0 +1,167 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} + +{-# HLINT ignore "Use newtype instead of data" #-} + + +module Simplex.Chat.MarkdownEditing + ( DiffedChar(..) + , DiffedPlainChar(..) + , DiffStatus(..) + , DiffPlainStatus(..) + , DiffFormatStatus(..) + , FormattedChar(..) + , LeftSide(..) + , RightSide(..) + , findDiffs + , findPlainDiffs + , toFormattedChars + ) + where + + +import qualified Data.Foldable as F +import qualified Data.Map.Strict as M +import Data.Sequence ( Seq(..), (><) ) +import qualified Data.Sequence as S +import qualified Data.Text as T +import qualified Data.Diff.Myers as D +import Simplex.Chat.Markdown ( FormattedText(..), Format ) + + +data DiffStatus + = UnchangedChar DiffFormatStatus + | Inserted + | Deleted + deriving (Show, Eq) + + +data DiffPlainStatus + = PlainUnchanged + | PlainInserted + | PlainDeleted + deriving (Show, Eq) + + +data DiffFormatStatus + = UnchangedFormat + | ChangedToFormat (Maybe Format) + deriving (Show, Eq) + + +data DiffedChar = DiffedChar FormattedChar DiffStatus + deriving (Show, Eq) + + +data DiffedPlainChar = DiffedPlainChar Char DiffPlainStatus + deriving (Show, Eq) + + +data FormattedChar = FormattedChar + { char :: Char + , format :: Maybe Format + } + deriving (Show, Eq) + + +newtype LeftSide a = LeftSide a +newtype RightSide a = RightSide a + + +newtype DeleteIndicies = DeleteIndicies (Seq Int) deriving (Show, Eq) +newtype InsertIndicies = InsertIndicies (Seq Int) deriving (Show, Eq) + + +toFormattedChars :: [FormattedText] -> [FormattedChar] +toFormattedChars = concatMap toChars + where toChars (FormattedText f t) = map (`FormattedChar` f) $ T.unpack t + + +toText :: Seq FormattedChar -> T.Text +toText = T.pack . F.toList . fmap char + + +indicesFromEdits :: Seq D.Edit -> (DeleteIndicies, InsertIndicies) +indicesFromEdits = F.foldl' f (DeleteIndicies S.empty, InsertIndicies S.empty) + where + f :: (DeleteIndicies, InsertIndicies) -> D.Edit -> (DeleteIndicies, InsertIndicies) + f (x@(DeleteIndicies ds), y@(InsertIndicies is)) e = case e of + D.EditDelete m n -> (x', y) where x' = DeleteIndicies $ ds >< S.fromList [m .. n] + D.EditInsert _ m n -> (x , y') where y' = InsertIndicies $ is >< S.fromList [m .. n] + + +findPlainDiffs :: LeftSide T.Text -> RightSide T.Text -> Seq DiffedPlainChar +findPlainDiffs (LeftSide left) (RightSide right) = f <$> diffs + where + diffs = findDiffs (LeftSide $ toFormattedCharsFromText left) (RightSide $ toFormattedCharsFromText right) + + toFormattedCharsFromText :: T.Text -> Seq FormattedChar + toFormattedCharsFromText = fmap (`FormattedChar` Nothing) . S.fromList . T.unpack + + f :: DiffedChar -> DiffedPlainChar + f (DiffedChar (FormattedChar c _) diffStatus) = DiffedPlainChar c diffStatusPlain + where diffStatusPlain = case diffStatus of + UnchangedChar _ -> PlainUnchanged + Inserted -> PlainInserted + Deleted -> PlainDeleted + + +findDiffs :: LeftSide (Seq FormattedChar) -> RightSide (Seq FormattedChar) -> Seq DiffedChar +findDiffs (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchangedChars + where + edits = D.diffTexts (toText left) (toText right) + (DeleteIndicies deleteIndicies, InsertIndicies insertIndicies) = indicesFromEdits edits + + unchangedChars :: M.Map Int DiffFormatStatus + unchangedChars = F.foldl' f mempty unchangedCharPairs + where + unchangedCharPairs :: Seq (Int, FormattedChar, FormattedChar) + unchangedCharPairs = g <$> S.zip leftWithoutDeletes rightWithoutInserts + + leftWithoutDeletes :: Seq (Int, FormattedChar) -- indexed in original left + leftWithoutDeletes = S.filter (\(i, _) -> i `notElem` deleteIndicies) leftZ + where leftZ = S.zip (S.fromList [0 .. S.length left]) left + + rightWithoutInserts :: Seq (Int, FormattedChar) -- indexed in original right + rightWithoutInserts = S.filter (\(i, _) -> i `notElem` insertIndicies) rightZ + where rightZ = S.zip (S.fromList [0 .. S.length right]) right + + f :: M.Map Int DiffFormatStatus -> (Int, FormattedChar, FormattedChar) -> M.Map Int DiffFormatStatus + f acc (i, FormattedChar _ fL, FormattedChar _ fR) = M.insert i x acc + where x = if fL == fR then UnchangedFormat else ChangedToFormat fR + + g :: ((Int, FormattedChar), (Int, FormattedChar)) -> (Int, FormattedChar, FormattedChar) + g ((i,c), (_j,d)) = (i,c,d) -- i and _j should always be equal + + markDeletesAndUnchangedChars :: Seq DiffedChar + markDeletesAndUnchangedChars = S.mapWithIndex f left + where + f :: Int -> FormattedChar -> DiffedChar + f i x = DiffedChar x $ + if i `elem` deleteIndicies then Deleted + else UnchangedChar $ unchangedChars M.! i -- should never error + + addInserts :: Seq DiffedChar -> Seq DiffedChar + addInserts base = F.foldr f base edits -- start from end and work backwards, hence foldr + where + f :: D.Edit -> Seq DiffedChar -> Seq DiffedChar + f e acc = case e of + D.EditDelete _ _ -> acc + D.EditInsert i m n -> S.take i' acc >< inserts >< S.drop i' acc + -- D.EditInsert i m n -> S.take i acc >< inserts >< S.drop i acc + -- if ok to have inserts before deletes, use i not i' + -- Using i of course is faster, but perhaps i' approach can be optimised + + where + i' = slidePastDeleteBlock i + + slidePastDeleteBlock :: Int -> Int + slidePastDeleteBlock x = case S.lookup x acc of + Nothing -> x + Just (DiffedChar _ diffStatus) -> + if diffStatus == Deleted then slidePastDeleteBlock (x + 1) + else x + + rightFormatChars = S.take (n - m + 1) $ S.drop m right + inserts = fmap (`DiffedChar` Inserted) rightFormatChars \ No newline at end of file diff --git a/tests/MarkdownEditingTests.hs b/tests/MarkdownEditingTests.hs new file mode 100644 index 0000000000..1ad148eab9 --- /dev/null +++ b/tests/MarkdownEditingTests.hs @@ -0,0 +1,324 @@ +{-# LANGUAGE BlockArguments #-} +{-# LANGUAGE OverloadedLists #-} +{-# LANGUAGE OverloadedStrings #-} + +module MarkdownEditingTests where + +import qualified Data.Sequence as S +import Simplex.Chat.Markdown + +import Simplex.Chat.MarkdownEditing + ( FormattedChar(..), + DiffedChar(..), + DiffedPlainChar(..), + DiffStatus(..), + DiffPlainStatus(..), + DiffFormatStatus(..), + LeftSide(..), + RightSide(..), + findDiffs, + findPlainDiffs ) + + +import System.Console.ANSI.Types +import Test.Hspec +import qualified Data.List.NonEmpty as NE + + +markdownEditingTests :: Spec +markdownEditingTests = do + formattedEditedTextTests + + +formattedEditedTextTests :: Spec +formattedEditedTextTests = describe "show edits" do + it "empty no change" do + findDiffs + (LeftSide $ S.fromList + [ + ]) + + (RightSide $ S.fromList + [ + ]) + + `shouldBe` S.fromList + [ + ] + + + it "no change" do + findDiffs + (LeftSide $ S.fromList + [ FormattedChar 'H' Nothing + ]) + + (RightSide $ S.fromList + [ FormattedChar 'H' Nothing + ]) + + `shouldBe` S.fromList + [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat + ] + + + it "add 1 char to empty" do + findDiffs + (LeftSide $ S.fromList + [ + ]) + + (RightSide $ S.fromList + [ FormattedChar 'H' Nothing + ]) + + `shouldBe` S.fromList + [ DiffedChar (FormattedChar 'H' Nothing) Inserted + ] + + + it "del the one and only" do + findDiffs + (LeftSide $ S.fromList + [ FormattedChar 'H' Nothing + ]) + + (RightSide $ S.fromList + [ + ]) + + `shouldBe` S.fromList + [ DiffedChar (FormattedChar 'H' Nothing) Deleted + ] + + + it "one character change" do + findDiffs + (LeftSide $ S.fromList + [ FormattedChar 'H' Nothing + , FormattedChar 'r' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'o' Nothing + ]) + + (RightSide $ S.fromList + [ FormattedChar 'H' Nothing + , FormattedChar 'e' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'o' Nothing + ]) + + `shouldBe` S.fromList + [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat + , DiffedChar (FormattedChar 'r' Nothing) Deleted + , DiffedChar (FormattedChar 'e' Nothing) Inserted + , DiffedChar (FormattedChar 'l' Nothing) $ UnchangedChar UnchangedFormat + , DiffedChar (FormattedChar 'l' Nothing) $ UnchangedChar UnchangedFormat + , DiffedChar (FormattedChar 'o' Nothing) $ UnchangedChar UnchangedFormat + ] + + + it "more1" do + findDiffs + (LeftSide $ S.fromList + [ FormattedChar 'H' Nothing + , FormattedChar 'r' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'o' Nothing + ]) + + (RightSide $ S.fromList + [ FormattedChar 'H' Nothing + , FormattedChar 'e' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'o' Nothing + , FormattedChar 'x' Nothing + , FormattedChar 'y' Nothing + , FormattedChar 'z' Nothing + ]) + + `shouldBe` S.fromList + [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat + , DiffedChar (FormattedChar 'r' Nothing) Deleted + , DiffedChar (FormattedChar 'e' Nothing) Inserted + , DiffedChar (FormattedChar 'l' Nothing) $ UnchangedChar UnchangedFormat + , DiffedChar (FormattedChar 'l' Nothing) $ UnchangedChar UnchangedFormat + , DiffedChar (FormattedChar 'o' Nothing) $ UnchangedChar UnchangedFormat + , DiffedChar (FormattedChar 'x' Nothing) Inserted + , DiffedChar (FormattedChar 'y' Nothing) Inserted + , DiffedChar (FormattedChar 'z' Nothing) Inserted + ] + + + it "more2" do + findDiffs + (LeftSide $ S.fromList + [ FormattedChar 'H' Nothing + , FormattedChar 'r' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'o' Nothing + ]) + + (RightSide $ S.fromList + [ FormattedChar 'H' Nothing + , FormattedChar 'e' Nothing + , FormattedChar 'x' Nothing + , FormattedChar 'y' Nothing + , FormattedChar 'z' Nothing + , FormattedChar 'o' Nothing + ]) + + `shouldBe` S.fromList + [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat + , DiffedChar (FormattedChar 'r' Nothing) Deleted + , DiffedChar (FormattedChar 'l' Nothing) Deleted + , DiffedChar (FormattedChar 'l' Nothing) Deleted + , DiffedChar (FormattedChar 'e' Nothing) Inserted + , DiffedChar (FormattedChar 'x' Nothing) Inserted + , DiffedChar (FormattedChar 'y' Nothing) Inserted + , DiffedChar (FormattedChar 'z' Nothing) Inserted + , DiffedChar (FormattedChar 'o' Nothing) $ UnchangedChar UnchangedFormat + ] + + + it "more3" do + findDiffs + (LeftSide $ S.fromList + [ FormattedChar 'H' (Just Bold) + , FormattedChar 'H' (Just Bold) + , FormattedChar 'r' Nothing + , FormattedChar 'l' (Just Secret) + , FormattedChar 'l' Nothing + , FormattedChar 'o' (Just $ colored Green) + ]) + + (RightSide $ S.fromList + [ FormattedChar 'H' (Just Italic) + , FormattedChar 'H' (Just Bold) + , FormattedChar 'e' (Just $ colored Cyan) + , FormattedChar 'x' Nothing + , FormattedChar 'y' Nothing + , FormattedChar 'z' (Just Secret) + , FormattedChar 'o' (Just $ colored Blue) + ]) + + `shouldBe` S.fromList + [ DiffedChar (FormattedChar 'H' (Just Bold)) $ UnchangedChar (ChangedToFormat (Just Italic)) + , DiffedChar (FormattedChar 'H' (Just Bold)) $ UnchangedChar UnchangedFormat + , DiffedChar (FormattedChar 'r' Nothing) Deleted + , DiffedChar (FormattedChar 'l' (Just Secret)) Deleted + , DiffedChar (FormattedChar 'l' Nothing) Deleted + , DiffedChar (FormattedChar 'e' (Just $ colored Cyan)) Inserted + , DiffedChar (FormattedChar 'x' Nothing) Inserted + , DiffedChar (FormattedChar 'y' Nothing) Inserted + , DiffedChar (FormattedChar 'z' (Just Secret)) Inserted + , DiffedChar (FormattedChar 'o' (Just $ colored Green)) $ UnchangedChar (ChangedToFormat (Just $ colored Blue)) + ] + + + it "more4" do + findDiffs + (LeftSide $ S.fromList + [ FormattedChar 'H' Nothing + , FormattedChar 'r' Nothing + , FormattedChar 'l' Nothing + , FormattedChar '~' Nothing + , FormattedChar '!' Nothing + , FormattedChar '@' Nothing + , FormattedChar 'l' Nothing + , FormattedChar 'o' Nothing + ]) + + (RightSide $ S.fromList + [ FormattedChar 'H' Nothing + , FormattedChar 'e' Nothing + , FormattedChar 'r' Nothing + , FormattedChar 'x' Nothing + , FormattedChar 'y' Nothing + , FormattedChar '!' Nothing + , FormattedChar '@' Nothing + , FormattedChar 'z' Nothing + , FormattedChar 'o' Nothing + , FormattedChar '1' Nothing + , FormattedChar '2' Nothing + ]) + + `shouldBe` S.fromList + [ DiffedChar (FormattedChar 'H' Nothing) (UnchangedChar UnchangedFormat) + , DiffedChar (FormattedChar 'e' Nothing) Inserted + , DiffedChar (FormattedChar 'r' Nothing) (UnchangedChar UnchangedFormat) + , DiffedChar (FormattedChar 'l' Nothing) Deleted + , DiffedChar (FormattedChar '~' Nothing) Deleted + , DiffedChar (FormattedChar 'x' Nothing) Inserted + , DiffedChar (FormattedChar 'y' Nothing) Inserted + , DiffedChar (FormattedChar '!' Nothing) (UnchangedChar UnchangedFormat) + , DiffedChar (FormattedChar '@' Nothing) (UnchangedChar UnchangedFormat) + , DiffedChar (FormattedChar 'l' Nothing) Deleted + , DiffedChar (FormattedChar 'z' Nothing) Inserted + , DiffedChar (FormattedChar 'o' Nothing) (UnchangedChar UnchangedFormat) + , DiffedChar (FormattedChar '1' Nothing) Inserted + , DiffedChar (FormattedChar '2' Nothing) Inserted + ] + + + it "SimplexLink" do + findDiffs + (LeftSide $ S.fromList + [ FormattedChar '>' $ Just $ SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/2/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host1", "host2", "host3"]} + ]) + + (RightSide $ S.fromList + [ FormattedChar '>' $ Just SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/3/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host0", "host2", "host3"] + } + ]) + + `shouldBe` S.fromList + [ DiffedChar + (FormattedChar '>' $ Just SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/2/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host1", "host2", "host3"] + } + ) $ + + UnchangedChar $ ChangedToFormat $ Just SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/3/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host0", "host2", "host3"] + } + ] + + + it "findPlainDiffs" do + findPlainDiffs (LeftSide "Hrl~!@lo") (RightSide "Herxy!@zo12") + `shouldBe` S.fromList + [ DiffedPlainChar 'H' PlainUnchanged + , DiffedPlainChar 'e' PlainInserted + , DiffedPlainChar 'r' PlainUnchanged + , DiffedPlainChar 'l' PlainDeleted + , DiffedPlainChar '~' PlainDeleted + , DiffedPlainChar 'x' PlainInserted + , DiffedPlainChar 'y' PlainInserted + , DiffedPlainChar '!' PlainUnchanged + , DiffedPlainChar '@' PlainUnchanged + , DiffedPlainChar 'l' PlainDeleted + , DiffedPlainChar 'z' PlainInserted + , DiffedPlainChar 'o' PlainUnchanged + , DiffedPlainChar '1' PlainInserted + , DiffedPlainChar '2' PlainInserted + ] \ No newline at end of file diff --git a/tests/Test.hs b/tests/Test.hs index 9010aefa0f..488898829e 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -3,6 +3,7 @@ import ChatTests import Control.Logger.Simple import Data.Time.Clock.System import MarkdownTests +import MarkdownEditingTests import MobileTests import ProtocolTests import SchemaDump @@ -15,19 +16,20 @@ main :: IO () main = do setLogLevel LogError -- LogDebug withGlobalLogging logCfg . hspec $ do - describe "SimpleX chat markdown" markdownTests - describe "SimpleX chat view" viewTests - describe "SimpleX chat protocol" protocolTests - describe "WebRTC encryption" webRTCTests - describe "Schema dump" schemaDumpTest - around testBracket $ do - describe "Mobile API Tests" mobileTests - describe "SimpleX chat client" chatTests - where - testBracket test = do - t <- getSystemTime - let ts = show (systemSeconds t) <> show (systemNanoseconds t) - withSmpServer $ withTmpFiles $ withTempDirectory "tests/tmp" ts test + describe "SimpleX chat markdown editing" markdownEditingTests + -- describe "SimpleX chat markdown" markdownTests + -- describe "SimpleX chat view" viewTests + -- describe "SimpleX chat protocol" protocolTests + -- describe "WebRTC encryption" webRTCTests + -- describe "Schema dump" schemaDumpTest + -- around testBracket $ do + -- describe "Mobile API Tests" mobileTests + -- describe "SimpleX chat client" chatTests + -- where + -- testBracket test = do + -- t <- getSystemTime + -- let ts = show (systemSeconds t) <> show (systemNanoseconds t) + -- withSmpServer $ withTmpFiles $ withTempDirectory "tests/tmp" ts test logCfg :: LogConfig logCfg = LogConfig {lc_file = Nothing, lc_stderr = True} From 4d99921bdeb5da1f66071a3d3a3bce2dd2a074d9 Mon Sep 17 00:00:00 2001 From: pdavidow Date: Wed, 16 Aug 2023 09:50:13 -0400 Subject: [PATCH 02/16] cleanup --- src/Simplex/Chat/MarkdownEditing.hs | 16 ++++++++-------- tests/MarkdownEditingTests.hs | 28 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs index 65c840f760..b4bd51f233 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -38,9 +38,9 @@ data DiffStatus data DiffPlainStatus - = PlainUnchanged - | PlainInserted - | PlainDeleted + = UnchangedP + | InsertedP + | DeletedP deriving (Show, Eq) @@ -65,8 +65,8 @@ data FormattedChar = FormattedChar deriving (Show, Eq) -newtype LeftSide a = LeftSide a -newtype RightSide a = RightSide a +newtype LeftSide a = LeftSide a deriving (Show, Eq) +newtype RightSide a = RightSide a deriving (Show, Eq) newtype DeleteIndicies = DeleteIndicies (Seq Int) deriving (Show, Eq) @@ -102,9 +102,9 @@ findPlainDiffs (LeftSide left) (RightSide right) = f <$> diffs f :: DiffedChar -> DiffedPlainChar f (DiffedChar (FormattedChar c _) diffStatus) = DiffedPlainChar c diffStatusPlain where diffStatusPlain = case diffStatus of - UnchangedChar _ -> PlainUnchanged - Inserted -> PlainInserted - Deleted -> PlainDeleted + UnchangedChar _ -> UnchangedP + Inserted -> InsertedP + Deleted -> DeletedP findDiffs :: LeftSide (Seq FormattedChar) -> RightSide (Seq FormattedChar) -> Seq DiffedChar diff --git a/tests/MarkdownEditingTests.hs b/tests/MarkdownEditingTests.hs index 1ad148eab9..645938cd33 100644 --- a/tests/MarkdownEditingTests.hs +++ b/tests/MarkdownEditingTests.hs @@ -307,18 +307,18 @@ formattedEditedTextTests = describe "show edits" do it "findPlainDiffs" do findPlainDiffs (LeftSide "Hrl~!@lo") (RightSide "Herxy!@zo12") `shouldBe` S.fromList - [ DiffedPlainChar 'H' PlainUnchanged - , DiffedPlainChar 'e' PlainInserted - , DiffedPlainChar 'r' PlainUnchanged - , DiffedPlainChar 'l' PlainDeleted - , DiffedPlainChar '~' PlainDeleted - , DiffedPlainChar 'x' PlainInserted - , DiffedPlainChar 'y' PlainInserted - , DiffedPlainChar '!' PlainUnchanged - , DiffedPlainChar '@' PlainUnchanged - , DiffedPlainChar 'l' PlainDeleted - , DiffedPlainChar 'z' PlainInserted - , DiffedPlainChar 'o' PlainUnchanged - , DiffedPlainChar '1' PlainInserted - , DiffedPlainChar '2' PlainInserted + [ DiffedPlainChar 'H' UnchangedP + , DiffedPlainChar 'e' InsertedP + , DiffedPlainChar 'r' UnchangedP + , DiffedPlainChar 'l' DeletedP + , DiffedPlainChar '~' DeletedP + , DiffedPlainChar 'x' InsertedP + , DiffedPlainChar 'y' InsertedP + , DiffedPlainChar '!' UnchangedP + , DiffedPlainChar '@' UnchangedP + , DiffedPlainChar 'l' DeletedP + , DiffedPlainChar 'z' InsertedP + , DiffedPlainChar 'o' UnchangedP + , DiffedPlainChar '1' InsertedP + , DiffedPlainChar '2' InsertedP ] \ No newline at end of file From bb8a9f4b1eadb599dabcb38151559a1e8c0890dd Mon Sep 17 00:00:00 2001 From: pdavidow Date: Wed, 16 Aug 2023 10:09:42 -0400 Subject: [PATCH 03/16] "findPlainDiffs 1" --- tests/MarkdownEditingTests.hs | 115 +++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 50 deletions(-) diff --git a/tests/MarkdownEditingTests.hs b/tests/MarkdownEditingTests.hs index 645938cd33..807cde6a37 100644 --- a/tests/MarkdownEditingTests.hs +++ b/tests/MarkdownEditingTests.hs @@ -37,61 +37,49 @@ formattedEditedTextTests = describe "show edits" do (LeftSide $ S.fromList [ ]) - (RightSide $ S.fromList [ ]) - `shouldBe` S.fromList [ ] - it "no change" do findDiffs (LeftSide $ S.fromList [ FormattedChar 'H' Nothing ]) - (RightSide $ S.fromList [ FormattedChar 'H' Nothing ]) - `shouldBe` S.fromList [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat ] - it "add 1 char to empty" do findDiffs (LeftSide $ S.fromList [ ]) - (RightSide $ S.fromList [ FormattedChar 'H' Nothing ]) - `shouldBe` S.fromList [ DiffedChar (FormattedChar 'H' Nothing) Inserted ] - it "del the one and only" do findDiffs (LeftSide $ S.fromList [ FormattedChar 'H' Nothing ]) - (RightSide $ S.fromList [ ]) - `shouldBe` S.fromList [ DiffedChar (FormattedChar 'H' Nothing) Deleted ] - it "one character change" do findDiffs (LeftSide $ S.fromList @@ -101,7 +89,6 @@ formattedEditedTextTests = describe "show edits" do , FormattedChar 'l' Nothing , FormattedChar 'o' Nothing ]) - (RightSide $ S.fromList [ FormattedChar 'H' Nothing , FormattedChar 'e' Nothing @@ -109,7 +96,6 @@ formattedEditedTextTests = describe "show edits" do , FormattedChar 'l' Nothing , FormattedChar 'o' Nothing ]) - `shouldBe` S.fromList [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat , DiffedChar (FormattedChar 'r' Nothing) Deleted @@ -119,7 +105,6 @@ formattedEditedTextTests = describe "show edits" do , DiffedChar (FormattedChar 'o' Nothing) $ UnchangedChar UnchangedFormat ] - it "more1" do findDiffs (LeftSide $ S.fromList @@ -129,7 +114,6 @@ formattedEditedTextTests = describe "show edits" do , FormattedChar 'l' Nothing , FormattedChar 'o' Nothing ]) - (RightSide $ S.fromList [ FormattedChar 'H' Nothing , FormattedChar 'e' Nothing @@ -140,7 +124,6 @@ formattedEditedTextTests = describe "show edits" do , FormattedChar 'y' Nothing , FormattedChar 'z' Nothing ]) - `shouldBe` S.fromList [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat , DiffedChar (FormattedChar 'r' Nothing) Deleted @@ -153,7 +136,6 @@ formattedEditedTextTests = describe "show edits" do , DiffedChar (FormattedChar 'z' Nothing) Inserted ] - it "more2" do findDiffs (LeftSide $ S.fromList @@ -163,7 +145,6 @@ formattedEditedTextTests = describe "show edits" do , FormattedChar 'l' Nothing , FormattedChar 'o' Nothing ]) - (RightSide $ S.fromList [ FormattedChar 'H' Nothing , FormattedChar 'e' Nothing @@ -172,7 +153,6 @@ formattedEditedTextTests = describe "show edits" do , FormattedChar 'z' Nothing , FormattedChar 'o' Nothing ]) - `shouldBe` S.fromList [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat , DiffedChar (FormattedChar 'r' Nothing) Deleted @@ -185,7 +165,6 @@ formattedEditedTextTests = describe "show edits" do , DiffedChar (FormattedChar 'o' Nothing) $ UnchangedChar UnchangedFormat ] - it "more3" do findDiffs (LeftSide $ S.fromList @@ -196,7 +175,6 @@ formattedEditedTextTests = describe "show edits" do , FormattedChar 'l' Nothing , FormattedChar 'o' (Just $ colored Green) ]) - (RightSide $ S.fromList [ FormattedChar 'H' (Just Italic) , FormattedChar 'H' (Just Bold) @@ -206,7 +184,6 @@ formattedEditedTextTests = describe "show edits" do , FormattedChar 'z' (Just Secret) , FormattedChar 'o' (Just $ colored Blue) ]) - `shouldBe` S.fromList [ DiffedChar (FormattedChar 'H' (Just Bold)) $ UnchangedChar (ChangedToFormat (Just Italic)) , DiffedChar (FormattedChar 'H' (Just Bold)) $ UnchangedChar UnchangedFormat @@ -220,7 +197,6 @@ formattedEditedTextTests = describe "show edits" do , DiffedChar (FormattedChar 'o' (Just $ colored Green)) $ UnchangedChar (ChangedToFormat (Just $ colored Blue)) ] - it "more4" do findDiffs (LeftSide $ S.fromList @@ -233,7 +209,6 @@ formattedEditedTextTests = describe "show edits" do , FormattedChar 'l' Nothing , FormattedChar 'o' Nothing ]) - (RightSide $ S.fromList [ FormattedChar 'H' Nothing , FormattedChar 'e' Nothing @@ -247,7 +222,6 @@ formattedEditedTextTests = describe "show edits" do , FormattedChar '1' Nothing , FormattedChar '2' Nothing ]) - `shouldBe` S.fromList [ DiffedChar (FormattedChar 'H' Nothing) (UnchangedChar UnchangedFormat) , DiffedChar (FormattedChar 'e' Nothing) Inserted @@ -265,8 +239,7 @@ formattedEditedTextTests = describe "show edits" do , DiffedChar (FormattedChar '2' Nothing) Inserted ] - - it "SimplexLink" do + it "SimplexLink 1" do findDiffs (LeftSide $ S.fromList [ FormattedChar '>' $ Just $ SimplexLink @@ -275,7 +248,6 @@ formattedEditedTextTests = describe "show edits" do , trustedUri = True , smpHosts = NE.fromList ["host1", "host2", "host3"]} ]) - (RightSide $ S.fromList [ FormattedChar '>' $ Just SimplexLink { linkType = XLContact @@ -284,7 +256,6 @@ formattedEditedTextTests = describe "show edits" do , smpHosts = NE.fromList ["host0", "host2", "host3"] } ]) - `shouldBe` S.fromList [ DiffedChar (FormattedChar '>' $ Just SimplexLink @@ -293,8 +264,7 @@ formattedEditedTextTests = describe "show edits" do , trustedUri = True , smpHosts = NE.fromList ["host1", "host2", "host3"] } - ) $ - + ) $ UnchangedChar $ ChangedToFormat $ Just SimplexLink { linkType = XLContact , simplexUri = "https://api.twitter.com/3/tweets/:id" @@ -303,22 +273,67 @@ formattedEditedTextTests = describe "show edits" do } ] + it "findPlainDiffs 1" do + findPlainDiffs + (LeftSide "https://api.twitter.com/2/tweets/:id") + (RightSide "https://api.twitter.com/3/tweets/:id") + `shouldBe` S.fromList + [ DiffedPlainChar 'h' UnchangedP + , DiffedPlainChar 't' UnchangedP + , DiffedPlainChar 't' UnchangedP + , DiffedPlainChar 'p' UnchangedP + , DiffedPlainChar 's' UnchangedP + , DiffedPlainChar ':' UnchangedP + , DiffedPlainChar '/' UnchangedP + , DiffedPlainChar '/' UnchangedP + , DiffedPlainChar 'a' UnchangedP + , DiffedPlainChar 'p' UnchangedP + , DiffedPlainChar 'i' UnchangedP + , DiffedPlainChar '.' UnchangedP + , DiffedPlainChar 't' UnchangedP + , DiffedPlainChar 'w' UnchangedP + , DiffedPlainChar 'i' UnchangedP + , DiffedPlainChar 't' UnchangedP + , DiffedPlainChar 't' UnchangedP + , DiffedPlainChar 'e' UnchangedP + , DiffedPlainChar 'r' UnchangedP + , DiffedPlainChar '.' UnchangedP + , DiffedPlainChar 'c' UnchangedP + , DiffedPlainChar 'o' UnchangedP + , DiffedPlainChar 'm' UnchangedP + , DiffedPlainChar '/' UnchangedP + , DiffedPlainChar '2' DeletedP + , DiffedPlainChar '3' InsertedP + , DiffedPlainChar '/' UnchangedP + , DiffedPlainChar 't' UnchangedP + , DiffedPlainChar 'w' UnchangedP + , DiffedPlainChar 'e' UnchangedP + , DiffedPlainChar 'e' UnchangedP + , DiffedPlainChar 't' UnchangedP + , DiffedPlainChar 's' UnchangedP + , DiffedPlainChar '/' UnchangedP + , DiffedPlainChar ':' UnchangedP + , DiffedPlainChar 'i' UnchangedP + , DiffedPlainChar 'd' UnchangedP + ] - it "findPlainDiffs" do - findPlainDiffs (LeftSide "Hrl~!@lo") (RightSide "Herxy!@zo12") - `shouldBe` S.fromList - [ DiffedPlainChar 'H' UnchangedP - , DiffedPlainChar 'e' InsertedP - , DiffedPlainChar 'r' UnchangedP - , DiffedPlainChar 'l' DeletedP - , DiffedPlainChar '~' DeletedP - , DiffedPlainChar 'x' InsertedP - , DiffedPlainChar 'y' InsertedP - , DiffedPlainChar '!' UnchangedP - , DiffedPlainChar '@' UnchangedP - , DiffedPlainChar 'l' DeletedP - , DiffedPlainChar 'z' InsertedP - , DiffedPlainChar 'o' UnchangedP - , DiffedPlainChar '1' InsertedP - , DiffedPlainChar '2' InsertedP - ] \ No newline at end of file + it "findPlainDiffs 2" do + findPlainDiffs + (LeftSide "Hrl~!@lo") + (RightSide "Herxy!@zo12") + `shouldBe` S.fromList + [ DiffedPlainChar 'H' UnchangedP + , DiffedPlainChar 'e' InsertedP + , DiffedPlainChar 'r' UnchangedP + , DiffedPlainChar 'l' DeletedP + , DiffedPlainChar '~' DeletedP + , DiffedPlainChar 'x' InsertedP + , DiffedPlainChar 'y' InsertedP + , DiffedPlainChar '!' UnchangedP + , DiffedPlainChar '@' UnchangedP + , DiffedPlainChar 'l' DeletedP + , DiffedPlainChar 'z' InsertedP + , DiffedPlainChar 'o' UnchangedP + , DiffedPlainChar '1' InsertedP + , DiffedPlainChar '2' InsertedP + ] \ No newline at end of file From a9048e72709946f72e47ef593b49b406c1bbffc4 Mon Sep 17 00:00:00 2001 From: pdavidow Date: Wed, 16 Aug 2023 10:25:19 -0400 Subject: [PATCH 04/16] "SimplexLink 2" "SimplexLink 3" --- tests/MarkdownEditingTests.hs | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/MarkdownEditingTests.hs b/tests/MarkdownEditingTests.hs index 807cde6a37..84764fbd2a 100644 --- a/tests/MarkdownEditingTests.hs +++ b/tests/MarkdownEditingTests.hs @@ -273,6 +273,74 @@ formattedEditedTextTests = describe "show edits" do } ] + it "SimplexLink 2" do + findDiffs + (LeftSide $ S.fromList + [ FormattedChar '>' $ Just $ SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/2/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host1", "host2", "host3"]} + ]) + (RightSide $ S.fromList + [ FormattedChar '>' $ Just SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/3/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host1", "host2", "host3"] + } + ]) + `shouldBe` S.fromList + [ DiffedChar + (FormattedChar '>' $ Just SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/2/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host1", "host2", "host3"] + } + ) $ + UnchangedChar $ ChangedToFormat $ Just SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/3/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host1", "host2", "host3"] + } + ] + + it "SimplexLink 3" do + findDiffs + (LeftSide $ S.fromList + [ FormattedChar '>' $ Just $ SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/2/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host1", "host2", "host3"]} + ]) + (RightSide $ S.fromList + [ FormattedChar '>' $ Just SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/2/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host0", "host2", "host3"] + } + ]) + `shouldBe` S.fromList + [ DiffedChar + (FormattedChar '>' $ Just SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/2/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host1", "host2", "host3"] + } + ) $ + UnchangedChar $ ChangedToFormat $ Just SimplexLink + { linkType = XLContact + , simplexUri = "https://api.twitter.com/2/tweets/:id" + , trustedUri = True + , smpHosts = NE.fromList ["host0", "host2", "host3"] + } + ] + it "findPlainDiffs 1" do findPlainDiffs (LeftSide "https://api.twitter.com/2/tweets/:id") From bd4077f04cfa5d97ac75d294c02817caf3ed6e2d Mon Sep 17 00:00:00 2001 From: pdavidow Date: Wed, 16 Aug 2023 10:55:17 -0400 Subject: [PATCH 05/16] cleanup --- src/Simplex/Chat/MarkdownEditing.hs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs index b4bd51f233..7ed0ef7cec 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -94,17 +94,20 @@ indicesFromEdits = F.foldl' f (DeleteIndicies S.empty, InsertIndicies S.empty) findPlainDiffs :: LeftSide T.Text -> RightSide T.Text -> Seq DiffedPlainChar findPlainDiffs (LeftSide left) (RightSide right) = f <$> diffs where - diffs = findDiffs (LeftSide $ toFormattedCharsFromText left) (RightSide $ toFormattedCharsFromText right) + diffs = findDiffs + (LeftSide $ toFormattedCharsFromText left ) + (RightSide $ toFormattedCharsFromText right) toFormattedCharsFromText :: T.Text -> Seq FormattedChar toFormattedCharsFromText = fmap (`FormattedChar` Nothing) . S.fromList . T.unpack f :: DiffedChar -> DiffedPlainChar f (DiffedChar (FormattedChar c _) diffStatus) = DiffedPlainChar c diffStatusPlain - where diffStatusPlain = case diffStatus of - UnchangedChar _ -> UnchangedP - Inserted -> InsertedP - Deleted -> DeletedP + where + diffStatusPlain = case diffStatus of + UnchangedChar _ -> UnchangedP + Inserted -> InsertedP + Deleted -> DeletedP findDiffs :: LeftSide (Seq FormattedChar) -> RightSide (Seq FormattedChar) -> Seq DiffedChar From 67bac7c7f927e85a13d2c45e7fd9902ccae213e5 Mon Sep 17 00:00:00 2001 From: pdavidow Date: Wed, 16 Aug 2023 11:39:04 -0400 Subject: [PATCH 06/16] cleanup --- src/Simplex/Chat/MarkdownEditing.hs | 52 ++++++++++++----------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs index 7ed0ef7cec..2131f51133 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -16,18 +16,17 @@ module Simplex.Chat.MarkdownEditing , RightSide(..) , findDiffs , findPlainDiffs - , toFormattedChars ) where import qualified Data.Foldable as F import qualified Data.Map.Strict as M -import Data.Sequence ( Seq(..), (><) ) +import Data.Sequence (Seq(..), (><)) import qualified Data.Sequence as S import qualified Data.Text as T import qualified Data.Diff.Myers as D -import Simplex.Chat.Markdown ( FormattedText(..), Format ) +import Simplex.Chat.Markdown (Format) data DiffStatus @@ -73,48 +72,39 @@ newtype DeleteIndicies = DeleteIndicies (Seq Int) deriving (Show, Eq) newtype InsertIndicies = InsertIndicies (Seq Int) deriving (Show, Eq) -toFormattedChars :: [FormattedText] -> [FormattedChar] -toFormattedChars = concatMap toChars - where toChars (FormattedText f t) = map (`FormattedChar` f) $ T.unpack t - - -toText :: Seq FormattedChar -> T.Text -toText = T.pack . F.toList . fmap char - - -indicesFromEdits :: Seq D.Edit -> (DeleteIndicies, InsertIndicies) -indicesFromEdits = F.foldl' f (DeleteIndicies S.empty, InsertIndicies S.empty) - where - f :: (DeleteIndicies, InsertIndicies) -> D.Edit -> (DeleteIndicies, InsertIndicies) - f (x@(DeleteIndicies ds), y@(InsertIndicies is)) e = case e of - D.EditDelete m n -> (x', y) where x' = DeleteIndicies $ ds >< S.fromList [m .. n] - D.EditInsert _ m n -> (x , y') where y' = InsertIndicies $ is >< S.fromList [m .. n] - - findPlainDiffs :: LeftSide T.Text -> RightSide T.Text -> Seq DiffedPlainChar -findPlainDiffs (LeftSide left) (RightSide right) = f <$> diffs +findPlainDiffs (LeftSide left) (RightSide right) = toPlain <$> diffs where - diffs = findDiffs - (LeftSide $ toFormattedCharsFromText left ) - (RightSide $ toFormattedCharsFromText right) + diffs = findDiffs (LeftSide $ toFormatted left) (RightSide $ toFormatted right) - toFormattedCharsFromText :: T.Text -> Seq FormattedChar - toFormattedCharsFromText = fmap (`FormattedChar` Nothing) . S.fromList . T.unpack - - f :: DiffedChar -> DiffedPlainChar - f (DiffedChar (FormattedChar c _) diffStatus) = DiffedPlainChar c diffStatusPlain + toPlain :: DiffedChar -> DiffedPlainChar + toPlain (DiffedChar (FormattedChar c _) diffStatus) = DiffedPlainChar c diffStatusPlain where diffStatusPlain = case diffStatus of UnchangedChar _ -> UnchangedP Inserted -> InsertedP Deleted -> DeletedP + toFormatted :: T.Text -> Seq FormattedChar + toFormatted = fmap (`FormattedChar` Nothing) . S.fromList . T.unpack + findDiffs :: LeftSide (Seq FormattedChar) -> RightSide (Seq FormattedChar) -> Seq DiffedChar findDiffs (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchangedChars where edits = D.diffTexts (toText left) (toText right) - (DeleteIndicies deleteIndicies, InsertIndicies insertIndicies) = indicesFromEdits edits + (DeleteIndicies deleteIndicies, InsertIndicies insertIndicies) = indices + + toText :: Seq FormattedChar -> T.Text + toText = T.pack . F.toList . fmap char + + indices :: (DeleteIndicies, InsertIndicies) + indices = F.foldl' f (DeleteIndicies S.empty, InsertIndicies S.empty) edits + where + f :: (DeleteIndicies, InsertIndicies) -> D.Edit -> (DeleteIndicies, InsertIndicies) + f (x@(DeleteIndicies ds), y@(InsertIndicies is)) e = case e of + D.EditDelete m n -> (x', y) where x' = DeleteIndicies $ ds >< S.fromList [m .. n] + D.EditInsert _ m n -> (x , y') where y' = InsertIndicies $ is >< S.fromList [m .. n] unchangedChars :: M.Map Int DiffFormatStatus unchangedChars = F.foldl' f mempty unchangedCharPairs From f859696b05cac0123e35fc41a829488084db6ed4 Mon Sep 17 00:00:00 2001 From: pdavidow Date: Wed, 16 Aug 2023 21:28:23 -0400 Subject: [PATCH 07/16] cleanup --- src/Simplex/Chat/MarkdownEditing.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs index 2131f51133..eb31c3ebd9 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -130,10 +130,10 @@ findDiffs (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchanged markDeletesAndUnchangedChars :: Seq DiffedChar markDeletesAndUnchangedChars = S.mapWithIndex f left where - f :: Int -> FormattedChar -> DiffedChar - f i x = DiffedChar x $ - if i `elem` deleteIndicies then Deleted - else UnchangedChar $ unchangedChars M.! i -- should never error + f :: Int -> FormattedChar -> DiffedChar + f i x = DiffedChar x $ + if i `elem` deleteIndicies then Deleted + else UnchangedChar $ unchangedChars M.! i -- should never error addInserts :: Seq DiffedChar -> Seq DiffedChar addInserts base = F.foldr f base edits -- start from end and work backwards, hence foldr From 827bff3cb4faaf4e1aacaac0db1ea453e578ca66 Mon Sep 17 00:00:00 2001 From: pdavidow Date: Wed, 16 Aug 2023 23:40:24 -0400 Subject: [PATCH 08/16] cleanup --- src/Simplex/Chat/MarkdownEditing.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs index eb31c3ebd9..3f198a6224 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -114,11 +114,11 @@ findDiffs (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchanged leftWithoutDeletes :: Seq (Int, FormattedChar) -- indexed in original left leftWithoutDeletes = S.filter (\(i, _) -> i `notElem` deleteIndicies) leftZ - where leftZ = S.zip (S.fromList [0 .. S.length left]) left + where leftZ = S.zip (S.fromList [0 .. S.length left - 1]) left rightWithoutInserts :: Seq (Int, FormattedChar) -- indexed in original right rightWithoutInserts = S.filter (\(i, _) -> i `notElem` insertIndicies) rightZ - where rightZ = S.zip (S.fromList [0 .. S.length right]) right + where rightZ = S.zip (S.fromList [0 .. S.length right - 1]) right f :: M.Map Int DiffFormatStatus -> (Int, FormattedChar, FormattedChar) -> M.Map Int DiffFormatStatus f acc (i, FormattedChar _ fL, FormattedChar _ fR) = M.insert i x acc From 62b30440014ef15ec5730424f2e8cb23ba79b4d4 Mon Sep 17 00:00:00 2001 From: pdavidow Date: Wed, 16 Aug 2023 23:59:27 -0400 Subject: [PATCH 09/16] cleanup --- src/Simplex/Chat/MarkdownEditing.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs index 3f198a6224..4388c53502 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -125,7 +125,7 @@ findDiffs (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchanged where x = if fL == fR then UnchangedFormat else ChangedToFormat fR g :: ((Int, FormattedChar), (Int, FormattedChar)) -> (Int, FormattedChar, FormattedChar) - g ((i,c), (_j,d)) = (i,c,d) -- i and _j should always be equal + g ((i,c), (_j,d)) = (i,c,d) -- peg on left with i markDeletesAndUnchangedChars :: Seq DiffedChar markDeletesAndUnchangedChars = S.mapWithIndex f left From 0ce77987e32e63e0a66f74eb5c3a134ab9fe19be Mon Sep 17 00:00:00 2001 From: pdavidow Date: Thu, 17 Aug 2023 00:32:44 -0400 Subject: [PATCH 10/16] cleanup --- src/Simplex/Chat/MarkdownEditing.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs index 4388c53502..03ec820fd0 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -106,17 +106,17 @@ findDiffs (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchanged D.EditDelete m n -> (x', y) where x' = DeleteIndicies $ ds >< S.fromList [m .. n] D.EditInsert _ m n -> (x , y') where y' = InsertIndicies $ is >< S.fromList [m .. n] - unchangedChars :: M.Map Int DiffFormatStatus + unchangedChars :: M.Map Int DiffFormatStatus -- indexed in left unchangedChars = F.foldl' f mempty unchangedCharPairs where unchangedCharPairs :: Seq (Int, FormattedChar, FormattedChar) unchangedCharPairs = g <$> S.zip leftWithoutDeletes rightWithoutInserts - leftWithoutDeletes :: Seq (Int, FormattedChar) -- indexed in original left + leftWithoutDeletes :: Seq (Int, FormattedChar) leftWithoutDeletes = S.filter (\(i, _) -> i `notElem` deleteIndicies) leftZ where leftZ = S.zip (S.fromList [0 .. S.length left - 1]) left - rightWithoutInserts :: Seq (Int, FormattedChar) -- indexed in original right + rightWithoutInserts :: Seq (Int, FormattedChar) rightWithoutInserts = S.filter (\(i, _) -> i `notElem` insertIndicies) rightZ where rightZ = S.zip (S.fromList [0 .. S.length right - 1]) right From 12a1b083c04e77b56a7f1ad7cd1995c089e89259 Mon Sep 17 00:00:00 2001 From: pdavidow Date: Thu, 17 Aug 2023 02:19:43 -0400 Subject: [PATCH 11/16] cleanup --- src/Simplex/Chat/MarkdownEditing.hs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs index 03ec820fd0..228cf3b70e 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -21,6 +21,7 @@ module Simplex.Chat.MarkdownEditing import qualified Data.Foldable as F +import Data.Function ((&)) import qualified Data.Map.Strict as M import Data.Sequence (Seq(..), (><)) import qualified Data.Sequence as S @@ -113,12 +114,16 @@ findDiffs (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchanged unchangedCharPairs = g <$> S.zip leftWithoutDeletes rightWithoutInserts leftWithoutDeletes :: Seq (Int, FormattedChar) - leftWithoutDeletes = S.filter (\(i, _) -> i `notElem` deleteIndicies) leftZ - where leftZ = S.zip (S.fromList [0 .. S.length left - 1]) left + leftWithoutDeletes = + left + & S.zip (S.fromList [0 .. S.length left - 1]) + & S.filter (\(i, _) -> i `notElem` deleteIndicies) rightWithoutInserts :: Seq (Int, FormattedChar) - rightWithoutInserts = S.filter (\(i, _) -> i `notElem` insertIndicies) rightZ - where rightZ = S.zip (S.fromList [0 .. S.length right - 1]) right + rightWithoutInserts = + right + & S.zip (S.fromList [0 .. S.length right - 1]) + & S.filter (\(i, _) -> i `notElem` insertIndicies) f :: M.Map Int DiffFormatStatus -> (Int, FormattedChar, FormattedChar) -> M.Map Int DiffFormatStatus f acc (i, FormattedChar _ fL, FormattedChar _ fR) = M.insert i x acc From d005d79d54aab6d8f0f0a703e3bde1fe7ef53f8c Mon Sep 17 00:00:00 2001 From: pdavidow Date: Thu, 17 Aug 2023 11:35:57 -0400 Subject: [PATCH 12/16] cleanup --- src/Simplex/Chat/MarkdownEditing.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs index 228cf3b70e..d3708759a8 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -130,7 +130,7 @@ findDiffs (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchanged where x = if fL == fR then UnchangedFormat else ChangedToFormat fR g :: ((Int, FormattedChar), (Int, FormattedChar)) -> (Int, FormattedChar, FormattedChar) - g ((i,c), (_j,d)) = (i,c,d) -- peg on left with i + g ((i,c), (_,d)) = (i,c,d) -- peg on left markDeletesAndUnchangedChars :: Seq DiffedChar markDeletesAndUnchangedChars = S.mapWithIndex f left From faf09acf65e541e67e45bf2437643d54283bf808 Mon Sep 17 00:00:00 2001 From: pdavidow Date: Fri, 18 Aug 2023 11:59:35 -0400 Subject: [PATCH 13/16] cleanup --- src/Simplex/Chat/MarkdownEditing.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownEditing.hs index d3708759a8..30dfb63393 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownEditing.hs @@ -130,7 +130,7 @@ findDiffs (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchanged where x = if fL == fR then UnchangedFormat else ChangedToFormat fR g :: ((Int, FormattedChar), (Int, FormattedChar)) -> (Int, FormattedChar, FormattedChar) - g ((i,c), (_,d)) = (i,c,d) -- peg on left + g ((i,c), (_,d)) = (i,c,d) markDeletesAndUnchangedChars :: Seq DiffedChar markDeletesAndUnchangedChars = S.mapWithIndex f left From c6db756b68fa85715d60ad7434cb604119a38764 Mon Sep 17 00:00:00 2001 From: pdavidow Date: Sat, 19 Aug 2023 23:46:11 -0400 Subject: [PATCH 14/16] rename to Diff motif --- simplex-chat.cabal | 4 +- .../{MarkdownEditing.hs => MarkdownDiff.hs} | 16 +++---- ...wnEditingTests.hs => MarkdownDiffTests.hs} | 44 +++++++++---------- tests/Test.hs | 4 +- 4 files changed, 34 insertions(+), 34 deletions(-) rename src/Simplex/Chat/{MarkdownEditing.hs => MarkdownDiff.hs} (91%) rename tests/{MarkdownEditingTests.hs => MarkdownDiffTests.hs} (97%) diff --git a/simplex-chat.cabal b/simplex-chat.cabal index dfaf53e9c3..cb1c51e994 100644 --- a/simplex-chat.cabal +++ b/simplex-chat.cabal @@ -33,7 +33,7 @@ library Simplex.Chat.Core Simplex.Chat.Help Simplex.Chat.Markdown - Simplex.Chat.MarkdownEditing + Simplex.Chat.MarkdownDiff Simplex.Chat.Messages Simplex.Chat.Messages.CIContent Simplex.Chat.Migrations.M20220101_initial @@ -390,7 +390,7 @@ test-suite simplex-chat-test ChatTests.Profiles ChatTests.Utils MarkdownTests - MarkdownEditingTests + MarkdownDiffTests MobileTests ProtocolTests SchemaDump diff --git a/src/Simplex/Chat/MarkdownEditing.hs b/src/Simplex/Chat/MarkdownDiff.hs similarity index 91% rename from src/Simplex/Chat/MarkdownEditing.hs rename to src/Simplex/Chat/MarkdownDiff.hs index 30dfb63393..b3a11478b2 100644 --- a/src/Simplex/Chat/MarkdownEditing.hs +++ b/src/Simplex/Chat/MarkdownDiff.hs @@ -5,7 +5,7 @@ {-# HLINT ignore "Use newtype instead of data" #-} -module Simplex.Chat.MarkdownEditing +module Simplex.Chat.MarkdownDiff ( DiffedChar(..) , DiffedPlainChar(..) , DiffStatus(..) @@ -14,8 +14,8 @@ module Simplex.Chat.MarkdownEditing , FormattedChar(..) , LeftSide(..) , RightSide(..) - , findDiffs - , findPlainDiffs + , diff + , plainDiff ) where @@ -73,10 +73,10 @@ newtype DeleteIndicies = DeleteIndicies (Seq Int) deriving (Show, Eq) newtype InsertIndicies = InsertIndicies (Seq Int) deriving (Show, Eq) -findPlainDiffs :: LeftSide T.Text -> RightSide T.Text -> Seq DiffedPlainChar -findPlainDiffs (LeftSide left) (RightSide right) = toPlain <$> diffs +plainDiff :: LeftSide T.Text -> RightSide T.Text -> Seq DiffedPlainChar +plainDiff (LeftSide left) (RightSide right) = toPlain <$> formattedDiff where - diffs = findDiffs (LeftSide $ toFormatted left) (RightSide $ toFormatted right) + formattedDiff = diff (LeftSide $ toFormatted left) (RightSide $ toFormatted right) toPlain :: DiffedChar -> DiffedPlainChar toPlain (DiffedChar (FormattedChar c _) diffStatus) = DiffedPlainChar c diffStatusPlain @@ -90,8 +90,8 @@ findPlainDiffs (LeftSide left) (RightSide right) = toPlain <$> diffs toFormatted = fmap (`FormattedChar` Nothing) . S.fromList . T.unpack -findDiffs :: LeftSide (Seq FormattedChar) -> RightSide (Seq FormattedChar) -> Seq DiffedChar -findDiffs (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchangedChars +diff :: LeftSide (Seq FormattedChar) -> RightSide (Seq FormattedChar) -> Seq DiffedChar +diff (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchangedChars where edits = D.diffTexts (toText left) (toText right) (DeleteIndicies deleteIndicies, InsertIndicies insertIndicies) = indices diff --git a/tests/MarkdownEditingTests.hs b/tests/MarkdownDiffTests.hs similarity index 97% rename from tests/MarkdownEditingTests.hs rename to tests/MarkdownDiffTests.hs index 84764fbd2a..15f818fae6 100644 --- a/tests/MarkdownEditingTests.hs +++ b/tests/MarkdownDiffTests.hs @@ -2,12 +2,12 @@ {-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedStrings #-} -module MarkdownEditingTests where +module MarkdownDiffTests where import qualified Data.Sequence as S import Simplex.Chat.Markdown -import Simplex.Chat.MarkdownEditing +import Simplex.Chat.MarkdownDiff ( FormattedChar(..), DiffedChar(..), DiffedPlainChar(..), @@ -16,8 +16,8 @@ import Simplex.Chat.MarkdownEditing DiffFormatStatus(..), LeftSide(..), RightSide(..), - findDiffs, - findPlainDiffs ) + diff, + plainDiff ) import System.Console.ANSI.Types @@ -25,15 +25,15 @@ import Test.Hspec import qualified Data.List.NonEmpty as NE -markdownEditingTests :: Spec -markdownEditingTests = do +markdownDiffTests :: Spec +markdownDiffTests = do formattedEditedTextTests formattedEditedTextTests :: Spec formattedEditedTextTests = describe "show edits" do it "empty no change" do - findDiffs + diff (LeftSide $ S.fromList [ ]) @@ -45,7 +45,7 @@ formattedEditedTextTests = describe "show edits" do ] it "no change" do - findDiffs + diff (LeftSide $ S.fromList [ FormattedChar 'H' Nothing ]) @@ -57,7 +57,7 @@ formattedEditedTextTests = describe "show edits" do ] it "add 1 char to empty" do - findDiffs + diff (LeftSide $ S.fromList [ ]) @@ -69,7 +69,7 @@ formattedEditedTextTests = describe "show edits" do ] it "del the one and only" do - findDiffs + diff (LeftSide $ S.fromList [ FormattedChar 'H' Nothing ]) @@ -81,7 +81,7 @@ formattedEditedTextTests = describe "show edits" do ] it "one character change" do - findDiffs + diff (LeftSide $ S.fromList [ FormattedChar 'H' Nothing , FormattedChar 'r' Nothing @@ -106,7 +106,7 @@ formattedEditedTextTests = describe "show edits" do ] it "more1" do - findDiffs + diff (LeftSide $ S.fromList [ FormattedChar 'H' Nothing , FormattedChar 'r' Nothing @@ -137,7 +137,7 @@ formattedEditedTextTests = describe "show edits" do ] it "more2" do - findDiffs + diff (LeftSide $ S.fromList [ FormattedChar 'H' Nothing , FormattedChar 'r' Nothing @@ -166,7 +166,7 @@ formattedEditedTextTests = describe "show edits" do ] it "more3" do - findDiffs + diff (LeftSide $ S.fromList [ FormattedChar 'H' (Just Bold) , FormattedChar 'H' (Just Bold) @@ -198,7 +198,7 @@ formattedEditedTextTests = describe "show edits" do ] it "more4" do - findDiffs + diff (LeftSide $ S.fromList [ FormattedChar 'H' Nothing , FormattedChar 'r' Nothing @@ -240,7 +240,7 @@ formattedEditedTextTests = describe "show edits" do ] it "SimplexLink 1" do - findDiffs + diff (LeftSide $ S.fromList [ FormattedChar '>' $ Just $ SimplexLink { linkType = XLContact @@ -274,7 +274,7 @@ formattedEditedTextTests = describe "show edits" do ] it "SimplexLink 2" do - findDiffs + diff (LeftSide $ S.fromList [ FormattedChar '>' $ Just $ SimplexLink { linkType = XLContact @@ -308,7 +308,7 @@ formattedEditedTextTests = describe "show edits" do ] it "SimplexLink 3" do - findDiffs + diff (LeftSide $ S.fromList [ FormattedChar '>' $ Just $ SimplexLink { linkType = XLContact @@ -341,8 +341,8 @@ formattedEditedTextTests = describe "show edits" do } ] - it "findPlainDiffs 1" do - findPlainDiffs + it "plainDiff 1" do + plainDiff (LeftSide "https://api.twitter.com/2/tweets/:id") (RightSide "https://api.twitter.com/3/tweets/:id") `shouldBe` S.fromList @@ -385,8 +385,8 @@ formattedEditedTextTests = describe "show edits" do , DiffedPlainChar 'd' UnchangedP ] - it "findPlainDiffs 2" do - findPlainDiffs + it "plainDiff 2" do + plainDiff (LeftSide "Hrl~!@lo") (RightSide "Herxy!@zo12") `shouldBe` S.fromList diff --git a/tests/Test.hs b/tests/Test.hs index 488898829e..32e26ddef2 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -3,7 +3,7 @@ import ChatTests import Control.Logger.Simple import Data.Time.Clock.System import MarkdownTests -import MarkdownEditingTests +import MarkdownDiffTests import MobileTests import ProtocolTests import SchemaDump @@ -16,7 +16,7 @@ main :: IO () main = do setLogLevel LogError -- LogDebug withGlobalLogging logCfg . hspec $ do - describe "SimpleX chat markdown editing" markdownEditingTests + describe "SimpleX chat markdown diff" markdownDiffTests -- describe "SimpleX chat markdown" markdownTests -- describe "SimpleX chat view" viewTests -- describe "SimpleX chat protocol" protocolTests From ea8f1ee9a4a039208d00563c29e1f92376d240c0 Mon Sep 17 00:00:00 2001 From: pdavidow Date: Sat, 19 Aug 2023 23:51:36 -0400 Subject: [PATCH 15/16] cleanup --- src/Simplex/Chat/MarkdownDiff.hs | 52 ++--- tests/MarkdownDiffTests.hs | 374 +++++++++++++++---------------- 2 files changed, 213 insertions(+), 213 deletions(-) diff --git a/src/Simplex/Chat/MarkdownDiff.hs b/src/Simplex/Chat/MarkdownDiff.hs index b3a11478b2..c3cbf9e7c4 100644 --- a/src/Simplex/Chat/MarkdownDiff.hs +++ b/src/Simplex/Chat/MarkdownDiff.hs @@ -6,12 +6,12 @@ module Simplex.Chat.MarkdownDiff - ( DiffedChar(..) - , DiffedPlainChar(..) + ( DiffChar(..) + , DiffPlainChar(..) , DiffStatus(..) , DiffPlainStatus(..) , DiffFormatStatus(..) - , FormattedChar(..) + , FormatChar(..) , LeftSide(..) , RightSide(..) , diff @@ -50,15 +50,15 @@ data DiffFormatStatus deriving (Show, Eq) -data DiffedChar = DiffedChar FormattedChar DiffStatus +data DiffChar = DiffChar FormatChar DiffStatus deriving (Show, Eq) -data DiffedPlainChar = DiffedPlainChar Char DiffPlainStatus +data DiffPlainChar = DiffPlainChar Char DiffPlainStatus deriving (Show, Eq) -data FormattedChar = FormattedChar +data FormatChar = FormatChar { char :: Char , format :: Maybe Format } @@ -73,30 +73,30 @@ newtype DeleteIndicies = DeleteIndicies (Seq Int) deriving (Show, Eq) newtype InsertIndicies = InsertIndicies (Seq Int) deriving (Show, Eq) -plainDiff :: LeftSide T.Text -> RightSide T.Text -> Seq DiffedPlainChar +plainDiff :: LeftSide T.Text -> RightSide T.Text -> Seq DiffPlainChar plainDiff (LeftSide left) (RightSide right) = toPlain <$> formattedDiff where formattedDiff = diff (LeftSide $ toFormatted left) (RightSide $ toFormatted right) - toPlain :: DiffedChar -> DiffedPlainChar - toPlain (DiffedChar (FormattedChar c _) diffStatus) = DiffedPlainChar c diffStatusPlain + toPlain :: DiffChar -> DiffPlainChar + toPlain (DiffChar (FormatChar c _) diffStatus) = DiffPlainChar c diffStatusPlain where diffStatusPlain = case diffStatus of UnchangedChar _ -> UnchangedP Inserted -> InsertedP Deleted -> DeletedP - toFormatted :: T.Text -> Seq FormattedChar - toFormatted = fmap (`FormattedChar` Nothing) . S.fromList . T.unpack + toFormatted :: T.Text -> Seq FormatChar + toFormatted = fmap (`FormatChar` Nothing) . S.fromList . T.unpack -diff :: LeftSide (Seq FormattedChar) -> RightSide (Seq FormattedChar) -> Seq DiffedChar +diff :: LeftSide (Seq FormatChar) -> RightSide (Seq FormatChar) -> Seq DiffChar diff (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchangedChars where edits = D.diffTexts (toText left) (toText right) (DeleteIndicies deleteIndicies, InsertIndicies insertIndicies) = indices - toText :: Seq FormattedChar -> T.Text + toText :: Seq FormatChar -> T.Text toText = T.pack . F.toList . fmap char indices :: (DeleteIndicies, InsertIndicies) @@ -110,40 +110,40 @@ diff (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchangedChars unchangedChars :: M.Map Int DiffFormatStatus -- indexed in left unchangedChars = F.foldl' f mempty unchangedCharPairs where - unchangedCharPairs :: Seq (Int, FormattedChar, FormattedChar) + unchangedCharPairs :: Seq (Int, FormatChar, FormatChar) unchangedCharPairs = g <$> S.zip leftWithoutDeletes rightWithoutInserts - leftWithoutDeletes :: Seq (Int, FormattedChar) + leftWithoutDeletes :: Seq (Int, FormatChar) leftWithoutDeletes = left & S.zip (S.fromList [0 .. S.length left - 1]) & S.filter (\(i, _) -> i `notElem` deleteIndicies) - rightWithoutInserts :: Seq (Int, FormattedChar) + rightWithoutInserts :: Seq (Int, FormatChar) rightWithoutInserts = right & S.zip (S.fromList [0 .. S.length right - 1]) & S.filter (\(i, _) -> i `notElem` insertIndicies) - f :: M.Map Int DiffFormatStatus -> (Int, FormattedChar, FormattedChar) -> M.Map Int DiffFormatStatus - f acc (i, FormattedChar _ fL, FormattedChar _ fR) = M.insert i x acc + f :: M.Map Int DiffFormatStatus -> (Int, FormatChar, FormatChar) -> M.Map Int DiffFormatStatus + f acc (i, FormatChar _ fL, FormatChar _ fR) = M.insert i x acc where x = if fL == fR then UnchangedFormat else ChangedToFormat fR - g :: ((Int, FormattedChar), (Int, FormattedChar)) -> (Int, FormattedChar, FormattedChar) + g :: ((Int, FormatChar), (Int, FormatChar)) -> (Int, FormatChar, FormatChar) g ((i,c), (_,d)) = (i,c,d) - markDeletesAndUnchangedChars :: Seq DiffedChar + markDeletesAndUnchangedChars :: Seq DiffChar markDeletesAndUnchangedChars = S.mapWithIndex f left where - f :: Int -> FormattedChar -> DiffedChar - f i x = DiffedChar x $ + f :: Int -> FormatChar -> DiffChar + f i x = DiffChar x $ if i `elem` deleteIndicies then Deleted else UnchangedChar $ unchangedChars M.! i -- should never error - addInserts :: Seq DiffedChar -> Seq DiffedChar + addInserts :: Seq DiffChar -> Seq DiffChar addInserts base = F.foldr f base edits -- start from end and work backwards, hence foldr where - f :: D.Edit -> Seq DiffedChar -> Seq DiffedChar + f :: D.Edit -> Seq DiffChar -> Seq DiffChar f e acc = case e of D.EditDelete _ _ -> acc D.EditInsert i m n -> S.take i' acc >< inserts >< S.drop i' acc @@ -157,9 +157,9 @@ diff (LeftSide left) (RightSide right) = addInserts markDeletesAndUnchangedChars slidePastDeleteBlock :: Int -> Int slidePastDeleteBlock x = case S.lookup x acc of Nothing -> x - Just (DiffedChar _ diffStatus) -> + Just (DiffChar _ diffStatus) -> if diffStatus == Deleted then slidePastDeleteBlock (x + 1) else x rightFormatChars = S.take (n - m + 1) $ S.drop m right - inserts = fmap (`DiffedChar` Inserted) rightFormatChars \ No newline at end of file + inserts = fmap (`DiffChar` Inserted) rightFormatChars \ No newline at end of file diff --git a/tests/MarkdownDiffTests.hs b/tests/MarkdownDiffTests.hs index 15f818fae6..f1ff4ff8fb 100644 --- a/tests/MarkdownDiffTests.hs +++ b/tests/MarkdownDiffTests.hs @@ -8,9 +8,9 @@ import qualified Data.Sequence as S import Simplex.Chat.Markdown import Simplex.Chat.MarkdownDiff - ( FormattedChar(..), - DiffedChar(..), - DiffedPlainChar(..), + ( FormatChar(..), + DiffChar(..), + DiffPlainChar(..), DiffStatus(..), DiffPlainStatus(..), DiffFormatStatus(..), @@ -47,13 +47,13 @@ formattedEditedTextTests = describe "show edits" do it "no change" do diff (LeftSide $ S.fromList - [ FormattedChar 'H' Nothing + [ FormatChar 'H' Nothing ]) (RightSide $ S.fromList - [ FormattedChar 'H' Nothing + [ FormatChar 'H' Nothing ]) `shouldBe` S.fromList - [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat + [ DiffChar (FormatChar 'H' Nothing) $ UnchangedChar UnchangedFormat ] it "add 1 char to empty" do @@ -62,194 +62,194 @@ formattedEditedTextTests = describe "show edits" do [ ]) (RightSide $ S.fromList - [ FormattedChar 'H' Nothing + [ FormatChar 'H' Nothing ]) `shouldBe` S.fromList - [ DiffedChar (FormattedChar 'H' Nothing) Inserted + [ DiffChar (FormatChar 'H' Nothing) Inserted ] it "del the one and only" do diff (LeftSide $ S.fromList - [ FormattedChar 'H' Nothing + [ FormatChar 'H' Nothing ]) (RightSide $ S.fromList [ ]) `shouldBe` S.fromList - [ DiffedChar (FormattedChar 'H' Nothing) Deleted + [ DiffChar (FormatChar 'H' Nothing) Deleted ] it "one character change" do diff (LeftSide $ S.fromList - [ FormattedChar 'H' Nothing - , FormattedChar 'r' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'o' Nothing + [ FormatChar 'H' Nothing + , FormatChar 'r' Nothing + , FormatChar 'l' Nothing + , FormatChar 'l' Nothing + , FormatChar 'o' Nothing ]) (RightSide $ S.fromList - [ FormattedChar 'H' Nothing - , FormattedChar 'e' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'o' Nothing + [ FormatChar 'H' Nothing + , FormatChar 'e' Nothing + , FormatChar 'l' Nothing + , FormatChar 'l' Nothing + , FormatChar 'o' Nothing ]) `shouldBe` S.fromList - [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat - , DiffedChar (FormattedChar 'r' Nothing) Deleted - , DiffedChar (FormattedChar 'e' Nothing) Inserted - , DiffedChar (FormattedChar 'l' Nothing) $ UnchangedChar UnchangedFormat - , DiffedChar (FormattedChar 'l' Nothing) $ UnchangedChar UnchangedFormat - , DiffedChar (FormattedChar 'o' Nothing) $ UnchangedChar UnchangedFormat + [ DiffChar (FormatChar 'H' Nothing) $ UnchangedChar UnchangedFormat + , DiffChar (FormatChar 'r' Nothing) Deleted + , DiffChar (FormatChar 'e' Nothing) Inserted + , DiffChar (FormatChar 'l' Nothing) $ UnchangedChar UnchangedFormat + , DiffChar (FormatChar 'l' Nothing) $ UnchangedChar UnchangedFormat + , DiffChar (FormatChar 'o' Nothing) $ UnchangedChar UnchangedFormat ] it "more1" do diff (LeftSide $ S.fromList - [ FormattedChar 'H' Nothing - , FormattedChar 'r' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'o' Nothing + [ FormatChar 'H' Nothing + , FormatChar 'r' Nothing + , FormatChar 'l' Nothing + , FormatChar 'l' Nothing + , FormatChar 'o' Nothing ]) (RightSide $ S.fromList - [ FormattedChar 'H' Nothing - , FormattedChar 'e' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'o' Nothing - , FormattedChar 'x' Nothing - , FormattedChar 'y' Nothing - , FormattedChar 'z' Nothing + [ FormatChar 'H' Nothing + , FormatChar 'e' Nothing + , FormatChar 'l' Nothing + , FormatChar 'l' Nothing + , FormatChar 'o' Nothing + , FormatChar 'x' Nothing + , FormatChar 'y' Nothing + , FormatChar 'z' Nothing ]) `shouldBe` S.fromList - [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat - , DiffedChar (FormattedChar 'r' Nothing) Deleted - , DiffedChar (FormattedChar 'e' Nothing) Inserted - , DiffedChar (FormattedChar 'l' Nothing) $ UnchangedChar UnchangedFormat - , DiffedChar (FormattedChar 'l' Nothing) $ UnchangedChar UnchangedFormat - , DiffedChar (FormattedChar 'o' Nothing) $ UnchangedChar UnchangedFormat - , DiffedChar (FormattedChar 'x' Nothing) Inserted - , DiffedChar (FormattedChar 'y' Nothing) Inserted - , DiffedChar (FormattedChar 'z' Nothing) Inserted + [ DiffChar (FormatChar 'H' Nothing) $ UnchangedChar UnchangedFormat + , DiffChar (FormatChar 'r' Nothing) Deleted + , DiffChar (FormatChar 'e' Nothing) Inserted + , DiffChar (FormatChar 'l' Nothing) $ UnchangedChar UnchangedFormat + , DiffChar (FormatChar 'l' Nothing) $ UnchangedChar UnchangedFormat + , DiffChar (FormatChar 'o' Nothing) $ UnchangedChar UnchangedFormat + , DiffChar (FormatChar 'x' Nothing) Inserted + , DiffChar (FormatChar 'y' Nothing) Inserted + , DiffChar (FormatChar 'z' Nothing) Inserted ] it "more2" do diff (LeftSide $ S.fromList - [ FormattedChar 'H' Nothing - , FormattedChar 'r' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'o' Nothing + [ FormatChar 'H' Nothing + , FormatChar 'r' Nothing + , FormatChar 'l' Nothing + , FormatChar 'l' Nothing + , FormatChar 'o' Nothing ]) (RightSide $ S.fromList - [ FormattedChar 'H' Nothing - , FormattedChar 'e' Nothing - , FormattedChar 'x' Nothing - , FormattedChar 'y' Nothing - , FormattedChar 'z' Nothing - , FormattedChar 'o' Nothing + [ FormatChar 'H' Nothing + , FormatChar 'e' Nothing + , FormatChar 'x' Nothing + , FormatChar 'y' Nothing + , FormatChar 'z' Nothing + , FormatChar 'o' Nothing ]) `shouldBe` S.fromList - [ DiffedChar (FormattedChar 'H' Nothing) $ UnchangedChar UnchangedFormat - , DiffedChar (FormattedChar 'r' Nothing) Deleted - , DiffedChar (FormattedChar 'l' Nothing) Deleted - , DiffedChar (FormattedChar 'l' Nothing) Deleted - , DiffedChar (FormattedChar 'e' Nothing) Inserted - , DiffedChar (FormattedChar 'x' Nothing) Inserted - , DiffedChar (FormattedChar 'y' Nothing) Inserted - , DiffedChar (FormattedChar 'z' Nothing) Inserted - , DiffedChar (FormattedChar 'o' Nothing) $ UnchangedChar UnchangedFormat + [ DiffChar (FormatChar 'H' Nothing) $ UnchangedChar UnchangedFormat + , DiffChar (FormatChar 'r' Nothing) Deleted + , DiffChar (FormatChar 'l' Nothing) Deleted + , DiffChar (FormatChar 'l' Nothing) Deleted + , DiffChar (FormatChar 'e' Nothing) Inserted + , DiffChar (FormatChar 'x' Nothing) Inserted + , DiffChar (FormatChar 'y' Nothing) Inserted + , DiffChar (FormatChar 'z' Nothing) Inserted + , DiffChar (FormatChar 'o' Nothing) $ UnchangedChar UnchangedFormat ] it "more3" do diff (LeftSide $ S.fromList - [ FormattedChar 'H' (Just Bold) - , FormattedChar 'H' (Just Bold) - , FormattedChar 'r' Nothing - , FormattedChar 'l' (Just Secret) - , FormattedChar 'l' Nothing - , FormattedChar 'o' (Just $ colored Green) + [ FormatChar 'H' (Just Bold) + , FormatChar 'H' (Just Bold) + , FormatChar 'r' Nothing + , FormatChar 'l' (Just Secret) + , FormatChar 'l' Nothing + , FormatChar 'o' (Just $ colored Green) ]) (RightSide $ S.fromList - [ FormattedChar 'H' (Just Italic) - , FormattedChar 'H' (Just Bold) - , FormattedChar 'e' (Just $ colored Cyan) - , FormattedChar 'x' Nothing - , FormattedChar 'y' Nothing - , FormattedChar 'z' (Just Secret) - , FormattedChar 'o' (Just $ colored Blue) + [ FormatChar 'H' (Just Italic) + , FormatChar 'H' (Just Bold) + , FormatChar 'e' (Just $ colored Cyan) + , FormatChar 'x' Nothing + , FormatChar 'y' Nothing + , FormatChar 'z' (Just Secret) + , FormatChar 'o' (Just $ colored Blue) ]) `shouldBe` S.fromList - [ DiffedChar (FormattedChar 'H' (Just Bold)) $ UnchangedChar (ChangedToFormat (Just Italic)) - , DiffedChar (FormattedChar 'H' (Just Bold)) $ UnchangedChar UnchangedFormat - , DiffedChar (FormattedChar 'r' Nothing) Deleted - , DiffedChar (FormattedChar 'l' (Just Secret)) Deleted - , DiffedChar (FormattedChar 'l' Nothing) Deleted - , DiffedChar (FormattedChar 'e' (Just $ colored Cyan)) Inserted - , DiffedChar (FormattedChar 'x' Nothing) Inserted - , DiffedChar (FormattedChar 'y' Nothing) Inserted - , DiffedChar (FormattedChar 'z' (Just Secret)) Inserted - , DiffedChar (FormattedChar 'o' (Just $ colored Green)) $ UnchangedChar (ChangedToFormat (Just $ colored Blue)) + [ DiffChar (FormatChar 'H' (Just Bold)) $ UnchangedChar (ChangedToFormat (Just Italic)) + , DiffChar (FormatChar 'H' (Just Bold)) $ UnchangedChar UnchangedFormat + , DiffChar (FormatChar 'r' Nothing) Deleted + , DiffChar (FormatChar 'l' (Just Secret)) Deleted + , DiffChar (FormatChar 'l' Nothing) Deleted + , DiffChar (FormatChar 'e' (Just $ colored Cyan)) Inserted + , DiffChar (FormatChar 'x' Nothing) Inserted + , DiffChar (FormatChar 'y' Nothing) Inserted + , DiffChar (FormatChar 'z' (Just Secret)) Inserted + , DiffChar (FormatChar 'o' (Just $ colored Green)) $ UnchangedChar (ChangedToFormat (Just $ colored Blue)) ] it "more4" do diff (LeftSide $ S.fromList - [ FormattedChar 'H' Nothing - , FormattedChar 'r' Nothing - , FormattedChar 'l' Nothing - , FormattedChar '~' Nothing - , FormattedChar '!' Nothing - , FormattedChar '@' Nothing - , FormattedChar 'l' Nothing - , FormattedChar 'o' Nothing + [ FormatChar 'H' Nothing + , FormatChar 'r' Nothing + , FormatChar 'l' Nothing + , FormatChar '~' Nothing + , FormatChar '!' Nothing + , FormatChar '@' Nothing + , FormatChar 'l' Nothing + , FormatChar 'o' Nothing ]) (RightSide $ S.fromList - [ FormattedChar 'H' Nothing - , FormattedChar 'e' Nothing - , FormattedChar 'r' Nothing - , FormattedChar 'x' Nothing - , FormattedChar 'y' Nothing - , FormattedChar '!' Nothing - , FormattedChar '@' Nothing - , FormattedChar 'z' Nothing - , FormattedChar 'o' Nothing - , FormattedChar '1' Nothing - , FormattedChar '2' Nothing + [ FormatChar 'H' Nothing + , FormatChar 'e' Nothing + , FormatChar 'r' Nothing + , FormatChar 'x' Nothing + , FormatChar 'y' Nothing + , FormatChar '!' Nothing + , FormatChar '@' Nothing + , FormatChar 'z' Nothing + , FormatChar 'o' Nothing + , FormatChar '1' Nothing + , FormatChar '2' Nothing ]) `shouldBe` S.fromList - [ DiffedChar (FormattedChar 'H' Nothing) (UnchangedChar UnchangedFormat) - , DiffedChar (FormattedChar 'e' Nothing) Inserted - , DiffedChar (FormattedChar 'r' Nothing) (UnchangedChar UnchangedFormat) - , DiffedChar (FormattedChar 'l' Nothing) Deleted - , DiffedChar (FormattedChar '~' Nothing) Deleted - , DiffedChar (FormattedChar 'x' Nothing) Inserted - , DiffedChar (FormattedChar 'y' Nothing) Inserted - , DiffedChar (FormattedChar '!' Nothing) (UnchangedChar UnchangedFormat) - , DiffedChar (FormattedChar '@' Nothing) (UnchangedChar UnchangedFormat) - , DiffedChar (FormattedChar 'l' Nothing) Deleted - , DiffedChar (FormattedChar 'z' Nothing) Inserted - , DiffedChar (FormattedChar 'o' Nothing) (UnchangedChar UnchangedFormat) - , DiffedChar (FormattedChar '1' Nothing) Inserted - , DiffedChar (FormattedChar '2' Nothing) Inserted + [ DiffChar (FormatChar 'H' Nothing) (UnchangedChar UnchangedFormat) + , DiffChar (FormatChar 'e' Nothing) Inserted + , DiffChar (FormatChar 'r' Nothing) (UnchangedChar UnchangedFormat) + , DiffChar (FormatChar 'l' Nothing) Deleted + , DiffChar (FormatChar '~' Nothing) Deleted + , DiffChar (FormatChar 'x' Nothing) Inserted + , DiffChar (FormatChar 'y' Nothing) Inserted + , DiffChar (FormatChar '!' Nothing) (UnchangedChar UnchangedFormat) + , DiffChar (FormatChar '@' Nothing) (UnchangedChar UnchangedFormat) + , DiffChar (FormatChar 'l' Nothing) Deleted + , DiffChar (FormatChar 'z' Nothing) Inserted + , DiffChar (FormatChar 'o' Nothing) (UnchangedChar UnchangedFormat) + , DiffChar (FormatChar '1' Nothing) Inserted + , DiffChar (FormatChar '2' Nothing) Inserted ] it "SimplexLink 1" do diff (LeftSide $ S.fromList - [ FormattedChar '>' $ Just $ SimplexLink + [ FormatChar '>' $ Just $ SimplexLink { linkType = XLContact , simplexUri = "https://api.twitter.com/2/tweets/:id" , trustedUri = True , smpHosts = NE.fromList ["host1", "host2", "host3"]} ]) (RightSide $ S.fromList - [ FormattedChar '>' $ Just SimplexLink + [ FormatChar '>' $ Just SimplexLink { linkType = XLContact , simplexUri = "https://api.twitter.com/3/tweets/:id" , trustedUri = True @@ -257,8 +257,8 @@ formattedEditedTextTests = describe "show edits" do } ]) `shouldBe` S.fromList - [ DiffedChar - (FormattedChar '>' $ Just SimplexLink + [ DiffChar + (FormatChar '>' $ Just SimplexLink { linkType = XLContact , simplexUri = "https://api.twitter.com/2/tweets/:id" , trustedUri = True @@ -276,14 +276,14 @@ formattedEditedTextTests = describe "show edits" do it "SimplexLink 2" do diff (LeftSide $ S.fromList - [ FormattedChar '>' $ Just $ SimplexLink + [ FormatChar '>' $ Just $ SimplexLink { linkType = XLContact , simplexUri = "https://api.twitter.com/2/tweets/:id" , trustedUri = True , smpHosts = NE.fromList ["host1", "host2", "host3"]} ]) (RightSide $ S.fromList - [ FormattedChar '>' $ Just SimplexLink + [ FormatChar '>' $ Just SimplexLink { linkType = XLContact , simplexUri = "https://api.twitter.com/3/tweets/:id" , trustedUri = True @@ -291,8 +291,8 @@ formattedEditedTextTests = describe "show edits" do } ]) `shouldBe` S.fromList - [ DiffedChar - (FormattedChar '>' $ Just SimplexLink + [ DiffChar + (FormatChar '>' $ Just SimplexLink { linkType = XLContact , simplexUri = "https://api.twitter.com/2/tweets/:id" , trustedUri = True @@ -310,14 +310,14 @@ formattedEditedTextTests = describe "show edits" do it "SimplexLink 3" do diff (LeftSide $ S.fromList - [ FormattedChar '>' $ Just $ SimplexLink + [ FormatChar '>' $ Just $ SimplexLink { linkType = XLContact , simplexUri = "https://api.twitter.com/2/tweets/:id" , trustedUri = True , smpHosts = NE.fromList ["host1", "host2", "host3"]} ]) (RightSide $ S.fromList - [ FormattedChar '>' $ Just SimplexLink + [ FormatChar '>' $ Just SimplexLink { linkType = XLContact , simplexUri = "https://api.twitter.com/2/tweets/:id" , trustedUri = True @@ -325,8 +325,8 @@ formattedEditedTextTests = describe "show edits" do } ]) `shouldBe` S.fromList - [ DiffedChar - (FormattedChar '>' $ Just SimplexLink + [ DiffChar + (FormatChar '>' $ Just SimplexLink { linkType = XLContact , simplexUri = "https://api.twitter.com/2/tweets/:id" , trustedUri = True @@ -346,43 +346,43 @@ formattedEditedTextTests = describe "show edits" do (LeftSide "https://api.twitter.com/2/tweets/:id") (RightSide "https://api.twitter.com/3/tweets/:id") `shouldBe` S.fromList - [ DiffedPlainChar 'h' UnchangedP - , DiffedPlainChar 't' UnchangedP - , DiffedPlainChar 't' UnchangedP - , DiffedPlainChar 'p' UnchangedP - , DiffedPlainChar 's' UnchangedP - , DiffedPlainChar ':' UnchangedP - , DiffedPlainChar '/' UnchangedP - , DiffedPlainChar '/' UnchangedP - , DiffedPlainChar 'a' UnchangedP - , DiffedPlainChar 'p' UnchangedP - , DiffedPlainChar 'i' UnchangedP - , DiffedPlainChar '.' UnchangedP - , DiffedPlainChar 't' UnchangedP - , DiffedPlainChar 'w' UnchangedP - , DiffedPlainChar 'i' UnchangedP - , DiffedPlainChar 't' UnchangedP - , DiffedPlainChar 't' UnchangedP - , DiffedPlainChar 'e' UnchangedP - , DiffedPlainChar 'r' UnchangedP - , DiffedPlainChar '.' UnchangedP - , DiffedPlainChar 'c' UnchangedP - , DiffedPlainChar 'o' UnchangedP - , DiffedPlainChar 'm' UnchangedP - , DiffedPlainChar '/' UnchangedP - , DiffedPlainChar '2' DeletedP - , DiffedPlainChar '3' InsertedP - , DiffedPlainChar '/' UnchangedP - , DiffedPlainChar 't' UnchangedP - , DiffedPlainChar 'w' UnchangedP - , DiffedPlainChar 'e' UnchangedP - , DiffedPlainChar 'e' UnchangedP - , DiffedPlainChar 't' UnchangedP - , DiffedPlainChar 's' UnchangedP - , DiffedPlainChar '/' UnchangedP - , DiffedPlainChar ':' UnchangedP - , DiffedPlainChar 'i' UnchangedP - , DiffedPlainChar 'd' UnchangedP + [ DiffPlainChar 'h' UnchangedP + , DiffPlainChar 't' UnchangedP + , DiffPlainChar 't' UnchangedP + , DiffPlainChar 'p' UnchangedP + , DiffPlainChar 's' UnchangedP + , DiffPlainChar ':' UnchangedP + , DiffPlainChar '/' UnchangedP + , DiffPlainChar '/' UnchangedP + , DiffPlainChar 'a' UnchangedP + , DiffPlainChar 'p' UnchangedP + , DiffPlainChar 'i' UnchangedP + , DiffPlainChar '.' UnchangedP + , DiffPlainChar 't' UnchangedP + , DiffPlainChar 'w' UnchangedP + , DiffPlainChar 'i' UnchangedP + , DiffPlainChar 't' UnchangedP + , DiffPlainChar 't' UnchangedP + , DiffPlainChar 'e' UnchangedP + , DiffPlainChar 'r' UnchangedP + , DiffPlainChar '.' UnchangedP + , DiffPlainChar 'c' UnchangedP + , DiffPlainChar 'o' UnchangedP + , DiffPlainChar 'm' UnchangedP + , DiffPlainChar '/' UnchangedP + , DiffPlainChar '2' DeletedP + , DiffPlainChar '3' InsertedP + , DiffPlainChar '/' UnchangedP + , DiffPlainChar 't' UnchangedP + , DiffPlainChar 'w' UnchangedP + , DiffPlainChar 'e' UnchangedP + , DiffPlainChar 'e' UnchangedP + , DiffPlainChar 't' UnchangedP + , DiffPlainChar 's' UnchangedP + , DiffPlainChar '/' UnchangedP + , DiffPlainChar ':' UnchangedP + , DiffPlainChar 'i' UnchangedP + , DiffPlainChar 'd' UnchangedP ] it "plainDiff 2" do @@ -390,18 +390,18 @@ formattedEditedTextTests = describe "show edits" do (LeftSide "Hrl~!@lo") (RightSide "Herxy!@zo12") `shouldBe` S.fromList - [ DiffedPlainChar 'H' UnchangedP - , DiffedPlainChar 'e' InsertedP - , DiffedPlainChar 'r' UnchangedP - , DiffedPlainChar 'l' DeletedP - , DiffedPlainChar '~' DeletedP - , DiffedPlainChar 'x' InsertedP - , DiffedPlainChar 'y' InsertedP - , DiffedPlainChar '!' UnchangedP - , DiffedPlainChar '@' UnchangedP - , DiffedPlainChar 'l' DeletedP - , DiffedPlainChar 'z' InsertedP - , DiffedPlainChar 'o' UnchangedP - , DiffedPlainChar '1' InsertedP - , DiffedPlainChar '2' InsertedP + [ DiffPlainChar 'H' UnchangedP + , DiffPlainChar 'e' InsertedP + , DiffPlainChar 'r' UnchangedP + , DiffPlainChar 'l' DeletedP + , DiffPlainChar '~' DeletedP + , DiffPlainChar 'x' InsertedP + , DiffPlainChar 'y' InsertedP + , DiffPlainChar '!' UnchangedP + , DiffPlainChar '@' UnchangedP + , DiffPlainChar 'l' DeletedP + , DiffPlainChar 'z' InsertedP + , DiffPlainChar 'o' UnchangedP + , DiffPlainChar '1' InsertedP + , DiffPlainChar '2' InsertedP ] \ No newline at end of file From 3e182dbca57273d6c15a6016eb26eca93e9e867e Mon Sep 17 00:00:00 2001 From: pdavidow Date: Sat, 19 Aug 2023 23:59:08 -0400 Subject: [PATCH 16/16] cleanup --- tests/MarkdownDiffTests.hs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/MarkdownDiffTests.hs b/tests/MarkdownDiffTests.hs index f1ff4ff8fb..5d35414d2d 100644 --- a/tests/MarkdownDiffTests.hs +++ b/tests/MarkdownDiffTests.hs @@ -168,24 +168,24 @@ formattedEditedTextTests = describe "show edits" do it "more3" do diff (LeftSide $ S.fromList - [ FormatChar 'H' (Just Bold) - , FormatChar 'H' (Just Bold) + [ FormatChar 'H' $ Just Bold + , FormatChar 'H' $ Just Bold , FormatChar 'r' Nothing - , FormatChar 'l' (Just Secret) + , FormatChar 'l' $ Just Secret , FormatChar 'l' Nothing - , FormatChar 'o' (Just $ colored Green) + , FormatChar 'o' $ Just $ colored Green ]) (RightSide $ S.fromList - [ FormatChar 'H' (Just Italic) - , FormatChar 'H' (Just Bold) - , FormatChar 'e' (Just $ colored Cyan) + [ FormatChar 'H' $ Just Italic + , FormatChar 'H' $ Just Bold + , FormatChar 'e' $ Just $ colored Cyan , FormatChar 'x' Nothing , FormatChar 'y' Nothing - , FormatChar 'z' (Just Secret) - , FormatChar 'o' (Just $ colored Blue) + , FormatChar 'z' $ Just Secret + , FormatChar 'o' $ Just $ colored Blue ]) `shouldBe` S.fromList - [ DiffChar (FormatChar 'H' (Just Bold)) $ UnchangedChar (ChangedToFormat (Just Italic)) + [ DiffChar (FormatChar 'H' (Just Bold)) $ UnchangedChar $ ChangedToFormat $ Just Italic , DiffChar (FormatChar 'H' (Just Bold)) $ UnchangedChar UnchangedFormat , DiffChar (FormatChar 'r' Nothing) Deleted , DiffChar (FormatChar 'l' (Just Secret)) Deleted @@ -194,7 +194,7 @@ formattedEditedTextTests = describe "show edits" do , DiffChar (FormatChar 'x' Nothing) Inserted , DiffChar (FormatChar 'y' Nothing) Inserted , DiffChar (FormatChar 'z' (Just Secret)) Inserted - , DiffChar (FormatChar 'o' (Just $ colored Green)) $ UnchangedChar (ChangedToFormat (Just $ colored Blue)) + , DiffChar (FormatChar 'o' (Just $ colored Green)) $ UnchangedChar $ ChangedToFormat $ Just $ colored Blue ] it "more4" do @@ -223,18 +223,18 @@ formattedEditedTextTests = describe "show edits" do , FormatChar '2' Nothing ]) `shouldBe` S.fromList - [ DiffChar (FormatChar 'H' Nothing) (UnchangedChar UnchangedFormat) + [ DiffChar (FormatChar 'H' Nothing) $ UnchangedChar UnchangedFormat , DiffChar (FormatChar 'e' Nothing) Inserted - , DiffChar (FormatChar 'r' Nothing) (UnchangedChar UnchangedFormat) + , DiffChar (FormatChar 'r' Nothing) $ UnchangedChar UnchangedFormat , DiffChar (FormatChar 'l' Nothing) Deleted , DiffChar (FormatChar '~' Nothing) Deleted , DiffChar (FormatChar 'x' Nothing) Inserted , DiffChar (FormatChar 'y' Nothing) Inserted - , DiffChar (FormatChar '!' Nothing) (UnchangedChar UnchangedFormat) - , DiffChar (FormatChar '@' Nothing) (UnchangedChar UnchangedFormat) + , DiffChar (FormatChar '!' Nothing) $ UnchangedChar UnchangedFormat + , DiffChar (FormatChar '@' Nothing) $ UnchangedChar UnchangedFormat , DiffChar (FormatChar 'l' Nothing) Deleted , DiffChar (FormatChar 'z' Nothing) Inserted - , DiffChar (FormatChar 'o' Nothing) (UnchangedChar UnchangedFormat) + , DiffChar (FormatChar 'o' Nothing) $ UnchangedChar UnchangedFormat , DiffChar (FormatChar '1' Nothing) Inserted , DiffChar (FormatChar '2' Nothing) Inserted ]