Clang: Add API to convert diagnostics to plain text

This is for a follow-up change that will allow to copy the diagnostic
text from the tooltip to the clipboard.

Change-Id: Iad5343a819c84ca83d562f69dcf2f50e1d4785c4
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Nikolai Kosjar
2019-01-28 11:43:18 +01:00
parent e648c079b8
commit e5fbeb6219
4 changed files with 38 additions and 26 deletions

View File

@@ -39,6 +39,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QHash> #include <QHash>
#include <QLabel> #include <QLabel>
#include <QTextDocument>
#include <QUrl> #include <QUrl>
using namespace ClangCodeModel; using namespace ClangCodeModel;
@@ -101,16 +102,6 @@ public:
bool allowTextSelection; bool allowTextSelection;
}; };
static QWidget *create(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
const DisplayHints &displayHints)
{
WidgetFromDiagnostics converter(displayHints);
return converter.createWidget(diagnostics);
}
private:
enum class IndentMode { Indent, DoNotIndent };
WidgetFromDiagnostics(const DisplayHints &displayHints) WidgetFromDiagnostics(const DisplayHints &displayHints)
: m_displayHints(displayHints) : m_displayHints(displayHints)
{ {
@@ -177,6 +168,9 @@ private:
return text; return text;
} }
private:
enum class IndentMode { Indent, DoNotIndent };
static bool isClazyOption(const QString &option) { return option.startsWith("-Wclazy"); } static bool isClazyOption(const QString &option) { return option.startsWith("-Wclazy"); }
class DiagnosticTextInfo class DiagnosticTextInfo
@@ -446,18 +440,11 @@ private:
QString m_mainFilePath; QString m_mainFilePath;
}; };
} // anonymous namespace WidgetFromDiagnostics::DisplayHints toHints(const ClangDiagnosticWidget::Destination &destination)
namespace ClangCodeModel {
namespace Internal {
QWidget *ClangDiagnosticWidget::create(
const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
const Destination &destination)
{ {
WidgetFromDiagnostics::DisplayHints hints; WidgetFromDiagnostics::DisplayHints hints;
if (destination == ToolTip) { if (destination == ClangDiagnosticWidget::ToolTip) {
hints.showCategoryAndEnableOption = true; hints.showCategoryAndEnableOption = true;
hints.showFileNameInMainDiagnostic = false; hints.showFileNameInMainDiagnostic = false;
hints.enableClickableFixits = true; hints.enableClickableFixits = true;
@@ -474,7 +461,28 @@ QWidget *ClangDiagnosticWidget::create(
hints.allowTextSelection = true; hints.allowTextSelection = true;
} }
return WidgetFromDiagnostics::create(diagnostics, hints); 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);
return document.toPlainText();
}
QWidget *ClangDiagnosticWidget::createWidget(
const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics, const Destination &destination)
{
return WidgetFromDiagnostics(toHints(destination)).createWidget(diagnostics);
} }
} // namespace Internal } // namespace Internal

View File

@@ -38,8 +38,10 @@ class ClangDiagnosticWidget {
public: public:
enum Destination { ToolTip, InfoBar }; enum Destination { ToolTip, InfoBar };
static QWidget *create(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics, static QString createText(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
const Destination &destination); const Destination &destination);
static QWidget *createWidget(const QVector<ClangBackEnd::DiagnosticContainer> &diagnostics,
const Destination &destination);
}; };
} // namespace Internal } // namespace Internal

View File

@@ -306,7 +306,8 @@ void ClangEditorDocumentProcessor::addDiagnosticToolTipToLayout(uint line,
const QVector<ClangBackEnd::DiagnosticContainer> diagnostics const QVector<ClangBackEnd::DiagnosticContainer> diagnostics
= m_diagnosticManager.diagnosticsAt(line, column); = m_diagnosticManager.diagnosticsAt(line, column);
target->addWidget(ClangDiagnosticWidget::create(diagnostics, ClangDiagnosticWidget::ToolTip)); target->addWidget(
ClangDiagnosticWidget::createWidget(diagnostics, ClangDiagnosticWidget::ToolTip));
auto link = TextEditor::DisplaySettings::createAnnotationSettingsLink(); auto link = TextEditor::DisplaySettings::createAnnotationSettingsLink();
target->addWidget(link); target->addWidget(link);
target->setAlignment(link, Qt::AlignRight); target->setAlignment(link, Qt::AlignRight);
@@ -648,8 +649,8 @@ ClangEditorDocumentProcessor::creatorForHeaderErrorDiagnosticWidget(
vbox->setContentsMargins(10, 0, 0, 2); vbox->setContentsMargins(10, 0, 0, 2);
vbox->setSpacing(2); vbox->setSpacing(2);
vbox->addWidget(ClangDiagnosticWidget::create({firstHeaderErrorDiagnostic}, vbox->addWidget(ClangDiagnosticWidget::createWidget({firstHeaderErrorDiagnostic},
ClangDiagnosticWidget::InfoBar)); ClangDiagnosticWidget::InfoBar));
auto widget = new QWidget; auto widget = new QWidget;
widget->setLayout(vbox); widget->setLayout(vbox);

View File

@@ -101,7 +101,8 @@ bool ClangTextMark::addToolTipContent(QLayout *target) const
{ {
using Internal::ClangDiagnosticWidget; using Internal::ClangDiagnosticWidget;
QWidget *widget = ClangDiagnosticWidget::create({m_diagnostic}, ClangDiagnosticWidget::ToolTip); QWidget *widget = ClangDiagnosticWidget::createWidget({m_diagnostic},
ClangDiagnosticWidget::ToolTip);
target->addWidget(widget); target->addWidget(widget);
return true; return true;