From fdc8d5210ab0056fb7b50360ae4880bb92ed5d68 Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Wed, 26 Sep 2018 13:56:20 -0500 Subject: [PATCH] Custom Show for Range and Span --- src/Data/Range.hs | 5 ++++- src/Data/Span.hs | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Data/Range.hs b/src/Data/Range.hs index 7dba51c39..601c5c79e 100644 --- a/src/Data/Range.hs +++ b/src/Data/Range.hs @@ -17,7 +17,7 @@ import Proto3.Wire.Decode as Decode -- | A half-open interval of integers, defined by start & end indices. data Range = Range { start :: {-# UNPACK #-} !Int, end :: {-# UNPACK #-} !Int } - deriving (Eq, Show, Generic, Named) + deriving (Eq, Generic, Named) emptyRange :: Range emptyRange = Range 0 0 @@ -53,6 +53,9 @@ instance Semigroup Range where instance Ord Range where a <= b = start a <= start b +instance Show Range where + showsPrec _ Range{..} = showChar '[' . shows start . showString " - " . shows end . showChar ']' + instance ToJSONFields Range where toJSONFields Range{..} = ["sourceRange" .= [ start, end ]] diff --git a/src/Data/Span.hs b/src/Data/Span.hs index d40b300f9..03c72f9a1 100644 --- a/src/Data/Span.hs +++ b/src/Data/Span.hs @@ -24,18 +24,21 @@ data Pos = Pos { posLine :: !Int , posColumn :: !Int } - deriving (Show, Read, Eq, Ord, Generic, Hashable) + deriving (Eq, Ord, Generic, Hashable) -- | A Span of position information data Span = Span { spanStart :: Pos , spanEnd :: Pos } - deriving (Show, Read, Eq, Ord, Generic, Hashable, Named) + deriving (Eq, Ord, Generic, Hashable, Named) -- Instances +instance Show Pos where + showsPrec _ Pos{..} = showChar '[' . shows posLine . showString ", " . shows posColumn . showChar ']' + instance Named Pos where nameOf _ = "Position" instance Message Pos where encodeMessage _ Pos{..} = encodeMessageField 1 posLine <> encodeMessageField 2 posColumn @@ -61,6 +64,9 @@ instance HasDefault Pos where def = lowerBound @Pos +instance Show Span where + showsPrec _ Span{..} = shows spanStart . showString " - " . shows spanEnd + instance Message Span where encodeMessage _ Span{..} = Encode.embedded 1 (encodeMessage 1 spanStart) <> Encode.embedded 2 (encodeMessage 1 spanEnd) decodeMessage _ = Span <$> embeddedAt (decodeMessage 1) 1 <*> embeddedAt (decodeMessage 1) 2