From e964a0e2a726e3a7c3d541356d4c31d4d487e95b Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 14 May 2024 09:34:50 +0200 Subject: [PATCH] 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 --- src/plugins/texteditor/texteditor.cpp | 41 +++++++++++++++++++++++---- src/plugins/texteditor/texteditor.h | 10 +++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 6cb9b00b490..5d7ddb49a23 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -354,6 +354,7 @@ public: BaseTextEditorPrivate() = default; TextEditorFactoryPrivate *m_origin = nullptr; + QByteArray m_savedNavigationState; }; class HoverHandlerRunner @@ -772,7 +773,6 @@ public: QSharedPointer m_document; QList 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(); diff --git a/src/plugins/texteditor/texteditor.h b/src/plugins/texteditor/texteditor.h index 78dc0e0b823..c155bb8ff21 100644 --- a/src/plugins/texteditor/texteditor.h +++ b/src/plugins/texteditor/texteditor.h @@ -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;