Skip to content

Commit e41f143

Browse files
committed
Add documentation for filter function with custom scoring function
Replace all functions using previous filter without custom scoring with a call to the new filter function where the scoring function is the basic match function.
1 parent 505809c commit e41f143

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

ghcide/src/Development/IDE/Plugin/Completions/Logic.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ getCompletions plugins ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls,
585585
$ (if T.null enteredQual then id else mapMaybe (T.stripPrefix enteredQual))
586586
allModNamesAsNS
587587

588-
filtCompls = Fuzzy.filter chunkSize maxC prefixText ctxCompls (label . snd)
588+
filtCompls = Fuzzy.filter chunkSize maxC prefixText ctxCompls (label . snd) Fuzzy.match
589589
where
590590

591591
mcc = case maybe_parsed of

ghcide/src/Text/Fuzzy/Parallel.hs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- | Parallel versions of 'filter' and 'simpleFilter'
22

33
module Text.Fuzzy.Parallel
4-
( filter, filter',
4+
( filter,
55
simpleFilter, simpleFilter',
66
match, defChunkSize, defMaxResults,
77
Scored(..)
@@ -29,7 +29,6 @@ data Scored a = Scored {score :: !Int, original:: !a}
2929
-- Just 5
3030
--
3131
{-# INLINABLE match #-}
32-
3332
match :: T.Text -- ^ Pattern in lowercase except for first character
3433
-> T.Text -- ^ The text to search in.
3534
-> Maybe Int -- ^ The score
@@ -70,23 +69,6 @@ match (T.Text pArr pOff pLen) (T.Text sArr sOff sLen) = go 0 1 pOff sOff
7069

7170
toLowerAscii w = if (w - 65) < 26 then w .|. 0x20 else w
7271

73-
-- | The function to filter a list of values by fuzzy search on the text extracted from them.
74-
filter :: Int -- ^ Chunk size. 1000 works well.
75-
-> Int -- ^ Max. number of results wanted
76-
-> T.Text -- ^ Pattern.
77-
-> [t] -- ^ The list of values containing the text to search in.
78-
-> (t -> T.Text) -- ^ The function to extract the text from the container.
79-
-> [Scored t] -- ^ The list of results, sorted, highest score first.
80-
filter chunkSize maxRes pattern ts extract = partialSortByAscScore maxRes perfectScore (concat vss)
81-
where
82-
-- Preserve case for the first character, make all others lowercase
83-
pattern' = case T.uncons pattern of
84-
Just (c, rest) -> T.cons c (T.toLower rest)
85-
_ -> pattern
86-
vss = map (mapMaybe (\t -> flip Scored t <$> match pattern' (extract t))) (chunkList chunkSize ts)
87-
`using` parList (evalList rseq)
88-
perfectScore = fromMaybe (error $ T.unpack pattern) $ match pattern' pattern'
89-
9072
-- | Sensible default value for chunk size to use when calling simple filter.
9173
defChunkSize :: Int
9274
defChunkSize = 1000
@@ -108,18 +90,21 @@ simpleFilter :: Int -- ^ Chunk size. 1000 works well.
10890
-> [T.Text] -- ^ List of texts to check.
10991
-> [Scored T.Text] -- ^ The ones that match.
11092
simpleFilter chunk maxRes pattern xs =
111-
filter chunk maxRes pattern xs id
93+
filter chunk maxRes pattern xs id match
11294

11395

114-
-- | The function to filter a list of values by fuzzy search on the text extracted from them.
115-
filter' :: Int -- ^ Chunk size. 1000 works well.
96+
-- | The function to filter a list of values by fuzzy search on the text extracted from them,
97+
-- using a custom matching function which determines how close words are.
98+
filter :: Int -- ^ Chunk size. 1000 works well.
11699
-> Int -- ^ Max. number of results wanted
117100
-> T.Text -- ^ Pattern.
118101
-> [t] -- ^ The list of values containing the text to search in.
119102
-> (t -> T.Text) -- ^ The function to extract the text from the container.
120-
-> (T.Text -> T.Text -> Maybe Int) -- ^ Function to use for matching
103+
-> (T.Text -> T.Text -> Maybe Int)
104+
-- ^ Custom scoring function to use for calculating how close words are
105+
-- When the function returns Nothing, this means the values are incomparable.
121106
-> [Scored t] -- ^ The list of results, sorted, highest score first.
122-
filter' chunkSize maxRes pattern ts extract match' = partialSortByAscScore maxRes perfectScore (concat vss)
107+
filter chunkSize maxRes pattern ts extract match' = partialSortByAscScore maxRes perfectScore (concat vss)
123108
where
124109
-- Preserve case for the first character, make all others lowercase
125110
pattern' = case T.uncons pattern of
@@ -129,21 +114,19 @@ filter' chunkSize maxRes pattern ts extract match' = partialSortByAscScore maxRe
129114
`using` parList (evalList rseq)
130115
perfectScore = fromMaybe (error $ T.unpack pattern) $ match' pattern' pattern'
131116

132-
-- | Return all elements of the list that have a fuzzy
133-
-- match against the pattern, using a custom match function. Runs with default settings where
134-
-- nothing is added around the matches, as case insensitive.
135-
--
136-
-- >>> simpleFilter 1000 10 "vm" ["vim", "emacs", "virtual machine"]
137-
-- [Scored {score = 4, original = "vim"},Scored {score = 4, original = "virtual machine"}]
117+
-- | Return all elements of the list that have a fuzzy match against the pattern,
118+
-- the closeness of the match is determined using the custom scoring match function that is passed.
119+
-- Runs with default settings where nothing is added around the matches, as case insensitive.
138120
{-# INLINABLE simpleFilter' #-}
139121
simpleFilter' :: Int -- ^ Chunk size. 1000 works well.
140122
-> Int -- ^ Max. number of results wanted
141123
-> T.Text -- ^ Pattern to look for.
142124
-> [T.Text] -- ^ List of texts to check.
143-
-> (T.Text -> T.Text -> Maybe Int) -- ^ Function to use for matching
125+
-> (T.Text -> T.Text -> Maybe Int)
126+
-- ^ Custom scoring function to use for calculating how close words are
144127
-> [Scored T.Text] -- ^ The ones that match.
145128
simpleFilter' chunk maxRes pattern xs match' =
146-
filter' chunk maxRes pattern xs id match'
129+
filter chunk maxRes pattern xs id match'
147130
--------------------------------------------------------------------------------
148131

149132
chunkList :: Int -> [a] -> [[a]]

0 commit comments

Comments
 (0)