def run(): script = get_jedi_script_from_document( document, self.get_locals(), self.get_globals()) # Show signatures in help text. if script: try: signatures = script.call_signatures() except ValueError: # e.g. in case of an invalid \\x escape. signatures = [] except Exception: # Sometimes we still get an exception (TypeError), because # of probably bugs in jedi. We can silence them. # See: https://github.com/davidhalter/jedi/issues/492 signatures = [] else: # Try to access the params attribute just once. For Jedi # signatures containing the keyword-only argument star, # this will crash when retrieving it the first time with # AttributeError. Every following time it works. # See: https://github.com/jonathanslenders/ptpython/issues/47 # https://github.com/davidhalter/jedi/issues/598 try: if signatures: signatures[0].params except AttributeError: pass else: signatures = [] self._get_signatures_thread_running = False # Set signatures and redraw if the text didn't change in the # meantime. Otherwise request new signatures. if buffer.text == document.text: self.signatures = signatures # Set docstring in docstring buffer. if signatures: string = signatures[0].docstring() if not isinstance(string, six.text_type): string = string.decode('utf-8') cli.buffers['docstring'].reset( initial_document=Document(string, cursor_position=0)) else: cli.buffers['docstring'].reset() cli.request_redraw() else: self._on_input_timeout(cli)
def get_completions(self, document, complete_event): """ Get Python completions. """ # Do Path completions if complete_event.completion_requested or self._complete_path_while_typing( document): for c in self._path_completer.get_completions(document, complete_event): yield c # If we are inside a string, Don't do Jedi completion. if self._path_completer_grammar.match(document.text_before_cursor): return # Do Jedi Python completions. if complete_event.completion_requested or self._complete_python_while_typing( document): script = get_jedi_script_from_document( document, self.get_locals(), self.get_globals()) if script: try: completions = script.completions() except TypeError: # Issue #9: bad syntax causes completions() to fail in jedi. # https://github.com/jonathanslenders/python-prompt-toolkit/issues/9 pass except UnicodeDecodeError: # Issue #43: UnicodeDecodeError on OpenBSD # https://github.com/jonathanslenders/python-prompt-toolkit/issues/43 pass except AttributeError: # Jedi issue #513: # https://github.com/davidhalter/jedi/issues/513 pass except ValueError: # Jedi issue: "ValueError: invalid \x escape" pass else: for c in completions: yield Completion(c.name_with_symbols, len(c.complete) - len(c.name_with_symbols), display=c.name_with_symbols)