Support creating directories from file system locator

It was already possible to create files, so add the option to create
directories instead.

Change-Id: I2e70dba0015ab30b1757f09c74eb2c2dd0db296b
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Eike Ziller
2023-04-26 10:44:46 +02:00
parent fad8ed8e28
commit 2b640f3dc3

View File

@@ -8,6 +8,7 @@
#include "../editormanager/editormanager.h"
#include "../icore.h"
#include "../vcsmanager.h"
#include "locatormanager.h"
#include <extensionsystem/pluginmanager.h>
@@ -56,12 +57,11 @@ static ILocatorFilter::MatchLevel matchLevelFor(const QRegularExpressionMatch &m
return ILocatorFilter::MatchLevel::Normal;
}
static void createAndOpen(const FilePath &filePath)
static bool askForCreating(const QString &title, const FilePath &filePath)
{
if (!filePath.exists()) {
if (CheckableMessageBox::shouldAskAgain(ICore::settings(), kAlwaysCreate)) {
CheckableMessageBox messageBox(ICore::dialogParent());
messageBox.setWindowTitle(Tr::tr("Create File"));
messageBox.setWindowTitle(title);
messageBox.setIcon(QMessageBox::Question);
messageBox.setText(Tr::tr("Create \"%1\"?").arg(filePath.shortNativePath()));
messageBox.setCheckBoxVisible(true);
@@ -73,18 +73,38 @@ static void createAndOpen(const FilePath &filePath)
messageBox.setDefaultButton(QDialogButtonBox::Cancel);
messageBox.exec();
if (messageBox.clickedButton() != createButton)
return;
return false;
if (messageBox.isChecked())
CheckableMessageBox::doNotAskAgain(ICore::settings(), kAlwaysCreate);
}
return true;
}
static void createAndOpenFile(const FilePath &filePath)
{
if (!filePath.exists()) {
if (askForCreating(Tr::tr("Create File"), filePath)) {
QFile file(filePath.toFSPathString());
file.open(QFile::WriteOnly);
file.close();
VcsManager::promptToAdd(filePath.absolutePath(), {filePath});
}
}
if (filePath.exists())
EditorManager::openEditor(filePath);
}
static bool createDirectory(const FilePath &filePath)
{
if (!filePath.exists()) {
if (askForCreating(Tr::tr("Create Directory"), filePath))
filePath.createDir();
}
if (filePath.exists())
return true;
return false;
}
static FilePaths deviceRoots()
{
const QString rootPath = FilePath::specialRootPath();
@@ -241,17 +261,47 @@ static LocatorFilterEntries matchesImpl(Promise &promise,
const FilePath fullFilePath = directory / entryFileName;
const bool containsWildcard = expandedEntry.contains('?') || expandedEntry.contains('*');
if (!containsWildcard && !fullFilePath.exists() && directory.exists()) {
// create and open file
{
LocatorFilterEntry filterEntry;
filterEntry.displayName = Tr::tr("Create and Open \"%1\"").arg(expandedEntry);
filterEntry.displayName = Tr::tr("Create and Open File \"%1\"").arg(expandedEntry);
filterEntry.acceptor = [fullFilePath] {
QMetaObject::invokeMethod(EditorManager::instance(),
[fullFilePath] { createAndOpen(fullFilePath); }, Qt::QueuedConnection);
QMetaObject::invokeMethod(
EditorManager::instance(),
[fullFilePath] { createAndOpenFile(fullFilePath); },
Qt::QueuedConnection);
return AcceptResult();
};
filterEntry.filePath = fullFilePath;
filterEntry.extraInfo = directory.absoluteFilePath().shortNativePath();
entries[int(ILocatorFilter::MatchLevel::Normal)].append(filterEntry);
}
// create directory
{
LocatorFilterEntry filterEntry;
filterEntry.displayName = Tr::tr("Create Directory \"%1\"").arg(expandedEntry);
filterEntry.acceptor = [fullFilePath, shortcutString] {
QMetaObject::invokeMethod(
EditorManager::instance(),
[fullFilePath, shortcutString] {
if (createDirectory(fullFilePath)) {
const QString value = shortcutString + ' '
+ fullFilePath.absoluteFilePath()
.cleanPath()
.pathAppended("/")
.toUserOutput();
LocatorManager::show(value, value.length());
}
},
Qt::QueuedConnection);
return AcceptResult();
};
filterEntry.filePath = fullFilePath;
filterEntry.extraInfo = directory.absoluteFilePath().shortNativePath();
entries[int(ILocatorFilter::MatchLevel::Normal)].append(filterEntry);
}
}
return std::accumulate(std::begin(entries), std::end(entries), LocatorFilterEntries());
}