From e66122f0da5c07926408ff0a4f2ad76ba12830a9 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Fri, 19 May 2017 11:39:45 -0400 Subject: [PATCH] Correct the definition of rangeToSourceSpan. --- src/Source.hs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Source.hs b/src/Source.hs index a2ea56b7c..fd52a46dd 100644 --- a/src/Source.hs +++ b/src/Source.hs @@ -3,6 +3,7 @@ module Source where import qualified Data.ByteString as B +import Data.List (span) import Data.String (IsString(..)) import qualified Data.Text as T import Language @@ -118,12 +119,13 @@ sourceSpanToRange source SourceSpan{..} = Range start end sumLengths = sum . fmap rangeLength rangeToSourceSpan :: Source -> Range -> SourceSpan -rangeToSourceSpan source range = SourceSpan startPos endPos - where startPos = maybe (SourcePos 1 1) (toStartPos 1) (head lineRanges) - endPos = toEndPos (Prologue.length lineRanges) (fromMaybe (Range 0 0) (snd <$> unsnoc lineRanges)) - lineRanges = actualLineRangesWithin range source - toStartPos line range = SourcePos line (succ (start range)) - toEndPos line range = SourcePos line (succ (end range)) +rangeToSourceSpan source (Range rangeStart rangeEnd) = SourceSpan startPos endPos + where startPos = SourcePos (succ (Prologue.length before)) (succ (rangeStart - start firstRange)) + endPos = SourcePos (Prologue.length before + Prologue.length lineRanges) (succ (rangeEnd - start lastRange)) + (before, rest) = span ((< rangeStart) . end) (actualLineRanges source) + (lineRanges, _) = span ((<= rangeEnd) . start) rest + Just firstRange = getFirst (foldMap (First . Just) lineRanges) + Just lastRange = getLast (foldMap (Last . Just) lineRanges) -- | Return a 'Range' that covers the entire text. totalRange :: Source -> Range