Repository: spyder-ide/spyder
Pull request: PR: Fix showing/hiding completions when backspace is pressed
<!--- Make sure to read the Contributing Guidelines:                   --->
<!--- https://github.com/spyder-ide/spyder/blob/master/CONTRIBUTING.md --->
<!--- and follow PEP 8, PEP 257 and Spyder's code style:               --->
<!--- https://github.com/spyder-ide/spyder/wiki/Dev:-Coding-Style      --->

## Description of Changes


* [ ] Wrote at least one-line docstrings (for any new functions)
* [ ] Added unit test(s) covering the changes (if testable)
<!--- Remember that an image/animation is worth a thousand words! --->
* [ ] Included a screenshot or animation (if affecting the UI, see [Licecap](https://www.cockos.com/licecap/))


<!--- Explain what you've done and why --->
Add counter to backspace and check if the text is not empty before showing the completions widget.

![completions](https://user-images.githubusercontent.com/20992645/81774402-11158780-94b0-11ea-99bd-1c5176c27544.gif)

### Issue(s) Resolved

<!--- List the issue(s) below, in the form "Fixes #1234"; one per line --->

Fixes #12704 


### Affirmation

By submitting this Pull Request or typing my (user)name below,
I affirm the [Developer Certificate of Origin](https://developercertificate.org)
with respect to all commits and content included in this PR,
and understand I am releasing the same under Spyder's MIT (Expat) license.

<!--- TYPE YOUR USER/NAME AFTER THE FOLLOWING: --->
I certify the above statement is true and correct: Steff456

<!--- Thanks for your help making Spyder better for everyone! --->


Problem statement:
Deleting space before a word trigger meaningless completion
Hi spyder devs,
I've find a new way of improving the completion:

## Problem Description
Deleting space before a word trigger meaningless completion.

### What steps reproduce the problem?

1. In a new document type `None`;
2. add a space before it;
3. remove this space.

### What is the expected output? What do you see instead?
I would expect nothing, I see the completion with all possibilities.


## 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. --->

* Spyder version: 4.1.3
* Python version: 3.8

I'm sorry, but this time I don't have time to investigate more.

Hints from issue discussion:
The same problem, when real-time code completion is available
Hi @ElieGouzien and @leavor,

We are going to fix this for spyder 4.1.4 release. Thanks for reporting!

Commit message: add counter to backspace and check if the text is not empty before showing completions

Patch to review:
diff --git a/spyder/plugins/editor/widgets/codeeditor.py b/spyder/plugins/editor/widgets/codeeditor.py
index 91f8dcde599..3c1d2e6c06a 100644
--- a/spyder/plugins/editor/widgets/codeeditor.py
+++ b/spyder/plugins/editor/widgets/codeeditor.py
@@ -364,6 +364,7 @@ def __init__(self, parent=None):
 
         # Typing keys / handling on the fly completions
         # See: keyPressEvent
+        self._count_backspace = 0
         self._last_key_pressed_text = ''
         self._last_pressed_key = None
         self._timer_autocomplete = QTimer(self)
@@ -4025,6 +4026,11 @@ def insert_text(event):
         elif not event.isAccepted():
             insert_text(event)
 
+        if key == Qt.Key_Backspace:
+            self._count_backspace += 1
+        else:
+            self._count_backspace = 0
+
         self._last_key_pressed_text = text
         self._last_pressed_key = key
         if self.automatic_completions_after_ms == 0 and not tab_pressed:
@@ -4057,6 +4063,7 @@ def _handle_completions(self):
             text = to_text_string(cursor.selectedText())
             if text != '.':
                 text = ''
+                self._count_backspace = 0
 
         # WordUnderCursor fails if the cursor is next to a right brace.
         # If the returned text starts with it, we move to the left.
@@ -4067,8 +4074,10 @@ def _handle_completions(self):
 
         self.document_did_change(text)
 
+        is_backspace = (
+            self._count_backspace >= self.automatic_completions_after_chars)
         if (len(text) >= self.automatic_completions_after_chars
-                and self._last_key_pressed_text) or key == Qt.Key_Backspace:
+                and self._last_key_pressed_text or is_backspace):
             # Perform completion on the fly
             if self.automatic_completions and not self.in_comment_or_string():
                 # Variables can include numbers and underscores


Code review comments:
diff_hunk: @@ -4067,8 +4074,10 @@ def _handle_completions(self):
 
         self.document_did_change(text)
 
+        is_backspace = (
+            self._count_backspace >= self.automatic_completions_after_chars)
path: spyder/plugins/editor/widgets/codeeditor.py
text: @user1:
Does this mean that you have to press Backspace at least `automatic_completions_after_chars` to get completions for it?

What happens if you press Backspace only one time?

@author:
This is true! It should be fixed by the next commit