Skip to content

Instantly share code, notes, and snippets.

@alanz
Last active October 28, 2021 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanz/5e262599ab79138606cdfcf3792ef635 to your computer and use it in GitHub Desktop.
Save alanz/5e262599ab79138606cdfcf3792ef635 to your computer and use it in GitHub Desktop.
-- ---------------------------------------------------------------------
-- Horrible hack for dealing with some things still having a SrcSpan,
-- not an Anchor.
{-
A SrcSpan is defined as
data SrcSpan =
RealSrcSpan !RealSrcSpan !(Maybe BufSpan) -- See Note [Why Maybe BufPos]
| UnhelpfulSpan !UnhelpfulSpanReason
data BufSpan =
BufSpan { bufSpanStart, bufSpanEnd :: {-# UNPACK #-} !BufPos }
deriving (Eq, Ord, Show)
newtype BufPos = BufPos { bufPos :: Int }
We use the BufPos to encode a delta, using bufSpanStart for the line,
and bufSpanEnd for the col.
To be absolutely sure, we make the delta versions use -ve values.
-}
hackSrcSpanToAnchor :: SrcSpan -> Anchor
hackSrcSpanToAnchor (UnhelpfulSpan _) = error "hackSrcSpanToAnchor"
hackSrcSpanToAnchor (RealSrcSpan rs Nothing) = Anchor rs UnchangedAnchor
hackSrcSpanToAnchor (RealSrcSpan rs (Just (BufSpan (BufPos s) (BufPos e))))
= if s <= 0 && e <= 0
then Anchor rs (MovedAnchor (deltaPos (-s) (-e)))
else Anchor rs UnchangedAnchor
hackAnchorToSrcSpan :: Anchor -> SrcSpan
hackAnchorToSrcSpan (Anchor rs UnchangedAnchor) = RealSrcSpan rs Nothing
hackAnchorToSrcSpan (Anchor rs (MovedAnchor dp))
= RealSrcSpan rs (Just (BufSpan (BufPos s) (BufPos e)))
where
s = - (getDeltaLine dp)
e = - (deltaColumn dp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment