summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibLine/SuggestionManager.cpp
blob: 6f0d5a5b57f7ca85cf3494fda14bf81be71cf9e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Function.h>
#include <LibLine/SuggestionManager.h>

namespace Line {

CompletionSuggestion::CompletionSuggestion(const StringView& completion, const StringView& trailing_trivia, Style style)
    : style(style)
    , text_string(completion)
    , is_valid(true)
{
    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() };
}

void SuggestionManager::set_suggestions(Vector<CompletionSuggestion>&& suggestions)
{
    m_suggestions = move(suggestions);

    // make sure we were not given invalid suggestions
    for (auto& suggestion : m_suggestions)
        VERIFY(suggestion.is_valid);

    size_t common_suggestion_prefix { 0 };
    if (m_suggestions.size() == 1) {
        m_largest_common_suggestion_prefix_length = m_suggestions[0].text_view.length();
    } else if (m_suggestions.size()) {
        u32 last_valid_suggestion_code_point;

        for (;; ++common_suggestion_prefix) {
            if (m_suggestions[0].text_view.length() <= common_suggestion_prefix)
                goto no_more_commons;

            last_valid_suggestion_code_point = m_suggestions[0].text_view.code_points()[common_suggestion_prefix];

            for (auto& suggestion : m_suggestions) {
                if (suggestion.text_view.length() <= common_suggestion_prefix || suggestion.text_view.code_points()[common_suggestion_prefix] != last_valid_suggestion_code_point) {
                    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--;
}

const CompletionSuggestion& SuggestionManager::suggest()
{
    m_last_shown_suggestion = m_suggestions[m_next_suggestion_index];
    m_selected_suggestion_index = m_next_suggestion_index;
    return m_last_shown_suggestion;
}

void SuggestionManager::set_current_suggestion_initiation_index(size_t index)
{
    if (m_last_shown_suggestion_display_length)
        m_last_shown_suggestion.start_index = index - m_next_suggestion_static_offset - m_last_shown_suggestion_display_length;
    else
        m_last_shown_suggestion.start_index = index - m_next_suggestion_static_offset - m_next_suggestion_invariant_offset;

    m_last_shown_suggestion_display_length = m_last_shown_suggestion.text_view.length();
    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()) {
        auto can_complete = m_next_suggestion_invariant_offset <= m_largest_common_suggestion_prefix_length;
        ssize_t actual_offset;
        size_t shown_length = m_last_shown_suggestion_display_length;
        switch (mode) {
        case CompletePrefix:
            actual_offset = 0;
            break;
        case ShowSuggestions:
            actual_offset = 0 - m_largest_common_suggestion_prefix_length + m_next_suggestion_invariant_offset;
            if (can_complete)
                shown_length = m_largest_common_suggestion_prefix_length + m_last_shown_suggestion.trivia_view.length();
            break;
        default:
            if (m_last_shown_suggestion_display_length == 0)
                actual_offset = 0;
            else
                actual_offset = 0 - m_last_shown_suggestion_display_length + m_next_suggestion_invariant_offset;
            break;
        }

        result.offset_region_to_remove = { m_next_suggestion_invariant_offset, shown_length };
        result.new_cursor_offset = actual_offset;

        auto& suggestion = suggest();
        set_current_suggestion_initiation_index(initiation_start_index);

        if (mode == CompletePrefix) {
            // Only auto-complete *if possible*.
            if (can_complete) {
                result.insert.append(suggestion.text_view.substring_view(m_next_suggestion_invariant_offset, m_largest_common_suggestion_prefix_length - m_next_suggestion_invariant_offset));
                m_last_shown_suggestion_display_length = m_largest_common_suggestion_prefix_length;
                // Do not increment the suggestion index, as the first tab should only be a *peek*.
                if (m_suggestions.size() == 1) {
                    // If there's one suggestion, commit and forget.
                    result.new_completion_mode = DontComplete;
                    // Add in the trivia of the last selected suggestion.
                    result.insert.append(suggestion.trivia_view);
                    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;
            m_last_shown_suggestion = String::empty();
        } else {
            result.insert.append(suggestion.text_view.substring_view(m_next_suggestion_invariant_offset, suggestion.text_view.length() - m_next_suggestion_invariant_offset));
            // Add in the trivia of the last selected suggestion.
            result.insert.append(suggestion.trivia_view);
            m_last_shown_suggestion_display_length += suggestion.trivia_view.length();
        }
    } else {
        m_next_suggestion_index = 0;
    }
    return result;
}

size_t SuggestionManager::for_each_suggestion(Function<IterationDecision(const CompletionSuggestion&, size_t)> callback) const
{
    size_t start_index { 0 };
    for (auto& suggestion : m_suggestions) {
        if (start_index++ < m_last_displayed_suggestion_index)
            continue;
        if (callback(suggestion, start_index - 1) == IterationDecision::Break)
            break;
    }
    return start_index;
}

}