diff --git a/ChatView/ChatRootView.cpp b/ChatView/ChatRootView.cpp index 1400229..c03367a 100644 --- a/ChatView/ChatRootView.cpp +++ b/ChatView/ChatRootView.cpp @@ -82,9 +82,20 @@ ChatRootView::ChatRootView(QQuickItem *parent) auto editors = Core::EditorManager::instance(); - connect(editors, &Core::EditorManager::editorOpened, this, &ChatRootView::onEditorOpened); - connect(editors, &Core::EditorManager::editorAboutToClose, this, &ChatRootView::onEditorAboutToClose); - connect(editors, &Core::EditorManager::editorsClosed, this, &ChatRootView::onEditorsClosed); + connect(editors, &Core::EditorManager::editorCreated, this, &ChatRootView::onEditorCreated); + connect( + editors, + &Core::EditorManager::editorAboutToClose, + this, + &ChatRootView::onEditorAboutToClose); + + connect(editors, &Core::EditorManager::currentEditorAboutToChange, this, [this]() { + if (m_isSyncOpenFiles) { + for (auto editor : m_currentEditors) { + onAppendLinkFileFromEditor(editor); + } + } + }); updateInputTokensCount(); } @@ -375,6 +386,12 @@ void ChatRootView::setIsSyncOpenFiles(bool state) m_isSyncOpenFiles = state; emit isSyncOpenFilesChanged(); } + + if (m_isSyncOpenFiles) { + for (auto editor : m_currentEditors) { + onAppendLinkFileFromEditor(editor); + } + } } void ChatRootView::updateInputTokensCount() @@ -416,7 +433,20 @@ bool ChatRootView::isSyncOpenFiles() const return m_isSyncOpenFiles; } -void ChatRootView::onEditorOpened(Core::IEditor *editor) +void ChatRootView::onEditorAboutToClose(Core::IEditor *editor) +{ + if (auto document = editor->document(); document && isSyncOpenFiles()) { + QString filePath = document->filePath().toString(); + m_linkedFiles.removeOne(filePath); + emit linkedFilesChanged(); + } + + if (editor) { + m_currentEditors.removeOne(editor); + } +} + +void ChatRootView::onAppendLinkFileFromEditor(Core::IEditor *editor) { if (auto document = editor->document(); document && isSyncOpenFiles()) { QString filePath = document->filePath().toString(); @@ -427,25 +457,10 @@ void ChatRootView::onEditorOpened(Core::IEditor *editor) } } -void ChatRootView::onEditorAboutToClose(Core::IEditor *editor) +void ChatRootView::onEditorCreated(Core::IEditor *editor, const Utils::FilePath &filePath) { - if (auto document = editor->document(); document && isSyncOpenFiles()) { - QString filePath = document->filePath().toString(); - m_linkedFiles.removeOne(filePath); - emit linkedFilesChanged(); - } -} - -void ChatRootView::onEditorsClosed(QList editors) -{ - if (isSyncOpenFiles()) { - for (Core::IEditor *editor : editors) { - if (auto document = editor->document()) { - QString filePath = document->filePath().toString(); - m_linkedFiles.removeOne(filePath); - } - } - emit linkedFilesChanged(); + if (editor && editor->document()) { + m_currentEditors.append(editor); } } diff --git a/ChatView/ChatRootView.hpp b/ChatView/ChatRootView.hpp index fb8adf2..54b4b44 100644 --- a/ChatView/ChatRootView.hpp +++ b/ChatView/ChatRootView.hpp @@ -70,9 +70,9 @@ public: bool isSyncOpenFiles() const; - void onEditorOpened(Core::IEditor *editor); void onEditorAboutToClose(Core::IEditor *editor); - void onEditorsClosed(QList editors); + void onAppendLinkFileFromEditor(Core::IEditor *editor); + void onEditorCreated(Core::IEditor *editor, const Utils::FilePath &filePath); QString chatFileName() const; void setRecentFilePath(const QString &filePath); @@ -106,6 +106,7 @@ private: int m_messageTokensCount{0}; int m_inputTokensCount{0}; bool m_isSyncOpenFiles; + QList m_currentEditors; }; } // namespace QodeAssist::Chat