From 35fcd96b8500472e80f11cd40182639ae1a18648 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 17 Aug 2021 17:59:51 +0200 Subject: [PATCH] TextEditor: Optionally auto-save refactored files This is particularly helpful with clangd, which considers only the on- disk state of header files when parsing dependent sources. Fixes: QTCREATORBUG-25924 Change-Id: I41d313f8a203a576d3ed5fbe75bbe918334486d4 Reviewed-by: David Schulz --- .../editormanager/editormanager.cpp | 21 + .../coreplugin/editormanager/editormanager.h | 2 + .../editormanager/editormanager_p.h | 3 + src/plugins/coreplugin/systemsettings.cpp | 6 + src/plugins/coreplugin/systemsettings.ui | 489 +++++++++--------- src/plugins/texteditor/refactoringchanges.cpp | 3 + 6 files changed, 283 insertions(+), 241 deletions(-) diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index e2e26201ef2..7204226d042 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -116,6 +116,7 @@ static const char documentStatesKey[] = "EditorManager/DocumentStates"; static const char reloadBehaviorKey[] = "EditorManager/ReloadBehavior"; static const char autoSaveEnabledKey[] = "EditorManager/AutoSaveEnabled"; static const char autoSaveIntervalKey[] = "EditorManager/AutoSaveInterval"; +static const char autoSaveAfterRefactoringKey[] = "EditorManager/AutoSaveAfterRefactoring"; static const char autoSuspendEnabledKey[] = "EditorManager/AutoSuspendEnabled"; static const char autoSuspendMinDocumentCountKey[] = "EditorManager/AutoSuspendMinDocuments"; static const char warnBeforeOpeningBigTextFilesKey[] = "EditorManager/WarnBeforeOpeningBigTextFiles"; @@ -1232,6 +1233,9 @@ void EditorManagerPrivate::saveSettings() qsettings->setValueWithDefault(autoSaveIntervalKey, d->m_settings.autoSaveInterval, def.autoSaveInterval); + qsettings->setValueWithDefault(autoSaveAfterRefactoringKey, + d->m_settings.autoSaveAfterRefactoring, + def.autoSaveAfterRefactoring); qsettings->setValueWithDefault(autoSuspendEnabledKey, d->m_settings.autoSuspendEnabled, def.autoSuspendEnabled); @@ -1303,6 +1307,8 @@ void EditorManagerPrivate::readSettings() d->m_settings.autoSaveEnabled = qs->value(autoSaveEnabledKey, def.autoSaveEnabled).toBool(); d->m_settings.autoSaveInterval = qs->value(autoSaveIntervalKey, def.autoSaveInterval).toInt(); + d->m_settings.autoSaveAfterRefactoring = qs->value(autoSaveAfterRefactoringKey, + def.autoSaveAfterRefactoring).toBool(); d->m_settings.autoSuspendEnabled = qs->value(autoSuspendEnabledKey, def.autoSuspendEnabled) .toBool(); @@ -1334,6 +1340,16 @@ int EditorManagerPrivate::autoSaveInterval() return d->m_settings.autoSaveInterval; } +void EditorManagerPrivate::setAutoSaveAfterRefactoring(bool enabled) +{ + d->m_settings.autoSaveAfterRefactoring = enabled; +} + +bool EditorManagerPrivate::autoSaveAfterRefactoring() +{ + return d->m_settings.autoSaveAfterRefactoring; +} + void EditorManagerPrivate::setAutoSuspendEnabled(bool enabled) { d->m_settings.autoSuspendEnabled = enabled; @@ -3141,6 +3157,11 @@ bool EditorManager::isAutoSaveFile(const QString &filePath) return filePath.endsWith(".autosave"); } +bool EditorManager::autoSaveAfterRefactoring() +{ + return EditorManagerPrivate::autoSaveAfterRefactoring(); +} + /*! Opens the document specified by \a filePath in the external editor specified by \a editorId. diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h index 363490ad356..65f1ea3663f 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.h +++ b/src/plugins/coreplugin/editormanager/editormanager.h @@ -154,6 +154,8 @@ public: static bool isAutoSaveFile(const QString &fileName); + static bool autoSaveAfterRefactoring(); + static QTextCodec *defaultTextCodec(); static Utils::TextFileFormat::LineTerminationMode defaultLineEnding(); diff --git a/src/plugins/coreplugin/editormanager/editormanager_p.h b/src/plugins/coreplugin/editormanager/editormanager_p.h index 816da3f6d3b..9d16c59ebec 100644 --- a/src/plugins/coreplugin/editormanager/editormanager_p.h +++ b/src/plugins/coreplugin/editormanager/editormanager_p.h @@ -116,6 +116,8 @@ public: static bool autoSaveEnabled(); static void setAutoSaveInterval(int interval); static int autoSaveInterval(); + static void setAutoSaveAfterRefactoring(bool enabled); + static bool autoSaveAfterRefactoring(); static void setAutoSuspendEnabled(bool enabled); static bool autoSuspendEnabled(); static void setAutoSuspendMinDocumentCount(int count); @@ -282,6 +284,7 @@ private: bool autoSuspendEnabled = true; int autoSuspendMinDocumentCount = 30; + bool autoSaveAfterRefactoring = true; bool warnBeforeOpeningBigFilesEnabled = true; int bigFileSizeLimitInMB = 5; int maxRecentFiles = 8; diff --git a/src/plugins/coreplugin/systemsettings.cpp b/src/plugins/coreplugin/systemsettings.cpp index b9787764369..844e1cc6955 100644 --- a/src/plugins/coreplugin/systemsettings.cpp +++ b/src/plugins/coreplugin/systemsettings.cpp @@ -126,6 +126,10 @@ public: "a crash or power failure, it asks whether to " "recover the auto-saved content.") .arg(Constants::IDE_DISPLAY_NAME)); + m_ui.autoSaveRefactoringCheckBox->setChecked(EditorManager::autoSaveAfterRefactoring()); + m_ui.autoSaveRefactoringCheckBox->setToolTip(tr("Automatically saves all open files " + "affected by a refactoring operation,\n provided they were unmodified before the " + "refactoring.")); m_ui.autoSaveInterval->setValue(EditorManagerPrivate::autoSaveInterval()); m_ui.autoSuspendCheckBox->setChecked(EditorManagerPrivate::autoSuspendEnabled()); m_ui.autoSuspendMinDocumentCount->setValue(EditorManagerPrivate::autoSuspendMinDocumentCount()); @@ -268,6 +272,8 @@ void SystemSettingsWidget::apply() PatchTool::setPatchCommand(m_ui.patchChooser->filePath()); EditorManagerPrivate::setAutoSaveEnabled(m_ui.autoSaveCheckBox->isChecked()); EditorManagerPrivate::setAutoSaveInterval(m_ui.autoSaveInterval->value()); + EditorManagerPrivate::setAutoSaveAfterRefactoring( + m_ui.autoSaveRefactoringCheckBox->isChecked()); EditorManagerPrivate::setAutoSuspendEnabled(m_ui.autoSuspendCheckBox->isChecked()); EditorManagerPrivate::setAutoSuspendMinDocumentCount(m_ui.autoSuspendMinDocumentCount->value()); EditorManagerPrivate::setWarnBeforeOpeningBigFilesEnabled( diff --git a/src/plugins/coreplugin/systemsettings.ui b/src/plugins/coreplugin/systemsettings.ui index 020910f5dfc..9b93d3672ab 100644 --- a/src/plugins/coreplugin/systemsettings.ui +++ b/src/plugins/coreplugin/systemsettings.ui @@ -6,7 +6,7 @@ 0 0 - 599 + 628 545 @@ -17,26 +17,10 @@ System - - - - Influences how file names are matched to decide if they are the same. - + + - File system case sensitivity: - - - - - - - Automatically free resources of old documents that are not visible and not modified. They stay visible in the list of open documents. - - - Auto-suspend unmodified files - - - true + Terminal: @@ -74,14 +58,17 @@ - - + + + + Influences how file names are matched to decide if they are the same. + - Terminal: + File system case sensitivity: - + @@ -121,212 +108,6 @@ - - - - Maximum number of entries in "Recent Files": - - - - - - - Auto-save modified files - - - true - - - - - - - When files are externally modified: - - - - - - - External file browser: - - - - - - - - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - - Interval: - - - - - - - min - - - 1 - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - - Warn before opening text files greater than - - - true - - - - - - - MB - - - 1 - - - 500 - - - 5 - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - - 0 - - - - Always Ask - - - - - Reload All Unchanged Editors - - - - - Ignore Modifications - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - Patch command: - - - - - - - - - Ask for confirmation before exiting - - - - - - - - - - - Clear Local Crash Reports - - - - - - - - - - - - @@ -365,6 +146,46 @@ + + + + Maximum number of entries in "Recent Files": + + + + + + + Automatically free resources of old documents that are not visible and not modified. They stay visible in the list of open documents. + + + Auto-suspend unmodified files + + + true + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + @@ -372,6 +193,90 @@ + + + + When files are externally modified: + + + + + + + + + Allow crashes to be automatically reported. Collected reports are used for the sole purpose of fixing bugs. + + + Enable crash reporting + + + + + + + ? + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Warn before opening text files greater than + + + true + + + + + + + MB + + + 1 + + + 500 + + + 5 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + @@ -429,6 +334,16 @@ + + + + Patch command: + + + + + + @@ -473,27 +388,43 @@ - - + + - - - Allow crashes to be automatically reported. Collected reports are used for the sole purpose of fixing bugs. - + - Enable crash reporting + Ask for confirmation before exiting + + + + - - - ? + + + 0 + + + Always Ask + + + + + Reload All Unchanged Editors + + + + + Ignore Modifications + + - + Qt::Horizontal @@ -507,6 +438,82 @@ + + + + External file browser: + + + + + + + Auto-save modified files + + + true + + + + + + + + + Clear Local Crash Reports + + + + + + + + + + + + + + + + + + Interval: + + + + + + + min + + + 1 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Auto-save files after refactoring + + + diff --git a/src/plugins/texteditor/refactoringchanges.cpp b/src/plugins/texteditor/refactoringchanges.cpp index d307b8cda75..6872c18c60a 100644 --- a/src/plugins/texteditor/refactoringchanges.cpp +++ b/src/plugins/texteditor/refactoringchanges.cpp @@ -342,6 +342,7 @@ bool RefactoringFile::apply() m_editorCursorPosition = -1; } + const bool withUnmodifiedEditor = m_editor && !m_editor->textDocument()->isModified(); bool result = true; // apply changes, if any @@ -390,6 +391,8 @@ bool RefactoringFile::apply() } fileChanged(); + if (withUnmodifiedEditor && EditorManager::autoSaveAfterRefactoring()) + m_editor->textDocument()->save(nullptr, m_filePath, false); } }