{-
    Copyright 2019 Vidar 'koala_man' Holen

    This file is part of ShellCheck.
    https://www.shellcheck.net

    ShellCheck is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ShellCheck is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
-}
{-# LANGUAGE TemplateHaskell #-}
module ShellCheck.Formatter.Diff (format, ShellCheck.Formatter.Diff.runTests) where

import ShellCheck.Interface
import ShellCheck.Fixer
import ShellCheck.Formatter.Format

import Control.Monad
import Data.Algorithm.Diff
import Data.Array
import Data.IORef
import Data.List
import qualified Data.Monoid as Monoid
import Data.Maybe
import qualified Data.Map as M
import GHC.Exts (sortWith)
import System.IO
import System.FilePath

import Test.QuickCheck

format :: FormatterOptions -> IO Formatter
format :: FormatterOptions -> IO Formatter
format FormatterOptions
options = do
    IORef Bool
foundIssues <- Bool -> IO (IORef Bool)
forall a. a -> IO (IORef a)
newIORef Bool
False
    IORef Bool
reportedIssues <- Bool -> IO (IORef Bool)
forall a. a -> IO (IORef a)
newIORef Bool
False
    Bool
shouldColor <- ColorOption -> IO Bool
shouldOutputColor (FormatterOptions -> ColorOption
foColorOption FormatterOptions
options)
    let color :: Int -> String -> String
color = if Bool
shouldColor then Int -> String -> String
forall {a}. Show a => a -> String -> String
colorize else Int -> String -> String
forall {p} {a}. p -> a -> a
nocolor
    Formatter -> IO Formatter
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Formatter {
        header :: IO ()
header = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (),
        footer :: IO ()
footer = IORef Bool -> IORef Bool -> (Int -> String -> String) -> IO ()
checkFooter IORef Bool
foundIssues IORef Bool
reportedIssues Int -> String -> String
color,
        onFailure :: String -> String -> IO ()
onFailure = (Int -> String -> String) -> String -> String -> IO ()
reportFailure Int -> String -> String
color,
        onResult :: CheckResult -> SystemInterface IO -> IO ()
onResult  = IORef Bool
-> IORef Bool
-> (Int -> String -> String)
-> CheckResult
-> SystemInterface IO
-> IO ()
reportResult IORef Bool
foundIssues IORef Bool
reportedIssues Int -> String -> String
color
    }


contextSize :: Int
contextSize = Int
3
red :: Int
red = Int
31
green :: Int
green = Int
32
yellow :: Integer
yellow = Integer
33
cyan :: Int
cyan = Int
36
bold :: Int
bold = Int
1

nocolor :: p -> a -> a
nocolor p
n = a -> a
forall a. a -> a
id
colorize :: a -> String -> String
colorize a
n String
s = (a -> String
forall {a}. Show a => a -> String
ansi a
n) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Integer -> String
forall {a}. Show a => a -> String
ansi Integer
0)
ansi :: a -> String
ansi a
n = String
"\x1B[" String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall {a}. Show a => a -> String
show a
n String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"m"

printErr :: ColorFunc -> String -> IO ()
printErr :: (Int -> String -> String) -> String -> IO ()
printErr Int -> String -> String
color = Handle -> String -> IO ()
hPutStrLn Handle
stderr (String -> IO ()) -> (String -> String) -> String -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
color Int
bold (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
color Int
red
reportFailure :: (Int -> String -> String) -> String -> String -> IO ()
reportFailure Int -> String -> String
color String
file String
msg = (Int -> String -> String) -> String -> IO ()
printErr Int -> String -> String
color (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
file String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
msg

checkFooter :: IORef Bool -> IORef Bool -> (Int -> String -> String) -> IO ()
checkFooter IORef Bool
foundIssues IORef Bool
reportedIssues Int -> String -> String
color = do
    Bool
found <- IORef Bool -> IO Bool
forall a. IORef a -> IO a
readIORef IORef Bool
foundIssues
    Bool
output <- IORef Bool -> IO Bool
forall a. IORef a -> IO a
readIORef IORef Bool
reportedIssues
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
found Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
output) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
            (Int -> String -> String) -> String -> IO ()
printErr Int -> String -> String
color String
"Issues were detected, but none were auto-fixable. Use another format to see them."

type ColorFunc = (Int -> String -> String)
data LFStatus = LinefeedMissing | LinefeedOk
data DiffDoc a = DiffDoc String LFStatus [DiffRegion a]
data DiffRegion a = DiffRegion (Int, Int) (Int, Int) [Diff a]

reportResult :: (IORef Bool) -> (IORef Bool) -> ColorFunc -> CheckResult -> SystemInterface IO -> IO ()
reportResult :: IORef Bool
-> IORef Bool
-> (Int -> String -> String)
-> CheckResult
-> SystemInterface IO
-> IO ()
reportResult IORef Bool
foundIssues IORef Bool
reportedIssues Int -> String -> String
color CheckResult
result SystemInterface IO
sys = do
    let comments :: [PositionedComment]
comments = CheckResult -> [PositionedComment]
crComments CheckResult
result
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([PositionedComment] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PositionedComment]
comments) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ IORef Bool -> Bool -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef Bool
foundIssues Bool
True
    let suggestedFixes :: [Fix]
suggestedFixes = (PositionedComment -> Maybe Fix) -> [PositionedComment] -> [Fix]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe PositionedComment -> Maybe Fix
pcFix [PositionedComment]
comments
    let fixmap :: Map String Fix
fixmap = [Fix] -> Map String Fix
buildFixMap [Fix]
suggestedFixes
    ((String, Fix) -> IO ()) -> [(String, Fix)] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (String, Fix) -> IO ()
output ([(String, Fix)] -> IO ()) -> [(String, Fix)] -> IO ()
forall a b. (a -> b) -> a -> b
$ Map String Fix -> [(String, Fix)]
forall k a. Map k a -> [(k, a)]
M.toList Map String Fix
fixmap
  where
    output :: (String, Fix) -> IO ()
output (String
name, Fix
fix) = do
        Either String String
file <- SystemInterface IO
-> Maybe Bool -> String -> IO (Either String String)
forall (m :: * -> *).
SystemInterface m
-> Maybe Bool -> String -> m (Either String String)
siReadFile SystemInterface IO
sys (Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True) String
name
        case Either String String
file of
            Right String
contents -> do
                String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ (Int -> String -> String) -> DiffDoc String -> String
formatDoc Int -> String -> String
color (DiffDoc String -> String) -> DiffDoc String -> String
forall a b. (a -> b) -> a -> b
$ String -> String -> Fix -> DiffDoc String
makeDiff String
name String
contents Fix
fix
                IORef Bool -> Bool -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef Bool
reportedIssues Bool
True
            Left String
msg -> (Int -> String -> String) -> String -> String -> IO ()
reportFailure Int -> String -> String
color String
name String
msg

hasTrailingLinefeed :: String -> Bool
hasTrailingLinefeed String
str =
    case String
str of
        [] -> Bool
True
        String
_ -> String -> Char
forall a. HasCallStack => [a] -> a
last String
str Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n'

coversLastLine :: [(Bool, b)] -> Bool
coversLastLine [(Bool, b)]
regions =
    case [(Bool, b)]
regions of
        [] -> Bool
False
        [(Bool, b)]
_ -> ((Bool, b) -> Bool
forall a b. (a, b) -> a
fst ((Bool, b) -> Bool) -> (Bool, b) -> Bool
forall a b. (a -> b) -> a -> b
$ [(Bool, b)] -> (Bool, b)
forall a. HasCallStack => [a] -> a
last [(Bool, b)]
regions)

-- TODO: Factor this out into a unified diff library because we're doing a lot
-- of the heavy lifting anyways.
makeDiff :: String -> String -> Fix -> DiffDoc String
makeDiff :: String -> String -> Fix -> DiffDoc String
makeDiff String
name String
contents Fix
fix = do
    let hunks :: [(Bool, [Diff String])]
hunks = [Diff String] -> [(Bool, [Diff String])]
forall a. [Diff a] -> [(Bool, [Diff a])]
groupDiff ([Diff String] -> [(Bool, [Diff String])])
-> [Diff String] -> [(Bool, [Diff String])]
forall a b. (a -> b) -> a -> b
$ String -> Fix -> [Diff String]
computeDiff String
contents Fix
fix
    let lf :: LFStatus
lf = if [(Bool, [Diff String])] -> Bool
forall {b}. [(Bool, b)] -> Bool
coversLastLine [(Bool, [Diff String])]
hunks Bool -> Bool -> Bool
&& Bool -> Bool
not (String -> Bool
hasTrailingLinefeed String
contents)
             then LFStatus
LinefeedMissing
             else LFStatus
LinefeedOk
    String -> LFStatus -> [DiffRegion String] -> DiffDoc String
forall a. String -> LFStatus -> [DiffRegion a] -> DiffDoc a
DiffDoc String
name LFStatus
lf ([DiffRegion String] -> DiffDoc String)
-> [DiffRegion String] -> DiffDoc String
forall a b. (a -> b) -> a -> b
$ [(Bool, [Diff String])] -> [DiffRegion String]
findRegions [(Bool, [Diff String])]
hunks

computeDiff :: String -> Fix -> [Diff String]
computeDiff :: String -> Fix -> [Diff String]
computeDiff String
contents Fix
fix =
    let old :: [String]
old = String -> [String]
lines String
contents
        array :: Array Int String
array = (Int, Int) -> [String] -> Array Int String
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
1, Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ ([String] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
old)) [String]
old
        new :: [String]
new = Fix -> Array Int String -> [String]
applyFix Fix
fix Array Int String
array
    in [String] -> [String] -> [Diff String]
forall a. Eq a => [a] -> [a] -> [Diff a]
getDiff [String]
old [String]
new

-- Group changes into hunks
groupDiff :: [Diff a] -> [(Bool, [Diff a])]
groupDiff :: forall a. [Diff a] -> [(Bool, [Diff a])]
groupDiff = ((Bool, [Diff a]) -> Bool)
-> [(Bool, [Diff a])] -> [(Bool, [Diff a])]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(Bool
_, [Diff a]
l) -> Bool -> Bool
not ([Diff a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Diff a]
l)) ([(Bool, [Diff a])] -> [(Bool, [Diff a])])
-> ([Diff a] -> [(Bool, [Diff a])])
-> [Diff a]
-> [(Bool, [Diff a])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Diff a] -> [Diff a] -> [(Bool, [Diff a])]
forall {a} {b}.
[PolyDiff a b] -> [PolyDiff a b] -> [(Bool, [PolyDiff a b])]
hunt []
  where
    -- Churn through 'Both's until we find a difference
    hunt :: [PolyDiff a b] -> [PolyDiff a b] -> [(Bool, [PolyDiff a b])]
hunt [PolyDiff a b]
current [] = [(Bool
False, [PolyDiff a b] -> [PolyDiff a b]
forall a. [a] -> [a]
reverse [PolyDiff a b]
current)]
    hunt [PolyDiff a b]
current (x :: PolyDiff a b
x@Both {}:[PolyDiff a b]
rest) = [PolyDiff a b] -> [PolyDiff a b] -> [(Bool, [PolyDiff a b])]
hunt (PolyDiff a b
xPolyDiff a b -> [PolyDiff a b] -> [PolyDiff a b]
forall a. a -> [a] -> [a]
:[PolyDiff a b]
current) [PolyDiff a b]
rest
    hunt [PolyDiff a b]
current [PolyDiff a b]
list =
        let ([PolyDiff a b]
context, [PolyDiff a b]
previous) = Int -> [PolyDiff a b] -> ([PolyDiff a b], [PolyDiff a b])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
contextSize [PolyDiff a b]
current
        in (Bool
False, [PolyDiff a b] -> [PolyDiff a b]
forall a. [a] -> [a]
reverse [PolyDiff a b]
previous) (Bool, [PolyDiff a b])
-> [(Bool, [PolyDiff a b])] -> [(Bool, [PolyDiff a b])]
forall a. a -> [a] -> [a]
: [PolyDiff a b] -> Int -> [PolyDiff a b] -> [(Bool, [PolyDiff a b])]
gather [PolyDiff a b]
context Int
0 [PolyDiff a b]
list

    -- Pick out differences until we find a run of Both's
    gather :: [PolyDiff a b] -> Int -> [PolyDiff a b] -> [(Bool, [PolyDiff a b])]
gather [PolyDiff a b]
current Int
n [] =
        let ([PolyDiff a b]
extras, [PolyDiff a b]
patch) = Int -> [PolyDiff a b] -> ([PolyDiff a b], [PolyDiff a b])
forall a. Int -> [a] -> ([a], [a])
splitAt (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
contextSize) [PolyDiff a b]
current
        in [(Bool
True, [PolyDiff a b] -> [PolyDiff a b]
forall a. [a] -> [a]
reverse [PolyDiff a b]
patch), (Bool
False, [PolyDiff a b] -> [PolyDiff a b]
forall a. [a] -> [a]
reverse [PolyDiff a b]
extras)]

    gather [PolyDiff a b]
current Int
n list :: [PolyDiff a b]
list@(Both {}:[PolyDiff a b]
_) | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
contextSizeInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
2 =
        let ([PolyDiff a b]
context, [PolyDiff a b]
previous) = Int -> [PolyDiff a b] -> ([PolyDiff a b], [PolyDiff a b])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
contextSize [PolyDiff a b]
current
        in (Bool
True, [PolyDiff a b] -> [PolyDiff a b]
forall a. [a] -> [a]
reverse [PolyDiff a b]
previous) (Bool, [PolyDiff a b])
-> [(Bool, [PolyDiff a b])] -> [(Bool, [PolyDiff a b])]
forall a. a -> [a] -> [a]
: [PolyDiff a b] -> [PolyDiff a b] -> [(Bool, [PolyDiff a b])]
hunt [PolyDiff a b]
context [PolyDiff a b]
list

    gather [PolyDiff a b]
current Int
n (x :: PolyDiff a b
x@Both {}:[PolyDiff a b]
rest) = [PolyDiff a b] -> Int -> [PolyDiff a b] -> [(Bool, [PolyDiff a b])]
gather (PolyDiff a b
xPolyDiff a b -> [PolyDiff a b] -> [PolyDiff a b]
forall a. a -> [a] -> [a]
:[PolyDiff a b]
current) (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [PolyDiff a b]
rest
    gather [PolyDiff a b]
current Int
n (PolyDiff a b
x:[PolyDiff a b]
rest) = [PolyDiff a b] -> Int -> [PolyDiff a b] -> [(Bool, [PolyDiff a b])]
gather (PolyDiff a b
xPolyDiff a b -> [PolyDiff a b] -> [PolyDiff a b]
forall a. a -> [a] -> [a]
:[PolyDiff a b]
current) Int
0 [PolyDiff a b]
rest

-- Get line numbers for hunks
findRegions :: [(Bool, [Diff String])] -> [DiffRegion String]
findRegions :: [(Bool, [Diff String])] -> [DiffRegion String]
findRegions = Int -> Int -> [(Bool, [Diff String])] -> [DiffRegion String]
forall {a}. Int -> Int -> [(Bool, [Diff a])] -> [DiffRegion a]
find' Int
1 Int
1
  where
    find' :: Int -> Int -> [(Bool, [Diff a])] -> [DiffRegion a]
find' Int
_ Int
_ [] = []
    find' Int
left Int
right ((Bool
output, [Diff a]
run):[(Bool, [Diff a])]
rest) =
        let (Int
dl, Int
dr) = [Diff a] -> (Int, Int)
forall a. [Diff a] -> (Int, Int)
countDelta [Diff a]
run
            remainder :: [DiffRegion a]
remainder = Int -> Int -> [(Bool, [Diff a])] -> [DiffRegion a]
find' (Int
leftInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
dl) (Int
rightInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
dr) [(Bool, [Diff a])]
rest
        in
            if Bool
output
            then (Int, Int) -> (Int, Int) -> [Diff a] -> DiffRegion a
forall a. (Int, Int) -> (Int, Int) -> [Diff a] -> DiffRegion a
DiffRegion (Int
left, Int
dl) (Int
right, Int
dr) [Diff a]
run DiffRegion a -> [DiffRegion a] -> [DiffRegion a]
forall a. a -> [a] -> [a]
: [DiffRegion a]
remainder
            else [DiffRegion a]
remainder

-- Get left/right line counts for a hunk
countDelta :: [Diff a] -> (Int, Int)
countDelta :: forall a. [Diff a] -> (Int, Int)
countDelta = Int -> Int -> [PolyDiff a a] -> (Int, Int)
forall {t} {t} {a} {b}.
(Num t, Num t) =>
t -> t -> [PolyDiff a b] -> (t, t)
count' Int
0 Int
0
  where
    count' :: t -> t -> [PolyDiff a b] -> (t, t)
count' t
left t
right [] = (t
left, t
right)
    count' t
left t
right (PolyDiff a b
x:[PolyDiff a b]
rest) =
        case PolyDiff a b
x of
            Both {} -> t -> t -> [PolyDiff a b] -> (t, t)
count' (t
leftt -> t -> t
forall a. Num a => a -> a -> a
+t
1) (t
rightt -> t -> t
forall a. Num a => a -> a -> a
+t
1) [PolyDiff a b]
rest
            First {} -> t -> t -> [PolyDiff a b] -> (t, t)
count' (t
leftt -> t -> t
forall a. Num a => a -> a -> a
+t
1) t
right [PolyDiff a b]
rest
            Second {} -> t -> t -> [PolyDiff a b] -> (t, t)
count' t
left (t
rightt -> t -> t
forall a. Num a => a -> a -> a
+t
1) [PolyDiff a b]
rest

formatRegion :: ColorFunc -> LFStatus -> DiffRegion String -> String
formatRegion :: (Int -> String -> String)
-> LFStatus -> DiffRegion String -> String
formatRegion Int -> String -> String
color LFStatus
lf (DiffRegion (Int, Int)
left (Int, Int)
right [Diff String]
diffs) =
    let header :: String
header = Int -> String -> String
color Int
cyan (String
"@@ -" String -> String -> String
forall a. [a] -> [a] -> [a]
++ ((Int, Int) -> String
forall {a} {a}. (Show a, Show a) => (a, a) -> String
tup (Int, Int)
left) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" +" String -> String -> String
forall a. [a] -> [a] -> [a]
++ ((Int, Int) -> String
forall {a} {a}. (Show a, Show a) => (a, a) -> String
tup (Int, Int)
right) String -> String -> String
forall a. [a] -> [a] -> [a]
++String
" @@")
    in
        [String] -> String
unlines ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ String
header String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String] -> [String]
forall a. [a] -> [a]
reverse (LFStatus -> [Diff String] -> [String]
getStrings LFStatus
lf ([Diff String] -> [Diff String]
forall a. [a] -> [a]
reverse [Diff String]
diffs))
  where
    noLF :: String
noLF = String
"\\ No newline at end of file"

    getStrings :: LFStatus -> [Diff String] -> [String]
getStrings LFStatus
LinefeedOk [Diff String]
list = (Diff String -> String) -> [Diff String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Diff String -> String
format [Diff String]
list
    getStrings LFStatus
LinefeedMissing list :: [Diff String]
list@((Both String
_ String
_):[Diff String]
_) = String
noLF String -> [String] -> [String]
forall a. a -> [a] -> [a]
: (Diff String -> String) -> [Diff String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Diff String -> String
format [Diff String]
list
    getStrings LFStatus
LinefeedMissing list :: [Diff String]
list@((First String
_):[Diff String]
_) = String
noLF String -> [String] -> [String]
forall a. a -> [a] -> [a]
: (Diff String -> String) -> [Diff String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Diff String -> String
format [Diff String]
list
    getStrings LFStatus
LinefeedMissing (Diff String
last:[Diff String]
rest) = Diff String -> String
format Diff String
last String -> [String] -> [String]
forall a. a -> [a] -> [a]
: LFStatus -> [Diff String] -> [String]
getStrings LFStatus
LinefeedMissing [Diff String]
rest

    tup :: (a, a) -> String
tup (a
a,a
b) = (a -> String
forall {a}. Show a => a -> String
show a
a) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"," String -> String -> String
forall a. [a] -> [a] -> [a]
++ (a -> String
forall {a}. Show a => a -> String
show a
b)
    format :: Diff String -> String
format (Both String
x String
_) = Char
' 'Char -> String -> String
forall a. a -> [a] -> [a]
:String
x
    format (First String
x) = Int -> String -> String
color Int
red (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Char
'-'Char -> String -> String
forall a. a -> [a] -> [a]
:String
x
    format (Second String
x) = Int -> String -> String
color Int
green (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Char
'+'Char -> String -> String
forall a. a -> [a] -> [a]
:String
x

splitLast :: [a] -> ([a], [a])
splitLast [] = ([], [])
splitLast [a]
x =
    let ([a]
last, [a]
rest) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
1 ([a] -> ([a], [a])) -> [a] -> ([a], [a])
forall a b. (a -> b) -> a -> b
$ [a] -> [a]
forall a. [a] -> [a]
reverse [a]
x
    in ([a] -> [a]
forall a. [a] -> [a]
reverse [a]
rest, [a]
last)

formatDoc :: (Int -> String -> String) -> DiffDoc String -> String
formatDoc Int -> String -> String
color (DiffDoc String
name LFStatus
lf [DiffRegion String]
regions) =
    let ([DiffRegion String]
most, [DiffRegion String]
last) = [DiffRegion String] -> ([DiffRegion String], [DiffRegion String])
forall {a}. [a] -> ([a], [a])
splitLast [DiffRegion String]
regions
    in
          (Int -> String -> String
color Int
bold (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"--- " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (String
"a" String -> String -> String
</> String
name)) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++
          (Int -> String -> String
color Int
bold (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"+++ " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (String
"b" String -> String -> String
</> String
name)) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++
          (DiffRegion String -> String) -> [DiffRegion String] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((Int -> String -> String)
-> LFStatus -> DiffRegion String -> String
formatRegion Int -> String -> String
color LFStatus
LinefeedOk) [DiffRegion String]
most String -> String -> String
forall a. [a] -> [a] -> [a]
++
          (DiffRegion String -> String) -> [DiffRegion String] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((Int -> String -> String)
-> LFStatus -> DiffRegion String -> String
formatRegion Int -> String -> String
color LFStatus
lf) [DiffRegion String]
last

-- Create a Map from filename to Fix
buildFixMap :: [Fix] -> M.Map String Fix
buildFixMap :: [Fix] -> Map String Fix
buildFixMap [Fix]
fixes = Map String Fix
perFile
  where
    splitFixes :: [Fix]
splitFixes = Fix -> [Fix]
splitFixByFile (Fix -> [Fix]) -> Fix -> [Fix]
forall a b. (a -> b) -> a -> b
$ [Fix] -> Fix
forall a. Monoid a => [a] -> a
mconcat [Fix]
fixes
    perFile :: Map String Fix
perFile = (Fix -> String) -> [Fix] -> Map String Fix
forall k v. (Ord k, Monoid v) => (v -> k) -> [v] -> Map k v
groupByMap (Position -> String
posFile (Position -> String) -> (Fix -> Position) -> Fix -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Replacement -> Position
repStartPos (Replacement -> Position)
-> (Fix -> Replacement) -> Fix -> Position
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Replacement] -> Replacement
forall a. HasCallStack => [a] -> a
head ([Replacement] -> Replacement)
-> (Fix -> [Replacement]) -> Fix -> Replacement
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fix -> [Replacement]
fixReplacements) [Fix]
splitFixes

splitFixByFile :: Fix -> [Fix]
splitFixByFile :: Fix -> [Fix]
splitFixByFile Fix
fix = ([Replacement] -> Fix) -> [[Replacement]] -> [Fix]
forall a b. (a -> b) -> [a] -> [b]
map [Replacement] -> Fix
makeFix ([[Replacement]] -> [Fix]) -> [[Replacement]] -> [Fix]
forall a b. (a -> b) -> a -> b
$ (Replacement -> Replacement -> Bool)
-> [Replacement] -> [[Replacement]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy Replacement -> Replacement -> Bool
sameFile (Fix -> [Replacement]
fixReplacements Fix
fix)
  where
    sameFile :: Replacement -> Replacement -> Bool
sameFile Replacement
rep1 Replacement
rep2 = (Position -> String
posFile (Position -> String) -> Position -> String
forall a b. (a -> b) -> a -> b
$ Replacement -> Position
repStartPos Replacement
rep1) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== (Position -> String
posFile (Position -> String) -> Position -> String
forall a b. (a -> b) -> a -> b
$ Replacement -> Position
repStartPos Replacement
rep2)
    makeFix :: [Replacement] -> Fix
makeFix [Replacement]
reps = Fix
newFix { fixReplacements = reps }

groupByMap :: (Ord k, Monoid v) => (v -> k) -> [v] -> M.Map k v
groupByMap :: forall k v. (Ord k, Monoid v) => (v -> k) -> [v] -> Map k v
groupByMap v -> k
f = (v -> v -> v) -> [(k, v)] -> Map k v
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
M.fromListWith v -> v -> v
forall a. Monoid a => a -> a -> a
Monoid.mappend ([(k, v)] -> Map k v) -> ([v] -> [(k, v)]) -> [v] -> Map k v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> (k, v)) -> [v] -> [(k, v)]
forall a b. (a -> b) -> [a] -> [b]
map (\v
x -> (v -> k
f v
x, v
x))

-- For building unit tests
b :: b -> PolyDiff b b
b b
n = b -> b -> PolyDiff b b
forall a b. a -> b -> PolyDiff a b
Both b
n b
n
l :: a -> PolyDiff a b
l = a -> PolyDiff a b
forall a b. a -> PolyDiff a b
First
r :: b -> PolyDiff a b
r = b -> PolyDiff a b
forall a b. b -> PolyDiff a b
Second

prop_identifiesProperContext :: Bool
prop_identifiesProperContext = [Diff Integer] -> [(Bool, [Diff Integer])]
forall a. [Diff a] -> [(Bool, [Diff a])]
groupDiff [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
1, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
2, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
3, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
4, Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
5, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
6, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
7, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
8, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
9] [(Bool, [Diff Integer])] -> [(Bool, [Diff Integer])] -> Bool
forall a. Eq a => a -> a -> Bool
==
    [(Bool
False, [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
1]), -- Omitted
    (Bool
True, [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
2, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
3, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
4, Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
5, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
6, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
7, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
8]), -- A change with three lines of context
    (Bool
False, [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
9])]  -- Omitted

prop_includesContextFromStartIfNecessary :: Bool
prop_includesContextFromStartIfNecessary = [Diff Integer] -> [(Bool, [Diff Integer])]
forall a. [Diff a] -> [(Bool, [Diff a])]
groupDiff [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
4, Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
5, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
6, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
7, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
8, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
9] [(Bool, [Diff Integer])] -> [(Bool, [Diff Integer])] -> Bool
forall a. Eq a => a -> a -> Bool
==
    [ -- Nothing omitted
    (Bool
True, [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
4, Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
5, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
6, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
7, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
8]), -- A change with three lines of context
    (Bool
False, [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
9])]  -- Omitted

prop_includesContextUntilEndIfNecessary :: Bool
prop_includesContextUntilEndIfNecessary = [Diff Integer] -> [(Bool, [Diff Integer])]
forall a. [Diff a] -> [(Bool, [Diff a])]
groupDiff [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
4, Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
5] [(Bool, [Diff Integer])] -> [(Bool, [Diff Integer])] -> Bool
forall a. Eq a => a -> a -> Bool
==
    [ -- Nothing omitted
        (Bool
True, [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
4, Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
5])
    ] -- Nothing Omitted

prop_splitsIntoMultipleHunks :: Bool
prop_splitsIntoMultipleHunks = [Diff Integer] -> [(Bool, [Diff Integer])]
forall a. [Diff a] -> [(Bool, [Diff a])]
groupDiff [Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
1, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
1, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
2, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
3, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
4, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
5, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
6, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
7, Integer -> Diff Integer
forall {b} {a}. b -> PolyDiff a b
r Integer
8] [(Bool, [Diff Integer])] -> [(Bool, [Diff Integer])] -> Bool
forall a. Eq a => a -> a -> Bool
==
    [ -- Nothing omitted
        (Bool
True, [Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
1, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
1, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
2, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
3]),
        (Bool
False, [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
4]),
        (Bool
True, [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
5, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
6, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
7, Integer -> Diff Integer
forall {b} {a}. b -> PolyDiff a b
r Integer
8])
    ] -- Nothing Omitted

prop_splitsIntoMultipleHunksUnlessTouching :: Bool
prop_splitsIntoMultipleHunksUnlessTouching = [Diff Integer] -> [(Bool, [Diff Integer])]
forall a. [Diff a] -> [(Bool, [Diff a])]
groupDiff [Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
1, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
1, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
2, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
3, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
4, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
5, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
6, Integer -> Diff Integer
forall {b} {a}. b -> PolyDiff a b
r Integer
7] [(Bool, [Diff Integer])] -> [(Bool, [Diff Integer])] -> Bool
forall a. Eq a => a -> a -> Bool
==
    [
        (Bool
True, [Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
1, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
1, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
2, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
3, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
4, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
5, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
6, Integer -> Diff Integer
forall {b} {a}. b -> PolyDiff a b
r Integer
7])
    ]

prop_countDeltasWorks :: Bool
prop_countDeltasWorks = [Diff Integer] -> (Int, Int)
forall a. [Diff a] -> (Int, Int)
countDelta [Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
1, Integer -> Diff Integer
forall a b. a -> PolyDiff a b
l Integer
2, Integer -> Diff Integer
forall {b} {a}. b -> PolyDiff a b
r Integer
3, Integer -> Diff Integer
forall {b} {a}. b -> PolyDiff a b
r Integer
4, Integer -> Diff Integer
forall {b}. b -> PolyDiff b b
b Integer
5] (Int, Int) -> (Int, Int) -> Bool
forall a. Eq a => a -> a -> Bool
== (Int
3,Int
4)
prop_countDeltasWorks2 :: Bool
prop_countDeltasWorks2 = [Diff Any] -> (Int, Int)
forall a. [Diff a] -> (Int, Int)
countDelta [] (Int, Int) -> (Int, Int) -> Bool
forall a. Eq a => a -> a -> Bool
== (Int
0,Int
0)

return []
runTests :: IO Bool
runTests = $Bool
String
[(String, Property)]
Bool -> Property
[(String, Property)] -> (Property -> IO Result) -> IO Bool
Property -> IO Result
forall prop. Testable prop => prop -> IO Result
forall prop. Testable prop => prop -> Property
prop_identifiesProperContext :: Bool
prop_includesContextFromStartIfNecessary :: Bool
prop_includesContextUntilEndIfNecessary :: Bool
prop_splitsIntoMultipleHunks :: Bool
prop_splitsIntoMultipleHunksUnlessTouching :: Bool
prop_countDeltasWorks :: Bool
prop_countDeltasWorks2 :: Bool
property :: forall prop. Testable prop => prop -> Property
quickCheckResult :: forall prop. Testable prop => prop -> IO Result
runQuickCheckAll :: [(String, Property)] -> (Property -> IO Result) -> IO Bool
quickCheckAll