mirror of
https://github.com/github/semantic.git
synced 2024-11-28 18:23:44 +03:00
Remove SourceSpan from Record
This commit is contained in:
parent
f7286f927c
commit
aa99744db7
@ -157,7 +157,7 @@ maybeParentContext annotations = if null annotations
|
||||
toDoc :: Text -> Doc
|
||||
toDoc = string . toS
|
||||
|
||||
diffSummary :: (HasCategory leaf, HasField fields Category, HasField fields SourceSpan, HasField fields Range) => Both SourceBlob -> Diff leaf (Record fields) -> [DiffSummary DiffInfo]
|
||||
diffSummary :: (HasCategory leaf, HasField fields Category, HasField fields Range) => Both SourceBlob -> Diff leaf (Record fields) -> [DiffSummary DiffInfo]
|
||||
diffSummary blobs = cata $ \case
|
||||
-- Skip comments and leaves since they don't have any changes
|
||||
(Free (_ :< Leaf _)) -> []
|
||||
@ -194,7 +194,7 @@ diffSummary blobs = cata $ \case
|
||||
annotateWithCategory infos = prependSummary (category $ snd infos)
|
||||
|
||||
|
||||
termToDiffInfo :: (HasCategory leaf, HasField fields Category, HasField fields SourceSpan, HasField fields Range) => Source Char -> Term leaf (Record fields) -> DiffInfo
|
||||
termToDiffInfo :: (HasCategory leaf, HasField fields Category, HasField fields Range) => Source Char -> Term leaf (Record fields) -> DiffInfo
|
||||
termToDiffInfo blob term = case unwrap term of
|
||||
Leaf _ -> LeafInfo (toCategoryName term) (toTermName' term)
|
||||
S.Indexed children -> BranchInfo (termToDiffInfo' <$> children) (toCategoryName term) BIndexed
|
||||
|
@ -32,10 +32,9 @@ import Term
|
||||
import TreeSitter
|
||||
import Text.Parser.TreeSitter.Language
|
||||
import qualified Data.Text as T
|
||||
import SourceSpan
|
||||
|
||||
-- | Return a parser based on the file extension (including the ".").
|
||||
parserForType :: Text -> Parser '[Range, Category, Cost, SourceSpan]
|
||||
parserForType :: Text -> Parser '[Range, Category, Cost]
|
||||
parserForType mediaType = case languageForType mediaType of
|
||||
Just C -> treeSitterParser C ts_language_c
|
||||
Just JavaScript -> treeSitterParser JavaScript ts_language_javascript
|
||||
@ -43,22 +42,21 @@ parserForType mediaType = case languageForType mediaType of
|
||||
_ -> lineByLineParser
|
||||
|
||||
-- | A fallback parser that treats a file simply as rows of strings.
|
||||
lineByLineParser :: Parser '[Range, Category, Cost, SourceSpan]
|
||||
lineByLineParser :: Parser '[Range, Category, Cost]
|
||||
lineByLineParser blob = pure . cofree . root $ case foldl' annotateLeaves ([], 0) lines of
|
||||
(leaves, _) -> cofree <$> leaves
|
||||
where
|
||||
input = source blob
|
||||
lines = actualLines input
|
||||
rootSpan = SourceSpan (toS $ path blob) (SourcePos 0 0) (SourcePos (length lines) (maybe 0 length $ lastMay lines))
|
||||
root children = let cost = 1 + fromIntegral (length children) in
|
||||
((Range 0 $ length input) .: Other "program" .: cost .: rootSpan.: RNil) :< Indexed children
|
||||
leaf charIndex line = ((Range charIndex $ charIndex + T.length line) .: Other "program" .: 1 .: rootSpan .: RNil) :< Leaf line
|
||||
((Range 0 $ length input) .: Other "program" .: cost .: RNil) :< Indexed children
|
||||
leaf charIndex line = ((Range charIndex $ charIndex + T.length line) .: Other "program" .: 1 .: RNil) :< Leaf line
|
||||
annotateLeaves (accum, charIndex) line =
|
||||
(accum <> [ leaf charIndex (toText line) ] , charIndex + length line)
|
||||
toText = T.pack . Source.toString
|
||||
|
||||
-- | Return the parser that should be used for a given path.
|
||||
parserForFilepath :: FilePath -> Parser '[Range, Category, Cost, SourceSpan]
|
||||
parserForFilepath :: FilePath -> Parser '[Range, Category, Cost]
|
||||
parserForFilepath = parserForType . toS . takeExtension
|
||||
|
||||
-- | Replace every string leaf with leaves of the words in the string.
|
||||
@ -122,7 +120,7 @@ diffCostWithCachedTermCosts diff = unCost $ case runFree diff of
|
||||
|
||||
|
||||
-- | Returns a rendered diff given a parser, diff arguments and two source blobs.
|
||||
textDiff :: (Eq (Record fields), HasField fields Category, HasField fields Cost, HasField fields Range, HasField fields SourceSpan) => Parser fields -> DiffArguments -> Both SourceBlob -> IO Text
|
||||
textDiff :: (Eq (Record fields), HasField fields Category, HasField fields Cost, HasField fields Range) => Parser fields -> DiffArguments -> Both SourceBlob -> IO Text
|
||||
textDiff parser arguments sources = case format arguments of
|
||||
Split -> diffFiles parser split sources
|
||||
Patch -> diffFiles parser patch sources
|
||||
@ -138,7 +136,7 @@ truncatedDiff arguments sources = case format arguments of
|
||||
Summary -> pure ""
|
||||
|
||||
-- | Prints a rendered diff to stdio or a filepath given a parser, diff arguments and two source blobs.
|
||||
printDiff :: (Eq (Record fields), HasField fields Category, HasField fields Cost, HasField fields Range, HasField fields SourceSpan) => Parser fields -> DiffArguments -> Both SourceBlob -> IO ()
|
||||
printDiff :: (Eq (Record fields), HasField fields Category, HasField fields Cost, HasField fields Range) => Parser fields -> DiffArguments -> Both SourceBlob -> IO ()
|
||||
printDiff parser arguments sources = do
|
||||
rendered <- textDiff parser arguments sources
|
||||
case (output arguments) of
|
||||
|
@ -1,22 +1,15 @@
|
||||
{-# LANGUAGE DataKinds, GeneralizedNewtypeDeriving #-}
|
||||
module Info (SourceSpan, sourceSpan, setSourceSpan, characterRange, setCharacterRange, category, setCategory, Cost(..), cost, setCost) where
|
||||
module Info (characterRange, setCharacterRange, category, setCategory, Cost(..), cost, setCost) where
|
||||
|
||||
import Data.Record
|
||||
import Prologue
|
||||
import Category
|
||||
import Range
|
||||
import SourceSpan
|
||||
import Test.QuickCheck
|
||||
|
||||
newtype Cost = Cost { unCost :: Integer }
|
||||
deriving (Eq, Num, Ord, Show)
|
||||
|
||||
sourceSpan :: HasField fields SourceSpan => Record fields -> SourceSpan
|
||||
sourceSpan = getField
|
||||
|
||||
setSourceSpan :: HasField fields SourceSpan => Record fields -> SourceSpan -> Record fields
|
||||
setSourceSpan = setField
|
||||
|
||||
characterRange :: HasField fields Range => Record fields -> Range
|
||||
characterRange = getField
|
||||
|
||||
|
@ -11,6 +11,7 @@ import qualified Syntax as S
|
||||
import Term
|
||||
import qualified Data.Set as Set
|
||||
import Source
|
||||
import SourceSpan
|
||||
|
||||
-- | A function that takes a source file and returns an annotated AST.
|
||||
-- | The return is in the IO monad because some of the parsers are written in C
|
||||
|
@ -6,9 +6,8 @@ import Renderer
|
||||
import Data.Aeson
|
||||
import Data.Record
|
||||
import Range
|
||||
import SourceSpan
|
||||
import DiffSummary
|
||||
|
||||
summary :: (HasField fields Category, HasField fields Range, HasField fields SourceSpan) => Renderer (Record fields)
|
||||
summary :: (HasField fields Category, HasField fields Range) => Renderer (Record fields)
|
||||
summary blobs diff = toS . encode $ summaries >>= annotatedSummaries
|
||||
where summaries = diffSummary blobs diff
|
@ -16,7 +16,7 @@ import qualified Text.Parser.TreeSitter as TS
|
||||
import SourceSpan
|
||||
|
||||
-- | Returns a TreeSitter parser for the given language and TreeSitter grammar.
|
||||
treeSitterParser :: Language -> Ptr TS.Language -> Parser '[Range, Category, Cost, SourceSpan]
|
||||
treeSitterParser :: Language -> Ptr TS.Language -> Parser '[Range, Category, Cost]
|
||||
treeSitterParser language grammar blob = do
|
||||
document <- ts_document_make
|
||||
ts_document_set_language document grammar
|
||||
@ -85,7 +85,7 @@ defaultCategoryForNodeName name = case name of
|
||||
_ -> Other name
|
||||
|
||||
-- | Return a parser for a tree sitter language & document.
|
||||
documentToTerm :: Language -> Ptr Document -> Parser '[Range, Category, Cost, SourceSpan]
|
||||
documentToTerm :: Language -> Ptr Document -> Parser '[Range, Category, Cost]
|
||||
documentToTerm language document blob = alloca $ \ root -> do
|
||||
ts_document_root_node_p document root
|
||||
toTerm root
|
||||
@ -102,7 +102,7 @@ documentToTerm language document blob = alloca $ \ root -> do
|
||||
, spanEnd = SourcePos (fromIntegral $ ts_node_p_end_point_row node) (fromIntegral $ ts_node_p_end_point_column node) }
|
||||
|
||||
let cost' = 1 + sum (cost . extract <$> children)
|
||||
let info = range .: (categoriesForLanguage language (toS name)) .: cost' .: sourceSpan .: RNil
|
||||
let info = range .: (categoriesForLanguage language (toS name)) .: cost' .: RNil
|
||||
pure $! termConstructor (source blob) sourceSpan info children
|
||||
getChild node n out = do
|
||||
_ <- ts_node_p_named_child node n out
|
||||
|
@ -71,7 +71,7 @@ normalizeName path = addExtension (dropExtension $ dropExtension path) (takeExte
|
||||
-- | Given file paths for A, B, and, optionally, a diff, return whether diffing
|
||||
-- | the files will produce the diff. If no diff is provided, then the result
|
||||
-- | is true, but the diff will still be calculated.
|
||||
testDiff :: Renderer (Record '[Range, Category, Cost, SourceSpan]) -> Both FilePath -> Maybe FilePath -> (Verbatim -> Verbatim -> Expectation) -> Expectation
|
||||
testDiff :: Renderer (Record '[Range, Category, Cost]) -> Both FilePath -> Maybe FilePath -> (Verbatim -> Verbatim -> Expectation) -> Expectation
|
||||
testDiff renderer paths diff matcher = do
|
||||
sources <- sequence $ readAndTranscodeFile <$> paths
|
||||
actual <- Verbatim <$> diffFiles parser renderer (sourceBlobs sources)
|
||||
|
@ -18,15 +18,14 @@ import Data.List (partition)
|
||||
import Term.Arbitrary
|
||||
import Interpreter
|
||||
import Info
|
||||
import SourceSpan
|
||||
|
||||
arrayInfo :: Record '[Category, SourceSpan]
|
||||
arrayInfo = ArrayLiteral .: SourceSpan "test.js" (SourcePos 0 0) (SourcePos 0 3) .: RNil
|
||||
arrayInfo :: Record '[Category]
|
||||
arrayInfo = ArrayLiteral .: RNil
|
||||
|
||||
literalInfo :: Record '[Category, SourceSpan]
|
||||
literalInfo = StringLiteral .: SourceSpan "test.js" (SourcePos 0 0) (SourcePos 0 1) .: RNil
|
||||
literalInfo :: Record '[Category]
|
||||
literalInfo = StringLiteral .: RNil
|
||||
|
||||
testDiff :: Diff Text (Record '[Category, SourceSpan])
|
||||
testDiff :: Diff Text (Record '[Category])
|
||||
testDiff = free $ Free (pure arrayInfo :< Indexed [ free $ Pure (Insert (cofree $ literalInfo :< Leaf "a")) ])
|
||||
|
||||
testSummary :: DiffSummary DiffInfo
|
||||
@ -42,7 +41,7 @@ spec = parallel $ do
|
||||
diffSummary testDiff `shouldBe` [ DiffSummary { patch = Insert (LeafInfo "string" "a"), parentAnnotations = [ ArrayLiteral ] } ]
|
||||
|
||||
prop "equal terms produce identity diffs" $
|
||||
\ a -> let term = toTerm (a :: ArbitraryTerm Text (Record '[Category, SourceSpan])) in
|
||||
\ a -> let term = toTerm (a :: ArbitraryTerm Text (Record '[Category])) in
|
||||
diffSummary (diffTerms wrap (==) diffCost term term) `shouldBe` []
|
||||
|
||||
describe "annotatedSummaries" $ do
|
||||
@ -53,7 +52,7 @@ spec = parallel $ do
|
||||
describe "DiffInfo" $ do
|
||||
prop "patches in summaries match the patches in diffs" $
|
||||
\a -> let
|
||||
diff = (toDiff (a :: ArbitraryDiff Text (Record '[Category, Cost, SourceSpan])))
|
||||
diff = (toDiff (a :: ArbitraryDiff Text (Record '[Category, Cost])))
|
||||
summaries = diffSummary diff
|
||||
patches = toList diff
|
||||
in
|
||||
@ -62,14 +61,14 @@ spec = parallel $ do
|
||||
(() <$ branchPatches, () <$ otherPatches) `shouldBe` (() <$ branchDiffPatches, () <$ otherDiffPatches)
|
||||
prop "generates one LeafInfo for each child in an arbitrary branch patch" $
|
||||
\a -> let
|
||||
diff = (toDiff (a :: ArbitraryDiff Text (Record '[Category, SourceSpan])))
|
||||
diff = (toDiff (a :: ArbitraryDiff Text (Record '[Category])))
|
||||
diffInfoPatches = patch <$> diffSummary diff
|
||||
syntaxPatches = toList diff
|
||||
extractLeaves :: DiffInfo -> [DiffInfo]
|
||||
extractLeaves (BranchInfo children _ _) = join $ extractLeaves <$> children
|
||||
extractLeaves leaf = [ leaf ]
|
||||
|
||||
extractDiffLeaves :: Term Text (Record '[Category, SourceSpan]) -> [ Term Text (Record '[Category, SourceSpan]) ]
|
||||
extractDiffLeaves :: Term Text (Record '[Category]) -> [ Term Text (Record '[Category]) ]
|
||||
extractDiffLeaves term = case unwrap term of
|
||||
(Indexed children) -> join $ extractDiffLeaves <$> children
|
||||
(Fixed children) -> join $ extractDiffLeaves <$> children
|
||||
|
Loading…
Reference in New Issue
Block a user