From 2bebfe9985e1da4a6227e82ef2b596b26ec964ea Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 12 Feb 2019 13:08:46 +0100 Subject: [PATCH] Help: Add fallback word lookup for context help to base text editor If we do not have a hover handler, or that did not produce any result, use the word under cursor and try with that. This adds at least some help fallback mechanism to all text editors, e.g. we get some context help (though a bit crude) for Pyside "for free". Change-Id: I1b6d91ad6f46bfd12e242b1aec9f4fcadae23403 Reviewed-by: Cristian Maureira-Fredes Reviewed-by: David Schulz --- src/libs/utils/textutils.cpp | 7 +++++++ src/libs/utils/textutils.h | 1 + src/plugins/texteditor/texteditor.cpp | 13 +++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) 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); } }