Cursor moves position after deleting a character and changing line
See original GitHub issueIssue Report Checklist
- Searched the issues page for similar reports
- Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
- Reproduced the issue after updating with
conda update spyder
(orpip
, if not using Anaconda) - Could not reproduce inside
jupyter qtconsole
(if console-related) - Tried basic troubleshooting (if a bug/error)
- Restarted Spyder
- Reset preferences with
spyder --reset
- Reinstalled the latest version of Anaconda
- Tried the other applicable steps from the Troubleshooting Guide
- Completed the Problem Description, Steps to Reproduce and Version sections below
Problem Description
When replacing a simple string in selected text with Replace selection
, Spyder takes a long time to replace it and to return control to the user.
What steps reproduce the problem?
- Open a new Python file (or use
temp.py
or any existing Python file) - Write three lines with
# Spyder is great
(each in its own line) - Move the cursor in the first line in front of
great
- Press
Del
to delete the characterg
from the wordgreat
- Press the key
Cursor down
once
What is the expected output? What do you see instead?
I would expect that the cursor stays in the position before the character g
in the next line, but it moves between g
and r
.
If I delete the character g
with BACKSPACE
, and move down one line, the cursor stays in the position between g
and r
, as expected.
Versions
- Spyder version: 4.1.2
- Python version: 3.6.9. 64-bit
- Qt version: 5.12.7
- PyQt version: 5.12.3
- Operating System name/version: Linux Mint 19.3 Cinnamon
Dependencies
# Mandatory:
atomicwrites >=1.2.0 : 1.3.0 (OK)
chardet >=2.0.0 : 3.0.4 (OK)
cloudpickle >=0.5.0 : 1.3.0 (OK)
diff_match_patch >=20181111 : 20181111 (OK)
intervaltree : None (OK)
IPython >=4.0 : 7.13.0 (OK)
jedi =0.15.2 : 0.15.2 (OK)
keyring : None (OK)
nbconvert >=4.0 : 5.6.1 (OK)
numpydoc >=0.6.0 : 0.9.2 (OK)
parso =0.5.2 : 0.5.2 (OK)
pexpect >=4.4.0 : 4.8.0 (OK)
pickleshare >=0.4 : 0.7.5 (OK)
psutil >=5.3 : 5.7.0 (OK)
pygments >=2.0 : 2.6.1 (OK)
pylint >=0.25 : 2.4.4 (OK)
pyls >=0.31.9;<0.32.0 : 0.31.9 (OK)
qdarkstyle >=2.8 : 2.8 (OK)
qtawesome >=0.5.7 : 0.7.0 (OK)
qtconsole >=4.6.0 : 4.7.1 (OK)
qtpy >=1.5.0 : 1.9.0 (OK)
sphinx >=0.6.6 : 2.4.4 (OK)
spyder_kernels >=1.9.0;<1.10.0 : 1.9.0 (OK)
watchdog : None (OK)
xdg >=0.26 : 0.26 (OK)
zmq >=17 : 19.0.0 (OK)
# Optional:
cython >=0.21 : None (OK)
matplotlib >=2.0.0 : None (OK)
numpy >=1.7 : 1.18.2 (OK)
pandas >=0.13.1 : None (OK)
scipy >=0.17.0 : 1.4.1 (OK)
sympy >=0.7.3 : None (OK)
Issue Analytics
- State:
- Created 3 years ago
- Comments:22 (19 by maintainers)
Top Results From Across the Web
Cursor jumps to the end of the document when deleting a line ...
Cursor jumps to the end of the document when deleting a line in Word and have Track Changes enabled (Word for Windows).
Read more >Cursor moves to end of the contenteditable div when ...
So the cursor can position itself only on an editable place which is either at the beginning of an editable element( div in...
Read more >Restoring cursor to position before delete after Undo
Move your cursor over 7 in normal mode, type d4h , press u . You're back where you started but the cursor is...
Read more >[Chapter 2] 2.2 Moving the Cursor
In command mode you can position the cursor anywhere in the file. Since you begin all basic edits (changing, deleting, and copying text)...
Read more >The Curious Case of Cursor Jumping - Mutually Human
Contrarily, when code programmatically changes the value of a text field the browser resets the cursor position. In fact, in some browsers, ahem...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Indeed, this is due to how the QPlainTextEdit widget behaves. Here’s a small PyQt5 application where the behaviour can be reproduced:
However, the Spyder CodeEditor widget already overrides keyboard input in https://github.com/spyder-ide/spyder/blob/9c84cd89625d9ad07f54810588ae6464fa4efef1/spyder/plugins/editor/widgets/codeeditor.py#L4254 so fixing this is easy. It suffices to replace the lines https://github.com/spyder-ide/spyder/blob/9c84cd89625d9ad07f54810588ae6464fa4efef1/spyder/plugins/editor/widgets/codeeditor.py#L4365 and https://github.com/spyder-ide/spyder/blob/9c84cd89625d9ad07f54810588ae6464fa4efef1/spyder/plugins/editor/widgets/codeeditor.py#L4386 by
self.stdkey_backspace()
: https://github.com/spyder-ide/spyder/blob/9c84cd89625d9ad07f54810588ae6464fa4efef1/spyder/plugins/editor/widgets/base.py#L933-L937However, Spyder also has a similarly annoying behaviour with the “Delete” key that is not reproduced by the PyQt snippet above. I’ll try to track down the reason for that before making a PR. But a similar workaround should be possible.
For some reason (platform compatibility?), the Delete-key is handled as a shortcut. The problem disappears if I disable the “delete” shortcut in Tools > Preferences > Keyboard shortcuts.
The responsible piece of code is: https://github.com/spyder-ide/spyder/blob/9c84cd89625d9ad07f54810588ae6464fa4efef1/spyder/plugins/editor/widgets/codeeditor.py#L2218-L2227
Apparently QPlainTextEdit handles Delete events differently than the above. Indeed, if I manually perform the steps above in a QPlainTextEdit I can reproduce the behaviour. The recording below shows the behaviour of three different methods of character deletion in a QPlainTextEdit (same snippet as I posted above). In each case I press up/down immediately after pressing backspace/delete.
Actually, much larger jumps can be produced:
The reason is most likely that the QPlainTextEdit remembers the rightmost position the cursor was at just before the text is deleted and wants to place the cursor there again. So we just need to clear this memory. The simplest method I’ve found so far is
I’ll go ahead and make a PR now.
EDIT: The backspace issue (which likely has the same underlying cause as the delete issue) was reported as QTBUG-35861 seven years ago.