Commit 9428624f authored by Marco Bubke's avatar Marco Bubke Committed by Tim Jenssen

Clang: Tool tips for clang query diagnostics

If you hover a diagnostics in for a clang query you get now a simple tool
tip.

Change-Id: I6352dd3d4b9a33c183e69037eac903469b90eea4
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 9b5d6546
......@@ -55,7 +55,7 @@ public:
return m_contextType;
}
Utils::SmallString contextTypeText() const;
CMBIPC_EXPORT Utils::SmallString contextTypeText() const;
const Utils::SmallStringVector &arguments() const
{
......
......@@ -33,8 +33,8 @@ namespace ClangBackEnd {
QDebug operator<<(QDebug debug, const DynamicASTMatcherDiagnosticMessageContainer &container)
{
debug.nospace() << "DynamicASTMatcherDiagnosticMessageContainer("
<< container.sourceRange() << ", "
<< container.errorTypeText() << ", "
<< container.sourceRange() << ", "
<< container.arguments()
<< ")";
......
......@@ -55,7 +55,7 @@ public:
return m_errorType;
}
Utils::SmallString errorTypeText() const;
CMBIPC_EXPORT Utils::SmallString errorTypeText() const;
const Utils::SmallStringVector &arguments() const
{
......
......@@ -72,6 +72,18 @@ bool ClangQueryHighlighter::hasDiagnostics() const
return m_marker.hasMessagesOrContexts();
}
ClangBackEnd::DynamicASTMatcherDiagnosticMessageContainers
ClangQueryHighlighter::messagesForLineAndColumn(uint line, uint column) const
{
return m_marker.messagesForLineAndColumn(line, column);
}
ClangBackEnd::DynamicASTMatcherDiagnosticContextContainers
ClangQueryHighlighter::contextsForLineAndColumn(uint line, uint column) const
{
return m_marker.contextsForLineAndColumn(line, column);
}
void ClangQueryHighlighter::highlightBlock(const QString &text)
{
int currentLineNumber = currentBlock().blockNumber() + 1;
......
......@@ -44,6 +44,12 @@ public:
bool hasDiagnostics() const;
ClangBackEnd::DynamicASTMatcherDiagnosticMessageContainers
messagesForLineAndColumn(uint line, uint column) const;
ClangBackEnd::DynamicASTMatcherDiagnosticContextContainers
contextsForLineAndColumn(uint line, uint column) const;
protected:
void highlightBlock(const QString &text) override;
......
......@@ -250,6 +250,85 @@ public:
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;
......
/****************************************************************************
**
** 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.
**
****************************************************************************/
#include "clangqueryhoverhandler.h"
#include "clangqueryhighlighter.h"
#include <dynamicastmatcherdiagnosticmessagecontainer.h>
#include <texteditor/texteditor.h>
namespace ClangRefactoring {
ClangQueryHoverHandler::ClangQueryHoverHandler(ClangQueryHighlighter *highligher)
: m_highligher(highligher)
{
}
void ClangQueryHoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorWidget, int position)
{
using Messages = ClangBackEnd::DynamicASTMatcherDiagnosticMessageContainers;
using Contexts = ClangBackEnd::DynamicASTMatcherDiagnosticContextContainers;
QTextCursor textCursor = editorWidget->textCursor();
textCursor.setPosition(position);
int line = textCursor.blockNumber() + 1;
int column = textCursor.columnNumber() + 1;
Messages messages = m_highligher->messagesForLineAndColumn(uint(line), uint(column));
Contexts contexts = m_highligher->contextsForLineAndColumn(uint(line), uint(column));
if (!messages.empty())
setToolTip(QString("%1: %2").arg(messages[0].errorTypeText()).arg(messages[0].arguments().join(", ")));
else if (!contexts.empty())
setToolTip(QString("%1: %2").arg(contexts[0].contextTypeText()).arg(contexts[0].arguments().join(", ")));
}
} // namespace ClangRefactoring
/****************************************************************************
**
** 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 <texteditor/basehoverhandler.h>
namespace ClangRefactoring {
class ClangQueryHighlighter;
class ClangQueryHoverHandler : public TextEditor::BaseHoverHandler
{
public:
ClangQueryHoverHandler(ClangQueryHighlighter *highligher);
protected:
void identifyMatch(TextEditor::TextEditorWidget *editorWidget, int position) override;
private:
ClangQueryHighlighter *m_highligher;
};
} // namespace ClangRefactoring
......@@ -26,19 +26,24 @@
#include "clangquerytexteditorwidget.h"
#include "clangqueryhighlighter.h"
#include "clangqueryhoverhandler.h"
#include <texteditor/textdocument.h>
namespace ClangRefactoring {
ClangQueryTextEditorWidget::ClangQueryTextEditorWidget(QWidget *parent)
: BaseClangQueryTextEditorWidget(parent)
: BaseClangQueryTextEditorWidget(parent),
m_syntaxHighlighter(new ClangQueryHighlighter),
m_hoverHandler(std::make_unique<ClangQueryHoverHandler>(m_syntaxHighlighter))
{
m_syntaxHighlighter = new ClangQueryHighlighter;
textDocument()->setSyntaxHighlighter(m_syntaxHighlighter);
addHoverHandler(m_hoverHandler.get());
}
ClangQueryTextEditorWidget::~ClangQueryTextEditorWidget() = default;
ClangQueryHighlighter *ClangQueryTextEditorWidget::syntaxHighlighter() const
{
return m_syntaxHighlighter;
......
......@@ -27,9 +27,12 @@
#include "baseclangquerytexteditorwidget.h"
#include <memory>
namespace ClangRefactoring {
class ClangQueryHighlighter;
class ClangQueryHoverHandler;
class ClangQueryTextEditorWidget : public BaseClangQueryTextEditorWidget
{
......@@ -37,11 +40,13 @@ class ClangQueryTextEditorWidget : public BaseClangQueryTextEditorWidget
public:
ClangQueryTextEditorWidget(QWidget *parent);
~ClangQueryTextEditorWidget();
ClangQueryHighlighter *syntaxHighlighter() const;
private:
ClangQueryHighlighter *m_syntaxHighlighter;
std::unique_ptr<ClangQueryHoverHandler> m_hoverHandler;
};
} // namespace ClangRefactoring
......@@ -14,7 +14,8 @@ HEADERS += \
clangqueryprojectsfindfilterwidget.h \
clangqueryexampletexteditorwidget.h \
clangquerytexteditorwidget.h \
baseclangquerytexteditorwidget.h
baseclangquerytexteditorwidget.h \
clangqueryhoverhandler.h
SOURCES += \
clangrefactoringplugin.cpp \
......@@ -24,7 +25,8 @@ SOURCES += \
clangqueryprojectsfindfilterwidget.cpp \
clangqueryexampletexteditorwidget.cpp \
clangquerytexteditorwidget.cpp \
baseclangquerytexteditorwidget.cpp
baseclangquerytexteditorwidget.cpp \
clangqueryhoverhandler.cpp
FORMS += \
clangqueryprojectsfindfilter.ui
......@@ -5339,6 +5339,11 @@ void TextEditorWidget::showDefaultContextMenu(QContextMenuEvent *e, Id menuConte
menu.exec(e->globalPos());
}
void TextEditorWidget::addHoverHandler(BaseHoverHandler *handler)
{
d->m_hoverHandlers.append(handler);
}
void TextEditorWidget::extraAreaLeaveEvent(QEvent *)
{
d->extraAreaPreviousMarkTooltipRequestedLine = -1;
......
......@@ -512,6 +512,8 @@ protected:
virtual void finalizeInitializationAfterDuplication(TextEditorWidget *) {}
static QTextCursor flippedCursor(const QTextCursor &cursor);
void addHoverHandler(BaseHoverHandler *handler);
public:
struct Link
{
......
......@@ -92,7 +92,7 @@ SourceRangesContainer ClangQuery::takeSourceRanges()
return std::move(sourceRangesContainer);
}
std::vector<DynamicASTMatcherDiagnosticContainer> ClangQuery::takeDiagnosticContainers()
DynamicASTMatcherDiagnosticContainers ClangQuery::takeDiagnosticContainers()
{
return std::move(diagnosticContainers_);
}
......
......@@ -58,7 +58,7 @@ public:
void findLocations();
SourceRangesContainer takeSourceRanges();
std::vector<DynamicASTMatcherDiagnosticContainer> takeDiagnosticContainers();
DynamicASTMatcherDiagnosticContainers takeDiagnosticContainers();
private:
void parseDiagnostics(const clang::ast_matchers::dynamic::Diagnostics &diagnostics);
......
......@@ -32,12 +32,16 @@
namespace {
using testing::AllOf;
using testing::Contains;
using testing::ElementsAre;
using testing::Pointwise;
using testing::IsEmpty;
using testing::Not;
using testing::InSequence;
using testing::_;
using SourceRange = ClangBackEnd::V2::SourceRangeContainer;
using Message = ClangBackEnd::DynamicASTMatcherDiagnosticMessageContainer;
using Context = ClangBackEnd::DynamicASTMatcherDiagnosticContextContainer;
using Messages = ClangBackEnd::DynamicASTMatcherDiagnosticMessageContainers;
using Contexts = ClangBackEnd::DynamicASTMatcherDiagnosticContextContainers;
using Marker = ClangRefactoring::ClangQueryHighlightMarker<MockSyntaxHighlighter>;
......@@ -54,7 +58,6 @@ protected:
QTextCharFormat contextTextFormat;
MockSyntaxHighlighter highlighter;
Marker marker{highlighter};
};
TEST_F(ClangQueryHighlightMarker, NoCallForNoMessagesAndContexts)
......@@ -120,6 +123,262 @@ TEST_F(ClangQueryHighlightMarker, CallForMessagesAndContextForAMultiLine)
marker.highlightBlock(3, " ) ");
}
TEST_F(ClangQueryHighlightMarker, NoMessagesIfEmpty)
{
Messages messages;
Contexts contexts;
marker.setMessagesAndContexts(std::move(messages), std::move(contexts));
auto foundMessages = marker.messagesForLineAndColumn(1, 1);
ASSERT_THAT(foundMessages, IsEmpty());
}
TEST_F(ClangQueryHighlightMarker, NoMessagesForBeforePosition)
{
Messages messages{{{1, 1, 5, 0, 3, 3, 0},
ErrorType::RegistryMatcherNotFound,
{"foo"}}};
Contexts contexts;
marker.setMessagesAndContexts(std::move(messages), std::move(contexts));
auto foundMessages = marker.messagesForLineAndColumn(1, 4);
ASSERT_THAT(foundMessages, IsEmpty());
}
TEST_F(ClangQueryHighlightMarker, NoMessagesForAfterPosition)
{
Messages messages{{{1, 1, 5, 0, 3, 3, 0},
ErrorType::RegistryMatcherNotFound,
{"foo"}}};
Contexts contexts;
marker.setMessagesAndContexts(std::move(messages), std::move(contexts));
auto foundMessages = marker.messagesForLineAndColumn(3, 4);
ASSERT_THAT(foundMessages, IsEmpty());
}
TEST_F(ClangQueryHighlightMarker, OneMessagesForInsidePosition)
{
Message message{{1, 1, 5, 0, 3, 3, 0},
ErrorType::RegistryMatcherNotFound,
{"foo"}};
Messages messages{message.clone()};
Contexts contexts;
marker.setMessagesAndContexts(std::move(messages), std::move(contexts));
auto foundMessages = marker.messagesForLineAndColumn(2, 3);
ASSERT_THAT(foundMessages, ElementsAre(message));
}
TEST_F(ClangQueryHighlightMarker, NoMessagesForOutsidePosition)
{
Message message{{1, 1, 5, 0, 3, 3, 0},
ErrorType::RegistryMatcherNotFound,
{"foo"}};
Messages messages{message.clone()};
Contexts contexts;
marker.setMessagesAndContexts(std::move(messages), std::move(contexts));
auto foundMessages = marker.messagesForLineAndColumn(3, 4);
ASSERT_THAT(foundMessages, IsEmpty());
}
TEST_F(ClangQueryHighlightMarker, AfterStartColumnBeforeLine)
{
SourceRange sourceRange{1, 2, 5, 0, 3, 3, 0};
bool isAfterStartColumn = marker.isInsideRange(sourceRange, 1, 6);
ASSERT_FALSE(isAfterStartColumn);
}
TEST_F(ClangQueryHighlightMarker, AfterStartColumnBeforeColumn)
{
SourceRange sourceRange{1, 2, 5, 0, 3, 3, 0};
bool isAfterStartColumn = marker.isInsideRange(sourceRange, 2, 4);
ASSERT_FALSE(isAfterStartColumn);
}
TEST_F(ClangQueryHighlightMarker, AfterStartColumnAtColumn)
{
SourceRange sourceRange{1, 2, 5, 0, 3, 3, 0};
bool isAfterStartColumn = marker.isInsideRange(sourceRange, 2, 5);
ASSERT_TRUE(isAfterStartColumn);
}
TEST_F(ClangQueryHighlightMarker, AfterStartColumnAfterColumn)
{
SourceRange sourceRange{1, 2, 5, 0, 3, 3, 0};
bool isAfterStartColumn = marker.isInsideRange(sourceRange, 2, 6);
ASSERT_TRUE(isAfterStartColumn);
}
TEST_F(ClangQueryHighlightMarker, BeforeEndColumnAfterLine)
{
SourceRange sourceRange{1, 2, 5, 0, 3, 3, 0};
bool isBeforeEndColumn = marker.isInsideRange(sourceRange, 4, 1);
ASSERT_FALSE(isBeforeEndColumn);
}
TEST_F(ClangQueryHighlightMarker, BeforeEndColumnAfterColumn)
{
SourceRange sourceRange{1, 2, 5, 0, 3, 3, 0};
bool isBeforeEndColumn = marker.isInsideRange(sourceRange, 3, 4);
ASSERT_FALSE(isBeforeEndColumn);
}
TEST_F(ClangQueryHighlightMarker, BeforeEndColumnAtColumn)
{
SourceRange sourceRange{1, 2, 5, 0, 3, 3, 0};
bool isBeforeEndColumn = marker.isInsideRange(sourceRange, 3, 3);
ASSERT_TRUE(isBeforeEndColumn);
}
TEST_F(ClangQueryHighlightMarker, BeforeEndColumnBeforeColumn)
{
SourceRange sourceRange{1, 2, 5, 0, 3, 3, 0};
bool isBeforeEndColumn = marker.isInsideRange(sourceRange, 3, 2);
ASSERT_TRUE(isBeforeEndColumn);
}
TEST_F(ClangQueryHighlightMarker, InBetweenLineBeforeLine)
{
SourceRange sourceRange{1, 2, 5, 0, 3, 3, 0};
bool isInBetween = marker.isInsideRange(sourceRange, 1, 6);
ASSERT_FALSE(isInBetween);
}
TEST_F(ClangQueryHighlightMarker, InBetweenLineAfterLine)
{
SourceRange sourceRange{1, 2, 5, 0, 4, 3, 0};
bool isInBetween = marker.isInsideRange(sourceRange, 5, 1);
ASSERT_FALSE(isInBetween);
}
TEST_F(ClangQueryHighlightMarker, InBetweenLine)
{
SourceRange sourceRange{1, 2, 5, 0, 4, 3, 0};
bool isInBetween = marker.isInsideRange(sourceRange, 3, 1);
ASSERT_TRUE(isInBetween);
}
TEST_F(ClangQueryHighlightMarker, SingleLineBefore)
{
SourceRange sourceRange{1, 2, 5, 0, 2, 10, 0};
bool isInRange = marker.isInsideRange(sourceRange, 2, 4);
ASSERT_FALSE(isInRange);
}
TEST_F(ClangQueryHighlightMarker, SingleLineAfter)
{
SourceRange sourceRange{1, 2, 5, 0, 2, 10, 0};
bool isInRange = marker.isInsideRange(sourceRange, 2, 11);
ASSERT_FALSE(isInRange);
}
TEST_F(ClangQueryHighlightMarker, SingleLineInRange)
{
SourceRange sourceRange{1, 2, 5, 0, 2, 10, 0};
bool isInRange = marker.isInsideRange(sourceRange, 2, 6);
ASSERT_TRUE(isInRange);
}
TEST_F(ClangQueryHighlightMarker, NoContextsIfEmpty)
{
Messages messages;
Contexts contexts;
marker.setMessagesAndContexts(std::move(messages), std::move(contexts));
auto foundContexts = marker.contextsForLineAndColumn(1, 1);
ASSERT_THAT(foundContexts, IsEmpty());
}
TEST_F(ClangQueryHighlightMarker, NoContextsForBeforePosition)
{
Messages messages;
Contexts contexts{{{1, 1, 5, 0, 3, 3, 0},
ContextType::MatcherArg,
{"foo"}}};
marker.setMessagesAndContexts(std::move(messages), std::move(contexts));
auto foundContexts = marker.contextsForLineAndColumn(1, 4);
ASSERT_THAT(foundContexts, IsEmpty());