title: How to reuse completions from PathCompleter in prompt_toolkit
body: <p>I am creating a REPL tool for my project that (simplified for clarity) either directly executes entered commands or (if a command ".x some/path/to/file" is entered) reads and executes them from file. My question is related to  auto-completing the user input (using prompt_toolkit).</p>
  
  <p>I have something like (minimum executable example):</p>
  
  <pre><code>import prompt_toolkit
  from prompt_toolkit.completion import Completer, Completion
  from prompt_toolkit.document import Document
  from prompt_toolkit.contrib.completers import PathCompleter
  
  
  class CommandCompleter(Completer):
      def __init__(self):
          self.path_completer = PathCompleter()
          self.commands = [".x", "command1", "command2"]
  
      def get_completions(self, document, complete_event):
          if document.text.startswith(".x "):
              sub_doc = Document(document.text[3:])
              yield from (Completion(cmd.text, -document.cursor_position)
  #                                  ????????  ?????????????????????????
                          for cmd
                          in self.path_completer.get_completions(sub_doc, complete_event))
  #                                                              ???????
          else:
              yield from (Completion(cmd, -document.cursor_position)
                          for cmd in self.commands
                          if cmd.startswith(document.text))
  
  if __name__ == "__main__":
      while True:
          other_args = {}
          input = prompt_toolkit.prompt("&gt;&gt;&gt; ", completer=CommandCompleter(), **other_args)
          # Do something with input (omitted)
  </code></pre>
  
  <p>The second if-branch (for commands) works correctly but I don't know how to properly call the <code>PathCompleter.get_completions()</code> method and reconstruct the <code>Completion</code> objects from its result (where the ???'s are) in the first branch. The trick is that I am using the completion only for a part of the input and various sub-stringing, position calculations etc. did not (yet) lead to the satisfactory behaviour (i.e. offering the paths and constructing the correct input line).</p>
  
  <p>I will definitely go on searching but if anyone knows how to rewrite this, it would be very useful.</p>
  
  <p>Note: <code>yield from self.path_completer.get_completions(document, complete_event)</code> would be used if the whole input would be just the path (and this works correctly).</p>