Skip to content

Commit 8ee003b

Browse files
committed
Use additional cursor to track insert position for text insert
Instead of using the main cursor (which the user can control by clicking), create a dedicated cursor `_insert_text_cursor` to track the position of where text should be inserted.
1 parent 96ab4e8 commit 8ee003b

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

qtconsole/console_widget.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ def __init__(self, parent=None, **kw):
297297
self._reading_callback = None
298298
self._tab_width = 4
299299

300+
# Cursor position of where to insert text.
301+
# Control characters allow this to move around on the current line.
302+
self._insert_text_cursor = self._control.textCursor()
303+
300304
# List of strings pending to be appended as plain text in the widget.
301305
# The text is not immediately inserted when available to not
302306
# choke the Qt event loop with paint events for the widget in
@@ -695,6 +699,9 @@ def do_execute(self, source, complete, indent):
695699
# effect when using a QTextEdit. I believe this is a Qt bug.
696700
self._control.moveCursor(QtGui.QTextCursor.End)
697701

702+
# Advance where text is inserted
703+
self._insert_text_cursor.movePosition(QtGui.QTextCursor.End)
704+
698705
def export_html(self):
699706
""" Shows a dialog to export HTML/XML in various formats.
700707
"""
@@ -712,6 +719,9 @@ def _finalize_input_request(self):
712719
self._append_before_prompt_cursor.setPosition(
713720
self._get_end_cursor().position())
714721

722+
self._insert_text_cursor.setPosition(
723+
self._get_end_cursor().position())
724+
715725
# The maximum block count is only in effect during execution.
716726
# This ensures that _prompt_pos does not become invalid due to
717727
# text truncation.
@@ -998,7 +1008,7 @@ def _append_custom(self, insert, input, before_prompt=False, *args, **kwargs):
9981008
current prompt, if there is one.
9991009
"""
10001010
# Determine where to insert the content.
1001-
cursor = self._control.textCursor()
1011+
cursor = self._insert_text_cursor
10021012
if before_prompt and (self._reading or not self._executing):
10031013
self._flush_pending_stream()
10041014
cursor._insert_mode=True
@@ -1009,6 +1019,11 @@ def _append_custom(self, insert, input, before_prompt=False, *args, **kwargs):
10091019

10101020
# Perform the insertion.
10111021
result = insert(cursor, input, *args, **kwargs)
1022+
1023+
# Remove insert mode tag
1024+
if hasattr(cursor, "_insert_mode"):
1025+
del cursor._insert_mode
1026+
10121027
return result
10131028

10141029
def _append_block(self, block_format=None, before_prompt=False):
@@ -1670,7 +1685,7 @@ def _flush_pending_stream(self):
16701685
text = self._get_last_lines_from_list(text, buffer_size)
16711686
text = ''.join(text)
16721687
t = time.time()
1673-
self._insert_plain_text(self._control.textCursor(), text, flush=True)
1688+
self._insert_plain_text(self._insert_text_cursor, text, flush=True)
16741689
# Set the flush interval to equal the maximum time to update text.
16751690
self._pending_text_flush_interval.setInterval(
16761691
int(max(100, (time.time() - t) * 1000))
@@ -2182,7 +2197,6 @@ def _insert_plain_text(self, cursor, text, flush=False):
21822197
else:
21832198
cursor.insertText(text)
21842199
cursor.endEditBlock()
2185-
self._control.setTextCursor(cursor)
21862200

21872201
if should_autoscroll:
21882202
self._scroll_to_end()

0 commit comments

Comments
 (0)