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 <cristian.maureira-fredes@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2019-02-12 13:08:46 +01:00
parent b1e1c91e04
commit 2bebfe9985
3 changed files with 19 additions and 2 deletions

View File

@@ -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)

View File

@@ -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,

View File

@@ -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);
}
}