1
1
mirror of https://github.com/github/semantic.git synced 2024-12-30 10:27:45 +03:00

📝 rangeToSourceSpan & sourceSpanToRange.

This commit is contained in:
Rob Rix 2017-05-19 11:43:10 -04:00
parent 9f847da265
commit 96469f0bd1

View File

@ -110,7 +110,7 @@ actualLineRangesWithin :: Range -> Source -> [Range]
actualLineRangesWithin range = Prologue.drop 1 . scanl toRange (Range (start range) (start range)) . actualLines . slice range actualLineRangesWithin range = Prologue.drop 1 . scanl toRange (Range (start range) (start range)) . actualLines . slice range
where toRange previous string = Range (end previous) $ end previous + B.length (sourceText string) where toRange previous string = Range (end previous) $ end previous + B.length (sourceText string)
-- | Compute the character range given a Source and a SourceSpan. -- | Compute the byte 'Range' corresponding to a given 'SourceSpan' in a 'Source'.
sourceSpanToRange :: Source -> SourceSpan -> Range sourceSpanToRange :: Source -> SourceSpan -> Range
sourceSpanToRange source SourceSpan{..} = Range start end sourceSpanToRange source SourceSpan{..} = Range start end
where start = pred (sumLengths leadingRanges + column spanStart) where start = pred (sumLengths leadingRanges + column spanStart)
@ -118,6 +118,7 @@ sourceSpanToRange source SourceSpan{..} = Range start end
(leadingRanges, remainingRanges) = splitAt (pred (line spanStart)) (actualLineRanges source) (leadingRanges, remainingRanges) = splitAt (pred (line spanStart)) (actualLineRanges source)
sumLengths = sum . fmap rangeLength sumLengths = sum . fmap rangeLength
-- | Compute the 'SourceSpan' corresponding to a given byte 'Range' in a 'Source'.
rangeToSourceSpan :: Source -> Range -> SourceSpan rangeToSourceSpan :: Source -> Range -> SourceSpan
rangeToSourceSpan source (Range rangeStart rangeEnd) = SourceSpan startPos endPos rangeToSourceSpan source (Range rangeStart rangeEnd) = SourceSpan startPos endPos
where startPos = SourcePos (succ (Prologue.length before)) (succ (rangeStart - start firstRange)) where startPos = SourcePos (succ (Prologue.length before)) (succ (rangeStart - start firstRange))