summaryrefslogtreecommitdiffstats
path: root/lldb/source/Host/common/Editline.cpp
diff options
context:
space:
mode:
authorRaphael Isemann <teemperor@gmail.com>2018-09-13 21:26:00 +0000
committerRaphael Isemann <teemperor@gmail.com>2018-09-13 21:26:00 +0000
commit7f88829ceabef9ea9ff4743e8ba67f676d8c0968 (patch)
treea6811b305d3e768aedfd9f73b41c1b95f5b2d651 /lldb/source/Host/common/Editline.cpp
parent9b3e7c365c3cd09954b7d3b4445d7b462bb3ec02 (diff)
downloadbcm5719-llvm-7f88829ceabef9ea9ff4743e8ba67f676d8c0968.tar.gz
bcm5719-llvm-7f88829ceabef9ea9ff4743e8ba67f676d8c0968.zip
Add support for descriptions with command completions.
Summary: This patch adds a framework for adding descriptions to the command completions we provide. It also adds descriptions for completed top-level commands so that we can test this code. Completions are in general supposed to be displayed alongside the completion itself. The descriptions can be used to provide additional information about the completion to the user. Examples for descriptions are function signatures when completing function calls in the expression command or the binary name when providing completion for a symbol. There is still some boilerplate code from the old completion API left in LLDB (mostly because the respective APIs are reused for non-completion related purposes, so the CompletionRequest doesn't make sense to be used), so that's why I still had to change some function signatures. Also, as the old API only passes around a list of matches, and the descriptions are for these functions just another list, I had to add some code that essentially just ensures that both lists are always the same side (e.g. all the manual calls to `descriptions->AddString(X)` below a `matches->AddString(Y)` call). The initial command descriptions that come with this patch are just reusing the existing short help that is already added in LLDB. An example completion with descriptions looks like this: ``` (lldb) pl Available completions: platform -- Commands to manage and create platforms. plugin -- Commands for managing LLDB plugins. ``` Reviewers: #lldb, jingham Reviewed By: #lldb, jingham Subscribers: jingham, JDevlieghere, lldb-commits Differential Revision: https://reviews.llvm.org/D51175 llvm-svn: 342181
Diffstat (limited to 'lldb/source/Host/common/Editline.cpp')
-rw-r--r--lldb/source/Host/common/Editline.cpp44
1 files changed, 34 insertions, 10 deletions
diff --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp
index 9c2583270ea..71b0332458e 100644
--- a/lldb/source/Host/common/Editline.cpp
+++ b/lldb/source/Host/common/Editline.cpp
@@ -853,19 +853,45 @@ unsigned char Editline::BufferEndCommand(int ch) {
return CC_NEWLINE;
}
+//------------------------------------------------------------------------------
+/// Prints completions and their descriptions to the given file. Only the
+/// completions in the interval [start, end) are printed.
+//------------------------------------------------------------------------------
+static void PrintCompletion(FILE *output_file, size_t start, size_t end,
+ StringList &completions, StringList &descriptions) {
+ // This is an 'int' because of printf.
+ int max_len = 0;
+
+ for (size_t i = start; i < end; i++) {
+ const char *completion_str = completions.GetStringAtIndex(i);
+ max_len = std::max((int)strlen(completion_str), max_len);
+ }
+
+ for (size_t i = start; i < end; i++) {
+ const char *completion_str = completions.GetStringAtIndex(i);
+ const char *description_str = descriptions.GetStringAtIndex(i);
+
+ fprintf(output_file, "\n\t%-*s", max_len, completion_str);
+
+ // Print the description if we got one.
+ if (strlen(description_str))
+ fprintf(output_file, " -- %s", description_str);
+ }
+}
+
unsigned char Editline::TabCommand(int ch) {
if (m_completion_callback == nullptr)
return CC_ERROR;
const LineInfo *line_info = el_line(m_editline);
- StringList completions;
+ StringList completions, descriptions;
int page_size = 40;
const int num_completions = m_completion_callback(
line_info->buffer, line_info->cursor, line_info->lastchar,
0, // Don't skip any matches (start at match zero)
-1, // Get all the matches
- completions, m_completion_callback_baton);
+ completions, descriptions, m_completion_callback_baton);
if (num_completions == 0)
return CC_ERROR;
@@ -893,10 +919,8 @@ unsigned char Editline::TabCommand(int ch) {
int num_elements = num_completions + 1;
fprintf(m_output_file, "\n" ANSI_CLEAR_BELOW "Available completions:");
if (num_completions < page_size) {
- for (int i = 1; i < num_elements; i++) {
- completion_str = completions.GetStringAtIndex(i);
- fprintf(m_output_file, "\n\t%s", completion_str);
- }
+ PrintCompletion(m_output_file, 1, num_elements, completions,
+ descriptions);
fprintf(m_output_file, "\n");
} else {
int cur_pos = 1;
@@ -906,10 +930,10 @@ unsigned char Editline::TabCommand(int ch) {
int endpoint = cur_pos + page_size;
if (endpoint > num_elements)
endpoint = num_elements;
- for (; cur_pos < endpoint; cur_pos++) {
- completion_str = completions.GetStringAtIndex(cur_pos);
- fprintf(m_output_file, "\n\t%s", completion_str);
- }
+
+ PrintCompletion(m_output_file, cur_pos, endpoint, completions,
+ descriptions);
+ cur_pos = endpoint;
if (cur_pos >= num_elements) {
fprintf(m_output_file, "\n");
OpenPOWER on IntegriCloud