From 13b64453097d896596dbdf757669b5d74535f199 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Fri, 19 May 2017 11:45:51 -0400 Subject: [PATCH] Share the computation of the first line. --- src/Source.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Source.hs b/src/Source.hs index 62c3f66a3..3d86019f8 100644 --- a/src/Source.hs +++ b/src/Source.hs @@ -121,8 +121,9 @@ sourceSpanToRange source SourceSpan{..} = Range start end -- | Compute the 'SourceSpan' corresponding to a given byte 'Range' in a 'Source'. rangeToSourceSpan :: Source -> Range -> SourceSpan rangeToSourceSpan source (Range rangeStart rangeEnd) = SourceSpan startPos endPos - where startPos = SourcePos (Prologue.length before + 1) (rangeStart - start firstRange + 1) - endPos = SourcePos (Prologue.length before + Prologue.length lineRanges) (rangeEnd - start lastRange + 1) + where startPos = SourcePos (firstLine + 1) (rangeStart - start firstRange + 1) + endPos = SourcePos (firstLine + Prologue.length lineRanges) (rangeEnd - start lastRange + 1) + firstLine = Prologue.length before (before, rest) = span ((< rangeStart) . end) (actualLineRanges source) (lineRanges, _) = span ((<= rangeEnd) . start) rest Just firstRange = getFirst (foldMap (First . Just) lineRanges)