We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Tab completing on empty line with prompt_toolkit kills terminal
Arch Linux with xonsh 0.2.2 and using prompt toolkit.  Hitting TAB on an empty line crashes the terminal (rxvt-unicode).  I can see the traceback for a split second but then the terminal is gone.  

This doesn't happen using readline.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `xonsh/prompt_toolkit_key_bindings.py`
Content:
```
1 """Key bindings for prompt_toolkit xonsh shell."""
2 import builtins
3 
4 from prompt_toolkit.filters import Filter
5 from prompt_toolkit.keys import Keys
6 
7 
8 class TabShouldInsertIndentFilter(Filter):
9     """
10     Filter that is intended to check if <Tab> should insert indent instead of
11     starting autocompletion.
12     It basically just checks if there are only whitespaces before the cursor -
13     if so indent should be inserted, otherwise autocompletion.
14     """
15     def __call__(self, cli):
16         before_cursor = cli.current_buffer.document.current_line_before_cursor
17 
18         return bool(not before_cursor or before_cursor.isspace())
19 
20 
21 def load_xonsh_bindings(key_bindings_manager):
22     """
23     Load custom key bindings.
24     """
25     handle = key_bindings_manager.registry.add_binding
26     env = builtins.__xonsh_env__
27 
28     @handle(Keys.Tab, filter=TabShouldInsertIndentFilter())
29     def _(event):
30         """
31         If there are only whitespaces before current cursor position insert
32         indent instead of autocompleting.
33         """
34         event.cli.current_buffer.insert_text(env['INDENT'])
35 
36 
37
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/xonsh/prompt_toolkit_key_bindings.py b/xonsh/prompt_toolkit_key_bindings.py
--- a/xonsh/prompt_toolkit_key_bindings.py
+++ b/xonsh/prompt_toolkit_key_bindings.py
@@ -15,7 +15,7 @@
     def __call__(self, cli):
         before_cursor = cli.current_buffer.document.current_line_before_cursor
 
-        return bool(not before_cursor or before_cursor.isspace())
+        return bool(before_cursor.isspace())
 
 
 def load_xonsh_bindings(key_bindings_manager):
@@ -31,6 +31,6 @@
         If there are only whitespaces before current cursor position insert
         indent instead of autocompleting.
         """
-        event.cli.current_buffer.insert_text(env['INDENT'])
+        event.cli.current_buffer.insert_text(env.get('INDENT'))

{"golden_diff": "diff --git a/xonsh/prompt_toolkit_key_bindings.py b/xonsh/prompt_toolkit_key_bindings.py\n--- a/xonsh/prompt_toolkit_key_bindings.py\n+++ b/xonsh/prompt_toolkit_key_bindings.py\n@@ -15,7 +15,7 @@\n     def __call__(self, cli):\n         before_cursor = cli.current_buffer.document.current_line_before_cursor\n \n-        return bool(not before_cursor or before_cursor.isspace())\n+        return bool(before_cursor.isspace())\n \n \n def load_xonsh_bindings(key_bindings_manager):\n@@ -31,6 +31,6 @@\n         If there are only whitespaces before current cursor position insert\n         indent instead of autocompleting.\n         \"\"\"\n-        event.cli.current_buffer.insert_text(env['INDENT'])\n+        event.cli.current_buffer.insert_text(env.get('INDENT'))\n", "issue": "Tab completing on empty line with prompt_toolkit kills terminal\nArch Linux with xonsh 0.2.2 and using prompt toolkit.  Hitting TAB on an empty line crashes the terminal (rxvt-unicode).  I can see the traceback for a split second but then the terminal is gone.  \n\nThis doesn't happen using readline.\n\n", "before_files": [{"content": "\"\"\"Key bindings for prompt_toolkit xonsh shell.\"\"\"\nimport builtins\n\nfrom prompt_toolkit.filters import Filter\nfrom prompt_toolkit.keys import Keys\n\n\nclass TabShouldInsertIndentFilter(Filter):\n    \"\"\"\n    Filter that is intended to check if <Tab> should insert indent instead of\n    starting autocompletion.\n    It basically just checks if there are only whitespaces before the cursor -\n    if so indent should be inserted, otherwise autocompletion.\n    \"\"\"\n    def __call__(self, cli):\n        before_cursor = cli.current_buffer.document.current_line_before_cursor\n\n        return bool(not before_cursor or before_cursor.isspace())\n\n\ndef load_xonsh_bindings(key_bindings_manager):\n    \"\"\"\n    Load custom key bindings.\n    \"\"\"\n    handle = key_bindings_manager.registry.add_binding\n    env = builtins.__xonsh_env__\n\n    @handle(Keys.Tab, filter=TabShouldInsertIndentFilter())\n    def _(event):\n        \"\"\"\n        If there are only whitespaces before current cursor position insert\n        indent instead of autocompleting.\n        \"\"\"\n        event.cli.current_buffer.insert_text(env['INDENT'])\n\n\n", "path": "xonsh/prompt_toolkit_key_bindings.py"}], "after_files": [{"content": "\"\"\"Key bindings for prompt_toolkit xonsh shell.\"\"\"\nimport builtins\n\nfrom prompt_toolkit.filters import Filter\nfrom prompt_toolkit.keys import Keys\n\n\nclass TabShouldInsertIndentFilter(Filter):\n    \"\"\"\n    Filter that is intended to check if <Tab> should insert indent instead of\n    starting autocompletion.\n    It basically just checks if there are only whitespaces before the cursor -\n    if so indent should be inserted, otherwise autocompletion.\n    \"\"\"\n    def __call__(self, cli):\n        before_cursor = cli.current_buffer.document.current_line_before_cursor\n\n        return bool(before_cursor.isspace())\n\n\ndef load_xonsh_bindings(key_bindings_manager):\n    \"\"\"\n    Load custom key bindings.\n    \"\"\"\n    handle = key_bindings_manager.registry.add_binding\n    env = builtins.__xonsh_env__\n\n    @handle(Keys.Tab, filter=TabShouldInsertIndentFilter())\n    def _(event):\n        \"\"\"\n        If there are only whitespaces before current cursor position insert\n        indent instead of autocompleting.\n        \"\"\"\n        event.cli.current_buffer.insert_text(env.get('INDENT'))\n\n\n", "path": "xonsh/prompt_toolkit_key_bindings.py"}]}