1
1
mirror of https://github.com/github/semantic.git synced 2024-12-30 18:36:27 +03:00

More straightforward computation of the source for a given diff/term.

This commit is contained in:
Rob Rix 2017-05-09 12:04:25 -04:00
parent 58ed93f559
commit e578772376

View File

@ -84,13 +84,13 @@ diffTOC blobs = removeDupes . diffToTOCSummaries >=> toJSONSummaries
diffToTOCSummaries :: HasDefaultFields fields => Diff (Syntax Text) (Record fields) -> [TOCSummary DiffInfo]
diffToTOCSummaries = para $ \diff -> case diff of
Free (Join (_, annotation) :< syntax)
| Just identifier <- identifierFor (textFor (source (Both.snd blobs)) . diffRange) diffUnwrap syntax ->
| Just identifier <- identifierFor (diffSource (source (Both.snd blobs))) diffUnwrap syntax ->
foldMap (fmap (contextualize (Summarizable (category annotation) identifier (sourceSpan annotation) "modified")) . snd) syntax
| otherwise -> foldMap snd syntax
Pure patch -> fmap summarize (sequenceA (runBothWith mapPatch (toInfo . source <$> blobs) patch))
toInfo :: HasDefaultFields fields => Source -> Term (Syntax Text) (Record fields) -> [DiffInfo]
toInfo source = para $ \ (annotation :< syntax) -> let termName = fromMaybe (textFor source (byteRange annotation)) (identifierFor (textFor source . termRange) (Just . unwrap) syntax) in case syntax of
toInfo source = para $ \ (annotation :< syntax) -> let termName = fromMaybe (textFor source (byteRange annotation)) (identifierFor (termSource source) (Just . tailF . runCofree) syntax) in case syntax of
S.ParseError{} -> [DiffInfo Nothing termName (sourceSpan annotation)]
S.Indexed{} -> foldMap snd syntax
S.Fixed{} -> foldMap snd syntax
@ -114,16 +114,18 @@ identifierFor getSource unwrap syntax = case syntax of
| otherwise -> Just $ getSource receiver <> "." <> getSource identifier
_ -> Nothing
diffRange :: (HasField fields Range, Functor f) => Diff f (Record fields) -> Range
diffRange = iter (byteRange . Both.snd . headF) . fmap (termRange . afterOrBefore)
diffSource :: HasField fields Range => Source -> Diff f (Record fields) -> Text
diffSource source diff = case runFree diff of
Free (a :< _) -> toText (Source.slice (byteRange (Both.snd a)) source)
Pure a -> termSource source (afterOrBefore a)
diffUnwrap :: Diff f (Record fields) -> Maybe (f (Diff f (Record fields)))
diffUnwrap diff = case runFree diff of
Free (_ :< syntax) -> Just syntax
_ -> Nothing
termRange :: (HasField fields Range, Functor f) => Term f (Record fields) -> Range
termRange = byteRange . extract
termSource :: HasField fields Range => Source -> Term f (Record fields) -> Text
termSource source = toText . flip Source.slice source . byteRange . headF . runCofree
textFor :: Source -> Range -> Text
textFor source = toText . flip Source.slice source