aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor/snippets/snippetoverlay.cpp
blob: 6824c0470acb3db5d1bff42bc027d274db24ebed (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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "snippetoverlay.h"

#include <utils/algorithm.h>
#include <utils/qtcassert.h>

namespace TextEditor {
namespace Internal {

void SnippetOverlay::clear()
{
    TextEditorOverlay::clear();
    m_selections.clear();
    m_variables.clear();
}

void SnippetOverlay::addSnippetSelection(const QTextCursor &cursor,
                                         const QColor &color,
                                         NameMangler *mangler,
                                         int variableIndex)
{
    m_variables[variableIndex] << selections().size();
    SnippetSelection selection;
    selection.variableIndex = variableIndex;
    selection.mangler = mangler;
    m_selections << selection;
    addOverlaySelection(cursor, color, color, TextEditorOverlay::ExpandBegin);
}

void SnippetOverlay::setFinalSelection(const QTextCursor &cursor, const QColor &color)
{
    m_finalSelectionIndex = selections().size();
    addOverlaySelection(cursor, color, color, TextEditorOverlay::ExpandBegin);
}

void SnippetOverlay::updateEquivalentSelections(const QTextCursor &cursor)
{
    const int &currentIndex = indexForCursor(cursor);
    if (currentIndex < 0)
        return;
    const QString &currentText = cursorForIndex(currentIndex).selectedText();
    const QList<int> &equivalents = m_variables.value(m_selections[currentIndex].variableIndex);
    for (int i : equivalents) {
        if (i == currentIndex)
            continue;
        QTextCursor cursor = cursorForIndex(i);
        const QString &equivalentText = cursor.selectedText();
        if (currentText != equivalentText) {
            cursor.joinPreviousEditBlock();
            cursor.insertText(currentText);
            cursor.endEditBlock();
        }
    }
}

void SnippetOverlay::accept()
{
    hide();
    for (int i = 0; i < m_selections.size(); ++i) {
        if (NameMangler *mangler = m_selections[i].mangler) {
            QTextCursor cursor = cursorForIndex(i);
            const QString current = cursor.selectedText();
            const QString result = mangler->mangle(current);
            if (result != current) {
                cursor.joinPreviousEditBlock();
                cursor.insertText(result);
                cursor.endEditBlock();
            }
        }
    }
    clear();
}

bool SnippetOverlay::hasCursorInSelection(const QTextCursor &cursor) const
{
    return indexForCursor(cursor) >= 0;
}

QTextCursor SnippetOverlay::firstSelectionCursor() const
{
    const QList<OverlaySelection> selections = TextEditorOverlay::selections();
    return selections.isEmpty() ? QTextCursor() : cursorForSelection(selections.first());
}

QTextCursor SnippetOverlay::nextSelectionCursor(const QTextCursor &cursor) const
{
    const QList<OverlaySelection> selections = TextEditorOverlay::selections();
    if (selections.isEmpty())
        return {};
    const SnippetSelection &currentSelection = selectionForCursor(cursor);
    if (currentSelection.variableIndex >= 0) {
        int nextVariableIndex = currentSelection.variableIndex + 1;
        if (!m_variables.contains(nextVariableIndex)) {
            if (m_finalSelectionIndex >= 0)
                return cursorForIndex(m_finalSelectionIndex);
            nextVariableIndex = m_variables.firstKey();
        }

        for (int selectionIndex : m_variables[nextVariableIndex]) {
            if (selections[selectionIndex].m_cursor_begin.position() > cursor.position())
                return cursorForIndex(selectionIndex);
        }
        return cursorForIndex(m_variables[nextVariableIndex].first());
    }
    // currently not over a variable simply select the next available one
    for (const OverlaySelection &candidate : selections) {
        if (candidate.m_cursor_begin.position() > cursor.position())
            return cursorForSelection(candidate);
    }
    return cursorForSelection(selections.first());
}

QTextCursor SnippetOverlay::previousSelectionCursor(const QTextCursor &cursor) const
{
    const QList<OverlaySelection> selections = TextEditorOverlay::selections();
    if (selections.isEmpty())
        return {};
    const SnippetSelection &currentSelection = selectionForCursor(cursor);
    if (currentSelection.variableIndex >= 0) {
        int previousVariableIndex = currentSelection.variableIndex - 1;
        if (!m_variables.contains(previousVariableIndex))
            previousVariableIndex = m_variables.lastKey();

        const QList<int> &equivalents = m_variables[previousVariableIndex];
        for (int i = equivalents.size() - 1; i >= 0; --i) {
            if (selections.at(equivalents.at(i)).m_cursor_end.position() < cursor.position())
                return cursorForIndex(equivalents.at(i));
        }
        return cursorForIndex(m_variables[previousVariableIndex].last());
    }
    // currently not over a variable simply select the previous available one
    for (int i = selections.size() - 1; i >= 0; --i) {
        if (selections.at(i).m_cursor_end.position() < cursor.position())
            return cursorForIndex(i);
    }
    return cursorForSelection(selections.last());
}

bool SnippetOverlay::isFinalSelection(const QTextCursor &cursor) const
{
    return m_finalSelectionIndex >= 0 ? cursor == cursorForIndex(m_finalSelectionIndex) : false;
}

int SnippetOverlay::indexForCursor(const QTextCursor &cursor) const
{
    return Utils::indexOf(selections(),
                          [pos = cursor.position()](const OverlaySelection &selection) {
                              return selection.m_cursor_begin.position() <= pos
                                     && selection.m_cursor_end.position() >= pos;
                          });
}

SnippetOverlay::SnippetSelection SnippetOverlay::selectionForCursor(const QTextCursor &cursor) const
{
    return m_selections.value(indexForCursor(cursor));
}

} // namespace Internal
} // namespace TextEditor