From 86770f28d0da13facafc5a8c03d84bb331b51382 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 15 Nov 2022 17:36:15 +0100 Subject: [PATCH] ClangCodeModel: Keep a ref count for "extra files" These are files that we temporarily report to the server as "open" without actually having a document for them. Since this mechanism is used in several places, we'd like to keep a consistent state even when such actions overlap. Change-Id: I0cfe3427740ec479926bc114848ad08f8866bdbb Reviewed-by: Reviewed-by: David Schulz --- src/plugins/clangcodemodel/clangdclient.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/plugins/clangcodemodel/clangdclient.cpp b/src/plugins/clangcodemodel/clangdclient.cpp index 329b91b6318..db76cd56982 100644 --- a/src/plugins/clangcodemodel/clangdclient.cpp +++ b/src/plugins/clangcodemodel/clangdclient.cpp @@ -295,6 +295,7 @@ public: QHash highlightingData; QHash parserConfigs; QHash issuePaneEntries; + QHash openedExtraFiles; VersionedDataCache astCache; VersionedDataCache externalAstCache; @@ -421,10 +422,11 @@ ClangdClient::ClangdClient(Project *project, const Utils::FilePath &jsonDbDir) } }); - connect(this, &Client::initialized, this, [] { + connect(this, &Client::initialized, this, [this] { auto currentDocumentFilter = static_cast( CppEditor::CppModelManager::instance()->currentDocumentFilter()); currentDocumentFilter->updateCurrentClient(); + d->openedExtraFiles.clear(); }); start(); @@ -444,6 +446,13 @@ bool ClangdClient::isFullyIndexed() const void ClangdClient::openExtraFile(const Utils::FilePath &filePath, const QString &content) { + const auto it = d->openedExtraFiles.find(filePath); + if (it != d->openedExtraFiles.end()) { + QTC_CHECK(it.value() > 0); + ++it.value(); + return; + } + QFile cxxFile(filePath.toString()); if (content.isEmpty() && !cxxFile.open(QIODevice::ReadOnly)) return; @@ -454,10 +463,18 @@ void ClangdClient::openExtraFile(const Utils::FilePath &filePath, const QString item.setVersion(0); sendMessage(DidOpenTextDocumentNotification(DidOpenTextDocumentParams(item)), SendDocUpdates::Ignore); + + d->openedExtraFiles.insert(filePath, 1); } void ClangdClient::closeExtraFile(const Utils::FilePath &filePath) { + const auto it = d->openedExtraFiles.find(filePath); + QTC_ASSERT(it != d->openedExtraFiles.end(), return); + QTC_CHECK(it.value() > 0); + if (--it.value() > 0) + return; + d->openedExtraFiles.erase(it); sendMessage(DidCloseTextDocumentNotification(DidCloseTextDocumentParams( TextDocumentIdentifier{DocumentUri::fromFilePath(filePath)})), SendDocUpdates::Ignore);