1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 07:25:44 +03:00

Move rangesOfWordsFrom into Range.hs.

This commit is contained in:
Rob Rix 2015-12-14 12:44:48 -05:00
parent 8acc962ad2
commit bf9aa915a3
2 changed files with 11 additions and 10 deletions

View File

@ -11,7 +11,6 @@ import Term
import TreeSitter
import Unified
import Control.Comonad.Cofree
import qualified Data.Char as Char
import qualified Data.Map as Map
import qualified Data.ByteString.Char8 as B1
import qualified Data.ByteString.Lazy as B2
@ -71,12 +70,3 @@ replaceLeavesWithWordBranches source term = replaceIn source 0 term
Keyed k -> Keyed $ replaceIn (substring range source) (start range) <$> k
_ -> syntax
makeLeaf source startIndex lineRange categories range = Info range lineRange categories :< Leaf (substring (offsetRange (negate startIndex) range) source)
rangesOfWordsFrom :: Int -> String -> [Range]
rangesOfWordsFrom startIndex string = case break Char.isSpace string of
([], []) -> []
([], rest) -> rangesOfWordsAfterWhitespace startIndex rest
(word, []) -> [ Range startIndex $ length word ]
(word, rest) -> (Range startIndex $ length word) : rangesOfWordsAfterWhitespace (startIndex + length word) rest
where
rangesOfWordsAfterWhitespace startIndex string | (whitespace, rest) <- break (not . Char.isSpace) string = rangesOfWordsFrom (startIndex + length whitespace) rest

View File

@ -1,5 +1,7 @@
module Range where
import qualified Data.Char as Char
data Range = Range { start :: Int, end :: Int }
deriving (Eq, Show)
@ -12,5 +14,14 @@ totalRange list = Range 0 $ length list
offsetRange :: Int -> Range -> Range
offsetRange i (Range start end) = Range (i + start) (i + end)
rangesOfWordsFrom :: Int -> String -> [Range]
rangesOfWordsFrom startIndex string = case break Char.isSpace string of
([], []) -> []
([], rest) -> rangesOfWordsAfterWhitespace startIndex rest
(word, []) -> [ Range startIndex $ length word ]
(word, rest) -> (Range startIndex $ length word) : rangesOfWordsAfterWhitespace (startIndex + length word) rest
where
rangesOfWordsAfterWhitespace startIndex string | (whitespace, rest) <- break (not . Char.isSpace) string = rangesOfWordsFrom (startIndex + length whitespace) rest
instance Ord Range where
a <= b = start a <= start b