Commit b1cc98f7 authored by Tobias Hunger's avatar Tobias Hunger Committed by Tobias Hunger
Browse files

JsonWizard: Support formatting of sources after creating contents



Change-Id: Ia0fbae6a1d1b2f3dd00c578a8d1ac60e0482fba9
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
parent 989ed95c
......@@ -31,8 +31,10 @@
#include "jsonwizard.h"
#include "../addnewmodel.h"
#include "../project.h"
#include "../projectexplorerconstants.h"
#include "../projectnodes.h"
#include "../session.h"
#include <coreplugin/coreconstants.h>
......@@ -79,7 +81,10 @@ static IWizardFactory::WizardKind wizardKind(JsonWizard *wiz)
JsonSummaryPage::JsonSummaryPage(QWidget *parent) :
Internal::ProjectWizardPage(parent)
{ }
{
connect(this, &Internal::ProjectWizardPage::projectNodeChanged,
this, &JsonSummaryPage::projectNodeHasChanged);
}
void JsonSummaryPage::initializePage()
{
......@@ -174,4 +179,17 @@ void JsonSummaryPage::addToProject(const JsonWizard::GeneratorFiles &files)
return;
}
void JsonSummaryPage::projectNodeHasChanged()
{
updateProjectData(currentNode());
}
void JsonSummaryPage::updateProjectData(FolderNode *node)
{
Project *project = SessionManager::projectForNode(node);
wizard()->setProperty("SelectedProject", QVariant::fromValue(project));
wizard()->setProperty("SelectedFolderNode", QVariant::fromValue(node));
}
} // namespace ProjectExplorer
......@@ -37,6 +37,8 @@
namespace ProjectExplorer {
class FolderNode;
// Documentation inside.
class JsonSummaryPage : public Internal::ProjectWizardPage
{
......@@ -51,6 +53,10 @@ public:
public slots:
void triggerCommit(const JsonWizard::GeneratorFiles &files);
void addToProject(const JsonWizard::GeneratorFiles &files);
void projectNodeHasChanged();
private:
void updateProjectData(FolderNode *node);
};
} // namespace ProjectExplorer
......
......@@ -143,7 +143,6 @@ void JsonWizard::removeAttributeFromAllFiles(Core::GeneratedFile::Attribute a)
void JsonWizard::accept()
{
Utils::Wizard::accept();
QString errorMessage;
......
......@@ -30,7 +30,6 @@
#include "jsonwizardfilegenerator.h"
#include "../customwizard/customwizardpreprocessor.h"
#include "../project.h"
#include "../projectexplorer.h"
#include "jsonwizard.h"
#include "jsonwizardfactory.h"
......@@ -158,15 +157,6 @@ Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::AbstractMacroExpan
return result;
}
bool JsonWizardFileGenerator::formatFile(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage)
{
Q_UNUSED(wizard);
Q_UNUSED(file);
Q_UNUSED(errorMessage);
// TODO: Implement me!
return true;
}
bool JsonWizardFileGenerator::writeFile(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage)
{
Q_UNUSED(wizard);
......
......@@ -47,7 +47,6 @@ public:
const QString &wizardDir, const QString &projectDir,
QString *errorMessage);
bool formatFile(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage);
bool writeFile(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage);
bool postWrite(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage);
......
......@@ -32,26 +32,97 @@
#include "jsonwizard.h"
#include "jsonwizardfilegenerator.h"
#include "../editorconfiguration.h"
#include "../project.h"
#include "../projectexplorerconstants.h"
#include <coreplugin/dialogs/promptoverwritedialog.h>
#include <coreplugin/mimedatabase.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/indenter.h>
#include <texteditor/normalindenter.h>
#include <texteditor/storagesettings.h>
#include <texteditor/tabsettings.h>
#include <texteditor/texteditorsettings.h>
#include <utils/algorithm.h>
#include <utils/stringutils.h>
#include <utils/qtcassert.h>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <QStringList>
#include <QTextCursor>
#include <QTextDocument>
using namespace Core;
using namespace TextEditor;
namespace ProjectExplorer {
// --------------------------------------------------------------------
// Helpers:
// --------------------------------------------------------------------
static ICodeStylePreferences *codeStylePreferences(Project *project, Id languageId)
{
if (!languageId.isValid())
return 0;
if (project)
return project->editorConfiguration()->codeStyle(languageId);
return TextEditorSettings::codeStyle(languageId);
}
// --------------------------------------------------------------------
// JsonWizardGenerator:
// --------------------------------------------------------------------
bool JsonWizardGenerator::formatFile(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage)
{
Q_UNUSED(errorMessage);
if (file->isBinary() || file->contents().isEmpty())
return true; // nothing to do
MimeType mt = MimeDatabase::findByFile(QFileInfo(file->path()));
Id languageId = TextEditorSettings::languageId(mt.type());
if (!languageId.isValid())
return true; // don't modify files like *.ui, *.pro
Project *baseProject = qobject_cast<Project *>(wizard->property("SelectedProject").value<QObject *>());
ICodeStylePreferencesFactory *factory = TextEditorSettings::codeStyleFactory(languageId);
Indenter *indenter = 0;
if (factory)
indenter = factory->createIndenter();
if (!indenter)
indenter = new NormalIndenter();
ICodeStylePreferences *codeStylePrefs = codeStylePreferences(baseProject, languageId);
indenter->setCodeStylePreferences(codeStylePrefs);
QTextDocument doc(file->contents());
QTextCursor cursor(&doc);
cursor.select(QTextCursor::Document);
indenter->indent(&doc, cursor, QChar::Null, codeStylePrefs->currentTabSettings());
delete indenter;
if (TextEditorSettings::storageSettings().m_cleanWhitespace) {
QTextBlock block = doc.firstBlock();
while (block.isValid()) {
codeStylePrefs->currentTabSettings().removeTrailingWhitespace(cursor, block);
block = block.next();
}
}
file->setContents(doc.toPlainText());
return true;
}
JsonWizardGenerator::OverwriteResult JsonWizardGenerator::promptForOverwrite(JsonWizard::GeneratorFiles *files,
QString *errorMessage)
{
......
......@@ -51,7 +51,7 @@ public:
virtual Core::GeneratedFiles fileList(Utils::AbstractMacroExpander *expander,
const QString &baseDir, const QString &projectDir,
QString *errorMessage) = 0;
virtual bool formatFile(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage) = 0;
virtual bool formatFile(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage);
virtual bool writeFile(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage) = 0;
virtual bool postWrite(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage) = 0;
......
......@@ -494,6 +494,7 @@ void ProjectWizardPage::slotProjectChanged(int index)
{
setProjectToolTip(index >= 0 && index < m_projectToolTips.size() ?
m_projectToolTips.at(index) : QString());
emit projectNodeChanged();
}
void ProjectWizardPage::slotManageVcs()
......
......@@ -76,6 +76,9 @@ public:
Core::IWizardFactory::WizardKind kind,
ProjectExplorer::ProjectAction action);
signals:
void projectNodeChanged();
public slots:
void initializeVersionControls();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment