Replaced more boilerplate code with CompletionRequest (NFC)

Summary:
As suggested in D48796, this patch replaces even more internal calls that were using the old
completion API style with a single CompletionRequest. In some cases we also pass an option
vector/index, but as we don't always have this information, it currently is not part of the
CompletionRequest class.

The constructor of the CompletionRequest is now also more sensible. You only pass the
user input, cursor position and your list of matches to the request and the rest will be
inferred (using the same code we used before to calculate this). You also have to pass these
match window parameters to it, even though they are unused right now.

The patch shouldn't change any behavior.

Reviewers: jingham

Reviewed By: jingham

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D48976

llvm-svn: 337031
diff --git a/lldb/source/Interpreter/Options.cpp b/lldb/source/Interpreter/Options.cpp
index 1840598..f475897 100644
--- a/lldb/source/Interpreter/Options.cpp
+++ b/lldb/source/Interpreter/Options.cpp
@@ -647,12 +647,10 @@
   return options_are_valid;
 }
 
-bool Options::HandleOptionCompletion(
-    Args &input, OptionElementVector &opt_element_vector, int cursor_index,
-    int char_pos, int match_start_point, int max_return_elements,
-    CommandInterpreter &interpreter, bool &word_complete,
-    lldb_private::StringList &matches) {
-  word_complete = true;
+bool Options::HandleOptionCompletion(CompletionRequest &request,
+                                     OptionElementVector &opt_element_vector,
+                                     CommandInterpreter &interpreter) {
+  request.SetWordComplete(true);
 
   // For now we just scan the completions to see if the cursor position is in
   // an option or its argument.  Otherwise we'll call HandleArgumentCompletion.
@@ -661,15 +659,14 @@
 
   auto opt_defs = GetDefinitions();
 
-  std::string cur_opt_std_str(input.GetArgumentAtIndex(cursor_index));
-  cur_opt_std_str.erase(char_pos);
+  std::string cur_opt_std_str = request.GetCursorArgumentPrefix().str();
   const char *cur_opt_str = cur_opt_std_str.c_str();
 
   for (size_t i = 0; i < opt_element_vector.size(); i++) {
     int opt_pos = opt_element_vector[i].opt_pos;
     int opt_arg_pos = opt_element_vector[i].opt_arg_pos;
     int opt_defs_index = opt_element_vector[i].opt_defs_index;
-    if (opt_pos == cursor_index) {
+    if (opt_pos == request.GetCursorIndex()) {
       // We're completing the option itself.
 
       if (opt_defs_index == OptionArgElement::eBareDash) {
@@ -683,7 +680,7 @@
           if (!def.short_option)
             continue;
           opt_str[1] = def.short_option;
-          matches.AppendString(opt_str);
+          request.GetMatches().AppendString(opt_str);
         }
 
         return true;
@@ -695,7 +692,7 @@
 
           full_name.erase(full_name.begin() + 2, full_name.end());
           full_name.append(def.long_option);
-          matches.AppendString(full_name.c_str());
+          request.GetMatches().AppendString(full_name.c_str());
         }
         return true;
       } else if (opt_defs_index != OptionArgElement::eUnrecognizedArg) {
@@ -708,10 +705,10 @@
             strcmp(opt_defs[opt_defs_index].long_option, cur_opt_str) != 0) {
           std::string full_name("--");
           full_name.append(opt_defs[opt_defs_index].long_option);
-          matches.AppendString(full_name.c_str());
+          request.GetMatches().AppendString(full_name.c_str());
           return true;
         } else {
-          matches.AppendString(input.GetArgumentAtIndex(cursor_index));
+          request.GetMatches().AppendString(request.GetCursorArgument());
           return true;
         }
       } else {
@@ -734,29 +731,30 @@
               // The options definitions table has duplicates because of the
               // way the grouping information is stored, so only add once.
               bool duplicate = false;
-              for (size_t k = 0; k < matches.GetSize(); k++) {
-                if (matches.GetStringAtIndex(k) == full_name) {
+              for (size_t k = 0; k < request.GetMatches().GetSize(); k++) {
+                if (request.GetMatches().GetStringAtIndex(k) == full_name) {
                   duplicate = true;
                   break;
                 }
               }
               if (!duplicate)
-                matches.AppendString(full_name.c_str());
+                request.GetMatches().AppendString(full_name.c_str());
             }
           }
         }
         return true;
       }
 
-    } else if (opt_arg_pos == cursor_index) {
+    } else if (opt_arg_pos == request.GetCursorIndex()) {
       // Okay the cursor is on the completion of an argument. See if it has a
       // completion, otherwise return no matches.
 
+      CompletionRequest subrequest = request;
+      subrequest.SetCursorCharPosition(subrequest.GetCursorArgument().size());
       if (opt_defs_index != -1) {
-        HandleOptionArgumentCompletion(
-            input, cursor_index, strlen(input.GetArgumentAtIndex(cursor_index)),
-            opt_element_vector, i, match_start_point, max_return_elements,
-            interpreter, word_complete, matches);
+        HandleOptionArgumentCompletion(subrequest, opt_element_vector, i,
+                                       interpreter);
+        request.SetWordComplete(subrequest.GetWordComplete());
         return true;
       } else {
         // No completion callback means no completions...
@@ -772,11 +770,8 @@
 }
 
 bool Options::HandleOptionArgumentCompletion(
-    Args &input, int cursor_index, int char_pos,
-    OptionElementVector &opt_element_vector, int opt_element_index,
-    int match_start_point, int max_return_elements,
-    CommandInterpreter &interpreter, bool &word_complete,
-    lldb_private::StringList &matches) {
+    CompletionRequest &request, OptionElementVector &opt_element_vector,
+    int opt_element_index, CommandInterpreter &interpreter) {
   auto opt_defs = GetDefinitions();
   std::unique_ptr<SearchFilter> filter_ap;
 
@@ -788,12 +783,14 @@
   OptionEnumValueElement *enum_values = opt_defs[opt_defs_index].enum_values;
   if (enum_values != nullptr) {
     bool return_value = false;
-    std::string match_string(input.GetArgumentAtIndex(opt_arg_pos),
-                             input.GetArgumentAtIndex(opt_arg_pos) + char_pos);
+    std::string match_string(
+        request.GetParsedLine().GetArgumentAtIndex(opt_arg_pos),
+        request.GetParsedLine().GetArgumentAtIndex(opt_arg_pos) +
+            request.GetCursorCharPosition());
     for (int i = 0; enum_values[i].string_value != nullptr; i++) {
       if (strstr(enum_values[i].string_value, match_string.c_str()) ==
           enum_values[i].string_value) {
-        matches.AppendString(enum_values[i].string_value);
+        request.GetMatches().AppendString(enum_values[i].string_value);
         return_value = true;
       }
     }
@@ -838,7 +835,8 @@
       // restrict it to that shared library.
       if (cur_opt_name && strcmp(cur_opt_name, "shlib") == 0 &&
           cur_arg_pos != -1) {
-        const char *module_name = input.GetArgumentAtIndex(cur_arg_pos);
+        const char *module_name =
+            request.GetParsedLine().GetArgumentAtIndex(cur_arg_pos);
         if (module_name) {
           FileSpec module_spec(module_name, false);
           lldb::TargetSP target_sp =
@@ -853,9 +851,7 @@
   }
 
   return CommandCompletions::InvokeCommonCompletionCallbacks(
-      interpreter, completion_mask, input.GetArgumentAtIndex(opt_arg_pos),
-      match_start_point, max_return_elements, filter_ap.get(), word_complete,
-      matches);
+      interpreter, completion_mask, request, filter_ap.get());
 }
 
 void OptionGroupOptions::Append(OptionGroup *group) {