2020-05-22 03:52:34 +04:30
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-05-22 03:52:34 +04:30
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-22 03:52:34 +04:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2020-05-28 20:40:53 +02:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2020-05-22 03:52:34 +04:30
|
|
|
#include <AK/Forward.h>
|
2023-12-07 03:49:35 +07:00
|
|
|
#include <AK/String.h>
|
2020-05-22 03:52:34 +04:30
|
|
|
#include <AK/Utf32View.h>
|
|
|
|
|
#include <AK/Utf8View.h>
|
|
|
|
|
#include <LibLine/Style.h>
|
|
|
|
|
|
|
|
|
|
namespace Line {
|
|
|
|
|
|
|
|
|
|
struct CompletionSuggestion {
|
2020-05-22 22:40:42 +04:30
|
|
|
private:
|
|
|
|
|
struct ForSearchTag {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static constexpr ForSearchTag ForSearch {};
|
|
|
|
|
|
2020-05-23 03:19:48 +04:30
|
|
|
// Intentionally not explicit. (To allow suggesting bare strings)
|
2023-12-16 17:49:34 +03:30
|
|
|
CompletionSuggestion(ByteString const& completion)
|
2022-07-11 17:32:29 +00:00
|
|
|
: CompletionSuggestion(completion, ""sv, {})
|
2020-05-22 03:52:34 +04:30
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 03:49:35 +07:00
|
|
|
CompletionSuggestion(StringView completion, ForSearchTag)
|
|
|
|
|
: text(MUST(String::from_utf8(completion)))
|
2020-05-22 22:40:42 +04:30
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
CompletionSuggestion(StringView completion, StringView trailing_trivia, StringView display_trivia = ""sv)
|
2022-03-23 17:56:48 +04:30
|
|
|
: CompletionSuggestion(completion, trailing_trivia, display_trivia, {})
|
2020-05-22 03:52:34 +04:30
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 17:56:48 +04:30
|
|
|
CompletionSuggestion(StringView completion, StringView trailing_trivia, StringView display_trivia, Style style);
|
2020-05-22 03:52:34 +04:30
|
|
|
|
2021-12-14 14:41:18 +03:30
|
|
|
bool operator==(CompletionSuggestion const& suggestion) const
|
2020-05-22 03:52:34 +04:30
|
|
|
{
|
2023-12-07 03:49:35 +07:00
|
|
|
return suggestion.text == text;
|
2020-05-22 03:52:34 +04:30
|
|
|
}
|
|
|
|
|
|
2023-12-07 03:49:35 +07:00
|
|
|
String text;
|
|
|
|
|
String trailing_trivia;
|
|
|
|
|
String display_trivia;
|
2020-05-22 03:52:34 +04:30
|
|
|
Style style;
|
|
|
|
|
size_t start_index { 0 };
|
2020-10-03 17:24:49 +03:30
|
|
|
size_t input_offset { 0 };
|
2022-02-28 17:28:47 +03:30
|
|
|
size_t static_offset { 0 };
|
|
|
|
|
size_t invariant_offset { 0 };
|
2022-04-15 08:10:27 +04:30
|
|
|
bool allow_commit_without_listing { true };
|
2020-05-22 03:52:34 +04:30
|
|
|
|
2023-12-07 03:49:35 +07:00
|
|
|
Utf8View text_view() const { return text.code_points(); }
|
|
|
|
|
Utf8View trivia_view() const { return trailing_trivia.code_points(); }
|
|
|
|
|
Utf8View display_trivia_view() const { return display_trivia.code_points(); }
|
|
|
|
|
StringView text_string() const { return text.bytes_as_string_view(); }
|
|
|
|
|
StringView display_trivia_string() const { return display_trivia.bytes_as_string_view(); }
|
2020-05-22 22:40:42 +04:30
|
|
|
bool is_valid { false };
|
2020-05-22 03:52:34 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SuggestionManager {
|
|
|
|
|
friend class Editor;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
void set_suggestions(Vector<CompletionSuggestion>&& suggestions);
|
2020-05-22 14:02:17 +04:30
|
|
|
void set_current_suggestion_initiation_index(size_t start_index);
|
2020-05-22 03:52:34 +04:30
|
|
|
|
|
|
|
|
size_t count() const { return m_suggestions.size(); }
|
|
|
|
|
size_t display_length() const { return m_last_shown_suggestion_display_length; }
|
2020-05-22 14:02:17 +04:30
|
|
|
size_t start_index() const { return m_last_displayed_suggestion_index; }
|
2020-05-22 03:52:34 +04:30
|
|
|
size_t next_index() const { return m_next_suggestion_index; }
|
2020-05-22 14:02:17 +04:30
|
|
|
void set_start_index(size_t index) const { m_last_displayed_suggestion_index = index; }
|
2020-05-22 03:52:34 +04:30
|
|
|
|
2023-01-13 12:29:46 +01:00
|
|
|
ErrorOr<size_t> for_each_suggestion(Function<ErrorOr<IterationDecision>(CompletionSuggestion const&, size_t)>) const;
|
2020-05-22 03:52:34 +04:30
|
|
|
|
|
|
|
|
enum CompletionMode {
|
|
|
|
|
DontComplete,
|
|
|
|
|
CompletePrefix,
|
|
|
|
|
ShowSuggestions,
|
|
|
|
|
CycleSuggestions,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class CompletionAttemptResult {
|
|
|
|
|
public:
|
|
|
|
|
CompletionMode new_completion_mode;
|
|
|
|
|
|
|
|
|
|
ssize_t new_cursor_offset { 0 };
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
size_t start;
|
|
|
|
|
size_t end;
|
|
|
|
|
} offset_region_to_remove { 0, 0 }; // The region to remove as defined by [start, end) translated by (old_cursor + new_cursor_offset)
|
|
|
|
|
|
2022-04-15 01:48:56 +04:30
|
|
|
// This bit of data will be removed, but restored if the suggestion is rejected.
|
|
|
|
|
size_t static_offset_from_cursor { 0 };
|
|
|
|
|
|
2023-12-07 03:49:35 +07:00
|
|
|
Vector<Utf8View> insert {};
|
2020-05-22 03:52:34 +04:30
|
|
|
|
|
|
|
|
Optional<Style> style_to_apply {};
|
2022-04-15 08:10:27 +04:30
|
|
|
|
|
|
|
|
bool avoid_committing_to_single_suggestion { false };
|
2020-05-22 03:52:34 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CompletionAttemptResult attempt_completion(CompletionMode, size_t initiation_start_index);
|
|
|
|
|
|
|
|
|
|
void next();
|
|
|
|
|
void previous();
|
|
|
|
|
|
2021-12-14 14:41:18 +03:30
|
|
|
CompletionSuggestion const& suggest();
|
|
|
|
|
CompletionSuggestion const& current_suggestion() const { return m_last_shown_suggestion; }
|
2020-05-22 03:52:34 +04:30
|
|
|
bool is_current_suggestion_complete() const { return m_last_shown_suggestion_was_complete; }
|
|
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
|
{
|
2023-12-16 17:49:34 +03:30
|
|
|
m_last_shown_suggestion = ByteString::empty();
|
2020-05-22 03:52:34 +04:30
|
|
|
m_last_shown_suggestion_display_length = 0;
|
|
|
|
|
m_suggestions.clear();
|
|
|
|
|
m_last_displayed_suggestion_index = 0;
|
2022-04-16 03:28:30 +04:30
|
|
|
m_next_suggestion_index = 0;
|
2020-05-22 03:52:34 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
SuggestionManager()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector<CompletionSuggestion> m_suggestions;
|
2023-12-16 17:49:34 +03:30
|
|
|
CompletionSuggestion m_last_shown_suggestion { ByteString::empty() };
|
2020-05-22 03:52:34 +04:30
|
|
|
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 };
|
|
|
|
|
size_t m_largest_common_suggestion_prefix_length { 0 };
|
2020-05-22 14:02:17 +04:30
|
|
|
mutable size_t m_last_displayed_suggestion_index { 0 };
|
2020-05-22 03:52:34 +04:30
|
|
|
size_t m_selected_suggestion_index { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|