aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/clangtools/diagnosticmark.cpp
blob: 90b4e19812d64e4b49cf11a9ce8ce42842ab5c20 (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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "diagnosticmark.h"

#include "clangtoolsconstants.h"
#include "clangtoolstr.h"
#include "clangtoolsutils.h"
#include "diagnosticconfigswidget.h"

#include <texteditor/textdocument.h>
#include <utils/utilsicons.h>
#include <utils/stringutils.h>

#include <QAction>

using namespace TextEditor;
using namespace Utils;

namespace ClangTools {
namespace Internal {

DiagnosticMark::DiagnosticMark(const Diagnostic &diagnostic, TextDocument *document)
    : TextMark(document,
               diagnostic.location.line,
               {Tr::tr("Clang Tools"), Id(Constants::DIAGNOSTIC_MARK_ID)})
    , m_diagnostic(diagnostic)
{
    setSettingsPage(Constants::SETTINGS_PAGE_ID);

    const bool isError = diagnostic.type == "error" || diagnostic.type == "fatal";
    setColor(isError ? Theme::CodeModel_Error_TextMarkColor : Theme::CodeModel_Warning_TextMarkColor);
    setPriority(isError ? TextEditor::TextMark::HighPriority : TextEditor::TextMark::NormalPriority);
    QIcon markIcon = diagnostic.icon();
    setIcon(markIcon.isNull() ? Icons::CODEMODEL_WARNING.icon() : markIcon);
    setToolTip(createDiagnosticToolTipString(diagnostic, std::nullopt, true));
    setLineAnnotation(diagnostic.description);
    setActionsProvider([diagnostic] {
        // Copy to clipboard action
        QList<QAction *> actions;
        QAction *action = new QAction();
        action->setIcon(Icon::fromTheme("edit-copy"));
        action->setToolTip(Tr::tr("Copy to Clipboard"));
        QObject::connect(action, &QAction::triggered, [diagnostic] {
            const QString text = createFullLocationString(diagnostic.location)
                                 + ": "
                                 + diagnostic.description;
            setClipboardAndSelection(text);
        });
        actions << action;

        // Disable diagnostic action
        action = new QAction();
        action->setIcon(Icons::BROKEN.icon());
        action->setToolTip(Tr::tr("Disable Diagnostic"));
        QObject::connect(action, &QAction::triggered, [diagnostic] { disableChecks({diagnostic}); });
        actions << action;
        return actions;
    });
}

DiagnosticMark::DiagnosticMark(const Diagnostic &diagnostic)
    : DiagnosticMark(diagnostic, TextDocument::textDocumentForFilePath(diagnostic.location.filePath))
{}

void DiagnosticMark::disable()
{
    if (!m_enabled)
        return;
    m_enabled = false;
    if (m_diagnostic.type == "error" || m_diagnostic.type == "fatal")
        setIcon(Icons::CODEMODEL_DISABLED_ERROR.icon());
    else
        setIcon(Icons::CODEMODEL_DISABLED_WARNING.icon());
    setColor(Theme::Color::IconsDisabledColor);
}

bool DiagnosticMark::enabled() const
{
    return m_enabled;
}

Diagnostic DiagnosticMark::diagnostic() const
{
    return m_diagnostic;
}

} // namespace Internal
} // namespace ClangTools