mirror of
https://github.com/github/semantic.git
synced 2024-11-24 00:42:33 +03:00
Get the substrings out at the same time as computing the ranges.
This commit is contained in:
parent
fdb1a6a9f7
commit
cded6c0d7d
@ -65,4 +65,4 @@ replaceLeavesWithWordBranches source term = replaceIn source 0 term
|
||||
Fixed f -> Fixed $ replaceIn substring (start range) <$> f
|
||||
Keyed k -> Keyed $ replaceIn substring (start range) <$> k
|
||||
_ -> syntax
|
||||
makeLeaf source startIndex lineRange categories range = Info range lineRange categories :< Leaf (substring (offsetRange (negate startIndex) range) source)
|
||||
makeLeaf source startIndex lineRange categories (range, substring) = Info range lineRange categories :< Leaf substring
|
||||
|
@ -14,13 +14,13 @@ totalRange list = Range 0 $ length list
|
||||
offsetRange :: Int -> Range -> Range
|
||||
offsetRange i (Range start end) = Range (i + start) (i + end)
|
||||
|
||||
rangesAndWordsFrom :: Int -> String -> [Range]
|
||||
rangesAndWordsFrom :: Int -> String -> [(Range, String)]
|
||||
rangesAndWordsFrom startIndex string = case break (not . isWord) string of
|
||||
([], []) -> []
|
||||
([], rest) -> rangesAndWordsAfterWhitespace startIndex rest
|
||||
(word, rest) -> (Range startIndex $ startIndex + length word) : rangesAndWordsAfterWhitespace (startIndex + length word) rest
|
||||
(word, rest) -> (Range startIndex $ startIndex + length word, word) : rangesAndWordsAfterWhitespace (startIndex + length word) rest
|
||||
where
|
||||
rangesAndWordsAfterWhitespace startIndex string | (whitespace, rest) <- break isWord string = rangesOfWordsFrom (startIndex + length whitespace) rest
|
||||
rangesAndWordsAfterWhitespace startIndex string | (whitespace, rest) <- break isWord string = rangesAndWordsFrom (startIndex + length whitespace) rest
|
||||
-- | Is this a word character?
|
||||
-- | Word characters are defined as in [Ruby’s `\p{Word}` syntax](http://ruby-doc.org/core-2.1.1/Regexp.html#class-Regexp-label-Character+Properties), i.e.:
|
||||
-- | > A member of one of the following Unicode general category _Letter_, _Mark_, _Number_, _Connector_Punctuation_
|
||||
|
12
test/Spec.hs
12
test/Spec.hs
@ -218,22 +218,22 @@ main = hspec $ do
|
||||
rangesAndWordsFrom 0 " \t\n " `shouldBe` []
|
||||
|
||||
it "should produce a list containing the range of the string for a single-word string" $
|
||||
rangesAndWordsFrom 0 "word" `shouldBe` [ Range 0 4 ]
|
||||
rangesAndWordsFrom 0 "word" `shouldBe` [ (Range 0 4, "word") ]
|
||||
|
||||
it "should produce a list of ranges for whitespace-separated words" $
|
||||
rangesAndWordsFrom 0 "wordOne wordTwo" `shouldBe` [ Range 0 7, Range 8 15 ]
|
||||
rangesAndWordsFrom 0 "wordOne wordTwo" `shouldBe` [ (Range 0 7, "wordOne"), (Range 8 15, "wordTwo") ]
|
||||
|
||||
it "should skip multiple whitespace characters" $
|
||||
rangesAndWordsFrom 0 "a b" `shouldBe` [ Range 0 1, Range 3 4 ]
|
||||
rangesAndWordsFrom 0 "a b" `shouldBe` [ (Range 0 1, "a"), (Range 3 4, "b") ]
|
||||
|
||||
it "should skip whitespace at the start" $
|
||||
rangesAndWordsFrom 0 " a b" `shouldBe` [ Range 2 3, Range 4 5 ]
|
||||
rangesAndWordsFrom 0 " a b" `shouldBe` [ (Range 2 3, "a"), (Range 4 5, "b") ]
|
||||
|
||||
it "should skip whitespace at the end" $
|
||||
rangesAndWordsFrom 0 "a b " `shouldBe` [ Range 0 1, Range 2 3 ]
|
||||
rangesAndWordsFrom 0 "a b " `shouldBe` [ (Range 0 1, "a"), (Range 2 3, "b") ]
|
||||
|
||||
it "should produce ranges offset by its start index" $
|
||||
rangesAndWordsFrom 100 "a b" `shouldBe` [ Range 100 101, Range 102 103 ]
|
||||
rangesAndWordsFrom 100 "a b" `shouldBe` [ (Range 100 101, "a"), (Range 102 103, "b") ]
|
||||
|
||||
where
|
||||
rightRowText text = rightRow [ Text text ]
|
||||
|
Loading…
Reference in New Issue
Block a user