From 7f4bf437dac692cd4bdd55fa3de06367b9eb8712 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Wed, 20 May 2020 12:47:09 +0300 Subject: [PATCH] QmlJS: Add a way to return just the context of the project of a file Added ModelManagerInterface::projectVContext() method to return just the context of the project the file belongs to and nothing more. To make this possible, fixed caching the file-to-project relationships and removed automatically adding the currently active project to list of projects the file belongs to in allProjectInfos(). Task-number: QDS-1495 Change-Id: I949c0202d0280264b6856562a2e7abc2f93d13c0 Reviewed-by: Ulf Hermann --- src/libs/qmljs/qmljsmodelmanagerinterface.cpp | 43 ++++++++++++++----- src/libs/qmljs/qmljsmodelmanagerinterface.h | 3 ++ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/libs/qmljs/qmljsmodelmanagerinterface.cpp b/src/libs/qmljs/qmljsmodelmanagerinterface.cpp index 1eb54f73031..402128d272d 100644 --- a/src/libs/qmljs/qmljsmodelmanagerinterface.cpp +++ b/src/libs/qmljs/qmljsmodelmanagerinterface.cpp @@ -561,11 +561,12 @@ void ModelManagerInterface::updateProjectInfo(const ProjectInfo &pinfo, ProjectE // parse any files not yet in the snapshot QStringList newFiles; for (const QString &file : qAsConst(pinfo.sourceFiles)) { + if (!m_fileToProject.contains(file, p)) + m_fileToProject.insert(file, p); if (!snapshot.document(file)) newFiles += file; } - for (const QString &newFile : qAsConst(newFiles)) - m_fileToProject.insert(newFile, p); + updateSourceFiles(newFiles, false); // update qrc cache @@ -636,7 +637,6 @@ QList ModelManagerInterface::allProjectInfos infos.append(info); } std::sort(infos.begin(), infos.end(), &pInfoLessThanImports); - infos.append(m_defaultProjectInfo); return infos; } @@ -1406,6 +1406,13 @@ LibraryInfo ModelManagerInterface::builtins(const Document::Ptr &doc) const ViewerContext ModelManagerInterface::completeVContext(const ViewerContext &vCtx, const Document::Ptr &doc) const +{ + return getVContext(vCtx, doc, false); +} + +ViewerContext ModelManagerInterface::getVContext(const ViewerContext &vCtx, + const Document::Ptr &doc, + bool limitToProject) const { ViewerContext res = vCtx; @@ -1444,20 +1451,27 @@ ViewerContext ModelManagerInterface::completeVContext(const ViewerContext &vCtx, { if (res.language == Dialect::QmlQtQuick2 || res.language == Dialect::QmlQtQuick2Ui) maybeAddPath(res, info.qtQmlPath); - QList allProjects; - { - QMutexLocker locker(&m_mutex); - allProjects = m_projects.values(); - } - std::sort(allProjects.begin(), allProjects.end(), &pInfoLessThanImports); + QList languages = res.language.companionLanguages(); - for (const ProjectInfo &pInfo : qAsConst(allProjects)) { - for (const auto &importPath : pInfo.importPaths) { + auto addPathsOnLanguageMatch = [&](const PathsAndLanguages &importPaths) { + for (const auto &importPath : importPaths) { if (languages.contains(importPath.language()) || importPath.language().companionLanguages().contains(res.language)) { maybeAddPath(res, importPath.path().toString()); } } + }; + if (limitToProject) { + addPathsOnLanguageMatch(info.importPaths); + } else { + QList allProjects; + { + QMutexLocker locker(&m_mutex); + allProjects = m_projects.values(); + } + std::sort(allProjects.begin(), allProjects.end(), &pInfoLessThanImports); + for (const ProjectInfo &pInfo : qAsConst(allProjects)) + addPathsOnLanguageMatch(pInfo.importPaths); } const auto environmentPaths = environmentImportPaths(); for (const QString &path : environmentPaths) @@ -1516,6 +1530,13 @@ ViewerContext ModelManagerInterface::defaultVContext(Dialect language, return autoComplete ? completeVContext(defaultCtx, doc) : defaultCtx; } +ViewerContext ModelManagerInterface::projectVContext(Dialect language, const Document::Ptr &doc) const +{ + // Returns context limited to the project the file belongs to + ViewerContext defaultCtx = defaultVContext(language, doc, false); + return getVContext(defaultCtx, doc, true); +} + ModelManagerInterface::ProjectInfo ModelManagerInterface::defaultProjectInfo() const { QMutexLocker l(mutex()); diff --git a/src/libs/qmljs/qmljsmodelmanagerinterface.h b/src/libs/qmljs/qmljsmodelmanagerinterface.h index 8e8b9d2a24c..088b4e593c4 100644 --- a/src/libs/qmljs/qmljsmodelmanagerinterface.h +++ b/src/libs/qmljs/qmljsmodelmanagerinterface.h @@ -174,6 +174,8 @@ public: ViewerContext defaultVContext(Dialect language = Dialect::Qml, const Document::Ptr &doc = Document::Ptr(nullptr), bool autoComplete = true) const; + ViewerContext projectVContext(Dialect language, const Document::Ptr &doc) const; + void setDefaultVContext(const ViewerContext &vContext); virtual ProjectInfo defaultProjectInfo() const; virtual ProjectInfo defaultProjectInfoForProject(ProjectExplorer::Project *project) const; @@ -241,6 +243,7 @@ private: void iterateQrcFiles(ProjectExplorer::Project *project, QrcResourceSelector resources, const std::function &callback); + ViewerContext getVContext(const ViewerContext &vCtx, const Document::Ptr &doc, bool limitToProject) const; mutable QMutex m_mutex; QmlJS::Snapshot m_validSnapshot;