{-# LANGUAGE RecordWildCards #-}

module Prolog.Programming.CodeAnalysis (
  checkForProblems,
  displayProblems,
)
where

import Data.Text.Lazy (pack)
import Language.Prolog (ProgramClause)
import Prolog.Programming.CodeAnalysis.Types (
  Problem (..),
  ProgramRule,
  Severity (Error),
  WithSeverity (..),
 )
import Text.PrettyPrint.Leijen.Text (Doc, brackets, string, vsep, (<$$>))

checkForProblems :: [ProgramClause] -> [WithSeverity ProgramRule] -> [WithSeverity Problem]
checkForProblems :: [ProgramClause]
-> [WithSeverity ProgramRule] -> [WithSeverity Problem]
checkForProblems [ProgramClause]
program = (WithSeverity ProgramRule -> [WithSeverity Problem])
-> [WithSeverity ProgramRule] -> [WithSeverity Problem]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((ProgramRule -> [Problem])
-> WithSeverity ProgramRule -> [WithSeverity Problem]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> WithSeverity a -> f (WithSeverity b)
traverse (ProgramRule -> ProgramRule
forall a b. (a -> b) -> a -> b
$ [ProgramClause]
program))

displayProblems :: [WithSeverity Problem] -> Either Doc Doc
displayProblems :: [WithSeverity Problem] -> Either Doc Doc
displayProblems [WithSeverity Problem]
pbs =
  Doc -> Either Doc Doc
forall {b}. b -> Either b b
cons
    (Doc -> Either Doc Doc) -> Doc -> Either Doc Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
vsep
    ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (WithSeverity Problem -> Doc) -> [WithSeverity Problem] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map
      ( \WithSeverity {Problem
Severity
severity :: Severity
value :: Problem
severity :: forall a. WithSeverity a -> Severity
value :: forall a. WithSeverity a -> a
..} ->
          Int -> String -> Doc -> Doc
padEnd Int
30 String
"-" (Doc -> Doc
brackets (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Text -> Doc
string (Text -> Doc) -> Text -> Doc
forall a b. (a -> b) -> a -> b
$ String -> Text
pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Severity -> String
forall a. Show a => a -> String
show Severity
severity) Doc -> Doc -> Doc
<$$> Problem -> Doc
problemDisplay Problem
value
      )
      [WithSeverity Problem]
pbs
  where
    cons :: b -> Either b b
cons = if (WithSeverity Problem -> Bool) -> [WithSeverity Problem] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Severity -> Severity -> Bool
forall a. Eq a => a -> a -> Bool
== Severity
Error) (Severity -> Bool)
-> (WithSeverity Problem -> Severity)
-> WithSeverity Problem
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WithSeverity Problem -> Severity
forall a. WithSeverity a -> Severity
severity) [WithSeverity Problem]
pbs then b -> Either b b
forall a b. a -> Either a b
Left else b -> Either b b
forall a b. b -> Either a b
Right

padEnd :: Int -> String -> Doc -> Doc
padEnd :: Int -> String -> Doc -> Doc
padEnd Int
maxWidth String
filler Doc
x = Doc
x Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> [Doc] -> Doc
forall a. Monoid a => [a] -> a
mconcat (Int -> Doc -> [Doc]
forall a. Int -> a -> [a]
replicate (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
maxWidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Doc -> Int
width Doc
x)) (Doc -> [Doc]) -> Doc -> [Doc]
forall a b. (a -> b) -> a -> b
$ Text -> Doc
string (Text -> Doc) -> Text -> Doc
forall a b. (a -> b) -> a -> b
$ String -> Text
pack String
filler)

width :: Doc -> Int
width :: Doc -> Int
width Doc
doc = String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (String -> Int) -> String -> Int
forall a b. (a -> b) -> a -> b
$ Doc -> String
forall a. Show a => a -> String
show Doc
doc