Repository: xonsh/xonsh

Issue:
Weird stacking auto-complete 
It happens a lot and is pretty annoying, anyone get this/know a fix?

https://user-images.githubusercontent.com/34655998/153747557-fc8d07f3-78e4-4606-b8f6-415c26a5503d.mp4

Here are my custom environment variables
```
$DOTGLOB = True
$COMPLETIONS_CONFIRM = False
$COMPLETION_IN_THREAD = True
$COMPLETIONS_BRACKETS = False
$COMPLETIONS_DISPLAY = "single"
$CASE_SENSITIVE_COMPLETIONS = False
$AUTO_SUGGEST_IN_COMPLETIONS = True
$UPDATE_COMPLETIONS_ON_KEYPRESS = True
$COMMANDS_CACHE_SAVE_INTERMEDIATE = True
$XONSH_HISTORY_BACKEND = "sqlite"
$XONSH_HISTORY_FILE = "/home/ganer/.xonsh_history"
$XONSH_CACHE_EVERYTHING = True
$XONSH_CTRL_BKSP_DELETION = True
$UPDATE_PROMPT_ON_KEYPRESS = True
$XONSH_COLOR_STYLE = 'default'
$XONSH_STYLE_OVERRIDES['Token.Literal.Number'] = '#1111ff'
```

edit: also on a another note, typing backshashes before most characters cause a error to appear in the console


Discussion:
Can you set `$XONSH_TRACE_COMPLETIONS` and check where does the completions come from?
Got one on camera

https://user-images.githubusercontent.com/34655998/153767136-46a5c6fd-1d61-4062-be6c-ca7d4594ff88.mp4


I have this. I think this is caused by `$AUTO_SUGGEST_IN_COMPLETIONS = True`. When I disabled that, this problem goes away.

Resolving patch:
diff --git a/xonsh/ptk_shell/completer.py b/xonsh/ptk_shell/completer.py
--- a/xonsh/ptk_shell/completer.py
+++ b/xonsh/ptk_shell/completer.py
@@ -72,6 +72,7 @@ def get_completions(self, document, complete_event):
             if sug_comp is None:
                 pass
             elif len(completions) == 0:
+                plen = len(prefix)
                 completions = (sug_comp,)
             else:
                 completions = set(completions)


Test patch:
diff --git a/tests/test_ptk_completer.py b/tests/test_ptk_completer.py
--- a/tests/test_ptk_completer.py
+++ b/tests/test_ptk_completer.py
@@ -54,6 +54,35 @@ def test_rich_completion(completion, lprefix, ptk_completion, monkeypatch, xessi
         assert completions == [ptk_completion]
 
 
+@pytest.mark.parametrize(
+    "completions, document_text, ptk_completion",
+    [
+        (set(), "", "test_completion"),
+        (set(), "test_", "test_completion"),
+        ({RichCompletion("test", 4, "test()", "func")}, "test", "test_completion"),
+    ],
+)
+def test_auto_suggest_completion(completions, document_text, ptk_completion, xession):
+    lprefix = len(document_text)
+
+    xonsh_completer_mock = MagicMock()
+    xonsh_completer_mock.complete.return_value = completions, lprefix
+
+    xession.env["AUTO_SUGGEST_IN_COMPLETIONS"] = True
+
+    ptk_completer = PromptToolkitCompleter(xonsh_completer_mock, None, None)
+    ptk_completer.reserve_space = lambda: None
+    ptk_completer.suggestion_completion = lambda _, __: ptk_completion
+
+    document_mock = MagicMock()
+    document_mock.text = document_text
+    document_mock.current_line = document_text
+    document_mock.cursor_position_col = lprefix
+
+    auto_suggested = list(ptk_completer.get_completions(document_mock, MagicMock()))
+    assert PTKCompletion(ptk_completion, -lprefix) in auto_suggested
+
+
 EXPANSION_CASES = (
     (
         "sanity",