Repository: spyder-ide/spyder

Issue:
Code completion is added when indenting code
See https://github.com/spyder-ide/spyder/issues/11493#issuecomment-588470235

Let me add that completion is also proposed (with everything possible) when indenting existing line.
To reproduce, just write

```python
x = 1
```

go at the beginning and press [tab].

I guess it is directly linked.

I haven't checked if #11531 also correct this.


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
@@ -3678,6 +3678,7 @@ def keyPressEvent(self, event):
         """Reimplement Qt method."""
         self._start_completion_timer()
 
+        tab_pressed = False
         if self.completions_hint_after_ms > 0:
             self._completions_hint_idle = False
             self._timer_completions_hint.start(self.completions_hint_after_ms)
@@ -3852,6 +3853,7 @@ def insert_text(event):
         elif key == Qt.Key_Tab and not ctrl:
             # Important note: <TAB> can't be called with a QShortcut because
             # of its singular role with respect to widget focus management
+            tab_pressed = True
             if not has_selection and not self.tab_mode:
                 self.intelligent_tab()
             else:
@@ -3860,6 +3862,7 @@ def insert_text(event):
         elif key == Qt.Key_Backtab and not ctrl:
             # Backtab, i.e. Shift+<TAB>, could be treated as a QShortcut but
             # there is no point since <TAB> can't (see above)
+            tab_pressed = True
             if not has_selection and not self.tab_mode:
                 self.intelligent_backtab()
             else:
@@ -3871,7 +3874,7 @@ def insert_text(event):
 
         self._last_key_pressed_text = text
         self._last_pressed_key = key
-        if self.automatic_completions_after_ms == 0:
+        if self.automatic_completions_after_ms == 0 and not tab_pressed:
             self._handle_completions()
 
         if not event.modifiers():
@@ -3889,7 +3892,8 @@ def _handle_completions(self):
 
         key = self._last_pressed_key
         if key is not None:
-            if key in [Qt.Key_Backspace, Qt.Key_Return, Qt.Key_Escape]:
+            if key in [Qt.Key_Backspace, Qt.Key_Return, Qt.Key_Escape,
+                       Qt.Key_Tab, Qt.Key_Backtab]:
                 self._last_pressed_key = None
                 return
 


Test patch:
diff --git a/spyder/plugins/editor/widgets/tests/test_introspection.py b/spyder/plugins/editor/widgets/tests/test_introspection.py
--- a/spyder/plugins/editor/widgets/tests/test_introspection.py
+++ b/spyder/plugins/editor/widgets/tests/test_introspection.py
@@ -231,6 +231,41 @@ def test_automatic_completions(lsp_codeeditor, qtbot):
     code_editor.toggle_code_snippets(True)
 
 
+@pytest.mark.slow
+@pytest.mark.first
+@flaky(max_runs=5)
+def test_automatic_completions_tab_bug(lsp_codeeditor, qtbot):
+    """
+    Test on-the-fly completions.
+
+    Autocompletions sohuld not be invoked when Tab/Backtab is pressed.
+
+    See: spyder-ide/spyder#11625
+    """
+    code_editor, _ = lsp_codeeditor
+    completion = code_editor.completion_widget
+    code_editor.toggle_code_snippets(False)
+
+    code_editor.set_text('x = 1')
+    code_editor.set_cursor_position('sol')
+
+    try:
+        with qtbot.waitSignal(completion.sig_show_completions,
+                              timeout=5000):
+            qtbot.keyPress(code_editor, Qt.Key_Tab)
+        assert False
+    except pytestqt.exceptions.TimeoutError:
+        pass
+
+    try:
+        with qtbot.waitSignal(completion.sig_show_completions,
+                              timeout=5000):
+            qtbot.keyPress(code_editor, Qt.Key_Backtab)
+        assert False
+    except pytestqt.exceptions.TimeoutError:
+        pass
+
+
 @pytest.mark.slow
 @flaky(max_runs=3)
 def test_automatic_completions_parens_bug(lsp_codeeditor, qtbot):