aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor/snippets/snippetoverlay.cpp
blob: 287d0d753167d3f9fb6befa190123de09f7f7879 (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
174
175
176
177
178
179
180
181
182
183
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#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