forked from qt-creator/qt-creator
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:
@@ -140,6 +140,13 @@ QTextCursor wordStartCursor(const QTextCursor &textCursor)
|
|||||||
return cursor;
|
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)
|
int utf8NthLineOffset(const QTextDocument *textDocument, const QByteArray &buffer, int line)
|
||||||
{
|
{
|
||||||
if (textDocument->blockCount() < line)
|
if (textDocument->blockCount() < line)
|
||||||
|
@@ -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 flippedCursor(const QTextCursor &cursor);
|
||||||
|
|
||||||
QTCREATOR_UTILS_EXPORT QTextCursor wordStartCursor(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,
|
QTCREATOR_UTILS_EXPORT int utf8NthLineOffset(const QTextDocument *textDocument,
|
||||||
const QByteArray &buffer,
|
const QByteArray &buffer,
|
||||||
|
@@ -8085,12 +8085,21 @@ 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() && !d->m_hoverHandlers.isEmpty()) {
|
if (d->m_contextHelpItem.isEmpty() && !d->m_hoverHandlers.isEmpty()) {
|
||||||
d->m_hoverHandlers.first()->contextHelpId(this,
|
d->m_hoverHandlers.first()->contextHelpId(this,
|
||||||
Text::wordStartCursor(textCursor()).position(),
|
Text::wordStartCursor(textCursor()).position(),
|
||||||
callback);
|
[fallbackWordUnderCursor, callback](const HelpItem &item) {
|
||||||
|
if (item.isEmpty())
|
||||||
|
callback(fallbackWordUnderCursor);
|
||||||
|
else
|
||||||
|
callback(item);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
callback(d->m_contextHelpItem);
|
if (d->m_contextHelpItem.isEmpty())
|
||||||
|
callback(fallbackWordUnderCursor);
|
||||||
|
else
|
||||||
|
callback(d->m_contextHelpItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user