completion_plain.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """A simple completer for the qtconsole"""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from qtpy import QtCore, QtGui, QtWidgets
  5. import ipython_genutils.text as text
  6. class CompletionPlain(QtWidgets.QWidget):
  7. """ A widget for tab completion, navigable by arrow keys """
  8. #--------------------------------------------------------------------------
  9. # 'QObject' interface
  10. #--------------------------------------------------------------------------
  11. def __init__(self, console_widget):
  12. """ Create a completion widget that is attached to the specified Qt
  13. text edit widget.
  14. """
  15. assert isinstance(console_widget._control, (QtWidgets.QTextEdit, QtWidgets.QPlainTextEdit))
  16. super(CompletionPlain, self).__init__()
  17. self._text_edit = console_widget._control
  18. self._console_widget = console_widget
  19. self._text_edit.installEventFilter(self)
  20. def eventFilter(self, obj, event):
  21. """ Reimplemented to handle keyboard input and to auto-hide when the
  22. text edit loses focus.
  23. """
  24. if obj == self._text_edit:
  25. etype = event.type()
  26. if etype in( QtCore.QEvent.KeyPress, QtCore.QEvent.FocusOut ):
  27. self.cancel_completion()
  28. return super(CompletionPlain, self).eventFilter(obj, event)
  29. #--------------------------------------------------------------------------
  30. # 'CompletionPlain' interface
  31. #--------------------------------------------------------------------------
  32. def cancel_completion(self):
  33. """Cancel the completion, reseting internal variable, clearing buffer """
  34. self._console_widget._clear_temporary_buffer()
  35. def show_items(self, cursor, items, prefix_length=0):
  36. """ Shows the completion widget with 'items' at the position specified
  37. by 'cursor'.
  38. """
  39. if not items :
  40. return
  41. self.cancel_completion()
  42. strng = text.columnize(items)
  43. # Move cursor to start of the prefix to replace it
  44. # when a item is selected
  45. cursor.movePosition(QtGui.QTextCursor.Left, n=prefix_length)
  46. self._console_widget._fill_temporary_buffer(cursor, strng, html=False)