1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00

Custom Show for Range and Span

This commit is contained in:
Timothy Clem 2018-09-26 13:56:20 -05:00
parent 1913277e74
commit fdc8d5210a
2 changed files with 12 additions and 3 deletions

View File

@ -17,7 +17,7 @@ import Proto3.Wire.Decode as Decode
-- | A half-open interval of integers, defined by start & end indices. -- | A half-open interval of integers, defined by start & end indices.
data Range = Range { start :: {-# UNPACK #-} !Int, end :: {-# UNPACK #-} !Int } data Range = Range { start :: {-# UNPACK #-} !Int, end :: {-# UNPACK #-} !Int }
deriving (Eq, Show, Generic, Named) deriving (Eq, Generic, Named)
emptyRange :: Range emptyRange :: Range
emptyRange = Range 0 0 emptyRange = Range 0 0
@ -53,6 +53,9 @@ instance Semigroup Range where
instance Ord Range where instance Ord Range where
a <= b = start a <= start b a <= b = start a <= start b
instance Show Range where
showsPrec _ Range{..} = showChar '[' . shows start . showString " - " . shows end . showChar ']'
instance ToJSONFields Range where instance ToJSONFields Range where
toJSONFields Range{..} = ["sourceRange" .= [ start, end ]] toJSONFields Range{..} = ["sourceRange" .= [ start, end ]]

View File

@ -24,18 +24,21 @@ data Pos = Pos
{ posLine :: !Int { posLine :: !Int
, posColumn :: !Int , posColumn :: !Int
} }
deriving (Show, Read, Eq, Ord, Generic, Hashable) deriving (Eq, Ord, Generic, Hashable)
-- | A Span of position information -- | A Span of position information
data Span = Span data Span = Span
{ spanStart :: Pos { spanStart :: Pos
, spanEnd :: Pos , spanEnd :: Pos
} }
deriving (Show, Read, Eq, Ord, Generic, Hashable, Named) deriving (Eq, Ord, Generic, Hashable, Named)
-- Instances -- Instances
instance Show Pos where
showsPrec _ Pos{..} = showChar '[' . shows posLine . showString ", " . shows posColumn . showChar ']'
instance Named Pos where nameOf _ = "Position" instance Named Pos where nameOf _ = "Position"
instance Message Pos where instance Message Pos where
encodeMessage _ Pos{..} = encodeMessageField 1 posLine <> encodeMessageField 2 posColumn encodeMessage _ Pos{..} = encodeMessageField 1 posLine <> encodeMessageField 2 posColumn
@ -61,6 +64,9 @@ instance HasDefault Pos where
def = lowerBound @Pos def = lowerBound @Pos
instance Show Span where
showsPrec _ Span{..} = shows spanStart . showString " - " . shows spanEnd
instance Message Span where instance Message Span where
encodeMessage _ Span{..} = Encode.embedded 1 (encodeMessage 1 spanStart) <> Encode.embedded 2 (encodeMessage 1 spanEnd) encodeMessage _ Span{..} = Encode.embedded 1 (encodeMessage 1 spanStart) <> Encode.embedded 2 (encodeMessage 1 spanEnd)
decodeMessage _ = Span <$> embeddedAt (decodeMessage 1) 1 <*> embeddedAt (decodeMessage 1) 2 decodeMessage _ = Span <$> embeddedAt (decodeMessage 1) 1 <*> embeddedAt (decodeMessage 1) 2