diff --git a/src/plugins/coreplugin/basefilewizardfactory.cpp b/src/plugins/coreplugin/basefilewizardfactory.cpp index 3f7d4c771fb..0e4652b7e53 100644 --- a/src/plugins/coreplugin/basefilewizardfactory.cpp +++ b/src/plugins/coreplugin/basefilewizardfactory.cpp @@ -165,12 +165,16 @@ bool BaseFileWizardFactory::postGenerateOpenEditors(const GeneratedFiles &l, QSt { for (const GeneratedFile &file : std::as_const(l)) { if (file.attributes() & GeneratedFile::OpenEditorAttribute) { - if (!EditorManager::openEditor(file.filePath(), file.editorId())) { - if (errorMessage) - *errorMessage = Tr::tr("Failed to open an editor for \"%1\"."). - arg(file.filePath().toUserOutput()); + IEditor * const editor = EditorManager::openEditor(file.filePath(), file.editorId()); + if (!editor) { + if (errorMessage) { + *errorMessage = Tr::tr("Failed to open an editor for \"%1\".") + .arg(file.filePath().toUserOutput()); + } return false; } + editor->document()->formatContents(); + editor->document()->save(nullptr); } } return true; diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index 261146797b4..e617e98d1c7 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -76,12 +76,14 @@ #include #include #include -#include #if defined(WITH_TESTS) #include #endif +#include +#include + enum { debugEditorManager=0 }; static const char kCurrentDocumentPrefix[] = "CurrentDocument"; @@ -2975,6 +2977,27 @@ void EditorManager::populateOpenWithMenu(QMenu *menu, const FilePath &filePath) menu->setEnabled(anyMatches); } +void EditorManager::runWithTemporaryEditor(const Utils::FilePath &filePath, + const std::function &callback) +{ + const MimeType mt = mimeTypeForFile(filePath, MimeMatchMode::MatchDefaultAndRemote); + const QList factories = Utils::transform( + EditorType::defaultEditorTypes(mt), [](EditorType *t) { + return t->asEditorFactory(); }); + for (IEditorFactory * const factory : factories) { + if (!factory) + continue; + std::unique_ptr editor(factory->createEditor()); + if (!editor) + continue; + editor->document()->setTemporary(true); + if (editor->document()->open(nullptr, filePath, filePath) != IDocument::OpenResult::Success) + continue; + callback(editor.get()); + break; + } +} + /*! Returns reload behavior settings. */ diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h index 09218f42c3e..ea0fd5df8cd 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.h +++ b/src/plugins/coreplugin/editormanager/editormanager.h @@ -143,6 +143,9 @@ public: static void addNativeDirAndOpenWithActions(QMenu *contextMenu, DocumentModel::Entry *entry); static void populateOpenWithMenu(QMenu *menu, const Utils::FilePath &filePath); + static void runWithTemporaryEditor(const Utils::FilePath &filePath, + const std::function &callback); + public: // for tests static IDocument::ReloadSetting reloadSetting(); static void setReloadSetting(IDocument::ReloadSetting behavior); diff --git a/src/plugins/coreplugin/idocument.cpp b/src/plugins/coreplugin/idocument.cpp index 5dcff9d3fc1..c6fdac85736 100644 --- a/src/plugins/coreplugin/idocument.cpp +++ b/src/plugins/coreplugin/idocument.cpp @@ -401,6 +401,13 @@ bool IDocument::setContents(const QByteArray &contents) return false; } +/*! + Formats the contents of the document, if the implementation supports such functionality. +*/ +void IDocument::formatContents() +{ +} + /*! Returns the absolute path of the file that this document refers to. May be empty for documents that are not backed by a file. diff --git a/src/plugins/coreplugin/idocument.h b/src/plugins/coreplugin/idocument.h index b771843056b..8fbca1d02f7 100644 --- a/src/plugins/coreplugin/idocument.h +++ b/src/plugins/coreplugin/idocument.h @@ -72,6 +72,7 @@ public: virtual QByteArray contents() const; virtual bool setContents(const QByteArray &contents); + virtual void formatContents(); const Utils::FilePath &filePath() const; virtual void setFilePath(const Utils::FilePath &filePath); diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp index 6bc09c5e58f..8204f7aaa25 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp @@ -424,6 +424,10 @@ void JsonWizard::openFiles(const JsonWizard::GeneratorFiles &files) { QString errorMessage; bool openedSomething = stringValue("DoNotOpenFile") == "true"; + static const auto formatFile = [](Core::IEditor *editor) { + editor->document()->formatContents(); + editor->document()->save(nullptr); + }; for (const JsonWizard::GeneratorFile &f : files) { const Core::GeneratedFile &file = f.file; if (!file.filePath().exists()) { @@ -454,8 +458,12 @@ void JsonWizard::openFiles(const JsonWizard::GeneratorFiles &files) break; } else if (file.attributes() & Core::GeneratedFile::TemporaryFile) { editor->document()->setTemporary(true); + } else { + formatFile(editor); } openedSomething = true; + } else if (file.filePath().fileSize() < 100 * 1024 ) { + Core::EditorManager::runWithTemporaryEditor(file.filePath(), formatFile); } } diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp index f8512e9a111..bff716ea8ba 100644 --- a/src/plugins/texteditor/textdocument.cpp +++ b/src/plugins/texteditor/textdocument.cpp @@ -717,6 +717,12 @@ bool TextDocument::setContents(const QByteArray &contents) return setPlainText(QString::fromUtf8(contents)); } +void TextDocument::formatContents() +{ + d->m_indenter->format({{document()->firstBlock().blockNumber() + 1, + document()->lastBlock().blockNumber() + 1}}); +} + bool TextDocument::shouldAutoSave() const { return d->m_autoSaveRevision != d->m_document.revision(); diff --git a/src/plugins/texteditor/textdocument.h b/src/plugins/texteditor/textdocument.h index 6527aefd5f4..a4dd3dd45f0 100644 --- a/src/plugins/texteditor/textdocument.h +++ b/src/plugins/texteditor/textdocument.h @@ -103,6 +103,7 @@ public: // IDocument implementation. QByteArray contents() const override; bool setContents(const QByteArray &contents) override; + void formatContents() override; bool shouldAutoSave() const override; bool isModified() const override; bool isSaveAsAllowed() const override;