Repository: xonsh/xonsh

Issue:
Error on Autocomplete using @()
Error trying to use autocomplete with undefined variables on @()

![imagen](https://user-images.githubusercontent.com/22831024/131755616-2eb3186e-a4c7-4212-a599-a9b77b833d92.png)

## xonfig

<details>

```
+------------------+----------------------+
| xonsh            | 0.10.1               |
| Git SHA          | e9b12c8b             |
| Commit Date      | Jul 24 22:47:37 2021 |
| Python           | 3.8.10               |
| PLY              | 3.11                 |
| have readline    | True                 |
| prompt toolkit   | 3.0.20               |
| shell type       | prompt_toolkit       |
| history backend  | json                 |
| pygments         | 2.10.0               |
| on posix         | True                 |
| on linux         | True                 |
| distro           | ubuntu               |
| on wsl           | False                |
| on darwin        | False                |
| on windows       | False                |
| on cygwin        | False                |
| on msys2         | False                |
| is superuser     | False                |
| default encoding | utf-8                |
| xonsh encoding   | utf-8                |
| encoding errors  | surrogateescape      |
| on jupyter       | False                |
| jupyter kernel   | None                 |
| xontrib 1        | add_variable         |
| xontrib 2        | base16_shell         |
+------------------+----------------------+
```

</details>

## Expected
Do not launch autocomplete and neither do the autocomplete menu because the variable does not exist

### Traceback (if applicable)

<details>

```
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/xonsh/__amalgam__.py", line 9290, in generate_completions
    out = func(completion_context)
  File "/usr/local/lib/python3.8/dist-packages/xonsh/completers/__amalgam__.py", line 1426, in complete_path
    prefix = line.rsplit(" ", 1)[1]
IndexError: list index out of range
Completer complete_path raises exception when gets old_args=('@(noexi', '@(noexi', 0, 7) / completion_context=CompletionContext(command=None, python=PythonContext('noexi', 5, is_sub_expression=True)):
list index out of range
```

</details>

## Steps to Reproduce
1. Launch new terminal
2. Type `@(` followed by the name of an undefined variable
3. Press Tab

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**



Discussion:

Resolving patch:
diff --git a/xonsh/completers/path.py b/xonsh/completers/path.py
--- a/xonsh/completers/path.py
+++ b/xonsh/completers/path.py
@@ -362,7 +362,7 @@ def complete_path(context):
     elif context.python:
         line = context.python.prefix
         # simple prefix _complete_path_raw will handle gracefully:
-        prefix = line.rsplit(" ", 1)[1]
+        prefix = line.rsplit(" ", 1)[-1]
         return _complete_path_raw(prefix, line, len(line) - len(prefix), len(line), {})
     return set(), 0
 


Test patch:
diff --git a/tests/test_path_completers.py b/tests/test_path_completers.py
--- a/tests/test_path_completers.py
+++ b/tests/test_path_completers.py
@@ -71,3 +71,18 @@ def test_path_from_partial_string(prefix):
     else:
         expected = (f"{quote}{string}{quote}", string, f"{prefix}{quote}", quote)
     assert out == expected
+
+
+@pytest.mark.parametrize("num_args", (0, 1, 2, 3))
+def test_path_in_python_code(num_args, completion_context_parse):
+    with tempfile.NamedTemporaryFile(prefix="long_name") as tmp:
+        args = []
+        if num_args:
+            args = ["blah"] * 3 + [tmp.name[:-2]]
+            args = args[-num_args:]
+
+        inner_line = " ".join(map(repr, args))
+        exp = xcp.complete_path(completion_context_parse(inner_line, len(inner_line)))
+        line = "@(" + inner_line
+        out = xcp.complete_path(completion_context_parse(line, len(line)))
+        assert out == exp