@@ -18,6 +18,8 @@ import Prelude
1818import Data.Foldable ( asum )
1919#endif
2020import Data.List ( isPrefixOf )
21+ import Data.List.NonEmpty (NonEmpty )
22+ import qualified Data.List.NonEmpty as NE
2123import Data.Maybe ( fromMaybe , listToMaybe )
2224
2325import Options.Applicative.Builder
@@ -120,7 +122,7 @@ bashCompletionQuery pinfo pprefs richness ws i _ = case runCompletion compl ppre
120122 | argumentIsUnreachable reachability
121123 -> return []
122124 | otherwise
123- -> return . with_cmd_help $ filter (is_completion . fst ) ns
125+ -> return . with_cmd_help $ filter_completions ns
124126
125127 -- When doing enriched completions, add any help specified
126128 -- to the completion variables (tab separated).
@@ -148,6 +150,22 @@ bashCompletionQuery pinfo pprefs richness ws i _ = case runCompletion compl ppre
148150 show_names :: [OptName ] -> [String ]
149151 show_names = filter is_completion . map showOption
150152
153+ -- Filter commands and aliases matching the completion e.g. if we have
154+ -- commands:
155+ --
156+ -- - ([retry], p1)
157+ -- - ([run, r, go], p2)
158+ -- - ([search], p3)
159+ --
160+ -- and the user types 'r', we should return
161+ --
162+ -- [(retry, p1), (run, p2), (r, p2)]
163+ filter_completions :: [(NonEmpty String , ParserInfo a )] -> [(String , ParserInfo a )]
164+ filter_completions =
165+ let matchZip :: (NonEmpty String , b ) -> [(String , b )]
166+ matchZip (aliases, painfo) = (\ x -> (x, painfo)) <$> NE. filter is_completion aliases
167+ in (>>= matchZip)
168+
151169 -- We only want to show a single line in the completion results description.
152170 -- If there was a line break, it would come across as a different completion
153171 -- possibility.
0 commit comments