mirror of
https://github.com/github/semantic.git
synced 2024-12-23 14:54:16 +03:00
93 lines
3.3 KiB
Haskell
93 lines
3.3 KiB
Haskell
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
module Source where
|
|
|
|
import Prologue hiding (uncons)
|
|
import Data.Text (unpack, pack)
|
|
import Data.String
|
|
import qualified Data.Vector as Vector
|
|
import Numeric
|
|
import Range
|
|
|
|
data SourceKind = PlainBlob Word32 | ExecutableBlob Word32 | SymlinkBlob Word32
|
|
deriving (Show, Eq)
|
|
|
|
modeToDigits :: SourceKind -> String
|
|
modeToDigits (PlainBlob mode) = showOct mode ""
|
|
modeToDigits (ExecutableBlob mode) = showOct mode ""
|
|
modeToDigits (SymlinkBlob mode) = showOct mode ""
|
|
|
|
data SourceBlob = SourceBlob { source :: Source Char, oid :: String, path :: FilePath, blobKind :: Maybe SourceKind }
|
|
deriving (Show, Eq)
|
|
|
|
-- | The default plain blob mode
|
|
defaultPlainBlob :: SourceKind
|
|
defaultPlainBlob = PlainBlob 0o100644
|
|
|
|
-- | The contents of a source file, backed by a vector for efficient slicing.
|
|
newtype Source a = Source { getVector :: Vector.Vector a }
|
|
deriving (Eq, Show, Foldable, Functor, Traversable)
|
|
|
|
|
|
-- | Map blobs with Nothing blobKind to empty blobs.
|
|
idOrEmptySourceBlob :: SourceBlob -> SourceBlob
|
|
idOrEmptySourceBlob blob = if isNothing (blobKind blob)
|
|
then blob { oid = nullOid, blobKind = Nothing }
|
|
else blob
|
|
|
|
nullOid :: String
|
|
nullOid = "0000000000000000000000000000000000000000"
|
|
|
|
-- | Return a Source from a list of items.
|
|
fromList :: [a] -> Source a
|
|
fromList = Source . Vector.fromList
|
|
|
|
-- | Return a Source of Chars from a Text.
|
|
fromText :: Text -> Source Char
|
|
fromText = Source . Vector.fromList . unpack
|
|
|
|
-- | Return a Source that contains a slice of the given Source.
|
|
slice :: Range -> Source a -> Source a
|
|
slice range = Source . Vector.slice (start range) (rangeLength range) . getVector
|
|
|
|
-- | Return a String with the contents of the Source.
|
|
toString :: Source Char -> String
|
|
toString = toList
|
|
|
|
-- | Return a text with the contents of the Source.
|
|
toText :: Source Char -> Text
|
|
toText = pack . toList
|
|
|
|
-- | Return the item at the given index.
|
|
at :: Source a -> Int -> a
|
|
at = (Vector.!) . getVector
|
|
|
|
-- | Remove the first item and return it with the rest of the source.
|
|
uncons :: Source a -> Maybe (a, Source a)
|
|
uncons (Source vector) = if null vector then Nothing else Just (Vector.head vector, Source $ Vector.tail vector)
|
|
|
|
-- | Split the source into the longest prefix of elements that do not satisfy the predicate and the rest without copying.
|
|
break :: (a -> Bool) -> Source a -> (Source a, Source a)
|
|
break predicate (Source vector) = let (start, remainder) = Vector.break predicate vector in (Source start, Source remainder)
|
|
|
|
-- | Concatenate two sources.
|
|
(++) :: Source a -> Source a -> Source a
|
|
(++) (Source a) = Source . (a Vector.++) . getVector
|
|
|
|
-- | Split the contents of the source after newlines.
|
|
actualLines :: Source Char -> [Source Char]
|
|
actualLines source | null source = [ source ]
|
|
actualLines source = case Source.break (== '\n') source of
|
|
(l, lines') -> case uncons lines' of
|
|
Nothing -> [ l ]
|
|
Just (_, lines') -> (l Source.++ fromList "\n") : actualLines lines'
|
|
|
|
-- | Compute the line ranges within a given range of a string.
|
|
actualLineRanges :: Range -> Source Char -> [Range]
|
|
actualLineRanges range = drop 1 . scanl toRange (Range (start range) (start range)) . actualLines . slice range
|
|
where toRange previous string = Range (end previous) $ end previous + length string
|
|
|
|
|
|
instance Monoid (Source a) where
|
|
mempty = fromList []
|
|
mappend = (Source.++)
|