module XMonad.Prompt.FuzzyMatch (
fuzzyMatch
, fuzzySort
) where
import XMonad.Prelude
import qualified Data.List.NonEmpty as NE
fuzzyMatch :: String -> String -> Bool
fuzzyMatch :: String -> String -> Bool
fuzzyMatch String
a String
b = String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
isSubsequenceOf ((Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower String
a) ((Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower String
b)
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 (Char
q:String
qs) 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
$ NonEmpty Char -> String -> [(Int, Int)]
findShortestMatches (Char
q Char -> String -> NonEmpty Char
forall a. a -> [a] -> NonEmpty a
:| String
qs) String
s
findShortestMatches :: NonEmpty Char -> String -> [(Int, Int)]
findShortestMatches :: NonEmpty Char -> String -> [(Int, Int)]
findShortestMatches NonEmpty Char
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]) -> NonEmpty Char -> NonEmpty [Int]
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map (String -> Char -> [Int]
findOccurrences String
s) NonEmpty Char
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'