Locator: Ask before creating files

With the option to not ask again.

It's too easy to create unwanted files when mistyping names in Locator.

Fixes: QTCREATORBUG-23078
Change-Id: I082d3e112db404813c7d8f46edb7a16836a98a92
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2019-10-22 13:22:06 +02:00
parent f68588a585
commit 1a6e441f1b

View File

@@ -29,13 +29,17 @@
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
#include <coreplugin/documentmanager.h> #include <coreplugin/documentmanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h> #include <coreplugin/idocument.h>
#include <utils/checkablemessagebox.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <QDir> #include <QDir>
#include <QPushButton>
#include <QRegularExpression> #include <QRegularExpression>
#include <QTimer>
using namespace Core; using namespace Core;
using namespace Core::Internal; using namespace Core::Internal;
@@ -148,8 +152,12 @@ QList<LocatorFilterEntry> FileSystemFilter::matchesFor(QFutureInterface<LocatorF
return std::accumulate(std::begin(entries), std::end(entries), QList<LocatorFilterEntry>()); return std::accumulate(std::begin(entries), std::end(entries), QList<LocatorFilterEntry>());
} }
const char kAlwaysCreate[] = "Locator/FileSystemFilter/AlwaysCreate";
void FileSystemFilter::accept(LocatorFilterEntry selection, void FileSystemFilter::accept(LocatorFilterEntry selection,
QString *newText, int *selectionStart, int *selectionLength) const QString *newText,
int *selectionStart,
int *selectionLength) const
{ {
Q_UNUSED(selectionLength) Q_UNUSED(selectionLength)
QString fileName = selection.fileName; QString fileName = selection.fileName;
@@ -159,15 +167,42 @@ void FileSystemFilter::accept(LocatorFilterEntry selection,
+ QDir::toNativeSeparators(info.absoluteFilePath() + '/'); + QDir::toNativeSeparators(info.absoluteFilePath() + '/');
*newText = value; *newText = value;
*selectionStart = value.length(); *selectionStart = value.length();
return; } else {
} else if (!info.exists()) { // Don't block locator filter execution with dialog
QFile file(selection.internalData.toString()); QTimer::singleShot(0, EditorManager::instance(), [info, selection] {
file.open(QFile::WriteOnly); const QString targetFile = selection.internalData.toString();
file.close(); if (!info.exists()) {
if (Utils::CheckableMessageBox::shouldAskAgain(ICore::settings(), kAlwaysCreate)) {
Utils::CheckableMessageBox messageBox(ICore::dialogParent());
messageBox.setWindowTitle(tr("Create File"));
messageBox.setIcon(QMessageBox::Question);
messageBox.setText(
tr("Create \"%1\"?")
.arg(Utils::FilePath::fromString(targetFile).shortNativePath()));
messageBox.setCheckBoxVisible(true);
messageBox.setCheckBoxText(tr("Always create"));
messageBox.setChecked(false);
messageBox.setStandardButtons(QDialogButtonBox::Cancel);
QPushButton *createButton = messageBox.addButton(tr("Create"),
QDialogButtonBox::AcceptRole);
messageBox.setDefaultButton(QDialogButtonBox::Cancel);
messageBox.exec();
if (messageBox.clickedButton() != createButton)
return;
if (messageBox.isChecked())
Utils::CheckableMessageBox::doNotAskAgain(ICore::settings(), kAlwaysCreate);
}
QFile file(targetFile);
file.open(QFile::WriteOnly);
file.close();
}
const QFileInfo fileInfo(targetFile);
const QString cleanedFilePath = QDir::cleanPath(fileInfo.absoluteFilePath());
EditorManager::openEditor(cleanedFilePath,
Id(),
EditorManager::CanContainLineAndColumnNumber);
});
} }
const QFileInfo fileInfo(selection.internalData.toString());
const QString cleanedFilePath = QDir::cleanPath(fileInfo.absoluteFilePath());
EditorManager::openEditor(cleanedFilePath, Id(), EditorManager::CanContainLineAndColumnNumber);
} }
bool FileSystemFilter::openConfigDialog(QWidget *parent, bool &needsRefresh) bool FileSystemFilter::openConfigDialog(QWidget *parent, bool &needsRefresh)