Commit be40d9ca authored by Jarek Kobus's avatar Jarek Kobus Committed by Jarek Kobus

Apply code style settings to templates

Task-number: QTCREATORBUG-169

Change-Id: I380c956d3256c186ff9497463ab845228017ff87
Reviewed-on: http://codereview.qt.nokia.com/2671Reviewed-by: default avatarQt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: default avatarJarek Kobus <jaroslaw.kobus@nokia.com>
Reviewed-by: default avatarFriedemann Kleint <Friedemann.Kleint@nokia.com>
parent 9e3e7d36
......@@ -599,6 +599,13 @@ void BaseFileWizard::runWizard(const QString &path, QWidget *parent)
case OverwriteOk:
break;
}
foreach (IFileWizardExtension *ex, extensions) {
for (int i = 0; i < files.count(); i++) {
ex->applyCodeStyle(&files[i]);
}
}
// Write
if (!writeFiles(files, &errorMessage)) {
QMessageBox::critical(parent, tr("File Generation Failure"), errorMessage);
......@@ -609,7 +616,7 @@ void BaseFileWizard::runWizard(const QString &path, QWidget *parent)
// Run the extensions
foreach (IFileWizardExtension *ex, extensions) {
bool remove;
if (!ex->process(files, &remove, &errorMessage)) {
if (!ex->processFiles(files, &remove, &errorMessage)) {
QMessageBox::critical(parent, tr("File Generation Failure"), errorMessage);
return;
}
......
......@@ -60,9 +60,14 @@ public:
virtual QList<QWizardPage *> extensionPages(const IWizard *wizard) = 0;
/* Process the files using the extension parameters */
virtual bool process(const QList<GeneratedFile> &files,
virtual bool processFiles(const QList<GeneratedFile> &files,
bool *removeOpenProjectAttribute,
QString *errorMessage) = 0;
/* Applies code style settings which may depend on the project to which
* the files will be added.
* This function is called before the files are actually written out,
* before processFiles() is called*/
virtual void applyCodeStyle(GeneratedFile *file) const = 0;
public slots:
/* Notification about the first extension page being shown. */
......
......@@ -35,6 +35,7 @@
#include "cppcodestylesettingspage.h"
#include "cppcodestylepreferences.h"
#include "cpptoolsconstants.h"
#include "cppqtstyleindenter.h"
#include <texteditor/tabpreferences.h>
#include <texteditor/tabsettings.h>
#include <QtGui/QLayout>
......@@ -74,3 +75,8 @@ QWidget *CppCodeStylePreferencesFactory::createEditor(TextEditor::IFallbackPrefe
return widget;
}
TextEditor::Indenter *CppCodeStylePreferencesFactory::createIndenter() const
{
return new CppQtStyleIndenter();
}
......@@ -48,6 +48,7 @@ public:
virtual QWidget *createEditor(TextEditor::IFallbackPreferences *settings,
TextEditor::TabPreferences *tabSettings,
QWidget *parent) const;
virtual TextEditor::Indenter *createIndenter() const;
};
......
......@@ -154,6 +154,12 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
mcpptools->addAction(command);
connect(switchAction, SIGNAL(triggered()), this, SLOT(switchHeaderSource()));
TextEditor::TextEditorSettings *ts = TextEditor::TextEditorSettings::instance();
ts->registerMimeTypeForLanguageId(QLatin1String(Constants::C_SOURCE_MIMETYPE), Constants::CPP_SETTINGS_ID);
ts->registerMimeTypeForLanguageId(QLatin1String(Constants::C_HEADER_MIMETYPE), Constants::CPP_SETTINGS_ID);
ts->registerMimeTypeForLanguageId(QLatin1String(Constants::CPP_SOURCE_MIMETYPE), Constants::CPP_SETTINGS_ID);
ts->registerMimeTypeForLanguageId(QLatin1String(Constants::CPP_HEADER_MIMETYPE), Constants::CPP_SETTINGS_ID);
return true;
}
......
......@@ -45,7 +45,16 @@
#include <coreplugin/icore.h>
#include <coreplugin/iversioncontrol.h>
#include <coreplugin/vcsmanager.h>
#include <coreplugin/mimedatabase.h>
#include <extensionsystem/pluginmanager.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/indenter.h>
#include <texteditor/codestylepreferencesmanager.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/normalindenter.h>
#include <texteditor/tabpreferences.h>
#include <projectexplorer/project.h>
#include <projectexplorer/editorconfiguration.h>
#include <QtCore/QVariant>
#include <QtCore/QtAlgorithms>
......@@ -53,6 +62,8 @@
#include <QtCore/QFileInfo>
#include <QtCore/QMultiMap>
#include <QtCore/QDir>
#include <QtGui/QTextDocument>
#include <QtGui/QTextCursor>
/*!
\class ProjectExplorer::Internal::ProjectFileWizardExtension
......@@ -408,7 +419,7 @@ void ProjectFileWizardExtension::initProjectChoices(const QString &generatedProj
m_context->page->setProjectToolTips(projectToolTips);
}
bool ProjectFileWizardExtension::process(
bool ProjectFileWizardExtension::processFiles(
const QList<Core::GeneratedFile> &files,
bool *removeOpenProjectAttribute, QString *errorMessage)
{
......@@ -487,5 +498,69 @@ bool ProjectFileWizardExtension::processVersionControl(const QList<Core::Generat
return true;
}
static TextEditor::TabPreferences *tabPreferences(ProjectExplorer::Project *project, const QString &languageId)
{
if (!languageId.isEmpty()) {
if (project)
return project->editorConfiguration()->tabPreferences(languageId);
return TextEditor::TextEditorSettings::instance()->tabPreferences(languageId);
} else if (project) {
return project->editorConfiguration()->tabPreferences();
}
return TextEditor::TextEditorSettings::instance()->tabPreferences();
}
static TextEditor::IFallbackPreferences *codeStylePreferences(ProjectExplorer::Project *project, const QString &languageId)
{
if (languageId.isEmpty())
return 0;
if (project)
return project->editorConfiguration()->codeStylePreferences(languageId);
return TextEditor::TextEditorSettings::instance()->codeStylePreferences(languageId);
}
void ProjectFileWizardExtension::applyCodeStyle(Core::GeneratedFile *file) const
{
if (file->isBinary() || file->contents().isEmpty())
return; // nothing to do
const Core::MimeDatabase *mdb = Core::ICore::instance()->mimeDatabase();
Core::MimeType mt = mdb->findByFile(QFileInfo(file->path()));
const QString languageId = TextEditor::TextEditorSettings::instance()->languageId(mt.type());
if (languageId.isEmpty())
return; // don't modify files like *.ui *.pro
ProjectNode *project = 0;
const int projectIndex = m_context->page->currentProjectIndex() - 1;
if (projectIndex >= 0 && projectIndex < m_context->projects.size())
project = m_context->projects.at(projectIndex).node;
ProjectExplorer::Project *baseProject
= ProjectExplorer::ProjectExplorerPlugin::instance()->session()->projectForNode(project);
TextEditor::ICodeStylePreferencesFactory *factory
= TextEditor::CodeStylePreferencesManager::instance()->factory(languageId);
TextEditor::Indenter *indenter = 0;
if (factory)
indenter = factory->createIndenter();
if (!indenter)
indenter = new TextEditor::NormalIndenter();
TextEditor::TabPreferences *tabPrefs = tabPreferences(baseProject, languageId);
TextEditor::IFallbackPreferences *codeStylePrefs = codeStylePreferences(baseProject, languageId);
indenter->setCodeStylePreferences(codeStylePrefs);
QTextDocument doc(file->contents());
QTextCursor cursor(&doc);
cursor.select(QTextCursor::Document);
indenter->indent(&doc, cursor, QChar::Null, tabPrefs->currentSettings());
file->setContents(doc.toPlainText());
delete indenter;
}
} // namespace Internal
} // namespace ProjectExplorer
......@@ -49,8 +49,9 @@ public:
~ProjectFileWizardExtension();
QList<QWizardPage *> extensionPages(const Core::IWizard *wizard);
bool process(const QList<Core::GeneratedFile> &files,
bool processFiles(const QList<Core::GeneratedFile> &files,
bool *removeOpenProjectAttribute, QString *errorMessage);
void applyCodeStyle(Core::GeneratedFile *file) const;
public slots:
void firstExtensionPageShown(const QList<Core::GeneratedFile> &files);
......
......@@ -33,6 +33,7 @@
#include "qmljscodestylesettingsfactory.h"
#include "qmljscodestylesettingspage.h"
#include "qmljstoolsconstants.h"
#include "qmljsindenter.h"
#include <texteditor/tabpreferences.h>
#include <texteditor/tabsettings.h>
#include <QtGui/QLayout>
......@@ -72,3 +73,8 @@ QWidget *QmlJSCodeStylePreferencesFactory::createEditor(TextEditor::IFallbackPre
return widget;
}
TextEditor::Indenter *QmlJSCodeStylePreferencesFactory::createIndenter() const
{
return new QmlJSEditor::Internal::Indenter();
}
......@@ -48,6 +48,7 @@ public:
virtual QWidget *createEditor(TextEditor::IFallbackPreferences *settings,
TextEditor::TabPreferences *tabSettings,
QWidget *parent) const;
virtual TextEditor::Indenter *createIndenter() const;
};
......
......@@ -126,6 +126,10 @@ bool QmlJSToolsPlugin::initialize(const QStringList &arguments, QString *error)
connect(core->progressManager(), SIGNAL(allTasksFinished(QString)),
this, SLOT(onAllTasksFinished(QString)));
TextEditor::TextEditorSettings *ts = TextEditor::TextEditorSettings::instance();
ts->registerMimeTypeForLanguageId(QLatin1String(Constants::QML_MIMETYPE), Constants::QML_JS_SETTINGS_ID);
ts->registerMimeTypeForLanguageId(QLatin1String(Constants::JS_MIMETYPE), Constants::QML_JS_SETTINGS_ID);
return true;
}
......
......@@ -41,6 +41,7 @@ namespace TextEditor {
class IFallbackPreferences;
class TabPreferences;
class Indenter;
class TEXTEDITOR_EXPORT ICodeStylePreferencesFactory : public QObject
{
......@@ -52,6 +53,7 @@ public:
virtual QString displayName() = 0;
virtual IFallbackPreferences *createPreferences(const QList<IFallbackPreferences *> &fallbacks) const = 0;
virtual QWidget *createEditor(IFallbackPreferences *preferences, TabPreferences *tabSettings, QWidget *parent) const = 0;
virtual TextEditor::Indenter *createIndenter() const = 0;
};
} // namespace TextEditor
......
......@@ -73,6 +73,8 @@ public:
QMap<QString, TabPreferences *> m_languageTabPreferences;
QMap<QString, IFallbackPreferences *> m_languageCodeStylePreferences;
QMap<QString, QString> m_mimeTypeToLanguage;
CompletionSettings m_completionSettings;
void fontZoomRequested(int pointSize);
......@@ -343,4 +345,14 @@ void TextEditorSettings::registerLanguageCodeStylePreferences(const QString &lan
m_d->m_languageCodeStylePreferences.insert(languageId, prefs);
}
void TextEditorSettings::registerMimeTypeForLanguageId(const QString &mimeType, const QString &languageId)
{
m_d->m_mimeTypeToLanguage.insert(mimeType, languageId);
}
QString TextEditorSettings::languageId(const QString &mimeType) const
{
return m_d->m_mimeTypeToLanguage.value(mimeType);
}
#include "moc_texteditorsettings.cpp"
......@@ -96,6 +96,9 @@ public:
QMap<QString, IFallbackPreferences *> languageCodeStylePreferences() const;
void registerLanguageCodeStylePreferences(const QString &languageId, IFallbackPreferences *prefs);
void registerMimeTypeForLanguageId(const QString &mimeType, const QString &languageId);
QString languageId(const QString &mimeType) const;
signals:
void fontSettingsChanged(const TextEditor::FontSettings &);
void storageSettingsChanged(const TextEditor::StorageSettings &);
......
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