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 <ulf.hermann@qt.io>
This commit is contained in:
Miikka Heikkinen
2020-05-20 12:47:09 +03:00
parent 346b28f5f9
commit 7f4bf437da
2 changed files with 35 additions and 11 deletions

View File

@@ -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::ProjectInfo> 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<ProjectInfo> allProjects;
{
QMutexLocker locker(&m_mutex);
allProjects = m_projects.values();
}
std::sort(allProjects.begin(), allProjects.end(), &pInfoLessThanImports);
QList<Dialect> 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<ProjectInfo> 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());

View File

@@ -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<void(Utils::QrcParser::ConstPtr)> &callback);
ViewerContext getVContext(const ViewerContext &vCtx, const Document::Ptr &doc, bool limitToProject) const;
mutable QMutex m_mutex;
QmlJS::Snapshot m_validSnapshot;