Repository: spyder-ide/spyder

Issue:
Autocompletions should not be triggered by backspace
## Problem Description
Autocompletions are triggered when deleting a character. In many cases, the user is strictly trying to delete text instead of inserting any new characters so showing autocompletions, which would insert new characters, is counterproductive.

### What steps reproduce the problem?

There are a couple of manifestations of this problem.

__Case 1: User is deleting a newline to move a line of code up one line__
1. Start with the file
```python
# example_file.py

foo = 'bar'
```
2. Position your cursor in front of `f` on the third line.
3. Hit backspace to move the line to the previous line.

__Expected:__ The line is moved with no other side effects.

__Actual:__ The line is moved and then autocompletions are triggered, showing these faulty completions from Jedi.

![image](https://user-images.githubusercontent.com/3752225/69291141-22dc3900-0bb7-11ea-9660-d0a9b2bd4593.png)

This happens regardless of the value of the `automatic_completions_after_chars` setting.

(This is similar but not the same as #10783)

__Case 2: User is deleting a token__
1. Start with the file
```python
this_message = 'foo'
that_message = 'bar'
print(this_message)
```
2. Start deleting the token `this_message` on line 3 using the backspace key.

__Expected:__ The token can be deleted with no interruptions.

__Actual:__ At a certain point, autocompletions are suggested, which is counter to what the user is trying to do.

![image](https://user-images.githubusercontent.com/3752225/69359808-26ff6980-0c3e-11ea-9133-4eb44df08876.png)

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->
Python 3.7.0 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Darwin 19.0.0 

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
cloudpickle >=0.5.0          :  1.2.2 (OK)
pygments >=2.0               :  2.4.2 (OK)
qtconsole >=4.6.0            :  4.6.0 (OK)
nbconvert >=4.0              :  5.6.1 (OK)
sphinx >=0.6.6               :  2.2.1 (OK)
pylint >=0.25                :  2.4.4 (OK)
psutil >=0.3                 :  5.6.5 (OK)
qtawesome >=0.5.7            :  0.6.0 (OK)
qtpy >=1.5.0                 :  1.9.0 (OK)
pickleshare >=0.4            :  0.7.5 (OK)
zmq >=17                     :  18.1.0 (OK)
chardet >=2.0.0              :  3.0.4 (OK)
numpydoc >=0.6.0             :  0.9.1 (OK)
spyder_kernels >=1.8.0;<2.0.0:  1.8.0 (OK)
qdarkstyle >=2.7             :  2.7 (OK)
atomicwrites >=1.2.0         :  1.3.0 (OK)
diff_match_patch >=20181111  :  20181111 (OK)
watchdog                     :  None (OK)
keyring                      :  None (OK)
pexpect >=4.4.0              :  4.7.0 (OK)
pympler                      :  None (OK)
sympy >=0.7.3                :  None (NOK)
cython >=0.21                :  None (NOK)
IPython >=4.0                :  7.9.0 (OK)
matplotlib >=2.0.0           :  None (NOK)
pandas >=0.13.1              :  None (NOK)
numpy >=1.7                  :  None (NOK)
scipy >=0.17.0               :  None (NOK)
pyls >=0.31.0;<0.32.0        :  0.31.1 (OK)
applaunchservices >=0.1.7    :  0.2.1 (OK)
rtree >=0.8.3                :  0.8.3 (OK)
```



Discussion:

Resolving patch:
diff --git a/spyder/plugins/editor/widgets/codeeditor.py b/spyder/plugins/editor/widgets/codeeditor.py
--- a/spyder/plugins/editor/widgets/codeeditor.py
+++ b/spyder/plugins/editor/widgets/codeeditor.py
@@ -356,6 +356,7 @@ def __init__(self, parent=None):
         # Typing keys / handling on the fly completions
         # See: keyPressEvent
         self._last_key_pressed_text = ''
+        self._last_pressed_key = None
         self._timer_autocomplete = QTimer(self)
         self._timer_autocomplete.setSingleShot(True)
         self._timer_autocomplete.timeout.connect(self._handle_completions)
@@ -3722,6 +3723,7 @@ def insert_text(event):
             insert_text(event)
 
         self._last_key_pressed_text = text
+        self._last_pressed_key = key
         if self.automatic_completions_after_ms == 0:
             self._handle_completions()
 
@@ -3738,6 +3740,12 @@ def _handle_completions(self):
         cursor.select(QTextCursor.WordUnderCursor)
         text = to_text_string(cursor.selectedText())
 
+        key = self._last_pressed_key
+        if key is not None:
+            if key == Qt.Key_Backspace or key == Qt.Key_Return:
+                self._last_pressed_key = None
+                return
+
         # Text might be after a dot '.'
         if text == '':
             cursor.setPosition(pos - 1, QTextCursor.MoveAnchor)
@@ -3764,6 +3772,7 @@ def _handle_completions(self):
                         or '.' in text):
                     self.do_completion(automatic=True)
                     self._last_key_pressed_text = ''
+                    self._last_pressed_key = None
 
     def fix_and_strip_indent(self, comment_or_string=False):
         """Automatically fix indent and strip previous automatic indent."""


Test patch: