aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/clangrefactoring/clangqueryhighlightmarker.h
blob: fbe7d31208f8aa96b3978b422d835d836aa900ac (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/****************************************************************************
**
** 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 <dynamicastmatcherdiagnosticcontextcontainer.h>
#include <dynamicastmatcherdiagnosticmessagecontainer.h>
#include <sourcerangecontainerv2.h>

#include <QString>
#include <QTextCharFormat>

namespace ClangRefactoring {

template<typename SyntaxHighlighter>
class ClangQueryHighlightMarker
{
    using SourceRange = ClangBackEnd::V2::SourceRangeContainer;
    using Message = ClangBackEnd::DynamicASTMatcherDiagnosticMessageContainer;
    using Context = ClangBackEnd::DynamicASTMatcherDiagnosticContextContainer;
    using Messages = ClangBackEnd::DynamicASTMatcherDiagnosticMessageContainers;
    using Contexts = ClangBackEnd::DynamicASTMatcherDiagnosticContextContainers;

public:
    ClangQueryHighlightMarker(SyntaxHighlighter &highlighter)
        : m_highlighter(highlighter)
    {
    }

    void setTextFormats(const QTextCharFormat &messageTextFormat,
                        const QTextCharFormat &contextTextFormat)
    {
        m_contextTextFormat = contextTextFormat;
        m_messageTextFormat = messageTextFormat;
    }

    void setMessagesAndContexts(Messages &&messages, Contexts &&contexts)
    {
        m_currentlyUsedContexts.clear();
        m_currentlyUsedMessages.clear();
        m_contexts = std::move(contexts);
        m_messages = std::move(messages);
        m_currentContextsIterator = m_contexts.begin();
        m_currentMessagesIterator = m_messages.begin();
    }

    bool hasMessage(uint currentLineNumber) const
    {
        return m_currentMessagesIterator != m_messages.end()
            && m_currentMessagesIterator->sourceRange.start.line == currentLineNumber;
    }

    bool hasContext(uint currentLineNumber) const
    {
        return m_currentContextsIterator != m_contexts.end()
            && m_currentContextsIterator->sourceRange.start.line == currentLineNumber;
    }

    bool isMessageNext() const
    {
        return m_currentMessagesIterator->sourceRange.start.column
             < m_currentContextsIterator->sourceRange.start.column;
    }

    void formatSameLineSourceRange(const SourceRange &sourceRange, const QTextCharFormat &textFormat)
    {
        uint startColumn = sourceRange.start.column;
        uint endColumn = sourceRange.end.column;
        m_highlighter.setFormat(startColumn - 1, endColumn - startColumn, textFormat);
    }

    void formatStartLineSourceRange(const SourceRange &sourceRange,
                                    int textSize,
                                    const QTextCharFormat &textFormat)
    {
        uint startColumn = sourceRange.start.column;
        m_highlighter.setFormat(startColumn - 1, textSize - startColumn, textFormat);
    }

    void formatEndLineSourceRange(const SourceRange &sourceRange, const QTextCharFormat &textFormat)
    {
        uint endColumn = sourceRange.end.column;
        m_highlighter.setFormat(0, endColumn - 1, textFormat);
    }

    void formatMiddleLineSourceRange(int textSize, const QTextCharFormat &textFormat)
    {
        m_highlighter.setFormat(0, textSize, textFormat);
    }

    static
    bool isSameLine(const SourceRange &sourceRange)
    {
        uint startLine = sourceRange.start.line;
        uint endLine = sourceRange.end.line;

        return startLine == endLine;
    }

    static
    bool isStartLine(const SourceRange &sourceRange, uint currentLineNumber)
    {
        uint startLine = sourceRange.start.line;

        return startLine == currentLineNumber;
    }

    static
    bool isEndLine(const SourceRange &sourceRange, uint currentLineNumber)
    {
        uint endLine = sourceRange.end.line;

        return endLine == currentLineNumber;
    }

    void formatLine(const SourceRange &sourceRange,
                    uint currentLineNumber,
                    int textSize,
                    const QTextCharFormat &textFormat)
    {
        if (isSameLine(sourceRange))
            formatSameLineSourceRange(sourceRange, textFormat);
        else if (isStartLine(sourceRange, currentLineNumber))
            formatStartLineSourceRange(sourceRange, textSize, textFormat);
        else if (isEndLine(sourceRange, currentLineNumber))
            formatEndLineSourceRange(sourceRange, textFormat);
        else
            formatMiddleLineSourceRange(textSize, textFormat);
    }

    template<typename Container>
    void format(Container &container,
                typename Container::iterator &iterator,
                uint currentLineNumber,
                int textSize,
                const QTextCharFormat &textFormat)
    {
        const SourceRange &sourceRange = iterator->sourceRange;

        formatLine(sourceRange, currentLineNumber, textSize, textFormat);

        if (isStartLine(sourceRange, currentLineNumber))
            container.push_back(*iterator);

        if (isSameLine(sourceRange) || isStartLine(sourceRange, currentLineNumber))
            ++iterator;
    }

    template<typename Container>
    static
    void removeEndedContainers(uint currentLineNumber, Container &container)
    {
        auto newEnd = std::remove_if(container.begin(),
                                     container.end(),
                                     [&] (const auto &entry) {
            return ClangQueryHighlightMarker::isEndLine(entry.sourceRange, currentLineNumber);
        });

        container.erase(newEnd, container.end());
    }

    template<typename Container>
    void formatCurrentlyUsed(Container container,
                             uint currentLineNumber,
                             int textSize,
                             const QTextCharFormat &textFormat)
    {
        for (const auto &entry : container) {
            formatLine(entry.sourceRange, currentLineNumber, textSize, textFormat);;
        }
    }

    void formatMessage(uint currentLineNumber, int textSize)
    {
        format(m_currentlyUsedMessages,
               m_currentMessagesIterator,
               currentLineNumber,
               textSize,
               m_messageTextFormat);
    }

    void formatContext(uint currentLineNumber, int textSize)
    {
        format(m_currentlyUsedContexts,
               m_currentContextsIterator,
               currentLineNumber,
               textSize,
               m_contextTextFormat);
    }

    void formatCurrentlyUsedMessagesAndContexts(uint currentLineNumber, int textSize)
    {
        formatCurrentlyUsed(m_currentlyUsedContexts, currentLineNumber, textSize, m_contextTextFormat);
        formatCurrentlyUsed(m_currentlyUsedMessages, currentLineNumber, textSize, m_messageTextFormat);

        removeEndedContainers(currentLineNumber, m_currentlyUsedContexts);
        removeEndedContainers(currentLineNumber, m_currentlyUsedMessages);
    }

    void formatCurrentMessageOrContext(uint currentLineNumber, int textSize)
    {
        bool hasContext = this->hasContext(currentLineNumber);
        bool hasMessage = this->hasMessage(currentLineNumber);

        while (hasContext || hasMessage) {
            if (!hasContext)
                formatMessage(currentLineNumber, textSize);
            else if (!hasMessage)
                formatContext(currentLineNumber, textSize);
            else if (isMessageNext())
                formatMessage(currentLineNumber, textSize);
            else
                formatContext(currentLineNumber, textSize);

            hasContext = this->hasContext(currentLineNumber);
            hasMessage = this->hasMessage(currentLineNumber);
        }
    }

    void highlightBlock(uint currentLineNumber, const QString &currentText)
    {
        formatCurrentlyUsedMessagesAndContexts(currentLineNumber, currentText.size());
        formatCurrentMessageOrContext(currentLineNumber, currentText.size());
    }

    bool hasMessagesOrContexts() const
    {
        return !m_messages.empty() || !m_contexts.empty();
    }

    static
    bool isAfterStartColumn(const SourceRange &sourceRange, uint line, uint column)
    {
        return sourceRange.start.line == line && sourceRange.start.column <= column;
    }

    static
    bool isBeforeEndColumn(const SourceRange &sourceRange, uint line, uint column)
    {
        return sourceRange.end.line == line && sourceRange.end.column >= column;
    }

    static
    bool isInBetweenLine(const SourceRange &sourceRange, uint line)
    {
        return sourceRange.start.line < line && sourceRange.end.line > line;
    }

    static
    bool isSingleLine(const SourceRange &sourceRange)
    {
        return sourceRange.start.line == sourceRange.end.line;
    }

    static
    bool isInsideMultiLine(const SourceRange &sourceRange, uint line, uint column)
    {
        return !isSingleLine(sourceRange)
            && (isAfterStartColumn(sourceRange, line, column)
             || isInBetweenLine(sourceRange, line)
             || isBeforeEndColumn(sourceRange, line, column));
    }

    static
    bool isInsideSingleLine(const SourceRange &sourceRange, uint line, uint column)
    {
        return isSingleLine(sourceRange)
            && isAfterStartColumn(sourceRange, line, column)
            && isBeforeEndColumn(sourceRange, line, column);
    }

    static
    bool isInsideRange(const SourceRange &sourceRange, uint line, uint column)
    {
        return isInsideSingleLine(sourceRange, line, column)
            || isInsideMultiLine(sourceRange, line, column);
    }

    Messages messagesForLineAndColumn(uint line, uint column) const
    {
        Messages messages;

        auto underPosition = [=] (const Message &message) {
            return ClangQueryHighlightMarker::isInsideRange(message.sourceRange, line, column);
        };

        std::copy_if(m_messages.begin(),
                     m_messages.end(),
                     std::back_inserter(messages),
                     underPosition);

        return messages;
    }

    Contexts contextsForLineAndColumn(uint line, uint column) const
    {
        Contexts contexts;

        auto underPosition = [=] (const Context &context) {
            return ClangQueryHighlightMarker::isInsideRange(context.sourceRange, line, column);
        };

        std::copy_if(m_contexts.begin(),
                     m_contexts.end(),
                     std::back_inserter(contexts),
                     underPosition);

        return contexts;
    }

private:
    Contexts m_contexts;
    Messages m_messages;
    Contexts m_currentlyUsedContexts;
    Messages m_currentlyUsedMessages;
    Contexts::iterator m_currentContextsIterator{m_contexts.begin()};
    Messages::iterator m_currentMessagesIterator{m_messages.begin()};
    QTextCharFormat m_messageTextFormat;
    QTextCharFormat m_contextTextFormat;
    SyntaxHighlighter &m_highlighter;
};

} // namespace ClangRefactoring