int CommandObjectMultiword::HandleCompletion(Args &input, int &cursor_index,
                                             int &cursor_char_position,
                                             int match_start_point,
                                             int max_return_elements,
                                             bool &word_complete,
                                             StringList &matches) {
  // Any of the command matches will provide a complete word, otherwise the
  // individual
  // completers will override this.
  word_complete = true;

  const char *arg0 = input.GetArgumentAtIndex(0);
  if (cursor_index == 0) {
    AddNamesMatchingPartialString(m_subcommand_dict, arg0, matches);

    if (matches.GetSize() == 1 && matches.GetStringAtIndex(0) != nullptr &&
        strcmp(arg0, matches.GetStringAtIndex(0)) == 0) {
      StringList temp_matches;
      CommandObject *cmd_obj = GetSubcommandObject(arg0, &temp_matches);
      if (cmd_obj != nullptr) {
        if (input.GetArgumentCount() == 1) {
          word_complete = true;
        } else {
          matches.DeleteStringAtIndex(0);
          input.Shift();
          cursor_char_position = 0;
          input.AppendArgument("");
          return cmd_obj->HandleCompletion(
              input, cursor_index, cursor_char_position, match_start_point,
              max_return_elements, word_complete, matches);
        }
      }
    }
    return matches.GetSize();
  } else {
    CommandObject *sub_command_object = GetSubcommandObject(arg0, &matches);
    if (sub_command_object == nullptr) {
      return matches.GetSize();
    } else {
      // Remove the one match that we got from calling GetSubcommandObject.
      matches.DeleteStringAtIndex(0);
      input.Shift();
      cursor_index--;
      return sub_command_object->HandleCompletion(
          input, cursor_index, cursor_char_position, match_start_point,
          max_return_elements, word_complete, matches);
    }
  }
}
Esempio n. 2
0
int
CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
                                             int &cursor_index,
                                             int &cursor_char_position,
                                             int match_start_point,
                                             int max_return_elements,
                                             bool &word_complete,
                                             StringList &matches)
{
    int num_command_matches = 0;
    bool look_for_subcommand = false;
    
    // For any of the command completions a unique match will be a complete word.
    word_complete = true;

    if (cursor_index == -1)
    {
        // We got nothing on the command line, so return the list of commands
        bool include_aliases = true;
        num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
    }
    else if (cursor_index == 0)
    {
        // The cursor is in the first argument, so just do a lookup in the dictionary.
        CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
        num_command_matches = matches.GetSize();

        if (num_command_matches == 1
            && cmd_obj && cmd_obj->IsMultiwordObject()
            && matches.GetStringAtIndex(0) != NULL
            && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
        {
            look_for_subcommand = true;
            num_command_matches = 0;
            matches.DeleteStringAtIndex(0);
            parsed_line.AppendArgument ("");
            cursor_index++;
            cursor_char_position = 0;
        }
    }

    if (cursor_index > 0 || look_for_subcommand)
    {
        // We are completing further on into a commands arguments, so find the command and tell it
        // to complete the command.
        // First see if there is a matching initial command:
        CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
        if (command_object == NULL)
        {
            return 0;
        }
        else
        {
            parsed_line.Shift();
            cursor_index--;
            num_command_matches = command_object->HandleCompletion (*this,
                                                                    parsed_line, 
                                                                    cursor_index, 
                                                                    cursor_char_position,
                                                                    match_start_point, 
                                                                    max_return_elements,
                                                                    word_complete, 
                                                                    matches);
        }
    }

    return num_command_matches;

}