1
1
mirror of https://github.com/github/semantic.git synced 2024-12-26 16:33:03 +03:00

Merge remote-tracking branch 'origin/master' into git-diff

This commit is contained in:
joshvera 2016-02-02 19:25:18 -05:00
commit ab91ecbe06
3 changed files with 18 additions and 39 deletions

View File

@ -7,6 +7,7 @@ import qualified Data.Text as T
import Diff
import Syntax
import Range
import Renderer
import qualified Data.ByteString.Char8 as B1
import qualified Data.Text.ICU.Detect as Detect
import qualified Data.Text.ICU.Convert as Convert
@ -17,7 +18,6 @@ import System.FilePath
import qualified System.IO as IO
import qualified Data.Text.Lazy.IO as TextIO
import qualified PatchOutput
import Interpreter
import qualified Parsers as P
import Rainbow
@ -45,28 +45,27 @@ transcode text = fromText <$> do
converter <- Convert.open match Nothing
return $ Convert.toUnicode converter text
-- | Read the file and convert it to Unicode.
readAndTranscodeFile :: FilePath -> IO (Source Char)
readAndTranscodeFile path = do
text <- B1.readFile path
transcode text
printDiff :: DiffArguments -> (Source Char, Source Char) -> (Term T.Text Info, Term T.Text Info) -> IO ()
printDiff arguments (aSource, bSource) (aTerm, bTerm) = case format arguments of
Unified -> put $ unified diff (aSource, bSource)
where put chunks = do
renderer <- byteStringMakerFromEnvironment
B1.putStr $ mconcat $ chunksToByteStrings renderer chunks
Split -> do
rendered <- split diff (aSource, bSource)
case output arguments of
Just path -> do
-- | Return a renderer from the command-line arguments that will print the diff.
printDiff :: DiffArguments -> Renderer T.Text (IO ())
printDiff arguments diff sources = case format arguments of
Unified -> put $ unified diff sources
where
put chunks = do
renderer <- byteStringMakerFromEnvironment
B1.putStr $ mconcat $ chunksToByteStrings renderer chunks
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 outputPath -<.> ".html")
else path
IO.withFile outputPath IO.WriteMode (write rendered)
Nothing -> TextIO.putStr rendered
Patch -> putStr $ PatchOutput.patch diff (aSource, bSource)
where diff = diffTerms aTerm bTerm
write rendered h = TextIO.hPutStr h rendered
IO.withFile outputPath IO.WriteMode (flip TextIO.hPutStr rendered)
Patch -> putStr $ PatchOutput.patch diff sources

View File

@ -1,27 +1,8 @@
{-# LANGUAGE RecordWildCards #-}
module Main where
import Categorizable
import Diff
import Interpreter
import qualified Parsers as P
import Syntax
import Range
import qualified PatchOutput
import Split
import Term
import Unified
import Source
import Control.Comonad.Cofree
import qualified Data.ByteString.Char8 as B1
import Options.Applicative
import System.Directory
import System.FilePath
import qualified Data.Text as T
import qualified Data.Text.Lazy.IO as TextIO
import qualified System.IO as IO
import qualified Data.Text.ICU.Detect as Detect
import qualified Data.Text.ICU.Convert as Convert
import Data.Bifunctor.Join
import qualified DiffOutput as DO
@ -44,7 +25,7 @@ main = do
let parse = DO.parserForFilepath sourceAPath
terms <- sequence $ parse <$> sources
let replaceLeaves = DO.breakDownLeavesByWord <$> sources
DO.printDiff (args arguments) (runJoin sources) (runJoin $ replaceLeaves <*> terms)
DO.printDiff (args arguments) (uncurry diffTerms $ runJoin $ replaceLeaves <*> terms) (runJoin sources)
where opts = info (helper <*> arguments)
(fullDesc <> progDesc "Diff some things" <> header "semantic-diff - diff semantically")
args Arguments{..} = DO.DiffArguments { format = format, output = output, outputPath = sourceA }

View File

@ -2,7 +2,6 @@
module Main where
import Interpreter
import Renderer
import Source
import Options.Applicative
import qualified Data.ByteString.Char8 as B1
@ -40,7 +39,7 @@ main = do
let parse = DO.parserForFilepath filepath
terms <- sequence $ parse <$> sources
let replaceLeaves = DO.breakDownLeavesByWord <$> sources
DO.printDiff (args arguments filepath) (runJoin sources) (runJoin $ replaceLeaves <*> terms)
DO.printDiff (args arguments filepath) (uncurry diffTerms $ runJoin $ replaceLeaves <*> terms) (runJoin sources)
where opts = info (helper <*> arguments)
(fullDesc <> progDesc "Diff some things" <> header "semantic-diff - diff semantically")
args Arguments{..} filepath = DO.DiffArguments { format = format, output = output, outputPath = filepath }