aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/clangcodemodel/clangdiagnostictooltipwidget.cpp
blob: 5151279a2833805642570fad93fbbd9c015c9598 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/****************************************************************************
**
** Copyright (C) 2016 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 "clangdiagnostictooltipwidget.h"
#include "clangfixitoperation.h"
#include "clangutils.h"

#include <coreplugin/editormanager/editormanager.h>

#include <cpptools/cpptoolsconstants.h>

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

#include <QApplication>
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QFileInfo>
#include <QHash>
#include <QLabel>
#include <QScreen>
#include <QTextDocument>
#include <QUrl>

using namespace ClangCodeModel;
using Internal::ClangDiagnosticWidget;
using Internal::ClangFixItOperation;

namespace {

// CLANG-UPGRADE-CHECK: Checks/update URLs.
//
// Once it gets dedicated documentation pages for released versions,
// use them instead of pointing to master, as checks might vanish.
const char CLAZY_DOCUMENTATION_URL_TEMPLATE[]
    = "https://github.com/KDE/clazy/blob/master/docs/checks/README-%1.md";

const char LINK_ACTION_GOTO_LOCATION[] = "#gotoLocation";
const char LINK_ACTION_APPLY_FIX[] = "#applyFix";

QString fileNamePrefix(const QString &mainFilePath,
                       const ClangBackEnd::SourceLocationContainer &location)
{
    const QString filePath = location.filePath.toString();
    if (filePath != mainFilePath)
        return QFileInfo(filePath).fileName() + QLatin1Char(':');

    return QString();
}

QString locationToString(const ClangBackEnd::SourceLocationContainer &location)
{
    return QString::number(location.line)
         + QStringLiteral(":")
         + QString::number(location.column);
}

void openEditorAt(const ClangBackEnd::DiagnosticContainer &diagnostic)
{
    const ClangBackEnd::SourceLocationContainer &location = diagnostic.location;

    Core::EditorManager::openEditorAt(location.filePath.toString(),
                                      int(location.line),
                                      int(location.column - 1));
}

void applyFixit(const ClangBackEnd::DiagnosticContainer &diagnostic)
{
    ClangFixItOperation operation(Utf8String(), diagnostic.fixIts);

    operation.perform();
}

class WidgetFromDiagnostics
{
public:
    struct DisplayHints {
        bool showCategoryAndEnableOption;
        bool showFileNameInMainDiagnostic;
        bool enableClickableFixits;
        bool limitWidth;
        bool hideTooltipAfterLinkActivation;
        bool allowTextSelection;
    };

    WidgetFromDiagnostics(const DisplayHints &displayHints)
        : m_displayHints(displayHints)
    {
    }

    QWidget *createWidget(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics)
    {
        const QString text = htmlText(diagnostics);

        auto *label = new QLabel;
        label->setTextFormat(Qt::RichText);
        label->setText(text);
        if (m_displayHints.allowTextSelection) {
            label->setTextInteractionFlags(Qt::TextBrowserInteraction);
        } else {
            label->setTextInteractionFlags(Qt::LinksAccessibleByMouse
                                           | Qt::LinksAccessibleByKeyboard);
        }

        if (m_displayHints.limitWidth) {
            const int limit = widthLimit();
            // Using "setWordWrap(true)" alone will wrap the text already for small
            // widths, so do not require word wrapping until we hit limits.
            if (label->sizeHint().width() > limit) {
                label->setMaximumWidth(limit);
                label->setWordWrap(true);
            }
        } else {
            label->setWordWrap(true);
        }

        const TargetIdToDiagnosticTable table = m_targetIdsToDiagnostics;
        const bool hideToolTipAfterLinkActivation = m_displayHints.hideTooltipAfterLinkActivation;
        QObject::connect(label, &QLabel::linkActivated, [table, hideToolTipAfterLinkActivation]
                         (const QString &action) {
            const ClangBackEnd::DiagnosticContainer diagnostic = table.value(action);

            if (diagnostic == ClangBackEnd::DiagnosticContainer())
                QDesktopServices::openUrl(QUrl(action));
            else if (action.startsWith(LINK_ACTION_GOTO_LOCATION))
                openEditorAt(diagnostic);
            else if (action.startsWith(LINK_ACTION_APPLY_FIX))
                applyFixit(diagnostic);
            else
                QTC_CHECK(!"Link target cannot be handled.");

            if (hideToolTipAfterLinkActivation)
                ::Utils::ToolTip::hideImmediately();
        });

        return label;
    }

    QString htmlText(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics)
    {
        // For debugging, add: style='border-width:1px;border-color:black'
        QString text = "<table cellspacing='0' cellpadding='0' width='100%'>";

        foreach (const ClangBackEnd::DiagnosticContainer &diagnostic, diagnostics)
            text.append(tableRows(diagnostic));

        text.append("</table>");

        return text;
    }

private:
    enum class IndentMode { Indent, DoNotIndent };

    // Diagnostics from clazy/tidy do not have any category or option set but
    // we will conclude them from the diagnostic message.
    //
    // Ideally, libclang should provide the correct category/option by default.
    // However, tidy and clazy diagnostics use "custom diagnostic ids" and
    // clang's static diagnostic table does not know anything about them.
    //
    // For clazy/tidy diagnostics, we expect something like "some text [some option]", e.g.:
    //  * clazy: "Use the static QFileInfo::exists() instead. It's documented to be faster. [-Wclazy-qfileinfo-exists]"
    //  * tidy:  "use emplace_back instead of push_back [modernize-use-emplace]"
    static ClangBackEnd::DiagnosticContainer supplementedDiagnostic(
        const ClangBackEnd::DiagnosticContainer &diagnostic)
    {
        if (!diagnostic.category.isEmpty())
            return diagnostic; // OK, diagnostics from clang itself have this set.

        ClangBackEnd::DiagnosticContainer supplementedDiagnostic = diagnostic;

        using namespace ClangCodeModel::Utils;
        DiagnosticTextInfo info(diagnostic.text);
        supplementedDiagnostic.enableOption = info.option();
        supplementedDiagnostic.category = info.category();
        supplementedDiagnostic.text = info.textWithoutOption();

        for (auto &child : supplementedDiagnostic.children)
            child.text = DiagnosticTextInfo(diagnostic.text.toString()).textWithoutOption();

        return supplementedDiagnostic;
    }

    QString tableRows(const ClangBackEnd::DiagnosticContainer &diagnostic)
    {
        m_mainFilePath = m_displayHints.showFileNameInMainDiagnostic
                ? Utf8String()
                : diagnostic.location.filePath;

        const ClangBackEnd::DiagnosticContainer diag = supplementedDiagnostic(diagnostic);

        QString text;
        if (m_displayHints.showCategoryAndEnableOption)
            text.append(diagnosticCategoryAndEnableOptionRow(diag));
        text.append(diagnosticRow(diag, IndentMode::DoNotIndent));
        text.append(diagnosticRowsForChildren(diag));

        return text;
    }

    static QString documentationUrlForOption(const Utf8String &optionAsUtf8String)
    {
        if (optionAsUtf8String.isEmpty())
            return QString();

        QString option = optionAsUtf8String.toString();

        // Clazy
        if (ClangCodeModel::Utils::DiagnosticTextInfo::isClazyOption(option)) {
            option = optionAsUtf8String.mid(8); // Remove "-Wclazy-" prefix.
            return QString::fromUtf8(CLAZY_DOCUMENTATION_URL_TEMPLATE).arg(option);
        }

        // Clang itself
        if (option.startsWith("-W"))
            return QString();

        // Clang-Tidy
        return QString::fromUtf8(CppTools::Constants::TIDY_DOCUMENTATION_URL_TEMPLATE).arg(option);
    }

    static QString maybeClickableOption(const Utf8String &option)
    {
        if (option.isEmpty())
            return option;

        const QString link = documentationUrlForOption(option);
        if (link.isEmpty())
            return option;

        return wrapInLink(option.toString(), link);
    }

    static QString diagnosticCategoryAndEnableOptionRow(
            const ClangBackEnd::DiagnosticContainer &diagnostic)
    {
        const QString text = QString::fromLatin1(
            "  <tr>"
            "    <td align='left'><b>%1</b></td>"
            "    <td align='right'>&nbsp;<font color='gray'>%2</font></td>"
            "  </tr>")
            .arg(diagnostic.category, maybeClickableOption(diagnostic.enableOption));

        return text;
    }

    QString diagnosticText(const ClangBackEnd::DiagnosticContainer &diagnostic)
    {
        const bool hasFixit = m_displayHints.enableClickableFixits
                && !diagnostic.fixIts.isEmpty();
        const QString diagnosticText = diagnostic.text.toString().toHtmlEscaped();
        const QString text = QString::fromLatin1("%1: %2")
            .arg(clickableLocation(diagnostic, m_mainFilePath),
                 clickableFixIt(diagnostic, diagnosticText, hasFixit));

        return text;
    }

    QString diagnosticRow(const ClangBackEnd::DiagnosticContainer &diagnostic,
                          IndentMode indentMode)
    {
        const QString text = QString::fromLatin1(
            "  <tr>"
            "    <td colspan='2' align='left' style='%1'>%2</td>"
            "  </tr>")
            .arg(indentModeToHtmlStyle(indentMode),
                 diagnosticText(diagnostic));

        return text;
    }

    QString diagnosticRowsForChildren(const ClangBackEnd::DiagnosticContainer &diagnostic)
    {
        const QVector<ClangBackEnd::DiagnosticContainer> &children = diagnostic.children;
        QString text;

        if (children.size() <= 10) {
            text += diagnosticRowsForChildren(children.begin(), children.end());
        } else {
            text += diagnosticRowsForChildren(children.begin(), children.begin() + 7);
            text += ellipsisRow();
            text += diagnosticRowsForChildren(children.end() - 3, children.end());
        }

        return text;
    }

    QString diagnosticRowsForChildren(
            const QVector<ClangBackEnd::DiagnosticContainer>::const_iterator first,
            const QVector<ClangBackEnd::DiagnosticContainer>::const_iterator last)
    {
        QString text;

        for (auto it = first; it != last; ++it)
            text.append(diagnosticRow(*it, IndentMode::Indent));

        return text;
    }

    QString clickableLocation(const ClangBackEnd::DiagnosticContainer &diagnostic,
                              const QString &mainFilePath)
    {
        const ClangBackEnd::SourceLocationContainer &location = diagnostic.location;

        const QString filePrefix = fileNamePrefix(mainFilePath, location);
        const QString lineColumn = locationToString(location);
        const QString linkText = filePrefix + lineColumn;
        const QString targetId = generateTargetId(LINK_ACTION_GOTO_LOCATION, diagnostic);

        return wrapInLink(linkText, targetId);
    }

    QString clickableFixIt(const ClangBackEnd::DiagnosticContainer &diagnostic,
                           const QString &text,
                           bool hasFixIt)
    {
        if (!hasFixIt)
            return text;

        QString clickableText = text;
        QString nonClickableCategory;
        const int colonPosition = text.indexOf(QStringLiteral(": "));

        if (colonPosition != -1) {
            nonClickableCategory = text.mid(0, colonPosition + 2);
            clickableText = text.mid(colonPosition + 2);
        }

        const QString targetId = generateTargetId(LINK_ACTION_APPLY_FIX, diagnostic);

        return nonClickableCategory + wrapInLink(clickableText, targetId);
    }

    QString generateTargetId(const QString &targetPrefix,
                             const ClangBackEnd::DiagnosticContainer &diagnostic)
    {
        const QString idAsString = QString::number(++m_targetIdCounter);
        const QString targetId = targetPrefix + idAsString;
        m_targetIdsToDiagnostics.insert(targetId, diagnostic);

        return targetId;
    }

    static QString wrapInLink(const QString &text, const QString &target)
    {
        return QStringLiteral("<a href='%1' style='text-decoration:none'>%2</a>").arg(target, text);
    }

    static QString ellipsisRow()
    {
        return QString::fromLatin1(
            "  <tr>"
            "    <td colspan='2' align='left' style='%1'>...</td>"
            "  </tr>")
            .arg(indentModeToHtmlStyle(IndentMode::Indent));
    }

    static QString indentModeToHtmlStyle(IndentMode indentMode)
    {
        return indentMode == IndentMode::Indent
             ? QString("padding-left:10px")
             : QString();
    }

    static int widthLimit()
    {
        auto screen = QGuiApplication::screenAt(QCursor::pos());
        if (!screen)
            screen = QGuiApplication::primaryScreen();
        return screen->availableGeometry().width() / 2;
    }

private:
    const DisplayHints m_displayHints;

    using TargetIdToDiagnosticTable = QHash<QString, ClangBackEnd::DiagnosticContainer>;
    TargetIdToDiagnosticTable m_targetIdsToDiagnostics;
    unsigned m_targetIdCounter = 0;

    QString m_mainFilePath;
};

WidgetFromDiagnostics::DisplayHints toHints(const ClangDiagnosticWidget::Destination &destination)
{
    WidgetFromDiagnostics::DisplayHints hints;

    if (destination == ClangDiagnosticWidget::ToolTip) {
        hints.showCategoryAndEnableOption = true;
        hints.showFileNameInMainDiagnostic = false;
        hints.enableClickableFixits = true;
        hints.limitWidth = true;
        hints.hideTooltipAfterLinkActivation = true;
        hints.allowTextSelection = false;
    } else { // Info Bar
        hints.showCategoryAndEnableOption = false;
        hints.showFileNameInMainDiagnostic = true;
        // Clickable fixits might change toolchain headers, so disable.
        hints.enableClickableFixits = false;
        hints.limitWidth = false;
        hints.hideTooltipAfterLinkActivation = false;
        hints.allowTextSelection = true;
    }

    return hints;
}

} // anonymous namespace

namespace ClangCodeModel {
namespace Internal {

QString ClangDiagnosticWidget::createText(
    const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
    const ClangDiagnosticWidget::Destination &destination)
{
    const QString htmlText = WidgetFromDiagnostics(toHints(destination)).htmlText(diagnostics);

    QTextDocument document;
    document.setHtml(htmlText);
    QString text = document.toPlainText();

    if (text.startsWith('\n'))
        text = text.mid(1);
    if (text.endsWith('\n'))
        text.chop(1);

    return text;
}

QWidget *ClangDiagnosticWidget::createWidget(
    const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics, const Destination &destination)
{
    return WidgetFromDiagnostics(toHints(destination)).createWidget(diagnostics);
}

} // namespace Internal
} // namespace ClangCodeModel