allow distinguishing between notes and hints at the bottom of a report

This commit is contained in:
Mesabloo 2022-06-26 14:20:21 +02:00
parent 770d8871db
commit 5042217e58
8 changed files with 41 additions and 16 deletions

View File

@ -55,7 +55,7 @@ msg ->
-- | A list of markers, along with the positions they span on
[(Position, Marker msg)] ->
-- | Some hints to be output at the bottom of the report
[msg] ->
[Note msg] ->
-- | The created report
Report msg
```
@ -96,6 +96,7 @@ let beautifulExample =
(Position (1, 8) (1, 9) "somefile.zc", Where "type 'a' is bound here without constraints")
]
["Adding 'Num(a)' to the list of constraints may solve this problem."]
-- ^^^^ This is a 'Note' not a 'Hint', as specified by its 'IsString' instance
-- Create the diagnostic
let diagnostic = addFile def "somefile.zc" "let id<a>(x : a) : a := x\n + 1"

View File

@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack
name: diagnose
version: 2.0.1
version: 2.1.0
synopsis: Beautiful error reporting done easily
description: This package provides a simple way of getting beautiful compiler/interpreter errors
using a very simple interface for the programmer.

View File

@ -1,5 +1,5 @@
name: diagnose
version: 2.0.1
version: 2.1.0
github: "mesabloo/diagnose"
license: BSD3
author: "Ghilain Bergeron"
@ -15,6 +15,8 @@ dependencies:
- data-default >= 0.7 && < 1
- wcwidth >= 0.0.1 && <1
- text >= 1.0.0.0 && <= 2.0
# ^^^ This is unfortunately required, but as 'prettyprinter' already depends on it, it will already have been fetched
# into the local cache anyway.
default-extensions:
- OverloadedStrings

View File

@ -192,7 +192,7 @@ import Error.Diagnose.Style as Export
-- > }
-- > , message: string
-- > }[]
-- > , hints: string[]
-- > , hints: ({ note: string } | { hint: string })[]
-- > }[]
-- > }
--

View File

@ -150,7 +150,7 @@ addReport (Diagnostic reports files) report =
-- > }
-- > , message: T
-- > }[]
-- > , hints: T[]
-- > , hints: ({ note: T } | { hint: T })[]
-- > }[]
-- > }
--

View File

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

View File

@ -37,6 +37,7 @@ import qualified Data.HashMap.Lazy as IntMap
import qualified Data.List as List
import qualified Data.List.Safe as List
import Data.Ord (Down (Down))
import Data.String (IsString (fromString))
import qualified Data.Text as Text
import Error.Diagnose.Position
import Error.Diagnose.Style (Annotation (..))
@ -54,8 +55,8 @@ data Report msg
-- ^ The message associated with the error.
[(Position, Marker msg)]
-- ^ A map associating positions with marker to show under the source code.
[msg]
-- ^ A list of hints to add at the end of the report.
[Note msg]
-- ^ A list of notes to add at the end of the report.
instance Semigroup msg => Semigroup (Report msg) where
Report isError1 code1 msg1 pos1 hints1 <> Report isError2 code2 msg2 pos2 hints2 =
@ -114,6 +115,23 @@ instance Ord (Marker msg) where
m1 <= m2 = m1 < m2 || m1 == m2
{-# INLINEABLE (<=) #-}
-- | A note is a piece of information that is found at the end of a report.
data Note msg
= -- | A note, which is meant to give valuable information related to the encountered error.
Note msg
| -- | A hint, to propose potential fixes or help towards fixing the issue.
Hint msg
#ifdef USE_AESON
instance ToJSON msg => ToJson (Note msg) where
toJSON (Note msg) = object [ "note" .= msg ]
toJSON (Hint msg) = object [ "hint" .= msg ]
#endif
-- | Constructs a 'Note' from the given message as a literal string.
instance IsString msg => IsString (Note msg) where
fromString = Note . fromString
-- | Constructs a warning or an error report.
warn,
err ::
@ -124,7 +142,7 @@ warn,
-- | A list associating positions with markers.
[(Position, Marker msg)] ->
-- | A possibly mempty list of hints to add at the end of the report.
[msg] ->
[Note msg] ->
Report msg
warn = Report False
{-# INLINE warn #-}
@ -605,7 +623,7 @@ markerMessage (Maybe m) = m
{-# INLINE markerMessage #-}
-- | Pretty prints all hints.
prettyAllHints :: Pretty msg => [msg] -> Int -> Bool -> Doc Annotation
prettyAllHints :: Pretty msg => [Note msg] -> Int -> Bool -> Doc Annotation
prettyAllHints [] _ _ = mempty
prettyAllHints (h : hs) leftLen withUnicode =
{-
@ -613,5 +631,11 @@ prettyAllHints (h : hs) leftLen withUnicode =
(1) : Hint: <hint message>
-}
let prefix = hardline <+> pipePrefix leftLen withUnicode
in prefix <+> annotate HintColor ("Hint:" <+> replaceLinesWith (prefix <+> " ") (pretty h))
in prefix <+> annotate HintColor (notePrefix h <+> replaceLinesWith (prefix <+> " ") (pretty $ noteMessage h))
<> prettyAllHints hs leftLen withUnicode
where
notePrefix (Note _) = "Note:"
notePrefix (Hint _) = "Hint:"
noteMessage (Note msg) = msg
noteMessage (Hint msg) = msg

View File

@ -8,15 +8,13 @@ import Data.HashMap.Lazy (HashMap)
import qualified Data.HashMap.Lazy as HashMap
import Error.Diagnose
( Marker (..),
Note (Hint),
Position (..),
Report,
addFile,
addReport,
def,
defaultStyle,
#ifdef USE_AESON
diagnosticToJson,
#endif
err,
printDiagnostic,
stdout,
@ -240,8 +238,8 @@ errorNoMarkersTwoHints =
Nothing
"Error with no markers and two hints"
[]
[ "First hint",
"Second hint"
[ "First note",
Hint "Second hint"
]
errorSingleMultilineMarkerNoHints :: Report String