mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:07:36 +00:00
LibLine: Add a display trivia field to suggestions
These strings will be shown next to the completions as an optional hint or description.
This commit is contained in:
parent
9453e0e6d2
commit
d995be428a
3 changed files with 21 additions and 7 deletions
|
@ -9,13 +9,15 @@
|
||||||
|
|
||||||
namespace Line {
|
namespace Line {
|
||||||
|
|
||||||
CompletionSuggestion::CompletionSuggestion(StringView completion, StringView trailing_trivia, Style style)
|
CompletionSuggestion::CompletionSuggestion(StringView completion, StringView trailing_trivia, StringView display_trivia, Style style)
|
||||||
: style(style)
|
: style(style)
|
||||||
, text_string(completion)
|
, text_string(completion)
|
||||||
|
, display_trivia_string(display_trivia)
|
||||||
, is_valid(true)
|
, is_valid(true)
|
||||||
{
|
{
|
||||||
Utf8View text_u8 { completion };
|
Utf8View text_u8 { completion };
|
||||||
Utf8View trivia_u8 { trailing_trivia };
|
Utf8View trivia_u8 { trailing_trivia };
|
||||||
|
Utf8View display_u8 { display_trivia };
|
||||||
|
|
||||||
for (auto cp : text_u8)
|
for (auto cp : text_u8)
|
||||||
text.append(cp);
|
text.append(cp);
|
||||||
|
@ -23,8 +25,12 @@ CompletionSuggestion::CompletionSuggestion(StringView completion, StringView tra
|
||||||
for (auto cp : trivia_u8)
|
for (auto cp : trivia_u8)
|
||||||
this->trailing_trivia.append(cp);
|
this->trailing_trivia.append(cp);
|
||||||
|
|
||||||
|
for (auto cp : display_u8)
|
||||||
|
this->display_trivia.append(cp);
|
||||||
|
|
||||||
text_view = Utf32View { text.data(), text.size() };
|
text_view = Utf32View { text.data(), text.size() };
|
||||||
trivia_view = Utf32View { this->trailing_trivia.data(), this->trailing_trivia.size() };
|
trivia_view = Utf32View { this->trailing_trivia.data(), this->trailing_trivia.size() };
|
||||||
|
display_trivia_view = Utf32View { this->display_trivia.data(), this->display_trivia.size() };
|
||||||
}
|
}
|
||||||
|
|
||||||
void SuggestionManager::set_suggestions(Vector<CompletionSuggestion>&& suggestions)
|
void SuggestionManager::set_suggestions(Vector<CompletionSuggestion>&& suggestions)
|
||||||
|
@ -36,6 +42,7 @@ void SuggestionManager::set_suggestions(Vector<CompletionSuggestion>&& suggestio
|
||||||
VERIFY(suggestion.is_valid);
|
VERIFY(suggestion.is_valid);
|
||||||
suggestion.text_view = { suggestion.text.data(), suggestion.text.size() };
|
suggestion.text_view = { suggestion.text.data(), suggestion.text.size() };
|
||||||
suggestion.trivia_view = { suggestion.trailing_trivia.data(), suggestion.trailing_trivia.size() };
|
suggestion.trivia_view = { suggestion.trailing_trivia.data(), suggestion.trailing_trivia.size() };
|
||||||
|
suggestion.display_trivia_view = { suggestion.display_trivia.data(), suggestion.display_trivia.size() };
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t common_suggestion_prefix { 0 };
|
size_t common_suggestion_prefix { 0 };
|
||||||
|
|
|
@ -36,12 +36,12 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletionSuggestion(StringView completion, StringView trailing_trivia)
|
CompletionSuggestion(StringView completion, StringView trailing_trivia, StringView display_trivia = "")
|
||||||
: CompletionSuggestion(completion, trailing_trivia, {})
|
: CompletionSuggestion(completion, trailing_trivia, display_trivia, {})
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletionSuggestion(StringView completion, StringView trailing_trivia, Style style);
|
CompletionSuggestion(StringView completion, StringView trailing_trivia, StringView display_trivia, Style style);
|
||||||
|
|
||||||
bool operator==(CompletionSuggestion const& suggestion) const
|
bool operator==(CompletionSuggestion const& suggestion) const
|
||||||
{
|
{
|
||||||
|
@ -50,6 +50,7 @@ public:
|
||||||
|
|
||||||
Vector<u32> text;
|
Vector<u32> text;
|
||||||
Vector<u32> trailing_trivia;
|
Vector<u32> trailing_trivia;
|
||||||
|
Vector<u32> display_trivia;
|
||||||
Style style;
|
Style style;
|
||||||
size_t start_index { 0 };
|
size_t start_index { 0 };
|
||||||
size_t input_offset { 0 };
|
size_t input_offset { 0 };
|
||||||
|
@ -58,7 +59,9 @@ public:
|
||||||
|
|
||||||
Utf32View text_view;
|
Utf32View text_view;
|
||||||
Utf32View trivia_view;
|
Utf32View trivia_view;
|
||||||
|
Utf32View display_trivia_view;
|
||||||
String text_string;
|
String text_string;
|
||||||
|
String display_trivia_string;
|
||||||
bool is_valid { false };
|
bool is_valid { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -22,11 +22,13 @@ void XtermSuggestionDisplay::display(SuggestionManager const& manager)
|
||||||
|
|
||||||
size_t longest_suggestion_length = 0;
|
size_t longest_suggestion_length = 0;
|
||||||
size_t longest_suggestion_byte_length = 0;
|
size_t longest_suggestion_byte_length = 0;
|
||||||
|
size_t longest_suggestion_byte_length_without_trivia = 0;
|
||||||
|
|
||||||
manager.set_start_index(0);
|
manager.set_start_index(0);
|
||||||
manager.for_each_suggestion([&](auto& suggestion, auto) {
|
manager.for_each_suggestion([&](auto& suggestion, auto) {
|
||||||
longest_suggestion_length = max(longest_suggestion_length, suggestion.text_view.length());
|
longest_suggestion_length = max(longest_suggestion_length, suggestion.text_view.length() + suggestion.display_trivia_view.length());
|
||||||
longest_suggestion_byte_length = max(longest_suggestion_byte_length, suggestion.text_string.length());
|
longest_suggestion_byte_length = max(longest_suggestion_byte_length, suggestion.text_string.length() + suggestion.display_trivia_string.length());
|
||||||
|
longest_suggestion_byte_length_without_trivia = max(longest_suggestion_byte_length_without_trivia, suggestion.text_string.length());
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -114,8 +116,10 @@ void XtermSuggestionDisplay::display(SuggestionManager const& manager)
|
||||||
if (spans_entire_line) {
|
if (spans_entire_line) {
|
||||||
num_printed += m_num_columns;
|
num_printed += m_num_columns;
|
||||||
stderr_stream.write(suggestion.text_string.bytes());
|
stderr_stream.write(suggestion.text_string.bytes());
|
||||||
|
stderr_stream.write(suggestion.display_trivia_string.bytes());
|
||||||
} else {
|
} else {
|
||||||
stderr_stream.write(String::formatted("{: <{}}", suggestion.text_string, longest_suggestion_byte_length + 2).bytes());
|
auto field = String::formatted("{: <{}} {}", suggestion.text_string, longest_suggestion_byte_length_without_trivia, suggestion.display_trivia_string);
|
||||||
|
stderr_stream.write(String::formatted("{: <{}}", field, longest_suggestion_byte_length + 2).bytes());
|
||||||
num_printed += longest_suggestion_length + 2;
|
num_printed += longest_suggestion_length + 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue