forked from qt-creator/qt-creator
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 <tim.jenssen@qt.io>
This commit is contained in:
@@ -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;
|
||||
|
||||
60
src/plugins/clangrefactoring/clangqueryhoverhandler.cpp
Normal file
60
src/plugins/clangrefactoring/clangqueryhoverhandler.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
46
src/plugins/clangrefactoring/clangqueryhoverhandler.h
Normal file
46
src/plugins/clangrefactoring/clangqueryhoverhandler.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** 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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user