From 87f7593bee3d8150e8c232f505b4d00af128f964 Mon Sep 17 00:00:00 2001 From: Mesabloo <22964017+Mesabloo@users.noreply.github.com> Date: Fri, 5 Aug 2022 14:00:45 +0200 Subject: [PATCH] add some utilities functions --- src/Error/Diagnose/Diagnostic.hs | 3 +++ src/Error/Diagnose/Diagnostic/Internal.hs | 15 ++++++++++++++- src/Error/Diagnose/Report.hs | 2 +- src/Error/Diagnose/Report/Internal.hs | 10 ++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Error/Diagnose/Diagnostic.hs b/src/Error/Diagnose/Diagnostic.hs index 9f37ad6..8774437 100644 --- a/src/Error/Diagnose/Diagnostic.hs +++ b/src/Error/Diagnose/Diagnostic.hs @@ -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) diff --git a/src/Error/Diagnose/Diagnostic/Internal.hs b/src/Error/Diagnose/Diagnostic/Internal.hs index d4f6cac..80fa1e9 100644 --- a/src/Error/Diagnose/Diagnostic/Internal.hs +++ b/src/Error/Diagnose/Diagnostic/Internal.hs @@ -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. diff --git a/src/Error/Diagnose/Report.hs b/src/Error/Diagnose/Report.hs index 0ee989f..42d4dca 100644 --- a/src/Error/Diagnose/Report.hs +++ b/src/Error/Diagnose/Report.hs @@ -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) diff --git a/src/Error/Diagnose/Report/Internal.hs b/src/Error/Diagnose/Report/Internal.hs index 065c42a..8496557 100644 --- a/src/Error/Diagnose/Report/Internal.hs +++ b/src/Error/Diagnose/Report/Internal.hs @@ -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 =>