55
66 :license: MIT, see LICENSE for more details.
77"""
8+ import itertools
89import shlex
910
1011import click
@@ -27,30 +28,29 @@ def _click_autocomplete(root, text):
2728 try :
2829 parts = shlex .split (text )
2930 except ValueError :
30- return []
31+ raise StopIteration
3132
3233 location , incomplete = _click_resolve_command (root , parts )
3334
3435 if not text .endswith (' ' ) and not incomplete and text :
35- return []
36+ raise StopIteration
3637
37- options = []
3838 if incomplete and not incomplete [0 :2 ].isalnum ():
3939 for param in location .params :
4040 if not isinstance (param , click .Option ):
4141 continue
42- options .extend (param .opts )
43- options .extend (param .secondary_opts )
42+ for opt in itertools .chain (param .opts , param .secondary_opts ):
43+ if opt .startswith (incomplete ):
44+ yield completion .Completion (opt , - len (incomplete ),
45+ display_meta = param .help )
46+
4447 elif isinstance (location , (click .MultiCommand , click .core .Group )):
45- options .extend (location .list_commands (click .Context (location )))
46-
47- # collect options that starts with the incomplete section
48- completions = []
49- for option in options :
50- if option .startswith (incomplete ):
51- completions .append (
52- completion .Completion (option , - len (incomplete )))
53- return completions
48+ ctx = click .Context (location )
49+ for command in location .list_commands (ctx ):
50+ if command .startswith (incomplete ):
51+ cmd = location .get_command (ctx , command )
52+ yield completion .Completion (command , - len (incomplete ),
53+ display_meta = cmd .short_help )
5454
5555
5656def _click_resolve_command (root , parts ):
0 commit comments