def _click_autocomplete(root, text): """Completer generator for click applications.""" try: parts = shlex.split(text) except ValueError: return location, incomplete = _click_resolve_command(root, parts) if not text.endswith(' ') and not incomplete and text: return if incomplete and not incomplete[0:2].isalnum(): for param in location.params: if not isinstance(param, click.Option): continue for opt in itertools.chain(param.opts, param.secondary_opts): if opt.startswith(incomplete): yield completion.Completion(opt, -len(incomplete), display_meta=param.help) elif isinstance(location, (click.MultiCommand, click.core.Group)): ctx = click.Context(location) commands = location.list_commands(ctx) for command in commands: if command.startswith(incomplete): cmd = location.get_command(ctx, command) yield completion.Completion(command, -len(incomplete), display_meta=cmd.short_help)
def UriPathCompletions(self, completions, offset, chop): """Returns the list of Completion objects for URI path completions. Args: completions: The list of file/path completion strings. offset: The Completion object offset used for dropdown display. chop: The minimum number of chars to chop from the dropdown items. Returns: The list of Completion objects for file path completions. """ def _Display(c): """Returns the annotated dropdown display spelling of completion c.""" d = c[chop:] if d.startswith('/'): d = d[1:] if d.startswith('/'): d = d[1:] if c.endswith('/') and not c.endswith('://'): c = c[:-1] return completion.Completion(c, display=d, start_position=offset) if len(completions) == 1: # No dropdown for singletons so just return the marked completion. choice = completions[0] return [completion.Completion(choice, start_position=offset)] # Return completion objects with annotated choices for the dropdown. return [_Display(c) for c in completions]
def _Display(c): """Returns the annotated dropdown display spelling of completion c.""" d = _Mark(c)[chop:] if chop and d.startswith('/'): # Some shell completers insert an '/' that spoils the dropdown. d = d[1:] return completion.Completion(c, display=d, start_position=offset)
def FilePathCompletions(self, completions, offset, chop): """Returns the list of Completion objects for file path completions. Args: completions: The list of file/path completion strings. offset: The Completion object offset used for dropdown display. chop: The minimum number of chars to chop from the dropdown items. Returns: The list of Completion objects for file path completions. """ def _Mark(c): """Returns completion c with a trailing '/' if it is a dir.""" if not c.endswith('/') and os.path.isdir(c): return c + '/' return c def _Display(c): """Returns the annotated dropdown display spelling of completion c.""" d = _Mark(c)[chop:] if chop and d.startswith('/'): # Some shell completers insert an '/' that spoils the dropdown. d = d[1:] return completion.Completion(c, display=d, start_position=offset) if len(completions) == 1: # No dropdown for singletons so just return the marked completion. choice = _Mark(completions[0]) return [completion.Completion(choice, start_position=offset)] # Return completion objects with annotated choices for the dropdown. return [_Display(c) for c in completions]
def MakePathCompletion(cls, value, offset, chop, strip_trailing_slash=True): """Returns the Completion object for a file/uri path completion value. Args: value: The file/path completion value string. offset: The Completion object offset used for dropdown display. chop: The minimum number of chars to chop from the dropdown items. strip_trailing_slash: Strip trailing '/' if True. Returns: The Completion object for a file path completion value or None if the chopped/stripped value is empty. """ display = value if chop: display = display[chop:].lstrip('/') if not display: return None if strip_trailing_slash and not value.endswith(_URI_SEP): value = value.rstrip('/') if not value: return None return completion.Completion(value, display=display, start_position=offset)
def _click_autocomplete(root, text): """Completer generator for click applications.""" try: parts = shlex.split(text) except ValueError: return [] location, incomplete = _click_resolve_command(root, parts) if not text.endswith(' ') and not incomplete and text: return [] options = [] if incomplete and not incomplete[0:2].isalnum(): for param in location.params: if not isinstance(param, click.Option): continue options.extend(param.opts) options.extend(param.secondary_opts) elif isinstance(location, (click.MultiCommand, click.core.Group)): options.extend(location.list_commands(click.Context(location))) # collect options that starts with the incomplete section completions = [] for option in options: if option.startswith(incomplete): completions.append( completion.Completion(option, -len(incomplete))) return completions
def get_completions(self, doc, complete_event): """Get the completions for doc. Args: doc: A Document instance containing the shell command line to complete. complete_event: The CompleteEvent that triggered this completion. Yields: List of completions for a given input """ input_line = doc.current_line_before_cursor # Make sure command contains gcloud if input_line.find('gcloud') == -1: return input_line = GetLastGcloudCmd(input_line) possible_completions = [] try: possible_completions = lookup.FindCompletions(self.table, input_line) except (lookup.CannotHandleCompletionError, ValueError): return for item in possible_completions: last = input_line.split(' ')[-1] token = 0 - len(last) yield completion.Completion(item, token)
def get_completions(self, doc, event): """Yields the completions for doc. Args: doc: A Document instance containing the interactive command line to complete. event: The CompleteEvent that triggered this completion. Yields: Completion instances for doc. """ args = self.parser.ParseCommand(doc.text_before_cursor) if not args: return self.last = doc.text_before_cursor[-1] if doc.text_before_cursor else '' self.empty = self.last.isspace() self.event = event for completer in ( self.CommandCompleter, self.FlagCompleter, self.PositionalCompleter, self.InteractiveCompleter, ): choices, offset = completer(args) if choices is not None: if offset is None: # The choices are already completion.Completion objects. for choice in choices: yield choice else: for choice in sorted(choices): yield completion.Completion(choice, start_position=offset) return
def _Display(c): """Returns the annotated dropdown display spelling of completion c.""" d = c[chop:] if d.startswith('/'): d = d[1:] if d.startswith('/'): d = d[1:] if c.endswith('/') and not c.endswith('://'): c = c[:-1] return completion.Completion(c, display=d, start_position=offset)
def get_completions(self, doc, event): """Yields the completions for doc. Args: doc: A Document instance containing the interactive command line to complete. event: The CompleteEvent that triggered this completion. Yields: Completion instances for doc. """ self.debug.tabs.count().text( 'explicit' if event.completion_requested else 'implicit') args = self.parser.ParseCommand(doc.text_before_cursor) if not args: return self.last = doc.text_before_cursor[-1] if doc.text_before_cursor else '' self.empty = self.last.isspace() self.event = event self.debug.last.text(self.last) self.debug.tokens.text(str(args) .replace("u'", "'") .replace('ArgTokenType.', '') .replace('ArgToken', '')) for completer in ( self.CommandCompleter, self.FlagCompleter, self.PositionalCompleter, self.InteractiveCompleter, ): choices, offset = completer(args) if choices is None: continue self.debug.tag(completer.__name__).count().text(str(len(list(choices)))) if offset is None: # The choices are already completion.Completion objects. for choice in choices: yield choice else: for choice in sorted(choices): yield completion.Completion(choice, start_position=offset) return
def get_completions(self, doc, event): """Yields the completions for doc. Args: doc: A Document instance containing the interactive command line to complete. event: The CompleteEvent that triggered this completion. Yields: Completion instances for doc. """ args = self.parser.ParseCommand(doc.text_before_cursor) if not args: return self.empty = doc.text_before_cursor and doc.text_before_cursor[ -1].isspace() self.event = event for completer in ( self.CommandCompleter, self.FlagCompleter, self.PositionalCompleter, self.InteractiveCompleter, ): choices, offset = completer(args) if choices is not None: for choice in sorted(choices): display = choice if choice.endswith('/'): choice = choice[:-1] yield completion.Completion(choice, display=display, start_position=offset) return if event.completion_requested: # default to path completions choices = self.path_completer.get_completions( document.Document('' if self.empty else args[-1].value), event) if choices: for choice in choices: yield choice return
def MakeUriPathCompletion(cls, value, offset, chop): """Returns the Completion object for a URI path completion value. Args: value: The file/path completion value string. offset: The Completion object offset used for dropdown display. chop: The minimum number of chars to chop from the dropdown items. Returns: The Completion object for a URI path completion value. """ display = value[chop:] if display.startswith('/'): display = display[1:] if display.startswith('/'): display = display[1:] if value.endswith('/') and not value.endswith('://'): value = value[:-1] return completion.Completion(value, display=display, start_position=offset)
def get_completions(self, doc, event): """Yields the completions for doc. Args: doc: A Document instance containing the interactive command line to complete. event: The CompleteEvent that triggered this completion. Yields: Completion instances for doc. """ self.debug.tabs.count().text('@{}:{}'.format( self.cli.command_count, 'explicit' if event.completion_requested else 'implicit')) # TAB on empty line toggles command PREFIX executable completions. if not doc.text_before_cursor and event.completion_requested: if self.DoExecutableCompletions(): self.DisableExecutableCompletions() else: self.EnableExecutableCompletions() return # Parse the arg types from the input buffer. args = self.parser.ParseCommand(doc.text_before_cursor) if not args: return # The default completer order. completers = ( self.CommandCompleter, self.FlagCompleter, self.PositionalCompleter, self.InteractiveCompleter, ) # Command PREFIX token may need a different order. if self.IsPrefixArg(args) and (self.DoExecutableCompletions() or event.completion_requested): completers = (self.InteractiveCompleter, ) self.last = doc.text_before_cursor[-1] if doc.text_before_cursor else '' self.empty = self.last.isspace() self.event = event self.debug.last.text(self.last) self.debug.tokens.text(_PrettyArgs(args)) # Apply the completers in order stopping at the first one that does not # return None. for completer in completers: choices, offset = completer(args) if choices is None: continue self.debug.tag(completer.__name__).count().text(len(list(choices))) if offset is None: # The choices are already completion.Completion objects. for choice in choices: yield choice else: for choice in sorted(choices): yield completion.Completion(choice, start_position=offset) return