diff --git a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp index 090a4549c3b..3bd9d2640f6 100644 --- a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp +++ b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp @@ -112,6 +112,7 @@ ClangModelManagerSupport::ClangModelManagerSupport() m_instance = this; watchForExternalChanges(); + watchForInternalChanges(); cppModelManager()->setCurrentDocumentFilter(std::make_unique()); cppModelManager()->setLocatorFilter(std::make_unique()); cppModelManager()->setClassesFilter(std::make_unique()); @@ -488,6 +489,29 @@ void ClangModelManagerSupport::watchForExternalChanges() }); } +void ClangModelManagerSupport::watchForInternalChanges() +{ + connect(Core::DocumentManager::instance(), &Core::DocumentManager::filesChangedInternally, + this, [this](const Utils::FilePaths &filePaths) { + for (const Utils::FilePath &fp : filePaths) { + ClangdClient * const client = clientForFile(fp); + if (!client || client->documentForFilePath(fp)) + continue; + client->openExtraFile(fp); + + // We need to give clangd some time to start re-parsing the file. + // Closing right away does not work, and neither does doing it queued. + // If it turns out that this delay is not always enough, we'll need to come up + // with something more clever. + // Ideally, clangd would implement workspace/didChangeWatchedFiles; let's keep + // any eye on that. + QTimer::singleShot(5000, client, [client, fp] { + if (!client->documentForFilePath(fp)) + client->closeExtraFile(fp); }); + } + }); +} + void ClangModelManagerSupport::onEditorOpened(Core::IEditor *editor) { QTC_ASSERT(editor, return); diff --git a/src/plugins/clangcodemodel/clangmodelmanagersupport.h b/src/plugins/clangcodemodel/clangmodelmanagersupport.h index fc4e1d1a4b4..5d591d6925f 100644 --- a/src/plugins/clangcodemodel/clangmodelmanagersupport.h +++ b/src/plugins/clangcodemodel/clangmodelmanagersupport.h @@ -135,8 +135,8 @@ private: ClangdClient *createClient(ProjectExplorer::Project *project, const Utils::FilePath &jsonDbDir); void claimNonProjectSources(ClangdClient *fallbackClient); void watchForExternalChanges(); + void watchForInternalChanges(); -private: UiHeaderOnDiskManager m_uiHeaderOnDiskManager; BackendCommunicator m_communicator; ClangCompletionAssistProvider m_completionAssistProvider;