forked from qt-creator/qt-creator
TextEditor: Handle context help requests like tooltip requests
That is, don't just ask one random hover handler, but try all of them and use the best one. Change-Id: I38d0a90e96090c56240314a90f0f428c88fd222c Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -296,6 +296,8 @@ public:
|
|||||||
class HoverHandlerRunner
|
class HoverHandlerRunner
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using Callback = std::function<void(TextEditorWidget *, BaseHoverHandler *, int)>;
|
||||||
|
|
||||||
HoverHandlerRunner(TextEditorWidget *widget, QList<BaseHoverHandler *> &handlers)
|
HoverHandlerRunner(TextEditorWidget *widget, QList<BaseHoverHandler *> &handlers)
|
||||||
: m_widget(widget)
|
: m_widget(widget)
|
||||||
, m_handlers(handlers)
|
, m_handlers(handlers)
|
||||||
@@ -304,7 +306,7 @@ public:
|
|||||||
|
|
||||||
~HoverHandlerRunner() { abortHandlers(); }
|
~HoverHandlerRunner() { abortHandlers(); }
|
||||||
|
|
||||||
void startChecking(const QTextCursor &textCursor, const QPoint &point)
|
void startChecking(const QTextCursor &textCursor, const Callback &callback)
|
||||||
{
|
{
|
||||||
if (m_handlers.empty())
|
if (m_handlers.empty())
|
||||||
return;
|
return;
|
||||||
@@ -313,7 +315,7 @@ public:
|
|||||||
const int documentRevision = textCursor.document()->revision();
|
const int documentRevision = textCursor.document()->revision();
|
||||||
const int position = Text::wordStartCursor(textCursor).position();
|
const int position = Text::wordStartCursor(textCursor).position();
|
||||||
if (m_lastHandlerInfo.applies(documentRevision, position)) {
|
if (m_lastHandlerInfo.applies(documentRevision, position)) {
|
||||||
m_lastHandlerInfo.handler->showToolTip(m_widget, point);
|
callback(m_widget, m_lastHandlerInfo.handler, position);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,7 +327,7 @@ public:
|
|||||||
// Update invocation data
|
// Update invocation data
|
||||||
m_documentRevision = documentRevision;
|
m_documentRevision = documentRevision;
|
||||||
m_position = position;
|
m_position = position;
|
||||||
m_point = point;
|
m_callback = callback;
|
||||||
|
|
||||||
// Re-initialize process data
|
// Re-initialize process data
|
||||||
m_currentHandlerIndex = 0;
|
m_currentHandlerIndex = 0;
|
||||||
@@ -375,7 +377,7 @@ public:
|
|||||||
// All were queried, run the best
|
// All were queried, run the best
|
||||||
if (m_bestHandler) {
|
if (m_bestHandler) {
|
||||||
m_lastHandlerInfo = LastHandlerInfo(m_bestHandler, m_documentRevision, m_position);
|
m_lastHandlerInfo = LastHandlerInfo(m_bestHandler, m_documentRevision, m_position);
|
||||||
m_bestHandler->showToolTip(m_widget, m_point);
|
m_callback(m_widget, m_bestHandler, m_position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -410,7 +412,7 @@ private:
|
|||||||
} m_lastHandlerInfo;
|
} m_lastHandlerInfo;
|
||||||
|
|
||||||
// invocation data
|
// invocation data
|
||||||
QPoint m_point;
|
Callback m_callback;
|
||||||
int m_position = -1;
|
int m_position = -1;
|
||||||
int m_documentRevision = -1;
|
int m_documentRevision = -1;
|
||||||
|
|
||||||
@@ -3550,7 +3552,10 @@ void TextEditorWidgetPrivate::processTooltipRequest(const QTextCursor &c)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_hoverHandlerRunner.startChecking(c, toolTipPoint);
|
const auto callback = [toolTipPoint](TextEditorWidget *widget, BaseHoverHandler *handler, int) {
|
||||||
|
handler->showToolTip(widget, toolTipPoint);
|
||||||
|
};
|
||||||
|
m_hoverHandlerRunner.startChecking(c, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TextEditorWidgetPrivate::processAnnotaionTooltipRequest(const QTextBlock &block,
|
bool TextEditorWidgetPrivate::processAnnotaionTooltipRequest(const QTextBlock &block,
|
||||||
@@ -8150,22 +8155,28 @@ void BaseTextEditor::setContextHelp(const HelpItem &item)
|
|||||||
|
|
||||||
void TextEditorWidget::contextHelpItem(const IContext::HelpCallback &callback)
|
void TextEditorWidget::contextHelpItem(const IContext::HelpCallback &callback)
|
||||||
{
|
{
|
||||||
const QString fallbackWordUnderCursor = Text::wordUnderCursor(textCursor());
|
if (!d->m_contextHelpItem.isEmpty()) {
|
||||||
if (d->m_contextHelpItem.isEmpty() && !d->m_hoverHandlers.isEmpty()) {
|
callback(d->m_contextHelpItem);
|
||||||
d->m_hoverHandlers.first()->contextHelpId(this,
|
return;
|
||||||
Text::wordStartCursor(textCursor()).position(),
|
|
||||||
[fallbackWordUnderCursor, callback](const HelpItem &item) {
|
|
||||||
if (item.isEmpty())
|
|
||||||
callback(fallbackWordUnderCursor);
|
|
||||||
else
|
|
||||||
callback(item);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
if (d->m_contextHelpItem.isEmpty())
|
|
||||||
callback(fallbackWordUnderCursor);
|
|
||||||
else
|
|
||||||
callback(d->m_contextHelpItem);
|
|
||||||
}
|
}
|
||||||
|
const QString fallbackWordUnderCursor = Text::wordUnderCursor(textCursor());
|
||||||
|
if (d->m_hoverHandlers.isEmpty()) {
|
||||||
|
callback(fallbackWordUnderCursor);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto hoverHandlerCallback = [fallbackWordUnderCursor, callback](
|
||||||
|
TextEditorWidget *widget, BaseHoverHandler *handler, int position) {
|
||||||
|
handler->contextHelpId(widget, position,
|
||||||
|
[fallbackWordUnderCursor, callback](const HelpItem &item) {
|
||||||
|
if (item.isEmpty())
|
||||||
|
callback(fallbackWordUnderCursor);
|
||||||
|
else
|
||||||
|
callback(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
d->m_hoverHandlerRunner.startChecking(textCursor(), hoverHandlerCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidget::setContextHelpItem(const HelpItem &item)
|
void TextEditorWidget::setContextHelpItem(const HelpItem &item)
|
||||||
|
|||||||
Reference in New Issue
Block a user