1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 05:41:54 +03:00

Correct the definition of rangeToSourceSpan.

This commit is contained in:
Rob Rix 2017-05-19 11:39:45 -04:00
parent 61e29e8fdb
commit e66122f0da

View File

@ -3,6 +3,7 @@
module Source where module Source where
import qualified Data.ByteString as B import qualified Data.ByteString as B
import Data.List (span)
import Data.String (IsString(..)) import Data.String (IsString(..))
import qualified Data.Text as T import qualified Data.Text as T
import Language import Language
@ -118,12 +119,13 @@ sourceSpanToRange source SourceSpan{..} = Range start end
sumLengths = sum . fmap rangeLength sumLengths = sum . fmap rangeLength
rangeToSourceSpan :: Source -> Range -> SourceSpan rangeToSourceSpan :: Source -> Range -> SourceSpan
rangeToSourceSpan source range = SourceSpan startPos endPos rangeToSourceSpan source (Range rangeStart rangeEnd) = SourceSpan startPos endPos
where startPos = maybe (SourcePos 1 1) (toStartPos 1) (head lineRanges) where startPos = SourcePos (succ (Prologue.length before)) (succ (rangeStart - start firstRange))
endPos = toEndPos (Prologue.length lineRanges) (fromMaybe (Range 0 0) (snd <$> unsnoc lineRanges)) endPos = SourcePos (Prologue.length before + Prologue.length lineRanges) (succ (rangeEnd - start lastRange))
lineRanges = actualLineRangesWithin range source (before, rest) = span ((< rangeStart) . end) (actualLineRanges source)
toStartPos line range = SourcePos line (succ (start range)) (lineRanges, _) = span ((<= rangeEnd) . start) rest
toEndPos line range = SourcePos line (succ (end range)) Just firstRange = getFirst (foldMap (First . Just) lineRanges)
Just lastRange = getLast (foldMap (Last . Just) lineRanges)
-- | Return a 'Range' that covers the entire text. -- | Return a 'Range' that covers the entire text.
totalRange :: Source -> Range totalRange :: Source -> Range