diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-17 17:28:16 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-07-17 17:28:16 -0400 |
commit | b0116dce61dc12207fa96c6a53d05d2301a9fe7a (patch) | |
tree | ded06d4b35ca775185661597122ece58076d5fe2 /cmd2/cmd2.py | |
parent | 2b38463868b5acf9b1d64fb480aba683fe56d6d7 (diff) | |
download | cmd2-git-b0116dce61dc12207fa96c6a53d05d2301a9fe7a.tar.gz |
Enabled tab completion of multiline commands
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 9e5f5d56..7a9245ab 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -428,6 +428,9 @@ class Cmd(cmd.Cmd): # Used to keep track of whether a continuation prompt is being displayed self._at_continuation_prompt = False + # The multiline command currently being typed which is used to tab complete multiline commands. + self._multiline_in_progress = '' + # The error that prints when no help information can be found self.help_error = "No help on {}" @@ -1362,15 +1365,25 @@ class Cmd(cmd.Cmd): unclosed_quote = '' self._reset_completion_defaults() - # lstrip the original line - orig_line = readline.get_line_buffer() - line = orig_line.lstrip() - stripped = len(orig_line) - len(line) + # Check if we are completing a multiline command + if self._at_continuation_prompt: + # lstrip and prepend the previously typed portion of this multiline command + lstripped_previous = self._multiline_in_progress.lstrip() + line = lstripped_previous + readline.get_line_buffer() + + # Increment the indexes to account for the prepended text + begidx = len(lstripped_previous) + readline.get_begidx() + endidx = len(lstripped_previous) + readline.get_endidx() + else: + # lstrip the original line + orig_line = readline.get_line_buffer() + line = orig_line.lstrip() + num_stripped = len(orig_line) - len(line) - # Calculate new indexes for the stripped line. If the cursor is at a position before the end of a - # line of spaces, then the following math could result in negative indexes. Enforce a max of 0. - begidx = max(readline.get_begidx() - stripped, 0) - endidx = max(readline.get_endidx() - stripped, 0) + # Calculate new indexes for the stripped line. If the cursor is at a position before the end of a + # line of spaces, then the following math could result in negative indexes. Enforce a max of 0. + begidx = max(readline.get_begidx() - num_stripped, 0) + endidx = max(readline.get_endidx() - num_stripped, 0) # Shortcuts are not word break characters when tab completing. Therefore shortcuts become part # of the text variable if there isn't a word break, like a space, after it. We need to remove it @@ -1854,15 +1867,19 @@ class Cmd(cmd.Cmd): # - a multiline command with unclosed quotation marks try: self._at_continuation_prompt = True - newline = self._pseudo_raw_input(self.continuation_prompt) - if newline == 'eof': + + # Save the command line up to this point + self._multiline_in_progress = line + '\n' + + nextline = self._pseudo_raw_input(self.continuation_prompt) + if nextline == 'eof': # they entered either a blank line, or we hit an EOF # for some other reason. Turn the literal 'eof' # into a blank line, which serves as a command # terminator - newline = '\n' - self.poutput(newline) - line = '{}\n{}'.format(statement.raw, newline) + nextline = '\n' + self.poutput(nextline) + line = '{}{}'.format(self._multiline_in_progress, nextline) except KeyboardInterrupt as ex: if self.quit_on_sigint: raise ex |