update documentation for TAB size

This commit is contained in:
Mesabloo 2022-04-27 00:26:12 +02:00
parent e75195cbb8
commit 328ef398e7
9 changed files with 54 additions and 23 deletions

View File

@ -97,17 +97,14 @@ let diagnostic = addFile def "somefile.zc" "let id<a>(x : a) : a := x\n + 1"
let diagnostic' = addReport diagnostic beautifulExample
-- Print with unicode characters and colors
printDiagnostic stdout True True diagnostic'
printDiagnostic stdout True True 4 diagnostic'
```
More examples are given in the [`test/rendering`](./test/rendering) folder.
## TODO list
- Handle variable-width characters such as tabs or some unicode characters.
For tabs, we may just resort to having the user fix a given number of
spaces in the `printDiagnostic` function.
<< empty, for now >>
## License

View File

@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack
name: diagnose
version: 1.7.2
version: 1.8.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.
@ -75,7 +75,8 @@ library
if flag(json)
cpp-options: -DUSE_AESON
build-depends:
aeson ==1.5.*
aeson >=1.5 && <2
, bytestring >=0.9 && <1
if flag(megaparsec-compat)
build-depends:
containers ==0.6.*
@ -116,7 +117,8 @@ test-suite diagnose-megaparsec-tests
if flag(json)
cpp-options: -DUSE_AESON
build-depends:
aeson ==1.5.*
aeson >=1.5 && <2
, bytestring >=0.9 && <1
if flag(megaparsec-compat)
build-depends:
containers ==0.6.*
@ -154,7 +156,8 @@ test-suite diagnose-parsec-tests
if flag(json)
cpp-options: -DUSE_AESON
build-depends:
aeson ==1.5.*
aeson >=1.5 && <2
, bytestring >=0.9 && <1
if flag(megaparsec-compat)
build-depends:
containers ==0.6.*
@ -191,7 +194,8 @@ test-suite diagnose-rendering-tests
if flag(json)
cpp-options: -DUSE_AESON
build-depends:
aeson ==1.5.*
aeson >=1.5 && <2
, bytestring >=0.9 && <1
if flag(megaparsec-compat)
build-depends:
containers ==0.6.*

View File

@ -1,5 +1,5 @@
name: diagnose
version: 1.7.2
version: 1.8.0
github: "mesabloo/diagnose"
license: BSD3
author: "Ghilain Bergeron"
@ -56,7 +56,8 @@ flags:
when:
- condition: flag(json)
dependencies:
- aeson == 1.5.*
- aeson >= 1.5 && <2
- bytestring >= 0.9 && < 1
cpp-options:
- -DUSE_AESON

View File

@ -150,6 +150,8 @@ import Error.Diagnose.Report as Export
--
-- - A 'Bool' set to 'False' if you don't want colors in the end result.
--
-- - A 'Int' describing the number of spaces with which to output a TAB character.
--
-- - And finally the 'Diagnostic' to output.
-- $diagnostic_json
@ -218,7 +220,7 @@ import Error.Diagnose.Report as Export
-- > -- Creates a new diagnostic with no default hints from the bundle returned by megaparsec
-- > diag' = addFile diag filename content
-- > -- Add the file used when parsing with the same filename given to 'MP.runParser'
-- > in printDiagnostic stderr True True diag'
-- > in printDiagnostic stderr True True 4 diag'
-- > Right res -> print res
--
-- This example will return the following error message (assuming default instances for @'Error.Diagnose.Compat.Megaparsec.HasHints' 'Data.Void.Void' msg@):
@ -259,7 +261,7 @@ import Error.Diagnose.Report as Export
-- > -- Creates a new diagnostic with no default hints from the bundle returned by megaparsec
-- > diag' = addFile diag filename content
-- > -- Add the file used when parsing with the same filename given to 'MP.runParser'
-- > in printDiagnostic stderr True True diag'
-- > in printDiagnostic stderr True True 4 diag'
-- > Right res -> print res
--
-- This will output the following errr on @stderr@:

View File

@ -69,9 +69,9 @@ prettyDiagnostic ::
Pretty msg =>
-- | Should we use unicode when printing paths?
Bool ->
-- | The number of spaces each TAB character will span
-- | The number of spaces each TAB character will span.
Int ->
-- | The diagnostic to print
-- | The diagnostic to print.
Diagnostic msg ->
Doc AnsiStyle
prettyDiagnostic withUnicode tabSize (Diagnostic reports file) =
@ -87,7 +87,7 @@ printDiagnostic ::
Bool ->
-- | 'False' to disable colors.
Bool ->
-- | The number of spaces each TAB character will span
-- | The number of spaces each TAB character will span.
Int ->
-- | The diagnostic to output.
Diagnostic msg ->

View File

@ -21,6 +21,9 @@
-- Please limit yourself to the "Error.Diagnose.Report" module, which exports some of the useful functions defined here.
module Error.Diagnose.Report.Internal where
#ifdef USE_AESON
import Data.Aeson (ToJSON(..), object, (.=))
#endif
import Control.Applicative ((<|>))
import Data.Bifunctor (bimap, first, second)
import Data.Char.WCWidth (wcwidth)
@ -59,6 +62,30 @@ instance Semigroup msg => Semigroup (Report msg) where
instance Monoid msg => Monoid (Report msg) where
mempty = Report False Nothing mempty mempty mempty
#ifdef USE_AESON
instance ToJSON msg => ToJSON (Report msg) where
toJSON (Report isError code msg markers hints) =
object [ "kind" .= (if isError then "error" else "warning" :: String)
, "code" .= code
, "message" .= msg
, "markers" .= fmap showMarker markers
, "hints" .= hints
]
where
showMarker (pos, marker) =
object $ [ "position" .= pos ]
<> case marker of
This m -> [ "message" .= m
, "kind" .= ("this" :: String)
]
Where m -> [ "message" .= m
, "kind" .= ("where" :: String)
]
Maybe m -> [ "message" .= m
, "kind" .= ("maybe" :: String)
]
#endif
-- | The type of markers with abstract message type, shown under code lines.
data Marker msg
= -- | A red or yellow marker under source code, marking important parts of the code.

View File

@ -30,8 +30,8 @@ main = do
res2 = first (errorDiagnosticFromBundle Nothing "Parse error on input" Nothing) $ MP.runParser @Void (MP.some MP.decimal <* MP.eof) filename content2
case res1 of
Left diag -> printDiagnostic stdout True True (addFile diag filename (Text.unpack content1) :: Diagnostic String)
Left diag -> printDiagnostic stdout True True 4 (addFile diag filename (Text.unpack content1) :: Diagnostic String)
Right res -> print res
case res2 of
Left diag -> printDiagnostic stdout True True (addFile diag filename (Text.unpack content2) :: Diagnostic String)
Left diag -> printDiagnostic stdout True True 4 (addFile diag filename (Text.unpack content2) :: Diagnostic String)
Right res -> print res

View File

@ -29,8 +29,8 @@ parser2 = op' "\\" *> letter
main :: IO ()
main = do
either (printDiagnostic stderr True True) print $ diagParse parser1 "issues/2.txt" "\\1"
either (printDiagnostic stderr True True) print $ diagParse parser2 "issues/2.txt" "\\1"
either (printDiagnostic stderr True True 4) print $ diagParse parser1 "issues/2.txt" "\\1"
either (printDiagnostic stderr True True 4) print $ diagParse parser2 "issues/2.txt" "\\1"
-- smaller example
op' :: String -> Parser String

View File

@ -30,10 +30,10 @@ main = do
res2 = first (errorDiagnosticFromParseError Nothing "Parse error on input" Nothing) $ P.parse (P.many1 P.digit <* P.eof) filename content2
case res1 of
Left diag -> printDiagnostic stdout True True (addFile diag filename (Text.unpack content1) :: Diagnostic String)
Left diag -> printDiagnostic stdout True True 4 (addFile diag filename (Text.unpack content1) :: Diagnostic String)
Right res -> print res
case res2 of
Left diag -> printDiagnostic stdout True True (addFile diag filename (Text.unpack content2) :: Diagnostic String)
Left diag -> printDiagnostic stdout True True 4 (addFile diag filename (Text.unpack content2) :: Diagnostic String)
Right res -> print res
-- all issue reproduction