2020-05-21 23:22:34 +00:00
|
|
|
/*
|
2021-04-28 20:46:44 +00:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-05-21 23:22:34 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-21 23:22:34 +00:00
|
|
|
*/
|
|
|
|
|
2023-12-06 20:49:35 +00:00
|
|
|
#include <AK/Assertions.h>
|
2020-05-21 23:22:34 +00:00
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <LibLine/SuggestionManager.h>
|
|
|
|
|
|
|
|
namespace Line {
|
|
|
|
|
2022-03-23 13:26:48 +00:00
|
|
|
CompletionSuggestion::CompletionSuggestion(StringView completion, StringView trailing_trivia, StringView display_trivia, Style style)
|
2023-12-06 20:49:35 +00:00
|
|
|
: text(MUST(String::from_utf8(completion)))
|
|
|
|
, trailing_trivia(MUST(String::from_utf8(trailing_trivia)))
|
|
|
|
, display_trivia(MUST(String::from_utf8(display_trivia)))
|
|
|
|
, style(style)
|
2020-05-22 18:10:42 +00:00
|
|
|
, is_valid(true)
|
2020-05-21 23:22:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SuggestionManager::set_suggestions(Vector<CompletionSuggestion>&& suggestions)
|
|
|
|
{
|
2023-12-06 20:49:35 +00:00
|
|
|
auto code_point_at = [](Utf8View view, size_t index) {
|
|
|
|
size_t count = 0;
|
|
|
|
for (auto cp : view) {
|
|
|
|
if (count == index) {
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
};
|
2020-05-22 18:10:42 +00:00
|
|
|
|
2023-12-06 20:49:35 +00:00
|
|
|
m_suggestions = move(suggestions);
|
2020-05-22 18:10:42 +00:00
|
|
|
|
2020-05-21 23:22:34 +00:00
|
|
|
size_t common_suggestion_prefix { 0 };
|
|
|
|
if (m_suggestions.size() == 1) {
|
2023-12-06 20:49:35 +00:00
|
|
|
m_largest_common_suggestion_prefix_length = m_suggestions[0].text_view().length();
|
2020-05-21 23:22:34 +00:00
|
|
|
} else if (m_suggestions.size()) {
|
2020-08-05 20:31:20 +00:00
|
|
|
u32 last_valid_suggestion_code_point;
|
2020-05-21 23:22:34 +00:00
|
|
|
|
|
|
|
for (;; ++common_suggestion_prefix) {
|
2023-12-06 20:49:35 +00:00
|
|
|
if (m_suggestions[0].text_view().length() <= common_suggestion_prefix)
|
2020-05-21 23:22:34 +00:00
|
|
|
goto no_more_commons;
|
|
|
|
|
2023-12-06 20:49:35 +00:00
|
|
|
last_valid_suggestion_code_point = code_point_at(m_suggestions[0].text_view(), common_suggestion_prefix);
|
2020-05-21 23:22:34 +00:00
|
|
|
|
|
|
|
for (auto& suggestion : m_suggestions) {
|
2023-12-06 20:49:35 +00:00
|
|
|
if (suggestion.text_view().length() <= common_suggestion_prefix || code_point_at(suggestion.text_view(), common_suggestion_prefix) != last_valid_suggestion_code_point) {
|
2020-05-21 23:22:34 +00:00
|
|
|
goto no_more_commons;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
no_more_commons:;
|
|
|
|
m_largest_common_suggestion_prefix_length = common_suggestion_prefix;
|
|
|
|
} else {
|
|
|
|
m_largest_common_suggestion_prefix_length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SuggestionManager::next()
|
|
|
|
{
|
|
|
|
if (m_suggestions.size())
|
|
|
|
m_next_suggestion_index = (m_next_suggestion_index + 1) % m_suggestions.size();
|
|
|
|
else
|
|
|
|
m_next_suggestion_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SuggestionManager::previous()
|
|
|
|
{
|
|
|
|
if (m_next_suggestion_index == 0)
|
|
|
|
m_next_suggestion_index = m_suggestions.size();
|
|
|
|
m_next_suggestion_index--;
|
|
|
|
}
|
|
|
|
|
2021-12-14 11:11:18 +00:00
|
|
|
CompletionSuggestion const& SuggestionManager::suggest()
|
2020-05-21 23:22:34 +00:00
|
|
|
{
|
2023-12-21 21:14:24 +00:00
|
|
|
auto const& suggestion = m_suggestions[m_next_suggestion_index];
|
2020-05-21 23:22:34 +00:00
|
|
|
m_selected_suggestion_index = m_next_suggestion_index;
|
2023-12-21 21:14:24 +00:00
|
|
|
m_last_shown_suggestion = suggestion;
|
|
|
|
return suggestion;
|
2020-05-21 23:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SuggestionManager::set_current_suggestion_initiation_index(size_t index)
|
|
|
|
{
|
2022-02-28 13:58:47 +00:00
|
|
|
auto& suggestion = m_suggestions[m_next_suggestion_index];
|
|
|
|
|
2020-05-21 23:22:34 +00:00
|
|
|
if (m_last_shown_suggestion_display_length)
|
2022-02-28 13:58:47 +00:00
|
|
|
m_last_shown_suggestion.start_index = index - suggestion.static_offset - m_last_shown_suggestion_display_length;
|
2020-05-21 23:22:34 +00:00
|
|
|
else
|
2022-02-28 13:58:47 +00:00
|
|
|
m_last_shown_suggestion.start_index = index - suggestion.static_offset - suggestion.invariant_offset;
|
2020-05-21 23:22:34 +00:00
|
|
|
|
2023-12-06 20:49:35 +00:00
|
|
|
m_last_shown_suggestion_display_length = m_last_shown_suggestion.text_view().length();
|
2020-05-21 23:22:34 +00:00
|
|
|
m_last_shown_suggestion_was_complete = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SuggestionManager::CompletionAttemptResult SuggestionManager::attempt_completion(CompletionMode mode, size_t initiation_start_index)
|
|
|
|
{
|
|
|
|
CompletionAttemptResult result { mode };
|
|
|
|
|
|
|
|
if (m_next_suggestion_index < m_suggestions.size()) {
|
2022-02-28 13:58:47 +00:00
|
|
|
auto& next_suggestion = m_suggestions[m_next_suggestion_index];
|
|
|
|
|
2022-04-15 03:40:27 +00:00
|
|
|
if (mode == CompletePrefix && !next_suggestion.allow_commit_without_listing) {
|
|
|
|
result.new_completion_mode = CompletionMode::ShowSuggestions;
|
|
|
|
result.avoid_committing_to_single_suggestion = true;
|
|
|
|
m_last_shown_suggestion_display_length = 0;
|
|
|
|
m_last_shown_suggestion_was_complete = false;
|
2023-12-16 14:19:34 +00:00
|
|
|
m_last_shown_suggestion = ByteString::empty();
|
2022-04-15 03:40:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:58:47 +00:00
|
|
|
auto can_complete = next_suggestion.invariant_offset <= m_largest_common_suggestion_prefix_length;
|
2021-05-16 06:47:13 +00:00
|
|
|
ssize_t actual_offset;
|
|
|
|
size_t shown_length = m_last_shown_suggestion_display_length;
|
|
|
|
switch (mode) {
|
|
|
|
case CompletePrefix:
|
|
|
|
actual_offset = 0;
|
|
|
|
break;
|
|
|
|
case ShowSuggestions:
|
2022-02-28 13:58:47 +00:00
|
|
|
actual_offset = 0 - m_largest_common_suggestion_prefix_length + next_suggestion.invariant_offset;
|
2022-04-15 03:40:27 +00:00
|
|
|
if (can_complete && next_suggestion.allow_commit_without_listing)
|
2023-12-06 20:49:35 +00:00
|
|
|
shown_length = m_largest_common_suggestion_prefix_length + m_last_shown_suggestion.trivia_view().length();
|
2021-05-16 06:47:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (m_last_shown_suggestion_display_length == 0)
|
2020-05-21 23:22:34 +00:00
|
|
|
actual_offset = 0;
|
2021-05-16 06:47:13 +00:00
|
|
|
else
|
2022-02-28 13:58:47 +00:00
|
|
|
actual_offset = 0 - m_last_shown_suggestion_display_length + next_suggestion.invariant_offset;
|
2021-05-16 06:47:13 +00:00
|
|
|
break;
|
2020-05-21 23:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto& suggestion = suggest();
|
|
|
|
set_current_suggestion_initiation_index(initiation_start_index);
|
|
|
|
|
2022-04-14 21:18:56 +00:00
|
|
|
result.offset_region_to_remove = { next_suggestion.invariant_offset, shown_length };
|
|
|
|
result.new_cursor_offset = actual_offset;
|
|
|
|
result.static_offset_from_cursor = next_suggestion.static_offset;
|
|
|
|
|
2020-05-21 23:22:34 +00:00
|
|
|
if (mode == CompletePrefix) {
|
2020-05-22 22:49:48 +00:00
|
|
|
// Only auto-complete *if possible*.
|
2020-05-21 23:22:34 +00:00
|
|
|
if (can_complete) {
|
2024-09-19 13:25:17 +00:00
|
|
|
result.insert.append(suggestion.text_view().unicode_substring_view(suggestion.invariant_offset, m_largest_common_suggestion_prefix_length - suggestion.invariant_offset));
|
2020-05-21 23:22:34 +00:00
|
|
|
m_last_shown_suggestion_display_length = m_largest_common_suggestion_prefix_length;
|
2020-05-22 22:49:48 +00:00
|
|
|
// Do not increment the suggestion index, as the first tab should only be a *peek*.
|
2020-05-21 23:22:34 +00:00
|
|
|
if (m_suggestions.size() == 1) {
|
2020-05-22 22:49:48 +00:00
|
|
|
// If there's one suggestion, commit and forget.
|
2020-05-21 23:22:34 +00:00
|
|
|
result.new_completion_mode = DontComplete;
|
2020-05-22 22:49:48 +00:00
|
|
|
// Add in the trivia of the last selected suggestion.
|
2024-09-19 13:25:17 +00:00
|
|
|
result.insert.append(suggestion.trailing_trivia.code_points());
|
2020-05-21 23:22:34 +00:00
|
|
|
m_last_shown_suggestion_display_length = 0;
|
|
|
|
result.style_to_apply = suggestion.style;
|
|
|
|
m_last_shown_suggestion_was_complete = true;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_last_shown_suggestion_display_length = 0;
|
|
|
|
}
|
|
|
|
result.new_completion_mode = CompletionMode::ShowSuggestions;
|
|
|
|
m_last_shown_suggestion_was_complete = false;
|
2023-12-16 14:19:34 +00:00
|
|
|
m_last_shown_suggestion = ByteString::empty();
|
2020-05-21 23:22:34 +00:00
|
|
|
} else {
|
2024-09-19 13:25:17 +00:00
|
|
|
result.insert.append(suggestion.text_view().unicode_substring_view(suggestion.invariant_offset, suggestion.text_view().length() - suggestion.invariant_offset));
|
2020-05-22 22:49:48 +00:00
|
|
|
// Add in the trivia of the last selected suggestion.
|
2024-09-19 13:25:17 +00:00
|
|
|
result.insert.append(suggestion.trailing_trivia.code_points());
|
2023-12-06 20:49:35 +00:00
|
|
|
m_last_shown_suggestion_display_length += suggestion.trivia_view().length();
|
2020-05-21 23:22:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_next_suggestion_index = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-01-13 11:29:46 +00:00
|
|
|
ErrorOr<size_t> SuggestionManager::for_each_suggestion(Function<ErrorOr<IterationDecision>(CompletionSuggestion const&, size_t)> callback) const
|
2020-05-21 23:22:34 +00:00
|
|
|
{
|
|
|
|
size_t start_index { 0 };
|
|
|
|
for (auto& suggestion : m_suggestions) {
|
|
|
|
if (start_index++ < m_last_displayed_suggestion_index)
|
|
|
|
continue;
|
2023-01-13 11:29:46 +00:00
|
|
|
if (TRY(callback(suggestion, start_index - 1)) == IterationDecision::Break)
|
2020-05-21 23:22:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return start_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|