TextEditor: Move navigation history update from Widget to IEditor

Telling the EditorManager to update the navigation history only really
makes sense in the context of IEditor, not if the widget is used in some
other context.

For this reason the code in the text editor widget also had a check to
only add history in case the current editor's widget is the text editor
widget - which does not work in case of e.g. the Markdown editor or the
Compiler Explorer. Signaling the request for adding navigation points to
the IEditor gives these other editors that integrate text editor(s) a
chance to implement this too.

Change-Id: Id1bb3516519f48a3f4448ac226be21e52bb02b2b
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2024-05-14 09:34:50 +02:00
parent 9f1c6e9c72
commit e964a0e2a7
2 changed files with 46 additions and 5 deletions

View File

@@ -354,6 +354,7 @@ public:
BaseTextEditorPrivate() = default;
TextEditorFactoryPrivate *m_origin = nullptr;
QByteArray m_savedNavigationState;
};
class HoverHandlerRunner
@@ -772,7 +773,6 @@ public:
QSharedPointer<TextDocument> m_document;
QList<QMetaObject::Connection> m_documentConnections;
QByteArray m_tempState;
QByteArray m_tempNavigationState;
bool m_parenthesesMatchingEnabled = false;
QTimer m_parenthesesMatchingTimer;
@@ -6498,7 +6498,7 @@ void TextEditorWidgetPrivate::slotUpdateRequest(const QRect &r, int dy)
void TextEditorWidgetPrivate::saveCurrentCursorPositionForNavigation()
{
m_lastCursorChangeWasInteresting = true;
m_tempNavigationState = q->saveState();
emit q->saveCurrentStateForNavigationHistory();
}
void TextEditorWidgetPrivate::updateCurrentLineHighlight()
@@ -6554,8 +6554,7 @@ void TextEditorWidget::slotCursorPositionChanged()
<< "indent:" << BaseTextDocumentLayout::userData(textCursor().block())->foldingIndent();
#endif
if (!d->m_contentsChanged && d->m_lastCursorChangeWasInteresting) {
if (EditorManager::currentEditor() && EditorManager::currentEditor()->widget() == this)
EditorManager::addCurrentPositionToNavigationHistory(d->m_tempNavigationState);
emit addSavedStateToNavigationHistory();
d->m_lastCursorChangeWasInteresting = false;
} else if (d->m_contentsChanged) {
d->saveCurrentCursorPositionForNavigation();
@@ -7588,7 +7587,7 @@ bool TextEditorWidget::openLink(const Utils::Link &link, bool inNextSplit)
}
if (!inNextSplit && textDocument()->filePath() == link.targetFilePath) {
EditorManager::addCurrentPositionToNavigationHistory();
emit addCurrentStateToNavigationHistory();
gotoLine(link.targetLine, link.targetColumn, true, true);
setFocus();
return true;
@@ -9604,6 +9603,23 @@ void BaseTextEditor::select(int toPos)
editorWidget()->setTextCursor(tc);
}
void BaseTextEditor::saveCurrentStateForNavigationHistory()
{
d->m_savedNavigationState = saveState();
}
void BaseTextEditor::addSavedStateToNavigationHistory()
{
if (EditorManager::currentEditor() == this)
EditorManager::addCurrentPositionToNavigationHistory(d->m_savedNavigationState);
}
void BaseTextEditor::addCurrentStateToNavigationHistory()
{
if (EditorManager::currentEditor() == this)
EditorManager::addCurrentPositionToNavigationHistory();
}
void TextEditorWidgetPrivate::updateCursorPosition()
{
m_contextHelpItem = HelpItem();
@@ -10235,6 +10251,21 @@ BaseTextEditor *TextEditorFactoryPrivate::createEditorHelper(const TextDocumentP
[editor](EditorManager::OpenEditorFlags flags) {
EditorManager::activateEditor(editor, flags);
});
QObject::connect(
textEditorWidget,
&TextEditorWidget::saveCurrentStateForNavigationHistory,
editor,
&BaseTextEditor::saveCurrentStateForNavigationHistory);
QObject::connect(
textEditorWidget,
&TextEditorWidget::addSavedStateToNavigationHistory,
editor,
&BaseTextEditor::addSavedStateToNavigationHistory);
QObject::connect(
textEditorWidget,
&TextEditorWidget::addCurrentStateToNavigationHistory,
editor,
&BaseTextEditor::addCurrentStateToNavigationHistory);
if (m_useGenericHighlighter)
textEditorWidget->setupGenericHighlighter();

View File

@@ -169,6 +169,11 @@ public:
private:
friend class TextEditorFactory;
friend class Internal::TextEditorFactoryPrivate;
void saveCurrentStateForNavigationHistory();
void addSavedStateToNavigationHistory();
void addCurrentStateToNavigationHistory();
Internal::BaseTextEditorPrivate *d;
};
@@ -532,6 +537,11 @@ signals:
void requestCallHierarchy(const QTextCursor &cursor);
void toolbarOutlineChanged(QWidget *newOutline);
// used by the IEditor
void saveCurrentStateForNavigationHistory();
void addSavedStateToNavigationHistory();
void addCurrentStateToNavigationHistory();
protected:
QTextBlock blockForVisibleRow(int row) const;
QTextBlock blockForVerticalOffset(int offset) const;