aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSami Shalayel <sami.shalayel@qt.io>2025-06-24 14:05:20 +0200
committerSami Shalayel <sami.shalayel@qt.io>2025-07-08 11:50:10 +0200
commit808d4d7128fd748521d35a1b6eb28a068698de2d (patch)
treeeb80616e37c01e51beae865466a2d96f17e3bedd /src
parent1d9a5534d0c536fb7ef1747516cfd32d79c0b431 (diff)
QQmlCodeModelManager: add method to find project root
Add a method that returns the project root for a file. The project root is the workspace with the shortest url that contains some file. This will be needed when searching for C++ headers, as they might be in a different subproject/workspace than the currently edited file. Task-number: QTBUG-128393 Change-Id: I63ce3895abb20c9851dc77da3b4d0c4bd267c871 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/qmlls/qqmlcodemodelmanager.cpp22
-rw-r--r--src/qmlls/qqmlcodemodelmanager_p.h1
2 files changed, 23 insertions, 0 deletions
diff --git a/src/qmlls/qqmlcodemodelmanager.cpp b/src/qmlls/qqmlcodemodelmanager.cpp
index a74dc52d70..23a7d49d68 100644
--- a/src/qmlls/qqmlcodemodelmanager.cpp
+++ b/src/qmlls/qqmlcodemodelmanager.cpp
@@ -213,6 +213,28 @@ QStringList QQmlCodeModelManager::buildPathsForFileUrl(const QByteArray &url)
return findCodeModelForFile(url)->buildPathsForFileUrl(url);
}
+QByteArray QQmlCodeModelManager::shortestRootUrlForFile(const QByteArray &fileUrl) const
+{
+ // fallback value for candidate is the empty url of the default workspace
+ QByteArray candidate;
+
+ // ignore the default workspace which is at the front of m_workspaces
+ Q_ASSERT(m_workspaces.size() > 0);
+ Q_ASSERT(m_workspaces.front().url.isEmpty());
+ auto it = std::find_if(
+ ++m_workspaces.cbegin(), m_workspaces.cend(),
+ [&fileUrl](const QQmlWorkspace &ws) { return fileUrl.startsWith(ws.url); });
+
+ if (it != m_workspaces.cend())
+ candidate = it->url;
+
+ for (; it != m_workspaces.cend(); ++it) {
+ if (it->url.length() < candidate.length() && fileUrl.startsWith(it->url))
+ candidate = it->url;
+ }
+ return candidate;
+}
+
void QQmlCodeModelManager::setDocumentationRootPath(const QString &path)
{
m_defaultDocumentationRootPath = path;
diff --git a/src/qmlls/qqmlcodemodelmanager_p.h b/src/qmlls/qqmlcodemodelmanager_p.h
index 9f3895c134..3ad14ae233 100644
--- a/src/qmlls/qqmlcodemodelmanager_p.h
+++ b/src/qmlls/qqmlcodemodelmanager_p.h
@@ -64,6 +64,7 @@ public:
QQmlToolingSharedSettings *settings() const { return m_settings; }
void disableCMakeCalls();
const QFactoryLoader &pluginLoader() const { return m_pluginLoader; }
+ QByteArray shortestRootUrlForFile(const QByteArray &fileUrl) const;
RegisteredSemanticTokens &registeredTokens(const QByteArray &);
const RegisteredSemanticTokens &registeredTokens(const QByteArray &) const;