summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibLine/SuggestionManager.h
blob: d1ad97bcf2298b5aed576948000d77dff4c35759 (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
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Forward.h>
#include <AK/String.h>
#include <AK/Utf32View.h>
#include <AK/Utf8View.h>
#include <LibLine/Style.h>
#include <stdlib.h>

namespace Line {

// FIXME: These objects are pretty heavy since they store two copies of text
//        somehow get rid of one.
struct CompletionSuggestion {
private:
    struct ForSearchTag {
    };

public:
    static constexpr ForSearchTag ForSearch {};

    // Intentionally not explicit. (To allow suggesting bare strings)
    CompletionSuggestion(String const& completion)
        : CompletionSuggestion(completion, "", {})
    {
    }

    CompletionSuggestion(String const& completion, ForSearchTag)
        : text_string(completion)
    {
    }

    CompletionSuggestion(StringView completion, StringView trailing_trivia)
        : CompletionSuggestion(completion, trailing_trivia, {})
    {
    }

    CompletionSuggestion(StringView completion, StringView trailing_trivia, Style style);

    bool operator==(CompletionSuggestion const& suggestion) const
    {
        return suggestion.text_string == text_string;
    }

    Vector<u32> text;
    Vector<u32> trailing_trivia;
    Style style;
    size_t start_index { 0 };
    size_t input_offset { 0 };

    Utf32View text_view;
    Utf32View trivia_view;
    String text_string;
    bool is_valid { false };
};

class SuggestionManager {
    friend class Editor;

public:
    void set_suggestions(Vector<CompletionSuggestion>&& suggestions);
    void set_current_suggestion_initiation_index(size_t start_index);

    size_t count() const { return m_suggestions.size(); }
    size_t display_length() const { return m_last_shown_suggestion_display_length; }
    size_t start_index() const { return m_last_displayed_suggestion_index; }
    size_t next_index() const { return m_next_suggestion_index; }
    void set_start_index(size_t index) const { m_last_displayed_suggestion_index = index; }

    size_t for_each_suggestion(Function<IterationDecision(CompletionSuggestion const&, size_t)>) const;

    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)

        Vector<Utf32View> insert {};

        Optional<Style> style_to_apply {};
    };

    CompletionAttemptResult attempt_completion(CompletionMode, size_t initiation_start_index);

    void next();
    void previous();
    void set_suggestion_variants(size_t static_offset, size_t invariant_offset, size_t suggestion_index) const
    {
        m_next_suggestion_index = suggestion_index;
        m_next_suggestion_static_offset = static_offset;
        m_next_suggestion_invariant_offset = invariant_offset;
    }

    CompletionSuggestion const& suggest();
    CompletionSuggestion const& current_suggestion() const { return m_last_shown_suggestion; }
    bool is_current_suggestion_complete() const { return m_last_shown_suggestion_was_complete; }

    void reset()
    {
        m_last_shown_suggestion = String::empty();
        m_last_shown_suggestion_display_length = 0;
        m_suggestions.clear();
        m_last_displayed_suggestion_index = 0;
    }

private:
    SuggestionManager()
    {
    }

    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 };
    mutable size_t m_last_displayed_suggestion_index { 0 };
    size_t m_selected_suggestion_index { 0 };
};

}