Skip to content

Commit

Permalink
Merge b51f13e into 2a32d06
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz committed Jul 16, 2019
2 parents 2a32d06 + b51f13e commit c1b9831
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
5 changes: 4 additions & 1 deletion qtconsole/completion_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,15 @@ def select_right(self):
r, c = self._index
self._select_index(r, c+1)

def show_items(self, cursor, items):
def show_items(self, cursor, items, prefix_length=0):
""" Shows the completion widget with 'items' at the position specified
by 'cursor'.
"""
if not items :
return
# Move cursor to start of the prefix to replace it
# when a item is selected
cursor.movePosition(QtGui.QTextCursor.Left, n=prefix_length)
self._start_position = cursor.position()
self._consecutive_tab = 1
# Calculate the number of characters available.
Expand Down
5 changes: 4 additions & 1 deletion qtconsole/completion_plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ def cancel_completion(self):
self._console_widget._clear_temporary_buffer()


def show_items(self, cursor, items):
def show_items(self, cursor, items, prefix_length=0):
""" Shows the completion widget with 'items' at the position specified
by 'cursor'.
"""
if not items :
return
self.cancel_completion()
strng = text.columnize(items)
# Move cursor to start of the prefix to replace it
# when a item is selected
cursor.movePosition(QtGui.QTextCursor.Left, n=prefix_length)
self._console_widget._fill_temporary_buffer(cursor, strng, html=False)
27 changes: 22 additions & 5 deletions qtconsole/completion_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, console_widget):
"""
text_edit = console_widget._control
assert isinstance(text_edit, (QtGui.QTextEdit, QtGui.QPlainTextEdit))
super(CompletionWidget, self).__init__()
super(CompletionWidget, self).__init__(parent=console_widget)

self._text_edit = text_edit
self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
Expand Down Expand Up @@ -94,15 +94,19 @@ def showEvent(self, event):
# 'CompletionWidget' interface
#--------------------------------------------------------------------------

def show_items(self, cursor, items):
def show_items(self, cursor, items, prefix_length=0):
""" Shows the completion widget with 'items' at the position specified
by 'cursor'.
"""
text_edit = self._text_edit
point = text_edit.cursorRect(cursor).bottomRight()
point = text_edit.mapToGlobal(point)
self.clear()
self.addItems(items)
for item in items:
list_item = QtGui.QListWidgetItem()
list_item.setData(QtCore.Qt.UserRole, item)
list_item.setText(item.split('.')[-1])
self.addItem(list_item)
height = self.sizeHint().height()
screen_rect = QtGui.QApplication.desktop().availableGeometry(self)
if (screen_rect.size().height() + screen_rect.y() -
Expand All @@ -112,6 +116,10 @@ def show_items(self, cursor, items):
w = (self.sizeHintForColumn(0) +
self.verticalScrollBar().sizeHint().width())
self.setGeometry(point.x(), point.y(), w, height)

# Move cursor to start of the prefix to replace it
# when a item is selected
cursor.movePosition(QtGui.QTextCursor.Left, n=prefix_length)
self._start_position = cursor.position()
self.setCurrentRow(0)
self.raise_()
Expand All @@ -124,7 +132,8 @@ def show_items(self, cursor, items):
def _complete_current(self):
""" Perform the completion with the currently selected item.
"""
self._current_text_cursor().insertText(self.currentItem().text())
text = self.currentItem().data(QtCore.Qt.UserRole)
self._current_text_cursor().insertText(text)
self.hide()

def _current_text_cursor(self):
Expand All @@ -138,8 +147,16 @@ def _current_text_cursor(self):
return cursor

def _update_current(self):
""" Updates the current item based on the current text.
""" Updates the current item based on the current text and the
position of the widget.
"""
# Update widget position
cursor = self._text_edit.textCursor()
point = self._text_edit.cursorRect(cursor).bottomRight()
position = self._text_edit.mapToGlobal(point)
self.move(position)

# Update current item
prefix = self._current_text_cursor().selection().toPlainText()
if prefix:
items = self.findItems(prefix, (QtCore.Qt.MatchStartsWith |
Expand Down
5 changes: 2 additions & 3 deletions qtconsole/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,9 +1022,8 @@ def _complete_with_items(self, cursor, items):
cursor.insertText(prefix)
current_pos = cursor.position()

cursor.movePosition(QtGui.QTextCursor.Left, n=len(prefix))
self._completion_widget.show_items(cursor, items)

self._completion_widget.show_items(cursor, items,
prefix_length=len(prefix))

def _fill_temporary_buffer(self, cursor, text, html=False):
"""fill the area below the active editting zone with text"""
Expand Down

0 comments on commit c1b9831

Please sign in to comment.