Merge pull request #282 from anthraxx/fixup/prompt-fuzzymatch

XMonad.Prompt sorter for FuzzyMatch
This commit is contained in:
Leary 2018-10-18 05:36:51 +13:00 committed by GitHub
commit 204524328d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 7 deletions

View File

@ -38,6 +38,12 @@
* `XMonad.Actions.MessageHandling` * `XMonad.Actions.MessageHandling`
Refresh-performing functions updated to better reflect the new `sendMessage`. Refresh-performing functions updated to better reflect the new `sendMessage`.
* `XMonad.Prompt`
Added `sorter` to `XPConfig` used to sort the possible completions by how
well they match the search string (example: `XMonad.Prompt.FuzzyMatch`).
## 0.14 ## 0.14
### Breaking Changes ### Breaking Changes

View File

@ -156,6 +156,10 @@ data XPConfig =
, searchPredicate :: String -> String -> Bool , searchPredicate :: String -> String -> Bool
-- ^ Given the typed string and a possible -- ^ Given the typed string and a possible
-- completion, is the completion valid? -- completion, is the completion valid?
, sorter :: String -> [String] -> [String]
-- ^ Used to sort the possible completions by how well they
-- match the search string (see X.P.FuzzyMatch for an
-- example).
} }
data XPType = forall p . XPrompt p => XPT p data XPType = forall p . XPrompt p => XPT p
@ -268,6 +272,7 @@ instance Default XPConfig where
, showCompletionOnTab = False , showCompletionOnTab = False
, searchPredicate = isPrefixOf , searchPredicate = isPrefixOf
, alwaysHighlight = False , alwaysHighlight = False
, sorter = const id
} }
{-# DEPRECATED defaultXPConfig "Use def (from Data.Default, and re-exported from XMonad.Prompt) instead." #-} {-# DEPRECATED defaultXPConfig "Use def (from Data.Default, and re-exported from XMonad.Prompt) instead." #-}
defaultXPConfig = def defaultXPConfig = def
@ -956,8 +961,10 @@ getCompletionFunction st = case operationMode st of
getCompletions :: XP [String] getCompletions :: XP [String]
getCompletions = do getCompletions = do
s <- get s <- get
io $ getCompletionFunction s (commandToComplete (currentXPMode s) (command s)) let q = commandToComplete (currentXPMode s) (command s)
`E.catch` \(SomeException _) -> return [] compl = getCompletionFunction s
srt = sorter (config s)
io $ (srt q <$> compl q) `E.catch` \(SomeException _) -> return []
setComplWin :: Window -> ComplWindowDim -> XP () setComplWin :: Window -> ComplWindowDim -> XP ()
setComplWin w wi = setComplWin w wi =

View File

@ -48,19 +48,19 @@ import Data.List
-- 11. "FastSPR" is ranked before "FasterSPR" because its match starts at -- 11. "FastSPR" is ranked before "FasterSPR" because its match starts at
-- position 5 while the match in "FasterSPR" starts at position 7. -- position 5 while the match in "FasterSPR" starts at position 7.
-- --
-- To use these functions in an XPrompt, for example, for windowPromptGoto: -- To use these functions in an XPrompt, for example, for windowPrompt:
-- --
-- > import XMonad.Prompt -- > import XMonad.Prompt
-- > import XMonad.Prompt.Window ( windowPromptGoto ) -- > import XMonad.Prompt.Window ( windowPrompt )
-- > import XMonad.Prompt.FuzzyMatch -- > import XMonad.Prompt.FuzzyMatch
-- > -- >
-- > myXPConfig = def { searchPredicate = fuzzyMatch -- > myXPConfig = def { searchPredicate = fuzzyMatch
-- , sorter = fuzzySort -- > , sorter = fuzzySort
-- } -- > }
-- --
-- then add this to your keys definition: -- then add this to your keys definition:
-- --
-- > , ((modm .|. shiftMask, xK_g), windowPromptGoto myXPConfig) -- > , ((modm .|. shiftMask, xK_g), windowPrompt myXPConfig Goto allWindows)
-- --
-- For detailed instructions on editing the key bindings, see -- For detailed instructions on editing the key bindings, see
-- "Xmonad.Doc.Extending#Editing_key_bindings". -- "Xmonad.Doc.Extending#Editing_key_bindings".