QtSupport: Move populateFileFinder to BaseQtVersion

This populates a file finder so that it is best able to find QML files,
prioritizing the given project/target. Some of it would also apply to a
non QML file finder, but as we don't use FileInProjectFinder for other
file types we keep the code here, for now.

Change-Id: I14e2ac63e699afe27d2f3af8ca3d57dfe732da8c
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Ulf Hermann
2017-09-27 17:02:13 +02:00
parent 0693db3579
commit 7749a47e62
3 changed files with 61 additions and 51 deletions

View File

@@ -34,7 +34,7 @@
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljstools/qmljsmodelmanager.h>
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/baseqtversion.h>
#include <utils/qtcassert.h>
@@ -209,55 +209,7 @@ void QmlProfilerDetailsRewriter::documentReady(QmlJS::Document::Ptr doc)
void QmlProfilerDetailsRewriter::populateFileFinder(const ProjectExplorer::Target *target)
{
// If target given, then use the project associated with that ...
const ProjectExplorer::Project *startupProject = target ? target->project() : nullptr;
// ... else try the session manager's global startup project ...
if (!startupProject)
startupProject = ProjectExplorer::SessionManager::startupProject();
// ... and if that is null, use the first project available.
const QList<ProjectExplorer::Project *> projects = ProjectExplorer::SessionManager::projects();
if (!startupProject && !projects.isEmpty())
startupProject = projects.first();
QString projectDirectory;
QStringList sourceFiles;
// Sort files from startupProject to the front of the list ...
if (startupProject) {
projectDirectory = startupProject->projectDirectory().toString();
sourceFiles.append(startupProject->files(ProjectExplorer::Project::SourceFiles));
}
// ... then add all the other projects' files.
for (const ProjectExplorer::Project *project : projects) {
if (project != startupProject)
sourceFiles.append(project->files(ProjectExplorer::Project::SourceFiles));
}
// If no target was given, but we've found a startupProject, then try to deduct a
// target from that.
if (!target && startupProject)
target = startupProject->activeTarget();
// ... and find the sysroot if we have any target at all.
QString activeSysroot;
QStringList additionalSearchDirectories;
if (target) {
if (const ProjectExplorer::Kit *kit = target->kit()) {
if (ProjectExplorer::SysRootKitInformation::hasSysRoot(kit))
activeSysroot = ProjectExplorer::SysRootKitInformation::sysRoot(kit).toString();
if (QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitInformation::qtVersion(kit))
additionalSearchDirectories.append(qtVersion->qmlPath().toString());
}
}
// Finally, do populate m_projectFinder
m_projectFinder.setProjectDirectory(projectDirectory);
m_projectFinder.setProjectFiles(sourceFiles);
m_projectFinder.setSysroot(activeSysroot);
m_projectFinder.setAdditionalSearchDirectories(additionalSearchDirectories);
QtSupport::BaseQtVersion::populateQmlFileFinder(&m_projectFinder, target);
m_filesCache.clear();
}

View File

@@ -37,6 +37,9 @@
#include <projectexplorer/toolchain.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/headerpath.h>
#include <projectexplorer/project.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/qtsupportconstants.h>
@@ -47,6 +50,7 @@
#include <utils/runextensions.h>
#include <utils/synchronousprocess.h>
#include <utils/winutils.h>
#include <utils/fileinprojectfinder.h>
#include <QDir>
#include <QUrl>
@@ -1318,6 +1322,53 @@ MacroExpander *BaseQtVersion::macroExpander() const
return &m_expander;
}
void BaseQtVersion::populateQmlFileFinder(FileInProjectFinder *finder, const Target *target)
{
// If target given, then use the project associated with that ...
const ProjectExplorer::Project *startupProject = target ? target->project() : nullptr;
// ... else try the session manager's global startup project ...
if (!startupProject)
startupProject = ProjectExplorer::SessionManager::startupProject();
// ... and if that is null, use the first project available.
const QList<ProjectExplorer::Project *> projects = ProjectExplorer::SessionManager::projects();
QTC_CHECK(projects.isEmpty() || startupProject);
QString projectDirectory;
QStringList sourceFiles;
// Sort files from startupProject to the front of the list ...
if (startupProject) {
projectDirectory = startupProject->projectDirectory().toString();
sourceFiles.append(startupProject->files(ProjectExplorer::Project::SourceFiles));
}
// ... then add all the other projects' files.
for (const ProjectExplorer::Project *project : projects) {
if (project != startupProject)
sourceFiles.append(project->files(ProjectExplorer::Project::SourceFiles));
}
// If no target was given, but we've found a startupProject, then try to deduce a
// target from that.
if (!target && startupProject)
target = startupProject->activeTarget();
// ... and find the sysroot and qml directory if we have any target at all.
const ProjectExplorer::Kit *kit = target ? target->kit() : nullptr;
QString activeSysroot = ProjectExplorer::SysRootKitInformation::sysRoot(kit).toString();
const QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitInformation::qtVersion(kit);
QStringList additionalSearchDirectories = qtVersion
? QStringList(qtVersion->qmlPath().toString()) : QStringList();
// Finally, do populate m_projectFinder
finder->setProjectDirectory(projectDirectory);
finder->setProjectFiles(sourceFiles);
finder->setSysroot(activeSysroot);
finder->setAdditionalSearchDirectories(additionalSearchDirectories);
}
void BaseQtVersion::addToEnvironment(const Kit *k, Environment &env) const
{
Q_UNUSED(k);

View File

@@ -36,7 +36,10 @@
#include <QStringList>
#include <QVariantMap>
namespace Utils { class Environment; }
namespace Utils {
class Environment;
class FileInProjectFinder;
}
namespace Core { class Id; }
namespace ProjectExplorer {
@@ -44,6 +47,7 @@ class IOutputParser;
class Kit;
class ToolChain;
class HeaderPath;
class Target;
class Task;
} // namespace ProjectExplorer
@@ -227,6 +231,9 @@ public:
Utils::MacroExpander *macroExpander() const; // owned by the Qt version
static void populateQmlFileFinder(Utils::FileInProjectFinder *finder,
const ProjectExplorer::Target *target);
protected:
BaseQtVersion();
BaseQtVersion(const Utils::FileName &path, bool isAutodetected = false, const QString &autodetectionSource = QString());