1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 17:04:47 +03:00
semantic/app/Split.hs

52 lines
1.8 KiB
Haskell
Raw Normal View History

2015-12-01 22:18:13 +03:00
module Split where
2015-12-01 19:33:16 +03:00
import Diff
2015-12-01 22:18:13 +03:00
import Patch
import Syntax
import Term
import Unified
2015-12-01 22:35:23 +03:00
import Control.Comonad.Cofree
2015-12-01 23:31:02 +03:00
import qualified Data.Map as Map
import qualified Data.Set as Set
2015-12-01 19:33:16 +03:00
import Rainbow
2015-12-01 22:35:23 +03:00
type ClassName = String
type Element a = Cofree (Syntax a) (Maybe ClassName, String)
2015-12-01 22:18:13 +03:00
2015-12-01 23:31:02 +03:00
data HTML =
Text String
2015-12-01 23:39:21 +03:00
| Span (Maybe ClassName) String
| Ul (Maybe ClassName) [HTML]
| Dl (Maybe ClassName) [HTML]
2015-12-02 00:18:08 +03:00
| Dt String
2015-12-01 23:31:02 +03:00
deriving (Eq, Show)
2015-12-01 19:33:16 +03:00
split :: Diff a Info -> String -> String -> IO ByteString
2015-12-01 22:18:13 +03:00
split _ _ _ = return mempty
splitDiff :: Diff a Info -> String -> String -> [(String, String)]
splitDiff _ _ _ = []
splitPatch :: String -> String -> Patch (Term a Info) -> (Maybe HTML, Maybe HTML)
splitPatch before after patch = (fmap (splitTerm before) $ Patch.before patch, fmap (splitTerm after) $ Patch.after patch)
2015-12-01 22:18:13 +03:00
splitTerm :: String -> Term a Info -> HTML
2015-12-01 23:56:15 +03:00
splitTerm source term = fst $ cata toElement term where
toElement (Info range lineRange categories) (Leaf _) = (Span (classify categories) $ substring range source, range)
2015-12-02 00:07:45 +03:00
toElement (Info range lineRange categories) (Indexed i) = makeList i range categories
2015-12-02 00:07:53 +03:00
toElement (Info range lineRange categories) (Fixed i) = makeList i range categories
2015-12-02 00:07:45 +03:00
2015-12-01 23:57:44 +03:00
accumulate (children, previous) (child, range) = (children ++ [ subtext previous $ start range, child ], end range)
2015-12-02 00:18:36 +03:00
accumulateFromMap (children, previous) (key, (child, range)) = (children ++ [ subtext previous $ start range, Dt key, child ], end range)
2015-12-02 00:07:45 +03:00
makeList i range categories = (Ul (classify categories) items, range)
where
(children, previous) = foldl accumulate ([], start range) i
items = children ++ [ subtext previous $ end range ]
subtext :: Int -> Int -> HTML
subtext start end = Text $ substring (Range start end) source
classify :: Set.Set Category -> Maybe ClassName
classify categories = foldr (const . Just) Nothing categories