add some utilities functions

This commit is contained in:
Mesabloo 2022-08-05 14:00:45 +02:00
parent 7650b2d020
commit 87f7593bee
4 changed files with 28 additions and 2 deletions

View File

@ -21,7 +21,10 @@ import Error.Diagnose.Diagnostic.Internal as Export
addFile,
addReport,
def,
errorsToWarnings,
hasReports,
prettyDiagnostic,
printDiagnostic,
warningsToErrors,
)
import System.IO as Export (stderr, stdout)

View File

@ -29,7 +29,7 @@ import Data.Foldable (fold, toList)
import qualified Data.HashMap.Lazy as HashMap
import Data.List (intersperse)
import Error.Diagnose.Report (Report)
import Error.Diagnose.Report.Internal (FileMap, prettyReport)
import Error.Diagnose.Report.Internal (FileMap, errorToWarning, prettyReport, warningToError)
import Error.Diagnose.Style (Annotation, Style)
import Prettyprinter (Doc, Pretty, hardline, unAnnotate)
import Prettyprinter.Render.Terminal (hPutDoc)
@ -67,6 +67,19 @@ instance ToJSON msg => ToJSON (Diagnostic msg) where
]
#endif
-- | Checks whether the given diagnostic has any report or not (if it is effectively empty).
hasReports :: Diagnostic msg -> Bool
hasReports (Diagnostic DL.Nil _) = True
hasReports _ = False
-- | Transforms every warning report in this diagnostic into an error report.
warningsToErrors :: Diagnostic msg -> Diagnostic msg
warningsToErrors (Diagnostic reports files) = Diagnostic (warningToError <$> reports) files
-- | Transforms every error report in this diagnostic into a warning report.
errorsToWarnings :: Diagnostic msg -> Diagnostic msg
errorsToWarnings (Diagnostic reports files) = Diagnostic (errorToWarning <$> reports) files
-- | Pretty prints a 'Diagnostic' into a 'Doc'ument that can be output using 'hPutDoc'.
--
-- Colors are put by default.

View File

@ -11,4 +11,4 @@ module Error.Diagnose.Report
)
where
import Error.Diagnose.Report.Internal as Export (Marker (..), Note (..), Report, err, warn)
import Error.Diagnose.Report.Internal as Export (Marker (..), Note (..), Report, err, errorToWarning, warn, warningToError)

View File

@ -160,6 +160,16 @@ warn = Report False
err = Report True
{-# INLINE err #-}
-- | Transforms a warning report into an error report.
warningToError :: Report msg -> Report msg
warningToError (Report False code msg markers notes) = Report True code msg markers notes
warningToError r@(Report True _ _ _ _) = r
-- | Transforms an error report into a warning report.
errorToWarning :: Report msg -> Report msg
errorToWarning (Report True code msg markers notes) = Report False code msg markers notes
errorToWarning r@(Report False _ _ _ _) = r
-- | Pretty prints a report to a 'Doc' handling colors.
prettyReport ::
Pretty msg =>