aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/clangrefactoring/clangqueryexamplehighlightmarker.h
blob: d3a6bab652e7521761c27e552ddc121d60f57c94 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/****************************************************************************
**
** Copyright (C) 2017 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.
**
****************************************************************************/

#pragma once

#include <sourcerangewithtextcontainer.h>

#include <QString>
#include <QTextCharFormat>

namespace ClangRefactoring {

template<typename SyntaxHighlighter>
class ClangQueryExampleHighlightMarker
{
    using SourceRange = ClangBackEnd::V2::SourceRangeContainer;
    using SourceRanges = ClangBackEnd::V2::SourceRangeContainers;
    using SourceRangeWithTexts = ClangBackEnd::SourceRangeWithTextContainers;
public:
    ClangQueryExampleHighlightMarker(SourceRangeWithTexts &&sourceRanges,
                                     SyntaxHighlighter &highlighter,
                                     const std::array<QTextCharFormat, 5> &textFormats)
        : m_sourceRanges(std::move(sourceRanges)),
          m_currentSourceRangeIterator(m_sourceRanges.begin()),
          m_highlighter(highlighter),
          m_textFormats(textFormats)
    {
    }

    ClangQueryExampleHighlightMarker(SyntaxHighlighter &highlighter)
        : m_currentSourceRangeIterator(m_sourceRanges.begin()),
          m_highlighter(highlighter)
    {
    }

    void setTextFormats(std::array<QTextCharFormat, 5> &&textFormats)
    {
        m_textFormats = std::move(textFormats);
    }

    void setSourceRanges(SourceRangeWithTexts &&sourceRanges)
    {
        m_currentlyUsedSourceRanges.clear();
        m_sourceRanges = std::move(sourceRanges);
        m_currentSourceRangeIterator = m_sourceRanges.begin();
    }

    void highlightSourceRanges(uint currentLineNumber, const QString &currentText)
    {
        while (hasSourceRangesForCurrentLine(currentLineNumber)) {
            SourceRange &sourceRange = *m_currentSourceRangeIterator;

            popSourceRangeIfMultiLineEndsHere(currentLineNumber, sourceRange.start.column);

            formatSourceRange(sourceRange,
                              currentLineNumber,
                              currentText.size(),
                              int(m_currentlyUsedSourceRanges.size()));

            pushSourceRangeIfMultiLine(sourceRange);
            ++m_currentSourceRangeIterator;
        }
    }

    void highlightCurrentlyUsedSourceRanges(uint currentLineNumber, const QString &currentText)
    {
        formatCurrentlyUsedSourceRanges(currentLineNumber, currentText.size());
        popSourceRangeIfMultiLineEndsHereAndAllSourceRangesAreConsumed(currentLineNumber);
    }

    void highlightBlock(uint currentLineNumber, const QString &currentText)
    {
        popSourceRangeIfMultiLineEndedBefore(currentLineNumber);
        highlightCurrentlyUsedSourceRanges(currentLineNumber, currentText);
        highlightSourceRanges(currentLineNumber, currentText);
    }

    bool hasSourceRangesForCurrentLine(uint currentLineNumber) const
    {
        return m_currentSourceRangeIterator != m_sourceRanges.end()
            && m_currentSourceRangeIterator->start.line == currentLineNumber;
    }

    bool hasOnlySCurrentlyUsedSourceRanges() const
    {
        return m_currentSourceRangeIterator == m_sourceRanges.end()
            && !m_currentlyUsedSourceRanges.empty();
    }

    void formatSingleSourceRange(const SourceRange &sourceRange,
                                 int textFormatIndex)
    {
        int size = int(sourceRange.end.column - sourceRange.start.column);
        m_highlighter.setFormat(int(sourceRange.start.column) - 1,
                                size,
                                m_textFormats[textFormatIndex]);
    }

    void formatStartMultipleSourceRange(const SourceRange &sourceRange,
                                        int textSize,
                                        int textFormatIndex)
    {
        int size = textSize - int(sourceRange.start.column) + 1;
        m_highlighter.setFormat(int(sourceRange.start.column) - 1,
                                size,
                                m_textFormats[textFormatIndex]);
    }

    void formatEndMultipleSourceRange(const SourceRange &sourceRange,
                                      int textFormatIndex)
    {
        int size = int(sourceRange.end.column) - 1;
        m_highlighter.setFormat(0, size, m_textFormats[textFormatIndex]);
    }

    void formatMiddleMultipleSourceRange(int textSize,
                                         int textFormatIndex)
    {
        m_highlighter.setFormat(0, textSize, m_textFormats[textFormatIndex]);
    }

    void formatSourceRange(const SourceRange &sourceRange,
                           uint currentLineNumber,
                           int textSize,
                           int textFormatIndex)
    {
        if (sourceRange.start.line == sourceRange.end.line)
            formatSingleSourceRange(sourceRange, textFormatIndex);
        else if (sourceRange.start.line == currentLineNumber)
            formatStartMultipleSourceRange(sourceRange, textSize, textFormatIndex);
        else if (sourceRange.end.line == currentLineNumber)
            formatEndMultipleSourceRange(sourceRange, textFormatIndex);
        else
            formatMiddleMultipleSourceRange(textSize, textFormatIndex);
    }

    void formatCurrentlyUsedSourceRanges(uint currentLineNumber, int textSize)
    {
        int textFormatIndex = 0;

        for (const SourceRange &sourceRange : m_currentlyUsedSourceRanges) {
            formatSourceRange(sourceRange, currentLineNumber, textSize, textFormatIndex);
            ++textFormatIndex;
        }
    }

    bool currentlyUsedHasEndLineAndColumnNumber(uint currentLineNumber, uint currentColumnNumber)
    {
        return !m_currentlyUsedSourceRanges.empty()
            && m_currentlyUsedSourceRanges.back().end.line <= currentLineNumber
            && m_currentlyUsedSourceRanges.back().end.column <= currentColumnNumber;
    }

    void popSourceRangeIfMultiLineEndsHere(uint currentLineNumber, uint currentColumnNumber)
    {
        while (currentlyUsedHasEndLineAndColumnNumber(currentLineNumber, currentColumnNumber))
            m_currentlyUsedSourceRanges.pop_back();
    }

    bool currentlyUsedHasEndLineNumberAndSourceRangesAreConsumed(uint currentLineNumber)
    {
        return !m_currentlyUsedSourceRanges.empty()
            && m_currentSourceRangeIterator == m_sourceRanges.end()
            && m_currentlyUsedSourceRanges.back().end.line == currentLineNumber;
    }

    void popSourceRangeIfMultiLineEndsHereAndAllSourceRangesAreConsumed(uint currentLineNumber)
    {
        while (currentlyUsedHasEndLineNumberAndSourceRangesAreConsumed(currentLineNumber))
            m_currentlyUsedSourceRanges.pop_back();
    }

    bool  currentlyUsedHasEndedBeforeLineNumber(uint currentLineNumber)
    {
        return !m_currentlyUsedSourceRanges.empty()
            && m_currentlyUsedSourceRanges.back().end.line < currentLineNumber;
    }

    void popSourceRangeIfMultiLineEndedBefore(uint currentLineNumber)
    {
        while (currentlyUsedHasEndedBeforeLineNumber(currentLineNumber))
            m_currentlyUsedSourceRanges.pop_back();
    }

    void pushSourceRangeIfMultiLine(SourceRange &sourceRange)
    {
        m_currentlyUsedSourceRanges.push_back(sourceRange);
    }

private:
    SourceRangeWithTexts m_sourceRanges;
    SourceRangeWithTexts::iterator m_currentSourceRangeIterator;
    SourceRanges m_currentlyUsedSourceRanges;
    SyntaxHighlighter &m_highlighter;
    std::array<QTextCharFormat, 5> m_textFormats;
};

} // namespace ClangRefactoring