def get_completions(self, document, complete_event): """ get completions filtered and colored To filter and color completions on type, we try get jedi completions first and check their type, because `prompt-toolkit.completion.Completion` does not contain type information """ try: script = get_jedi_script_from_document(document, self.get_globals(), self.get_locals()) except: return if script: try: reference = script.get_references()[0] except Exception: reference = "" for c in sorted(script.completions(), key=sort_completions_key): if c.name.startswith(HIDDEN_PREFIXES): if not reference or not reference.description.startswith("_"): continue yield Completion( c.name_with_symbols, len(c.complete) - len(c.name_with_symbols), display=c.name_with_symbols, selected_style="bg:ansidarkgray", style=get_style_for_completion(c), )
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 except KeyError: # Jedi issue: "KeyError: u'a_lambda'." # https://github.com/jonathanslenders/ptpython/issues/89 pass except IOError: # Jedi issue: "IOError: No such file or directory." # https://github.com/jonathanslenders/ptpython/issues/71 pass except AssertionError: # In jedi.parser.__init__.py: 227, in remove_last_newline, # the assertion "newline.value.endswith('\n')" can fail. pass except SystemError: # In jedi.api.helpers.py: 144, in get_stack_at_position # raise SystemError("This really shouldn't happen. There's a bug in Jedi.") 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)
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 except KeyError: # Jedi issue: "KeyError: u'a_lambda'." # https://github.com/jonathanslenders/ptpython/issues/89 pass except IOError: # Jedi issue: "IOError: No such file or directory." # https://github.com/jonathanslenders/ptpython/issues/71 pass except AssertionError: # In jedi.parser.__init__.py: 227, in remove_last_newline, # the assertion "newline.value.endswith('\n')" can fail. pass except SystemError: # In jedi.api.helpers.py: 144, in get_stack_at_position # raise SystemError("This really shouldn't happen. There's a bug in Jedi.") pass except NotImplementedError: # See: https://github.com/jonathanslenders/ptpython/issues/223 pass except Exception: # Supress all other Jedi exceptions. 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)
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 except KeyError: # Jedi issue: "KeyError: u'a_lambda'." # https://github.com/jonathanslenders/ptpython/issues/89 pass except IOError: # Jedi issue: "IOError: No such file or directory." # https://github.com/jonathanslenders/ptpython/issues/71 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)
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 except KeyError: # Jedi issue: "KeyError: u'a_lambda'." # https://github.com/jonathanslenders/ptpython/issues/89 pass except IOError: # Jedi issue: "IOError: No such file or directory." # https://github.com/jonathanslenders/ptpython/issues/71 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)
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: 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.settings.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') self.cli.buffers['docstring'].reset( initial_document=Document(string, cursor_position=0)) else: self.cli.buffers['docstring'].reset() self.cli.request_redraw() else: on_input_timeout()
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: 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: Document, complete_event: CompleteEvent) -> Iterable[Completion]: """ Get Python completions. """ # Do dictionary key completions. if self.get_enable_dictionary_completion(): has_dict_completions = False for c in self.dictionary_completer.get_completions( document, complete_event): if c.text not in "[.": # If we get the [ or . completion, still include the other # completions. has_dict_completions = True yield c if has_dict_completions: return # Do Path completions (if there were no dictionary 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: jedi_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 except KeyError: # Jedi issue: "KeyError: u'a_lambda'." # https://github.com/jonathanslenders/ptpython/issues/89 pass except IOError: # Jedi issue: "IOError: No such file or directory." # https://github.com/jonathanslenders/ptpython/issues/71 pass except AssertionError: # In jedi.parser.__init__.py: 227, in remove_last_newline, # the assertion "newline.value.endswith('\n')" can fail. pass except SystemError: # In jedi.api.helpers.py: 144, in get_stack_at_position # raise SystemError("This really shouldn't happen. There's a bug in Jedi.") pass except NotImplementedError: # See: https://github.com/jonathanslenders/ptpython/issues/223 pass except Exception: # Supress all other Jedi exceptions. pass else: for jc in jedi_completions: yield Completion( jc.name_with_symbols, len(jc.complete) - len(jc.name_with_symbols), display=jc.name_with_symbols, style=_get_style_for_name(jc.name_with_symbols), )
def get_completions( self, document: Document, complete_event: CompleteEvent ) -> Iterable[Completion]: script = get_jedi_script_from_document( document, self.get_locals(), self.get_globals() ) if script: try: jedi_completions = script.complete( column=document.cursor_position_col, line=document.cursor_position_row + 1, ) 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 except KeyError: # Jedi issue: "KeyError: u'a_lambda'." # https://github.com/jonathanslenders/ptpython/issues/89 pass except IOError: # Jedi issue: "IOError: No such file or directory." # https://github.com/jonathanslenders/ptpython/issues/71 pass except AssertionError: # In jedi.parser.__init__.py: 227, in remove_last_newline, # the assertion "newline.value.endswith('\n')" can fail. pass except SystemError: # In jedi.api.helpers.py: 144, in get_stack_at_position # raise SystemError("This really shouldn't happen. There's a bug in Jedi.") pass except NotImplementedError: # See: https://github.com/jonathanslenders/ptpython/issues/223 pass except Exception: # Supress all other Jedi exceptions. pass else: # Move function parameters to the top. jedi_completions = sorted( jedi_completions, key=lambda jc: ( # Params first. jc.type != "param", # Private at the end. jc.name.startswith("_"), # Then sort by name. jc.name_with_symbols.lower(), ), ) for jc in jedi_completions: if jc.type == "function": suffix = "()" else: suffix = "" if jc.type == "param": suffix = "..." yield Completion( jc.name_with_symbols, len(jc.complete) - len(jc.name_with_symbols), display=jc.name_with_symbols + suffix, display_meta=jc.type, style=_get_style_for_jedi_completion(jc), )