Warn when editing generated files

We already warn when editing a file that is not corresponding to a node
in the project and not under some project directory and not under some
project's version control.

Generated files are part of the project tree (not shown by default, if
"Hide Generated Files" is not turned off in the filter settings). So,
add another warning that the file is generated when editing a file that
is part of the project tree, but has the isGenerated flag set.

Task-number: QTCREATORBUG-27173
Change-Id: Id554d0e97bd5e033e957e7b3a863897845b7b654
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Eike Ziller
2022-05-09 14:55:18 +02:00
parent e657bc8781
commit 08359cafde
2 changed files with 31 additions and 13 deletions

View File

@@ -52,7 +52,9 @@
#include <QMenu>
#include <QTimer>
namespace { const char EXTERNAL_FILE_WARNING[] = "ExternalFile"; }
namespace {
const char EXTERNAL_OR_GENERATED_FILE_WARNING[] = "ExternalOrGeneratedFile";
}
using namespace Utils;
@@ -214,14 +216,20 @@ void ProjectTree::setCurrent(Node *node, Project *project)
}
if (Core::IDocument *document = Core::EditorManager::currentDocument()) {
if (node) {
disconnect(document, &Core::IDocument::changed,
this, &ProjectTree::updateExternalFileWarning);
document->infoBar()->removeInfo(EXTERNAL_FILE_WARNING);
} else {
if (!node) {
connect(document, &Core::IDocument::changed,
this, &ProjectTree::updateExternalFileWarning,
Qt::UniqueConnection);
} else if (node->isGenerated()) {
connect(document, &Core::IDocument::changed,
this, &ProjectTree::updateGeneratedFileWarning,
Qt::UniqueConnection);
} else {
disconnect(document, &Core::IDocument::changed,
this, &ProjectTree::updateExternalFileWarning);
disconnect(document, &Core::IDocument::changed,
this, &ProjectTree::updateGeneratedFileWarning);
document->infoBar()->removeInfo(EXTERNAL_OR_GENERATED_FILE_WARNING);
}
}
@@ -304,18 +312,18 @@ void ProjectTree::changeProjectRootDirectory()
m_currentProject->changeRootProjectDirectory();
}
void ProjectTree::updateExternalFileWarning()
void ProjectTree::updateFileWarning(const QString &text)
{
auto document = qobject_cast<Core::IDocument *>(sender());
if (!document || document->filePath().isEmpty())
return;
Utils::InfoBar *infoBar = document->infoBar();
Utils::Id externalFileId(EXTERNAL_FILE_WARNING);
Utils::Id infoId(EXTERNAL_OR_GENERATED_FILE_WARNING);
if (!document->isModified()) {
infoBar->removeInfo(externalFileId);
infoBar->removeInfo(infoId);
return;
}
if (!infoBar->canInfoBeAdded(externalFileId))
if (!infoBar->canInfoBeAdded(infoId))
return;
const FilePath fileName = document->filePath();
const QList<Project *> projects = SessionManager::projects();
@@ -335,9 +343,17 @@ void ProjectTree::updateExternalFileWarning()
}
}
infoBar->addInfo(
Utils::InfoBarEntry(externalFileId,
tr("<b>Warning:</b> This file is outside the project directory."),
Utils::InfoBarEntry::GlobalSuppression::Enabled));
Utils::InfoBarEntry(infoId, text, Utils::InfoBarEntry::GlobalSuppression::Enabled));
}
void ProjectTree::updateExternalFileWarning()
{
updateFileWarning(tr("<b>Warning:</b> This file is outside the project directory."));
}
void ProjectTree::updateGeneratedFileWarning()
{
updateFileWarning(tr("<b>Warning:</b> This file is generated."));
}
bool ProjectTree::hasFocus(ProjectTreeWidget *widget)

View File

@@ -132,7 +132,9 @@ private:
void updateFromFocus();
void updateFileWarning(const QString &text);
void updateExternalFileWarning();
void updateGeneratedFileWarning();
static bool hasFocus(Internal::ProjectTreeWidget *widget);
Internal::ProjectTreeWidget *currentWidget() const;
void hideContextMenu();