diff --git a/app/Main.hs b/app/Main.hs index 27b80c84b..e1a77f33e 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -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 diff --git a/src/Range.hs b/src/Range.hs index 91c7148e7..33d5335d8 100644 --- a/src/Range.hs +++ b/src/Range.hs @@ -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