diff --git a/src/libs/utils/textutils.cpp b/src/libs/utils/textutils.cpp index f72dd89321c..21fb3caf511 100644 --- a/src/libs/utils/textutils.cpp +++ b/src/libs/utils/textutils.cpp @@ -140,6 +140,13 @@ QTextCursor wordStartCursor(const QTextCursor &textCursor) return cursor; } +QString wordUnderCursor(const QTextCursor &cursor) +{ + QTextCursor tc(cursor); + tc.select(QTextCursor::WordUnderCursor); + return tc.selectedText(); +} + int utf8NthLineOffset(const QTextDocument *textDocument, const QByteArray &buffer, int line) { if (textDocument->blockCount() < line) diff --git a/src/libs/utils/textutils.h b/src/libs/utils/textutils.h index 1d4e8f7ffbf..895023903ee 100644 --- a/src/libs/utils/textutils.h +++ b/src/libs/utils/textutils.h @@ -53,6 +53,7 @@ QTCREATOR_UTILS_EXPORT QTextCursor selectAt(QTextCursor textCursor, uint line, u QTCREATOR_UTILS_EXPORT QTextCursor flippedCursor(const QTextCursor &cursor); QTCREATOR_UTILS_EXPORT QTextCursor wordStartCursor(const QTextCursor &cursor); +QTCREATOR_UTILS_EXPORT QString wordUnderCursor(const QTextCursor &cursor); QTCREATOR_UTILS_EXPORT int utf8NthLineOffset(const QTextDocument *textDocument, const QByteArray &buffer, diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 5ac69be40ce..235ac7f673e 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -8085,12 +8085,21 @@ void BaseTextEditor::setContextHelp(const HelpItem &item) void TextEditorWidget::contextHelpItem(const IContext::HelpCallback &callback) { + const QString fallbackWordUnderCursor = Text::wordUnderCursor(textCursor()); if (d->m_contextHelpItem.isEmpty() && !d->m_hoverHandlers.isEmpty()) { d->m_hoverHandlers.first()->contextHelpId(this, Text::wordStartCursor(textCursor()).position(), - callback); + [fallbackWordUnderCursor, callback](const HelpItem &item) { + if (item.isEmpty()) + callback(fallbackWordUnderCursor); + else + callback(item); + }); } else { - callback(d->m_contextHelpItem); + if (d->m_contextHelpItem.isEmpty()) + callback(fallbackWordUnderCursor); + else + callback(d->m_contextHelpItem); } }