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

41 lines
1.5 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) (Map.Map String HTML)
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)
toElement (Info range lineRange categories) (Indexed i) = (Ul (classify categories) $ children ++ [ Text $ substring Range { start = previous, end = end range } source ], range) where
(children, previous) = foldl accumulate ([], start range) i
accumulate (children, previous) (child, range) = (children ++ [ Text $ substring Range { start = previous, end = start range } source, child ], end range)
classify :: Set.Set Category -> Maybe ClassName
classify categories = foldr (const . Just) Nothing categories