module XMonad.Prompt.FuzzyMatch (
fuzzyMatch
, fuzzySort
) where
import XMonad.Prelude
fuzzyMatch :: String -> String -> Bool
fuzzyMatch :: String -> String -> Bool
fuzzyMatch [] String
_ = Bool
True
fuzzyMatch String
_ [] = Bool
False
fuzzyMatch xxs :: String
xxs@(Char
x:String
xs) (Char
y:String
ys) | Char -> Char
toLower Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Char
toLower Char
y = String -> String -> Bool
fuzzyMatch String
xs String
ys
| Bool
otherwise = String -> String -> Bool
fuzzyMatch String
xxs String
ys
fuzzySort :: String -> [String] -> [String]
fuzzySort :: String -> [String] -> [String]
fuzzySort String
q = (((Int, Int), String) -> String)
-> [((Int, Int), String)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((Int, Int), String) -> String
forall a b. (a, b) -> b
snd ([((Int, Int), String)] -> [String])
-> ([String] -> [((Int, Int), String)]) -> [String] -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [((Int, Int), String)] -> [((Int, Int), String)]
forall a. Ord a => [a] -> [a]
sort ([((Int, Int), String)] -> [((Int, Int), String)])
-> ([String] -> [((Int, Int), String)])
-> [String]
-> [((Int, Int), String)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> ((Int, Int), String))
-> [String] -> [((Int, Int), String)]
forall a b. (a -> b) -> [a] -> [b]
map (String -> String -> ((Int, Int), String)
rankMatch String
q)
rankMatch :: String -> String -> ((Int, Int), String)
rankMatch :: String -> String -> ((Int, Int), String)
rankMatch String
q String
s = (if [(Int, Int)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Int, Int)]
matches then (Int
forall a. Bounded a => a
maxBound, Int
forall a. Bounded a => a
maxBound) else [(Int, Int)] -> (Int, Int)
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [(Int, Int)]
matches, String
s)
where matches :: [(Int, Int)]
matches = String -> String -> [(Int, Int)]
rankMatches String
q String
s
rankMatches :: String -> String -> [(Int, Int)]
rankMatches :: String -> String -> [(Int, Int)]
rankMatches [] String
_ = [(Int
0, Int
0)]
rankMatches String
q String
s = ((Int, Int) -> (Int, Int)) -> [(Int, Int)] -> [(Int, Int)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Int
l, Int
r) -> (Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l, Int
l)) ([(Int, Int)] -> [(Int, Int)]) -> [(Int, Int)] -> [(Int, Int)]
forall a b. (a -> b) -> a -> b
$ String -> String -> [(Int, Int)]
findShortestMatches String
q String
s
findShortestMatches :: String -> String -> [(Int, Int)]
findShortestMatches :: String -> String -> [(Int, Int)]
findShortestMatches String
q String
s = ([(Int, Int)] -> [Int] -> [(Int, Int)])
-> [(Int, Int)] -> [[Int]] -> [(Int, Int)]
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' [(Int, Int)] -> [Int] -> [(Int, Int)]
extendMatches [(Int, Int)]
spans [[Int]]
oss
where ([Int]
os:[[Int]]
oss) = (Char -> [Int]) -> String -> [[Int]]
forall a b. (a -> b) -> [a] -> [b]
map (String -> Char -> [Int]
findOccurrences String
s) String
q
spans :: [(Int, Int)]
spans = [(Int
o, Int
o) | Int
o <- [Int]
os]
findOccurrences :: String -> Char -> [Int]
findOccurrences :: String -> Char -> [Int]
findOccurrences String
s Char
c = ((Char, Int) -> Int) -> [(Char, Int)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Char, Int) -> Int
forall a b. (a, b) -> b
snd ([(Char, Int)] -> [Int]) -> [(Char, Int)] -> [Int]
forall a b. (a -> b) -> a -> b
$ ((Char, Int) -> Bool) -> [(Char, Int)] -> [(Char, Int)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Char -> Char
toLower Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==) (Char -> Bool) -> ((Char, Int) -> Char) -> (Char, Int) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Char
toLower (Char -> Char) -> ((Char, Int) -> Char) -> (Char, Int) -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char, Int) -> Char
forall a b. (a, b) -> a
fst) ([(Char, Int)] -> [(Char, Int)]) -> [(Char, Int)] -> [(Char, Int)]
forall a b. (a -> b) -> a -> b
$ String -> [Int] -> [(Char, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip String
s [Int
0..]
extendMatches :: [(Int, Int)] -> [Int] -> [(Int, Int)]
extendMatches :: [(Int, Int)] -> [Int] -> [(Int, Int)]
extendMatches [(Int, Int)]
spans = ([(Int, Int)] -> (Int, Int)) -> [[(Int, Int)]] -> [(Int, Int)]
forall a b. (a -> b) -> [a] -> [b]
map [(Int, Int)] -> (Int, Int)
forall a. [a] -> a
last ([[(Int, Int)]] -> [(Int, Int)])
-> ([Int] -> [[(Int, Int)]]) -> [Int] -> [(Int, Int)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Int) -> (Int, Int) -> Bool)
-> [(Int, Int)] -> [[(Int, Int)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
(==) (Int -> Int -> Bool)
-> ((Int, Int) -> Int) -> (Int, Int) -> (Int, Int) -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (Int, Int) -> Int
forall a b. (a, b) -> b
snd) ([(Int, Int)] -> [[(Int, Int)]])
-> ([Int] -> [(Int, Int)]) -> [Int] -> [[(Int, Int)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Int, Int)] -> [Int] -> [(Int, Int)]
extendMatches' [(Int, Int)]
spans
extendMatches' :: [(Int, Int)] -> [Int] -> [(Int, Int)]
extendMatches' :: [(Int, Int)] -> [Int] -> [(Int, Int)]
extendMatches' [] [Int]
_ = []
extendMatches' [(Int, Int)]
_ [] = []
extendMatches' spans :: [(Int, Int)]
spans@((Int
l, Int
r):[(Int, Int)]
spans') xs :: [Int]
xs@(Int
x:[Int]
xs') | Int
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
x = (Int
l, Int
x) (Int, Int) -> [(Int, Int)] -> [(Int, Int)]
forall a. a -> [a] -> [a]
: [(Int, Int)] -> [Int] -> [(Int, Int)]
extendMatches' [(Int, Int)]
spans' [Int]
xs
| Bool
otherwise = [(Int, Int)] -> [Int] -> [(Int, Int)]
extendMatches' [(Int, Int)]
spans [Int]
xs'