Commit 4988b41b authored by Lorenz Haas's avatar Lorenz Haas Committed by David Schulz

Introduce new Beautifier plug-in

Beautifier is a plug-in for source code beautifiers.
Currently Artistic Style, Clang-format and Uncrustify are supported.

Task-number: QTCREATORBUG-7489
Change-Id: I8dde9a8ba6a6a55537bfd450ec82ef966ed283c0
Reviewed-by: default avatarDavid Schulz <david.schulz@digia.com>
parent f2ae69b1
<plugin name=\"Beautifier\" version=\"$$QTCREATOR_VERSION\" compatVersion=\"$$QTCREATOR_VERSION\" experimental=\"true\">
<vendor>Lorenz Haas</vendor>
<copyright>(C) 2014 Lorenz Haas</copyright>
<license>
Commercial Usage
Licensees holding valid Qt Commercial licenses may use this plugin in accordance with the Qt Commercial License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written agreement between you and Digia.
GNU Lesser General Public License Usage
Alternatively, this plugin may be used under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. Please review the following information to ensure the GNU Lesser General Public License version 2.1 requirements will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
</license>
<description>Format source files with the help of beautifiers like AStyle, uncrustify or clang-format.</description>
<category>C++</category>
<url>http://www.qt-project.org</url>
$$dependencyList
</plugin>
This diff is collapsed.
/**************************************************************************
**
** Copyright (c) 2014 Lorenz Haas
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef BEAUTIFIER_ABSTRACTSETTINGS_H
#define BEAUTIFIER_ABSTRACTSETTINGS_H
#include <QCoreApplication>
#include <QDir>
#include <QHash>
#include <QMap>
#include <QSet>
#include <QString>
#include <QStringList>
namespace Beautifier {
namespace Internal {
class AbstractSettings
{
Q_DECLARE_TR_FUNCTIONS(AbstractSettings)
public:
explicit AbstractSettings(const QString &name, const QString &ending);
virtual ~AbstractSettings();
void read();
void save();
virtual QString documentationFilePath() const = 0;
virtual void createDocumentationFile() const;
virtual QStringList completerWords();
QStringList styles() const;
QString style(const QString &key) const;
bool styleExists(const QString &key) const;
bool styleIsReadOnly(const QString &key);
void setStyle(const QString &key, const QString &value);
void removeStyle(const QString &key);
void replaceStyle(const QString &oldKey, const QString &newKey, const QString &value);
QString styleFileName(const QString &key) const;
QString command() const;
void setCommand(const QString &command);
QStringList options();
QString documentation(const QString &option) const;
protected:
QMap<QString, QString> m_styles;
QMap<QString, QVariant> m_settings;
void readDocumentation();
private:
QString m_name;
QString m_ending;
QDir m_styleDir;
QStringList m_stylesToRemove;
QSet<QString> m_changedStyles;
QString m_command;
QHash<QString, int> m_options;
QStringList m_docu;
};
} // namespace Internal
} // namespace Beautifier
#endif // BEAUTIFIER_ABSTRACTSETTINGS_H
/**************************************************************************
**
** Copyright (c) 2014 Lorenz Haas
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "artisticstyle.h"
#include "artisticstyleconstants.h"
#include "artisticstyleoptionspage.h"
#include "artisticstylesettings.h"
#include "../beautifierconstants.h"
#include "../beautifierplugin.h"
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <cppeditor/cppeditorconstants.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
#include <utils/fileutils.h>
#include <QAction>
#include <QMenu>
namespace Beautifier {
namespace Internal {
namespace ArtisticStyle {
ArtisticStyle::ArtisticStyle(QObject *parent) :
BeautifierAbstractTool(parent),
m_settings(new ArtisticStyleSettings)
{
}
ArtisticStyle::~ArtisticStyle()
{
delete m_settings;
}
bool ArtisticStyle::initialize()
{
Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::ArtisticStyle::MENU_ID);
menu->menu()->setTitle(QLatin1String("Artistic Style"));
m_formatFile = new QAction(tr("Format Current File"), this);
Core::Command *cmd
= Core::ActionManager::registerAction(m_formatFile,
Constants::ArtisticStyle::ACTION_FORMATFILE,
Core::Context(Core::Constants::C_GLOBAL));
menu->addAction(cmd);
connect(m_formatFile, SIGNAL(triggered()), this, SLOT(formatFile()));
Core::ActionManager::actionContainer(Constants::MENU_ID)->addMenu(menu);
return true;
}
void ArtisticStyle::updateActions(Core::IEditor *editor)
{
m_formatFile->setEnabled(editor && editor->id() == CppEditor::Constants::CPPEDITOR_ID);
}
QList<QObject *> ArtisticStyle::autoReleaseObjects()
{
ArtisticStyleOptionsPage *optionsPage = new ArtisticStyleOptionsPage(m_settings, this);
return QList<QObject *>() << optionsPage;
}
void ArtisticStyle::formatFile()
{
QString cfgFileName;
if (m_settings->useOtherFiles()) {
if (const ProjectExplorer::Project *project
= ProjectExplorer::ProjectExplorerPlugin::currentProject()) {
const QStringList files = project->files(ProjectExplorer::Project::AllFiles);
for (int i = 0, total = files.size(); i < total; ++i) {
const QString &file = files.at(i);
if (!file.endsWith(QLatin1String(".astylerc")))
continue;
const QFileInfo fi(file);
if (fi.isReadable()) {
cfgFileName = file;
break;
}
}
}
}
if (cfgFileName.isEmpty() && m_settings->useHomeFile()) {
QString file = QDir::home().filePath(QLatin1String(".astylerc"));
if (QFile::exists(file)) {
cfgFileName = file;
} else {
file = QDir::home().filePath(QLatin1String("astylerc"));
if (QFile::exists(file))
cfgFileName = file;
}
}
if (m_settings->useCustomStyle())
cfgFileName = m_settings->styleFileName(m_settings->customStyle());
if (cfgFileName.isEmpty()) {
BeautifierPlugin::showError(tr("Could not get configuration file for Artistic Style."));
} else {
BeautifierPlugin::formatCurrentFile(QStringList()
<< m_settings->command()
<< QLatin1String("-q")
<< QLatin1String("--options=") + cfgFileName
<< QLatin1String("%file"));
}
}
} // namespace ArtisticStyle
} // namespace Internal
} // namespace Beautifier
/**************************************************************************
**
** Copyright (c) 2014 Lorenz Haas
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef BEAUTIFIER_ARTISTICSTYLE_H
#define BEAUTIFIER_ARTISTICSTYLE_H
#include "../beautifierabstracttool.h"
#include <utils/qtcoverride.h>
QT_FORWARD_DECLARE_CLASS(QAction)
namespace Beautifier {
namespace Internal {
namespace ArtisticStyle {
class ArtisticStyleSettings;
class ArtisticStyle : public BeautifierAbstractTool
{
Q_OBJECT
public:
explicit ArtisticStyle(QObject *parent = 0);
virtual ~ArtisticStyle();
bool initialize() QTC_OVERRIDE;
void updateActions(Core::IEditor *editor) QTC_OVERRIDE;
QList<QObject *> autoReleaseObjects() QTC_OVERRIDE;
private slots:
void formatFile();
private:
QAction *m_formatFile;
ArtisticStyleSettings *m_settings;
};
} // namespace ArtisticStyle
} // namespace Internal
} // namespace Beautifier
#endif // BEAUTIFIER_ARTISTICSTYLE_H
/**************************************************************************
**
** Copyright (c) 2014 Lorenz Haas
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef BEAUTIFIER_ARTISTICSTYLECONSTANTS_H
#define BEAUTIFIER_ARTISTICSTYLECONSTANTS_H
namespace Beautifier {
namespace Constants {
namespace ArtisticStyle {
const char ACTION_FORMATFILE[] = "ArtisticStyle.FormatFile";
const char MENU_ID[] = "ArtisticStyle.Menu";
const char OPTION_ID[] = "ArtisticStyle";
const char SETTINGS_NAME[] = "artisticstyle";
} // namespace ArtisticStyle
} // namespace Constants
} // namespace Beautifier
#endif // BEAUTIFIER_ARTISTICSTYLECONSTANTS_H
/**************************************************************************
**
** Copyright (c) 2014 Lorenz Haas
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "artisticstyleoptionspage.h"
#include "ui_artisticstyleoptionspage.h"
#include "artisticstyleconstants.h"
#include "artisticstylesettings.h"
#include "../beautifierconstants.h"
#include <coreplugin/icore.h>
#include <QTextStream>
namespace Beautifier {
namespace Internal {
namespace ArtisticStyle {
ArtisticStyleOptionsPageWidget::ArtisticStyleOptionsPageWidget(ArtisticStyleSettings *settings,
QWidget *parent)
: QWidget(parent)
, ui(new Ui::ArtisticStyleOptionsPage)
, m_settings(settings)
{
ui->setupUi(this);
ui->command->setExpectedKind(Utils::PathChooser::ExistingCommand);
ui->command->setPromptDialogTitle(tr("Artistic Style Command"));
connect(ui->command, SIGNAL(validChanged(bool)), ui->options, SLOT(setEnabled(bool)));
ui->configurations->setSettings(m_settings);
}
ArtisticStyleOptionsPageWidget::~ArtisticStyleOptionsPageWidget()
{
delete ui;
}
QString ArtisticStyleOptionsPageWidget::searchKeywords() const
{
QString keywords;
const QLatin1Char sep(' ');
QTextStream(&keywords) << sep << ui->configuration->title()
<< sep << ui->commandLabel->text()
<< sep << ui->options->title()
<< sep << ui->useOtherFiles->text()
<< sep << ui->useHomeFile->text()
<< sep << ui->useCustomStyle->text();
keywords.remove(QLatin1Char('&'));
return keywords;
}
void ArtisticStyleOptionsPageWidget::restore()
{
ui->command->setPath(m_settings->command());
ui->useOtherFiles->setChecked(m_settings->useOtherFiles());
ui->useHomeFile->setChecked(m_settings->useHomeFile());
ui->useCustomStyle->setChecked(m_settings->useCustomStyle());
ui->configurations->setCurrentConfiguration(m_settings->customStyle());
}
void ArtisticStyleOptionsPageWidget::apply()
{
m_settings->setCommand(ui->command->path());
m_settings->setUseOtherFiles(ui->useOtherFiles->isChecked());
m_settings->setUseHomeFile(ui->useHomeFile->isChecked());
m_settings->setUseCustomStyle(ui->useCustomStyle->isChecked());
m_settings->setCustomStyle(ui->configurations->currentConfiguration());
m_settings->save();
}
/* ---------------------------------------------------------------------------------------------- */
ArtisticStyleOptionsPage::ArtisticStyleOptionsPage(ArtisticStyleSettings *settings, QObject *parent) :
IOptionsPage(parent),
m_widget(0),
m_settings(settings),
m_searchKeywords()
{
setId(Constants::ArtisticStyle::OPTION_ID);
setDisplayName(tr("Artistic Style"));
setCategory(Constants::OPTION_CATEGORY);
setDisplayCategory(QCoreApplication::translate("Beautifier", Constants::OPTION_TR_CATEGORY));
setCategoryIcon(QLatin1String(Constants::OPTION_CATEGORY_ICON));
}
QWidget *ArtisticStyleOptionsPage::widget()
{
m_settings->read();
if (!m_widget) {
m_widget = new ArtisticStyleOptionsPageWidget(m_settings);
if (m_searchKeywords.isEmpty())
m_searchKeywords = m_widget->searchKeywords();
}
m_widget->restore();
return m_widget;
}
void ArtisticStyleOptionsPage::apply()
{
if (m_widget)
m_widget->apply();
}
void ArtisticStyleOptionsPage::finish()
{
}
bool ArtisticStyleOptionsPage::matches(const QString &searchKeyWord) const
{
return m_searchKeywords.contains(searchKeyWord, Qt::CaseInsensitive);
}
} // namespace ArtisticStyle
} // namespace Internal
} // namespace Beautifier
/**************************************************************************
**
** Copyright (c) 2014 Lorenz Haas
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef BEAUTIFIER_ARTISTICSTYLEOPTIONSPAGE_H
#define BEAUTIFIER_ARTISTICSTYLEOPTIONSPAGE_H
#include <coreplugin/dialogs/ioptionspage.h>
#include <utils/qtcoverride.h>
#include <QPointer>
#include <QWidget>
namespace Beautifier {
namespace Internal {
namespace ArtisticStyle {
class ArtisticStyleSettings;
namespace Ui { class ArtisticStyleOptionsPage; }
class ArtisticStyleOptionsPageWidget : public QWidget
{
Q_OBJECT
public:
explicit ArtisticStyleOptionsPageWidget(ArtisticStyleSettings *settings, QWidget *parent = 0);
virtual ~ArtisticStyleOptionsPageWidget();
QString searchKeywords() const;
void restore();
void apply();
private:
Ui::ArtisticStyleOptionsPage *ui;
ArtisticStyleSettings *m_settings;
};
class ArtisticStyleOptionsPage : public Core::IOptionsPage
{
Q_OBJECT
public:
explicit ArtisticStyleOptionsPage(ArtisticStyleSettings *settings, QObject *parent = 0);
QWidget *widget() QTC_OVERRIDE;
void apply() QTC_OVERRIDE;
void finish() QTC_OVERRIDE;
bool matches(const QString &searchKeywords) const QTC_OVERRIDE;
private:
QPointer<ArtisticStyleOptionsPageWidget> m_widget;
ArtisticStyleSettings *m_settings;
QString m_searchKeywords;
};
} // namespace ArtisticStyle
} // namespace Internal
} // namespace Beautifier
#endif // BEAUTIFIER_ARTISTICSTYLEOPTIONSPAGE_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Beautifier::Internal::ArtisticStyle::ArtisticStyleOptionsPage</class>
<widget class="QWidget" name="Beautifier::Internal::ArtisticStyle::ArtisticStyleOptionsPage">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>