1
1
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:
Patrick Thomson 2019-09-20 12:02:41 -04:00
parent 099aa6a7af
commit 1cc29f4361
3 changed files with 11 additions and 5 deletions

View File

@ -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.

View File

@ -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\"")))) ])

View File

@ -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)