1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:37:35 +00:00

LibLine: Refactor suggestion handling and display logic out

This commit is contained in:
AnotherTest 2020-05-22 03:52:34 +04:30 committed by Andreas Kling
parent 65adf2aea2
commit f0862cf2b7
8 changed files with 692 additions and 303 deletions

View file

@ -40,59 +40,14 @@
#include <LibCore/DirIterator.h>
#include <LibLine/Span.h>
#include <LibLine/Style.h>
#include <LibLine/SuggestionDisplay.h>
#include <LibLine/SuggestionManager.h>
#include <LibLine/VT.h>
#include <sys/stat.h>
#include <termios.h>
namespace Line {
class Editor;
// FIXME: These objects are pretty heavy since they store two copies of text
// somehow get rid of one
struct CompletionSuggestion {
friend class Editor;
// intentionally not explicit (allows suggesting bare strings)
CompletionSuggestion(const String& completion)
: CompletionSuggestion(completion, "", {})
{
}
CompletionSuggestion(const StringView& completion, const StringView& trailing_trivia)
: CompletionSuggestion(completion, trailing_trivia, {})
{
}
CompletionSuggestion(const StringView& completion, const StringView& trailing_trivia, Style style)
: style(style)
, text_string(completion)
{
Utf8View text_u8 { completion };
Utf8View trivia_u8 { trailing_trivia };
for (auto cp : text_u8)
text.append(cp);
for (auto cp : trivia_u8)
this->trailing_trivia.append(cp);
text_view = Utf32View { text.data(), text.size() };
trivia_view = Utf32View { this->trailing_trivia.data(), this->trailing_trivia.size() };
}
bool operator==(const CompletionSuggestion& suggestion) const
{
return suggestion.text == text;
}
Vector<u32> text;
Vector<u32> trailing_trivia;
Style style;
size_t token_start_index { 0 };
private:
Utf32View text_view;
Utf32View trivia_view;
String text_string;
};
struct Configuration {
enum TokenSplitMechanism {
Spaces,
@ -215,13 +170,6 @@ private:
Function<bool(Editor&)> callback;
};
void vt_save_cursor();
void vt_restore_cursor();
void vt_clear_to_end_of_line();
void vt_clear_lines(size_t count_above, size_t count_below = 0);
void vt_move_relative(int x, int y);
void vt_move_absolute(u32 x, u32 y);
void vt_apply_style(const Style&, bool is_starting = true);
Vector<size_t, 2> vt_dsr();
void remove_at_index(size_t);
@ -258,8 +206,7 @@ private:
m_drawn_cursor = 0;
m_inline_search_cursor = 0;
m_old_prompt_length = m_cached_prompt_length;
m_origin_x = 0;
m_origin_y = 0;
set_origin(0, 0);
m_prompt_lines_at_suggestion_initiation = 0;
m_refresh_needed = true;
}
@ -297,8 +244,14 @@ private:
void set_origin()
{
auto position = vt_dsr();
m_origin_x = position[0];
m_origin_y = position[1];
set_origin(position[0], position[1]);
}
void set_origin(int x, int y)
{
m_origin_x = x;
m_origin_y = y;
m_suggestion_display->set_origin(x, y, {});
}
bool should_break_token(Vector<u32, 1024>& buffer, size_t index);
@ -335,7 +288,6 @@ private:
size_t m_cached_prompt_length { 0 };
size_t m_old_prompt_length { 0 };
size_t m_cached_buffer_size { 0 };
size_t m_lines_used_for_last_suggestions { 0 };
size_t m_prompt_lines_at_suggestion_initiation { 0 };
bool m_cached_prompt_valid { false };
@ -343,16 +295,11 @@ private:
size_t m_origin_x { 0 };
size_t m_origin_y { 0 };
OwnPtr<SuggestionDisplay> m_suggestion_display;
String m_new_prompt;
Vector<CompletionSuggestion> m_suggestions;
CompletionSuggestion m_last_shown_suggestion { String::empty() };
size_t m_last_shown_suggestion_display_length { 0 };
bool m_last_shown_suggestion_was_complete { false };
mutable size_t m_next_suggestion_index { 0 };
mutable size_t m_next_suggestion_invariant_offset { 0 };
mutable size_t m_next_suggestion_static_offset { 0 };
size_t m_largest_common_suggestion_prefix_length { 0 };
size_t m_last_displayed_suggestion_index { 0 };
SuggestionManager m_suggestion_manager;
bool m_always_refresh { false };