mirror of
https://github.com/github/semantic.git
synced 2024-11-24 08:54:07 +03:00
Add Data.BLob.fileForRelPath and avoid early toString calls.
This commit is contained in:
parent
099aa6a7af
commit
1cc29f4361
@ -2,6 +2,7 @@
|
||||
module Data.Blob
|
||||
( File(..)
|
||||
, fileForPath
|
||||
, fileForRelPath
|
||||
, Blob(..)
|
||||
, Blobs(..)
|
||||
, blobLanguage
|
||||
@ -32,6 +33,7 @@ import qualified Data.ByteString.Lazy as BL
|
||||
import Data.JSON.Fields
|
||||
import Data.Language
|
||||
import Data.Source as Source
|
||||
import qualified System.Path as Path
|
||||
|
||||
-- | A 'FilePath' paired with its corresponding 'Language'.
|
||||
-- Unpacked to have the same size overhead as (FilePath, Language).
|
||||
@ -40,9 +42,13 @@ data File = File
|
||||
, fileLanguage :: Language
|
||||
} deriving (Show, Eq, Generic)
|
||||
|
||||
-- | Prefer 'fileForRelPath' if at all possible.
|
||||
fileForPath :: FilePath -> File
|
||||
fileForPath p = File p (languageForFilePath p)
|
||||
|
||||
fileForRelPath :: Path.RelFile -> File
|
||||
fileForRelPath = fileForPath . Path.toString
|
||||
|
||||
-- | The source, path information, and language of a file read from disk.
|
||||
data Blob = Blob
|
||||
{ blobSource :: Source -- ^ The UTF-8 encoded source text of the blob.
|
||||
|
@ -220,7 +220,7 @@ isMethodOrFunction a
|
||||
| otherwise = False
|
||||
|
||||
blobsForPaths :: Both Path.RelFile -> IO BlobPair
|
||||
blobsForPaths = readFilePathPair . fmap Path.toString . fmap (Path.relDir "test/fixtures" </>)
|
||||
blobsForPaths = readFilePathPair . fmap (Path.relDir "test/fixtures" </>)
|
||||
|
||||
blankDiff :: Diff'
|
||||
blankDiff = merge (Nothing, Nothing) (inject [ inserting (termIn Nothing (inject (Syntax.Identifier (name "\"a\"")))) ])
|
||||
|
@ -88,20 +88,20 @@ instance IsString Name where
|
||||
-- | Returns an s-expression formatted diff for the specified FilePath pair.
|
||||
diffFilePaths :: TaskSession -> Both Path.RelFile -> IO ByteString
|
||||
diffFilePaths session paths
|
||||
= readFilePathPair (fmap Path.toString paths)
|
||||
= readFilePathPair paths
|
||||
>>= runTask session . parseDiffBuilder @[] DiffSExpression . pure
|
||||
>>= either (die . displayException) (pure . runBuilder)
|
||||
|
||||
-- | Returns an s-expression parse tree for the specified path.
|
||||
parseFilePath :: TaskSession -> Path.RelFile -> IO (Either SomeException ByteString)
|
||||
parseFilePath session path = do
|
||||
blob <- readBlobFromFile (fileForPath (Path.toString path))
|
||||
blob <- readBlobFromFile (fileForRelPath path)
|
||||
res <- runTask session $ parseTermBuilder TermSExpression (toList blob)
|
||||
pure (runBuilder <$> res)
|
||||
|
||||
-- | Read two files to a BlobPair.
|
||||
readFilePathPair :: Both FilePath -> IO BlobPair
|
||||
readFilePathPair paths = let paths' = fmap fileForPath paths in
|
||||
readFilePathPair :: Both Path.RelFile -> IO BlobPair
|
||||
readFilePathPair paths = let paths' = fmap fileForRelPath paths in
|
||||
runBothWith readFilePair paths'
|
||||
|
||||
parseTestFile :: Parser term -> Path.RelFile -> IO (Blob, term)
|
||||
|
Loading…
Reference in New Issue
Block a user