Commit b064b067 authored by Nikolai Kosjar's avatar Nikolai Kosjar Committed by Alessandro Portale

Clang: Display also child diagnostics in tooltips

...by introducing a custom tooltip widget for diagnostics.

Locations and fixits of child diagnostics are presented as clickable
links, leading to that position in the editor or to the execution of
that fix it.

Change-Id: I83e801e22d0421dd29275e333e5dd91587885cf1
Reviewed-by: default avatarAlessandro Portale <alessandro.portale@theqtcompany.com>
parent 47a5c7f4
......@@ -206,6 +206,11 @@ void ToolTip::hide()
instance()->hideTipWithDelay();
}
void ToolTip::hideImmediately()
{
instance()->hideTipImmediately();
}
void ToolTip::hideTipWithDelay()
{
if (!m_hideDelayTimer.isActive())
......
......@@ -78,6 +78,7 @@ public:
const QString &helpId = QString(), const QRect &rect = QRect());
static void move(const QPoint &pos, QWidget *w);
static void hide();
static void hideImmediately();
static bool isVisible();
static QPoint offsetFromPosition();
......
......@@ -20,6 +20,7 @@ SOURCES += \
clangcompletioncontextanalyzer.cpp \
clangdiagnosticfilter.cpp \
clangdiagnosticmanager.cpp \
clangdiagnostictooltipwidget.cpp \
clangeditordocumentparser.cpp \
clangeditordocumentprocessor.cpp \
clangfixitoperation.cpp \
......@@ -47,6 +48,7 @@ HEADERS += \
clangconstants.h \
clangdiagnosticfilter.h \
clangdiagnosticmanager.h \
clangdiagnostictooltipwidget.h \
clangeditordocumentparser.h \
clangeditordocumentprocessor.h \
clangfixitoperation.h \
......
......@@ -65,6 +65,8 @@ QtcPlugin {
"clangdiagnosticfilter.h",
"clangdiagnosticmanager.cpp",
"clangdiagnosticmanager.h",
"clangdiagnostictooltipwidget.cpp",
"clangdiagnostictooltipwidget.h",
"clangeditordocumentparser.cpp",
"clangeditordocumentparser.h",
"clangeditordocumentprocessor.cpp",
......
/****************************************************************************
**
** 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 <coreplugin/editormanager/editormanager.h>
#include <utils/tooltip/tooltip.h>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
namespace {
const char LINK_ACTION_GOTO_LOCATION[] = "#gotoLocation";
const char LINK_ACTION_APPLY_FIX[] = "#applyFix";
QString wrapInBoldTags(const QString &text)
{
return QStringLiteral("<b>") + text + QStringLiteral("</b>");
}
QString wrapInLink(const QString &text, const QString &target)
{
return QStringLiteral("<a href='%1' style='text-decoration:none'>%2</a>").arg(target, text);
}
QString wrapInColor(const QString &text, const QByteArray &color)
{
return QStringLiteral("<font color='%2'>%1</font>").arg(text, QString::fromUtf8(color));
}
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());
}
QString clickableLocation(const QString &mainFilePath,
const ClangBackEnd::SourceLocationContainer &location)
{
const QString filePrefix = fileNamePrefix(mainFilePath, location);
const QString lineColumn = locationToString(location);
const QString linkText = filePrefix + lineColumn;
return wrapInLink(linkText, QLatin1String(LINK_ACTION_GOTO_LOCATION));
}
void openEditorAt(const ClangBackEnd::SourceLocationContainer &location)
{
Core::EditorManager::openEditorAt(location.filePath().toString(),
int(location.line()),
int(location.column() - 1));
}
void applyFixit(const ClangBackEnd::SourceLocationContainer &location,
const QVector<ClangBackEnd::FixItContainer> &fixits)
{
ClangCodeModel::ClangFixItOperation operation(location.filePath(), Utf8String(), fixits);
operation.perform();
}
template <typename LayoutType>
LayoutType *createLayout()
{
auto *layout = new LayoutType;
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(2);
return layout;
}
class MainDiagnosticWidget : public QWidget
{
Q_OBJECT
public:
MainDiagnosticWidget(const ClangBackEnd::DiagnosticContainer &diagnostic)
{
setContentsMargins(0, 0, 0, 0);
auto *mainLayout = createLayout<QVBoxLayout>();
// Set up header row: category + responsible option
const QString category = diagnostic.category();
const QString diagnosticText = diagnostic.text().toString().toHtmlEscaped();
const QString responsibleOption = diagnostic.enableOption();
const ClangBackEnd::SourceLocationContainer location = diagnostic.location();
auto *headerLayout = createLayout<QHBoxLayout>();
headerLayout->addWidget(new QLabel(wrapInBoldTags(category)), 1);
auto *responsibleOptionLabel = new QLabel(wrapInColor(responsibleOption, "gray"));
headerLayout->addWidget(responsibleOptionLabel, 0);
mainLayout->addLayout(headerLayout);
// Set up main row: diagnostic text
const QString text = clickableLocation(location.filePath(), location)
+ QStringLiteral(": ")
+ diagnosticText;
auto *mainTextLabel = new QLabel(text);
mainTextLabel->setTextFormat(Qt::RichText);
QObject::connect(mainTextLabel, &QLabel::linkActivated, [location](const QString &) {
openEditorAt(location);
Utils::ToolTip::hideImmediately();
});
mainLayout->addWidget(mainTextLabel);
setLayout(mainLayout);
}
};
QString clickableFixIt(const QString &text, bool hasFixIt)
{
if (!hasFixIt)
return text;
const QString notePrefix = QStringLiteral("note: ");
QString modifiedText = text;
if (modifiedText.startsWith(notePrefix))
modifiedText = modifiedText.mid(notePrefix.size());
return notePrefix + wrapInLink(modifiedText, QLatin1String(LINK_ACTION_APPLY_FIX));
}
QWidget *createChildDiagnosticLabel(const ClangBackEnd::DiagnosticContainer &diagnostic,
const QString &mainFilePath)
{
const bool hasFixit = !diagnostic.fixIts().isEmpty();
const QString diagnosticText = diagnostic.text().toString().toHtmlEscaped();
const QString text = clickableLocation(mainFilePath, diagnostic.location())
+ QStringLiteral(": ")
+ clickableFixIt(diagnosticText, hasFixit);
const ClangBackEnd::SourceLocationContainer location = diagnostic.location();
const QVector<ClangBackEnd::FixItContainer> fixits = diagnostic.fixIts();
auto *label = new QLabel(text);
label->setContentsMargins(10, 0, 0, 0); // indent
label->setTextFormat(Qt::RichText);
QObject::connect(label, &QLabel::linkActivated, [location, fixits](const QString &action) {
if (action == QLatin1String(LINK_ACTION_APPLY_FIX))
applyFixit(location, fixits);
else
openEditorAt(location);
Utils::ToolTip::hideImmediately();
});
return label;
}
} // anonymous namespace
namespace ClangCodeModel {
namespace Internal {
ClangDiagnosticToolTipWidget::ClangDiagnosticToolTipWidget(
const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
QWidget *parent)
: Utils::FakeToolTip(parent)
{
auto *mainLayout = createLayout<QVBoxLayout>();
foreach (const auto &diagnostic, diagnostics) {
// Set up header and text row of main diagnostic
mainLayout->addWidget(new MainDiagnosticWidget(diagnostic));
// Set up child rows
const QString mainFilePath = diagnostic.location().filePath();
foreach (const auto &childDiagnostic, diagnostic.children())
mainLayout->addWidget(createChildDiagnosticLabel(childDiagnostic, mainFilePath));
}
setLayout(mainLayout);
}
} // namespace Internal
} // namespace ClangCodeModel
#include "clangdiagnostictooltipwidget.moc"
/****************************************************************************
**
** 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.
**
****************************************************************************/
#pragma once
#include <clangbackendipc/diagnosticcontainer.h>
#include <utils/faketooltip.h>
namespace ClangCodeModel {
namespace Internal {
class ClangDiagnosticToolTipWidget : public Utils::FakeToolTip
{
Q_OBJECT
public:
explicit ClangDiagnosticToolTipWidget(
const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
QWidget *parent = 0);
};
} // namespace Internal
} // namespace ClangCodeModel
......@@ -26,6 +26,7 @@
#include "clangeditordocumentprocessor.h"
#include "clangbackendipcintegration.h"
#include "clangdiagnostictooltipwidget.h"
#include "clangfixitoperation.h"
#include "clangfixitoperationsextractor.h"
#include "clanghighlightingmarksreporter.h"
......@@ -239,55 +240,6 @@ bool ClangEditorDocumentProcessor::hasDiagnosticsAt(uint line, uint column) cons
return m_diagnosticManager.hasDiagnosticsAt(line, column);
}
namespace {
bool isHelpfulChildDiagnostic(const ClangBackEnd::DiagnosticContainer &parentDiagnostic,
const ClangBackEnd::DiagnosticContainer &childDiagnostic)
{
auto parentLocation = parentDiagnostic.location();
auto childLocation = childDiagnostic.location();
return parentLocation == childLocation;
}
QString diagnosticText(const ClangBackEnd::DiagnosticContainer &diagnostic)
{
QString text = diagnostic.category().toString()
+ QStringLiteral("\n\n")
+ diagnostic.text().toString();
#ifdef QT_DEBUG
if (!diagnostic.disableOption().isEmpty()) {
text += QStringLiteral(" (disable with ")
+ diagnostic.disableOption().toString()
+ QStringLiteral(")");
}
#endif
for (auto &&childDiagnostic : diagnostic.children()) {
if (isHelpfulChildDiagnostic(diagnostic, childDiagnostic))
text += QStringLiteral("\n ") + childDiagnostic.text().toString();
}
return text;
}
QString generateTooltipText(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics)
{
QString text;
foreach (const ClangBackEnd::DiagnosticContainer &diagnostic, diagnostics) {
if (text.isEmpty())
text += diagnosticText(diagnostic);
else
text += QStringLiteral("\n\n\n") + diagnosticText(diagnostic);
}
return text;
}
} // anonymous namespace
void ClangEditorDocumentProcessor::showDiagnosticTooltip(const QPoint &point,
QWidget *parent,
uint line,
......@@ -295,10 +247,9 @@ void ClangEditorDocumentProcessor::showDiagnosticTooltip(const QPoint &point,
{
const QVector<ClangBackEnd::DiagnosticContainer> diagnostics
= m_diagnosticManager.diagnosticsAt(line, column);
auto *tooltipWidget = new ClangDiagnosticToolTipWidget(diagnostics, parent);
const QString tooltipText = generateTooltipText(diagnostics);
::Utils::ToolTip::show(point, tooltipText, parent);
::Utils::ToolTip::show(point, tooltipWidget, parent);
}
ClangBackEnd::FileContainer ClangEditorDocumentProcessor::fileContainerWithArguments() const
......
......@@ -133,7 +133,7 @@ DiagnosticSet Diagnostic::childDiagnostics() const
return DiagnosticSet(clang_getChildDiagnostics(cxDiagnostic));
}
DiagnosticContainer Diagnostic::toDiagnosticContainer(const IsAcceptedDiagnostic &isAcceptedChildDiagnostic) const
DiagnosticContainer Diagnostic::toDiagnosticContainer() const
{
return DiagnosticContainer(text(),
category(),
......@@ -142,14 +142,7 @@ DiagnosticContainer Diagnostic::toDiagnosticContainer(const IsAcceptedDiagnostic
location().toSourceLocationContainer(),
getSourceRangeContainers(),
getFixItContainers(),
childDiagnostics().toDiagnosticContainers(isAcceptedChildDiagnostic));
}
DiagnosticContainer Diagnostic::toDiagnosticContainer() const
{
const auto acceptAllDiagnostics = [](const Diagnostic &) { return true; };
return toDiagnosticContainer(acceptAllDiagnostics);
childDiagnostics().toDiagnosticContainers());
}
QVector<SourceRangeContainer> Diagnostic::getSourceRangeContainers() const
......
......@@ -71,9 +71,6 @@ public:
std::vector<FixIt> fixIts() const;
DiagnosticSet childDiagnostics() const;
using IsAcceptedDiagnostic = std::function<bool (const Diagnostic &)>;
DiagnosticContainer toDiagnosticContainer(
const IsAcceptedDiagnostic &isAcceptedChildDiagnostic) const;
DiagnosticContainer toDiagnosticContainer() const;
private:
......
......@@ -88,14 +88,14 @@ QVector<DiagnosticContainer> DiagnosticSet::toDiagnosticContainers() const
}
QVector<DiagnosticContainer> DiagnosticSet::toDiagnosticContainers(
const Diagnostic::IsAcceptedDiagnostic &isAcceptedDiagnostic) const
const IsAcceptedDiagnostic &isAcceptedDiagnostic) const
{
QVector<DiagnosticContainer> diagnosticContainers;
diagnosticContainers.reserve(size());
for (const Diagnostic &diagnostic : *this) {
if (isAcceptedDiagnostic(diagnostic))
diagnosticContainers.push_back(diagnostic.toDiagnosticContainer(isAcceptedDiagnostic));
diagnosticContainers.push_back(diagnostic.toDiagnosticContainer());
}
return diagnosticContainers;
......
......@@ -67,9 +67,10 @@ public:
ConstIterator begin() const;
ConstIterator end() const;
using IsAcceptedDiagnostic = std::function<bool (const Diagnostic &)>;
QVector<DiagnosticContainer> toDiagnosticContainers() const;
QVector<DiagnosticContainer> toDiagnosticContainers(
const Diagnostic::IsAcceptedDiagnostic &isAcceptedDiagnostic) const;
const IsAcceptedDiagnostic &isAcceptedDiagnostic) const;
private:
DiagnosticSet(CXDiagnosticSet cxDiagnosticSet);
......
......@@ -160,18 +160,6 @@ TEST_F(DiagnosticSet, ToDiagnosticContainersFiltersOutTopLevelItem)
ASSERT_TRUE(diagnostics.isEmpty());
}
TEST_F(DiagnosticSet, ToDiagnosticContainersFiltersOutChildren)
{
const auto diagnosticContainerWithoutChild = expectedDiagnostic(WithoutChild);
const auto acceptMainFileDiagnostics = [this](const Diagnostic &diagnostic) {
return diagnostic.location().filePath() == translationUnitMainFile.filePath();
};
const auto diagnostics = diagnosticSetWithChildren.toDiagnosticContainers(acceptMainFileDiagnostics);
ASSERT_THAT(diagnostics, Contains(IsDiagnosticContainer(diagnosticContainerWithoutChild)));
}
DiagnosticContainer DiagnosticSet::expectedDiagnostic(DiagnosticSet::ChildMode childMode) const
{
QVector<DiagnosticContainer> children;
......
......@@ -170,18 +170,6 @@ TEST_F(Diagnostic, toDiagnosticContainerLetChildrenThroughByDefault)
ASSERT_THAT(diagnostic, IsDiagnosticContainer(diagnosticWithChild));
}
TEST_F(Diagnostic, toDiagnosticContainerFiltersOutChildren)
{
const auto diagnosticWithoutChild = expectedDiagnostic(WithoutChild);
const auto acceptDiagnosticWithSpecificText = [](const ::Diagnostic &diagnostic) {
return diagnostic.text() != Utf8StringLiteral("note: previous declaration is here");
};
const auto diagnostic = diagnosticSet.front().toDiagnosticContainer(acceptDiagnosticWithSpecificText);
ASSERT_THAT(diagnostic, IsDiagnosticContainer(diagnosticWithoutChild));
}
DiagnosticContainer Diagnostic::expectedDiagnostic(Diagnostic::ChildMode childMode) const
{
QVector<DiagnosticContainer> children;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment