Skip to content

Commit 82a6ced

Browse files
More specific search error messages.
1 parent 17ef129 commit 82a6ced

3 files changed

Lines changed: 26 additions & 23 deletions

File tree

Core.hs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,19 +444,22 @@ genericJumpToMatch :: Lookup a
444444
-> (Int -> HState -> Int)
445445
-> IO ()
446446
genericJumpToMatch re sw k sel = do
447-
found <- modifyHS \st -> let
447+
mmsg <- modifyHS \st -> let
448448
info = case re of
449449
Just s -> Just (st { searchFw = sw }, s, sw)
450450
_ -> listToMaybe [ (st, s, st.searchFw == sw) | s <- st.searchHist ]
451-
in flip (maybe (st, False)) info \(st', p, forwards) -> do
451+
in flip (maybe (st, Just "No previous search.")) info
452+
\ (st', p, forwards) -> do
452453
let (fs, cur, m) = k st
453454
l = if forwards then [cur+1 .. m-1] ++ [0 .. cur]
454455
else [cur-1, cur-2 .. 0] ++ [m-1, m-2 .. cur]
455-
match = matches p
456-
case [ i | i <- l, match $ extract (fs ! i) ] of
457-
i:_ -> (st' { cursor = sel i st }, True)
458-
_ -> (st', False)
459-
unless found $ putMessage [plainSeg "No match found."]
456+
case matches p of
457+
Just match ->
458+
case [ i | i <- l, match $ extract (fs ! i) ] of
459+
i:_ -> (st' { cursor = sel i st }, Nothing)
460+
_ -> (st', Just "No match found.")
461+
_ -> (st', Just "Invalid ERE search pattern.")
462+
whenJust mmsg \msg -> putMessage [plainSeg msg]
460463

461464
------------------------------------------------------------------------
462465

Text.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ spaces :: Int -> ByteString
3232
spaces = flip P.replicate ' '
3333

3434
-- | Swappable API for searching
35-
-- TODO report invalid regex
36-
matches :: ByteString -> ByteString -> Bool
37-
matches s = maybe (const False) match $
38-
makeRegexOptsM (compIgnoreCase + compExtended) 0 s
35+
matches :: ByteString -> Maybe (ByteString -> Bool)
36+
matches s = match <$> makeRegexOptsM (compIgnoreCase + compExtended) 0 s
3937

4038
readIntM :: ByteString -> Maybe Int
4139
readIntM = fmap fst . P.readInt

test/TextSpec.hs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module TextSpec (tests) where
33
import Test.Tasty
44
import Test.Tasty.HUnit
55

6+
import Base ((<&>))
67
import Text
78

89
-- These tests depend on wcwidth's behavior under a UTF-8 locale and on a
@@ -16,16 +17,17 @@ import Text
1617
tests :: TestTree
1718
tests = testGroup "Text"
1819
[ testGroup "match"
19-
[ m True "exact" "foo" "fooBar"
20-
, m True "caseless" "FOO" "fooBar"
21-
, m False "invalid" "[" "any[thing"
22-
, m True "alt" "(foo|az)Q" "bazQux"
23-
, m True "dot" "o.a" "foobar"
24-
, m True "Unicode" "jör" "Björk"
25-
, m True "dot Unicode" "j.r" "Björk"
26-
, m True "Nordic case" "bør" "BØrnE"
27-
, m True "Greek case" "Λω" "ΦλΩα"
28-
, m True "dot CJK" "中.人" "中國人"
20+
[ m (Just True) "exact" "foo" "fooBar"
21+
, m (Just True) "caseless" "FOO" "fooBar"
22+
, m Nothing "invalid" "[" "any[thing"
23+
, m (Just True) "alt" "(foo|az)Q" "bazQux"
24+
, m (Just True) "dot" "o.a" "foobar"
25+
, m (Just True) "Unicode" "jör" "Björk"
26+
, m (Just True) "dot Unicode" "j.r" "Björk"
27+
, m (Just True) "Nordic case" "bør" "BØrnE"
28+
, m (Just True) "Greek case" "Λω" "ΦλΩα"
29+
, m (Just True) "dot CJK" "中.人" "中國人"
30+
, m (Just False) "byte dots" "c..te" "côte"
2931
]
3032
, testGroup "dropLastUTF8"
3133
[ testCase "ASCII" $ dropLastUTF8 "abc" @?= "ab"
@@ -88,6 +90,6 @@ tests = testGroup "Text"
8890
]
8991

9092

91-
m :: Bool -> String -> String -> String -> TestTree
92-
m b tag pat str = testCase tag $ matches (u pat) (u str) @?= b
93+
m :: Maybe Bool -> String -> String -> String -> TestTree
94+
m b tag pat str = testCase tag $ (matches (u pat) <&> ($ u str)) @?= b
9395

0 commit comments

Comments
 (0)