mirror of
https://github.com/github/semantic.git
synced 2024-11-24 08:54:07 +03:00
Merge pull request #408 from github/extract-diff
Create a diff function and clarify the role of printDiff
This commit is contained in:
commit
fe472fc60c
32
app/Main.hs
32
app/Main.hs
@ -7,6 +7,7 @@ import qualified Parsers as P
|
||||
import Syntax
|
||||
import Range
|
||||
import qualified PatchOutput
|
||||
import Renderer
|
||||
import Split
|
||||
import Term
|
||||
import Unified
|
||||
@ -47,29 +48,28 @@ main = do
|
||||
let parse = (P.parserForType . T.pack . takeExtension) sourceAPath
|
||||
terms <- sequence $ parse <$> sources
|
||||
let replaceLeaves = breakDownLeavesByWord <$> sources
|
||||
printDiff arguments (runJoin sources) (runJoin $ replaceLeaves <*> terms)
|
||||
printDiff arguments (uncurry diff $ runJoin $ replaceLeaves <*> terms) (runJoin sources)
|
||||
where opts = info (helper <*> arguments)
|
||||
(fullDesc <> progDesc "Diff some things" <> header "semantic-diff - diff semantically")
|
||||
|
||||
-- | Print a diff, given the command-line arguments, source files, and terms.
|
||||
printDiff :: Arguments -> (Source Char, Source Char) -> (Term T.Text Info, Term T.Text Info) -> IO ()
|
||||
printDiff arguments (aSource, bSource) (aTerm, bTerm) = case format arguments of
|
||||
Unified -> do
|
||||
rendered <- unified diff aSource bSource
|
||||
B1.putStr rendered
|
||||
Split -> do
|
||||
rendered <- split diff aSource bSource
|
||||
case output arguments of
|
||||
Just path -> do
|
||||
-- | Diff two terms.
|
||||
diff :: (Eq a, Eq annotation, Categorizable annotation) => Term a annotation -> Term a annotation -> Diff a annotation
|
||||
diff = interpret comparable
|
||||
|
||||
-- | Return a renderer from the command-line arguments that will print the diff.
|
||||
printDiff :: Arguments -> Renderer T.Text (IO ())
|
||||
printDiff arguments diff sources = case format arguments of
|
||||
Unified -> B1.putStr =<< unified diff sources
|
||||
Split -> put (output arguments) =<< split diff sources
|
||||
where
|
||||
put Nothing rendered = TextIO.putStr rendered
|
||||
put (Just path) rendered = do
|
||||
isDir <- doesDirectoryExist path
|
||||
let outputPath = if isDir
|
||||
then path </> (takeFileName (sourceB arguments) -<.> ".html")
|
||||
else path
|
||||
IO.withFile outputPath IO.WriteMode (write rendered)
|
||||
Nothing -> TextIO.putStr rendered
|
||||
Patch -> putStr $ PatchOutput.patch diff aSource bSource
|
||||
where diff = interpret comparable aTerm bTerm
|
||||
write rendered h = TextIO.hPutStr h rendered
|
||||
IO.withFile outputPath IO.WriteMode (flip TextIO.hPutStr rendered)
|
||||
Patch -> putStr $ PatchOutput.patch diff sources
|
||||
|
||||
-- | Replace every string leaf with leaves of the words in the string.
|
||||
breakDownLeavesByWord :: Source Char -> Term T.Text Info -> Term T.Text Info
|
||||
|
@ -17,7 +17,7 @@ import Data.Maybe
|
||||
import Data.Monoid
|
||||
|
||||
patch :: Renderer a String
|
||||
patch diff sourceA sourceB = mconcat $ showHunk (sourceA, sourceB) <$> hunks diff (sourceA, sourceB)
|
||||
patch diff (sourceA, sourceB) = mconcat $ showHunk (sourceA, sourceB) <$> hunks diff (sourceA, sourceB)
|
||||
|
||||
data Hunk a = Hunk { offset :: (Sum Int, Sum Int), changes :: [Change a], trailingContext :: [Row a] }
|
||||
deriving (Eq, Show)
|
||||
|
@ -4,4 +4,4 @@ import Diff
|
||||
import Source
|
||||
|
||||
-- | A function that will render a diff, given the two source files.
|
||||
type Renderer a b = Diff a Info -> Source Char -> Source Char -> b
|
||||
type Renderer a b = Diff a Info -> (Source Char, Source Char) -> b
|
||||
|
@ -32,7 +32,7 @@ classifyMarkup :: Foldable f => f String -> Markup -> Markup
|
||||
classifyMarkup categories element = maybe element ((element !) . A.class_ . stringValue . ("category-" ++)) $ maybeFirst categories
|
||||
|
||||
split :: Renderer leaf (IO TL.Text)
|
||||
split diff before after = return . renderHtml
|
||||
split diff (before, after) = return . renderHtml
|
||||
. docTypeHtml
|
||||
. ((head $ link ! A.rel "stylesheet" ! A.href "style.css") <>)
|
||||
. body
|
||||
|
@ -15,7 +15,7 @@ import qualified Data.OrderedMap as Map
|
||||
import Rainbow
|
||||
|
||||
unified :: Renderer a (IO ByteString)
|
||||
unified diff before after = do
|
||||
unified diff (before, after) = do
|
||||
renderer <- byteStringMakerFromEnvironment
|
||||
return . mconcat . chunksToByteStrings renderer . fst $ iter g mapped where
|
||||
mapped = fmap (unifiedPatch &&& range) diff
|
||||
|
Loading…
Reference in New Issue
Block a user