1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 00:42:33 +03:00

Merge origin/master into git-diff

This commit is contained in:
joshvera 2016-01-15 16:15:44 -05:00
commit 33b2331a23
5 changed files with 20 additions and 20 deletions

View File

@ -8,6 +8,7 @@ import qualified Parsers as P
import Syntax
import Range
import qualified PatchOutput
import Renderer
import Split
import Term
import Unified
@ -57,7 +58,7 @@ main = do
let parse = (P.parserForType . T.pack . takeExtension) filepath
terms <- sequence $ parse <$> sources
let replaceLeaves = breakDownLeavesByWord <$> sources
printDiff arguments filepath (runJoin sources) (runJoin $ replaceLeaves <*> terms)
printDiff arguments filepath (uncurry diff . runJoin $ replaceLeaves <*> terms) (runJoin sources)
where opts = info (helper <*> arguments)
(fullDesc <> progDesc "Diff some things" <> header "semantic-diff - diff semantically")
@ -79,25 +80,24 @@ fetchFromGitRepo repoPath path sha = join $ withRepository lgFactory repoPath $
return s
return $ transcode bytestring
-- | Print a diff, given the command-line arguments, source files, and terms.
printDiff :: Arguments -> FilePath -> (Source Char, Source Char) -> (Term T.Text Info, Term T.Text Info) -> IO ()
printDiff arguments filepath (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 -> FilePath -> Renderer T.Text (IO ())
printDiff arguments filepath 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 filepath -<.> ".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

View File

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

View File

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

View File

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

View File

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