Commit 811f3627 authored by Friedemann Kleint's avatar Friedemann Kleint

ProjectExplorer command line parsing: Handle directories.

Prevent the editor from complaining about not being able to
open directories. Instead, check whether the directory name
matches a session or the the directory contains a project

The idea is that one can type "qtcreator ." which results in
'Do what I mean'.

Change-Id: I45c5836567f8d1ff1024aca12db3603d6539d376
Reviewed-by: default avatarLeena Miettinen <>
Reviewed-by: default avatarDaniel Teske <>
parent 0d2b1aed
......@@ -55,6 +55,12 @@
To open a project that is located in a particular folder, you can pass on the folder
name as a command line argument. \QC looks for a session that matches the folder name and
loads it. Or it looks for a project file in the folder and opens it. For example:
\c {qtcreator .}
The following table summarizes the available options:
......@@ -1456,6 +1456,21 @@ void ProjectExplorerPlugin::determineSessionToRestoreAtStartup()
// Return a list of glob patterns for project files ("*.pro", etc), use first, main pattern only.
static inline QStringList projectFileGlobs()
QStringList result;
const Core::MimeDatabase *mimeDatabase = Core::ICore::instance()->mimeDatabase();
foreach (const IProjectManager *ipm, ExtensionSystem::PluginManager::getObjects<IProjectManager>()) {
if (const Core::MimeType mimeType = mimeDatabase->findByType(ipm->mimeType())) {
const QList<Core::MimeGlobPattern> patterns = mimeType.globPatterns();
if (!patterns.isEmpty())
return result;
\fn void ProjectExplorerPlugin::restoreSession()
......@@ -1471,8 +1486,55 @@ void ProjectExplorerPlugin::restoreSession()
// We have command line arguments, try to find a session in them
QStringList arguments = ExtensionSystem::PluginManager::arguments();
if (!d->m_sessionToRestoreAtStartup.isEmpty() && !arguments.isEmpty())
// Massage the argument list.
// Be smart about directories: If there is a session of that name, load it.
// Other than that, look for project files in it. The idea is to achieve
// 'Do what I mean' functionality when starting Creator in a directory with
// the single command line argument '.' and avoid editor warnings about not
// being able to open directories.
// In addition, convert "filename" "+45" or "filename" ":23" into
// "filename+45" and "filename:23".
if (!arguments.isEmpty()) {
const QStringList sessions = d->m_session->sessions();
QStringList projectGlobs = projectFileGlobs();
for (int a = 0; a < arguments.size(); ) {
const QString &arg =;
const QFileInfo fi(arg);
if (fi.isDir()) {
const QDir dir(fi.absoluteFilePath());
// Does the directory name match a session?
if (d->m_sessionToRestoreAtStartup.isEmpty()
&& sessions.contains(dir.dirName())) {
d->m_sessionToRestoreAtStartup = dir.dirName();
} else {
// Are there project files in that directory?
const QFileInfoList proFiles
= dir.entryInfoList(projectGlobs, QDir::Files);
if (!proFiles.isEmpty()) {
arguments[a] = proFiles.front().absoluteFilePath();
// Cannot handle: Avoid mime type warning for directory.
qWarning("Skipping directory '%s' passed on to command line.",
} // Done directories.
// Converts "filename" "+45" or "filename" ":23" into "filename+45" and "filename:23"
if (a && (arg.startsWith(QLatin1Char('+')) || arg.startsWith(QLatin1Char(':')))) {
arguments[a - 1].append(arguments.takeAt(a));
} // for arguments
} // !arguments.isEmpty()
// Restore latest session or what was passed on the command line
if (!d->m_sessionToRestoreAtStartup.isEmpty())
......@@ -1484,19 +1546,7 @@ void ProjectExplorerPlugin::restoreSession()
connect(d->m_welcomePage, SIGNAL(requestSession(QString)), this, SLOT(loadSession(QString)));
connect(d->m_welcomePage, SIGNAL(requestProject(QString)), this, SLOT(openProjectWelcomePage(QString)));
QStringList combinedList;
// Converts "filename" "+45" or "filename" ":23"
// into "filename+45" and "filename:23"
foreach (const QString &str, arguments) {
if (!combinedList.isEmpty() && (str.startsWith(QLatin1Char('+'))
|| str.startsWith(QLatin1Char(':')))) {
} else {
combinedList << str;
Core::ICore::openFiles(combinedList, Core::ICore::OpenFilesFlags(Core::ICore::CanContainLineNumbers | Core::ICore::SwitchMode));
Core::ICore::openFiles(arguments, Core::ICore::OpenFilesFlags(Core::ICore::CanContainLineNumbers | Core::ICore::SwitchMode));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment