-- | Translates char-based span to byte-based offsets, using the table
-- constructed for the analysed file in context.
spanToOffsets :: Span -> Conversion (Maybe OffsetRange)
spanToOffsets (Span (Pos l1 c1 _) (Pos l2 c2 _)) = do
    t <- asks offsets
    let start = Offset.lineColToByteOffset t (l1 - 1) (c1 - 1)
        -- Being just at line end is acceptable for end span.
        end = case Offset.lineColToByteOffsetDetail t (l2 - 1) (c2 - 1) of
            Right o                                          -> Just o
            Left (Offset.OverLineEnd o Offset.JustAtLineEnd) -> Just o
            Left _                                           -> Nothing
    return $! OffsetRange <$> start <*> end

-- Boilerplate usually found in upstream libs.

-- | Like 'show' but return 'Text' instead 'String'.
spanToOffsets :: Span -> Conversion (Maybe OffsetRange)
spanToOffsets (Span (Pos l1 c1 _) (Pos l2 c2 _)) = do
    t <- asks offsets
    let start = Offset.lineColToByteOffset t (l1 - 1) (c1 - 1)
        -- Being just at line end is acceptable for end span.
        end = case Offset.lineColToByteOffsetDetail t (l2 - 1) (c2 - 1) of
            Right o                                          -> Just o
            Left (Offset.OverLineEnd o Offset.JustAtLineEnd) -> Just o
            Left _                                           -> Nothing
    return $! OffsetRange <$> start <*> end

-- Boilerplate usually found in upstream libs.

-- | Like 'show' but return 'Text' instead 'String'.
spanToOffsets (Span (Pos l1 c1 _) (Pos l2 c2 _)) = do
    t <- asks offsets
    let start = Offset.lineColToByteOffset t (l1 - 1) (c1 - 1)
        -- Being just at line end is acceptable for end span.
        end = case Offset.lineColToByteOffsetDetail t (l2 - 1) (c2 - 1) of
            Right o                                          -> Just o
            Left (Offset.OverLineEnd o Offset.JustAtLineEnd) -> Just o
            Left _                                           -> Nothing
    return $! OffsetRange <$> start <*> end

-- Boilerplate usually found in upstream libs.

-- | Like 'show' but return 'Text' instead 'String'.