]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: move display of completion results into completion_result class
authorAndrew Burgess <aburgess@redhat.com>
Wed, 21 Feb 2024 11:28:31 +0000 (11:28 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Sat, 7 Sep 2024 19:28:58 +0000 (20:28 +0100)
This commit moves the printing of the 'complete' command results out
of the 'complete_command' function.  The printing is now done in a new
member function 'completion_result::print_matches'.  At this point,
this is entirely a refactor.

The motivation for this refactor is how 'complete' should print the
completion of filename arguments.  In some cases the filename results
need to have escaping added to the output.  This escaping needs to be
done immediately prior to printing the result as adding too early will
result in multiple 'complete' results potentially being sorted
incorrectly.  See the subsequent commits for more details.

There should be no user visible changes after this commit.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/cli/cli-cmds.c
gdb/completer.c
gdb/completer.h

index 58bb62e89d69539f8cd792fb6ff0451fd84e2aa3..fd8e27735f086c1ea73b56b47da4b4db05ac88b6 100644 (file)
@@ -423,31 +423,7 @@ complete_command (const char *arg, int from_tty)
     {
       std::string arg_prefix (arg, word - arg);
 
-      if (result.number_matches == 1)
-       printf_unfiltered ("%s%s\n", arg_prefix.c_str (), result.match_list[0]);
-      else
-       {
-         result.sort_match_list ();
-
-         for (size_t i = 0; i < result.number_matches; i++)
-           {
-             printf_unfiltered ("%s%s",
-                                arg_prefix.c_str (),
-                                result.match_list[i + 1]);
-             if (quote_char)
-               printf_unfiltered ("%c", quote_char);
-             printf_unfiltered ("\n");
-           }
-       }
-
-      if (result.number_matches == max_completions)
-       {
-         /* ARG_PREFIX and WORD are included in the output so that emacs
-            will include the message in the output.  */
-         printf_unfiltered (_("%s%s %s\n"),
-                            arg_prefix.c_str (), word,
-                            get_max_completions_reached_message ());
-       }
+      result.print_matches (arg_prefix, word, quote_char);
     }
 }
 
index 3ab342dab4f3dcef8fba5e9ab04677ec32443553..370a57e6d7fd9387f124e2b3d0a544049d01ac3d 100644 (file)
@@ -2504,6 +2504,39 @@ completion_result::reset_match_list ()
     }
 }
 
+/* See completer.h  */
+
+void
+completion_result::print_matches (const std::string &prefix,
+                                 const char *word, int quote_char)
+{
+  if (this->number_matches == 1)
+    printf_unfiltered ("%s%s\n", prefix.c_str (), this->match_list[0]);
+  else
+    {
+      this->sort_match_list ();
+
+      for (size_t i = 0; i < this->number_matches; i++)
+       {
+         printf_unfiltered ("%s%s", prefix.c_str (),
+                            this->match_list[i + 1]);
+         if (quote_char)
+           printf_unfiltered ("%c", quote_char);
+         printf_unfiltered ("\n");
+       }
+    }
+
+  if (this->number_matches == max_completions)
+    {
+      /* PREFIX and WORD are included in the output so that emacs will
+        include the message in the output.  */
+      printf_unfiltered (_("%s%s %s\n"),
+                        prefix.c_str (), word,
+                        get_max_completions_reached_message ());
+    }
+
+}
+
 /* Helper for gdb_rl_attempted_completion_function, which does most of
    the work.  This is called by readline to build the match list array
    and to determine the lowest common denominator.  The real matches
index 70f123ec32f30431e813ecce07bc70a46b76b3cd..e6dc9417932b24e8c18b67a7fca03e509a486af7 100644 (file)
@@ -268,6 +268,19 @@ struct completion_result
   /* Sort the match list.  */
   void sort_match_list ();
 
+  /* Called to display all matches (used by the 'complete' command).
+     PREFIX is everything before the completion word.  WORD is the word
+     being completed, this is only used if we reach the maximum number of
+     completions, otherwise, each line of output consists of PREFIX
+     followed by one of the possible completion words.
+
+     The QUOTE_CHAR is appended after each possible completion word and
+     should be the quote character that appears before the completion word,
+     or the null-character if there is no quote before the completion
+     word.  */
+  void print_matches (const std::string &prefix, const char *word,
+                     int quote_char);
+
 private:
   /* Destroy the match list array and its contents.  */
   void reset_match_list ();