Commit a871e053 authored by Lorenz Haas's avatar Lorenz Haas Committed by David Schulz

Beautifier: Unification of the tool's command generation

It's a simple refactoring change. The change, however, is a preparation
for further commits and to make the command generation similar to the
one used in ClangFormat.

Change-Id: I28ecc816dd7b271ff6cda908079a60ab70f145fd
Reviewed-by: default avatarJochen Becher <jochen_becher@gmx.de>
Reviewed-by: default avatarDavid Schulz <david.schulz@theqtcompany.com>
parent 34b92b75
......@@ -101,7 +101,19 @@ QList<QObject *> ArtisticStyle::autoReleaseObjects()
void ArtisticStyle::formatFile()
{
QString cfgFileName;
const QString cfgFileName = configurationFile();
if (cfgFileName.isEmpty()) {
BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(
QLatin1String(Constants::ArtisticStyle::DISPLAY_NAME)));
} else {
m_beautifierPlugin->formatCurrentFile(command(cfgFileName));
}
}
QString ArtisticStyle::configurationFile() const
{
if (m_settings->useCustomStyle())
return m_settings->styleFileName(m_settings->customStyle());
if (m_settings->useOtherFiles()) {
if (const ProjectExplorer::Project *project
......@@ -112,48 +124,41 @@ void ArtisticStyle::formatFile()
if (!file.endsWith(QLatin1String(".astylerc")))
continue;
const QFileInfo fi(file);
if (fi.isReadable()) {
cfgFileName = file;
break;
}
if (fi.isReadable())
return file;
}
}
}
if (cfgFileName.isEmpty() && m_settings->useHomeFile()) {
if (m_settings->useHomeFile()) {
const QDir homeDirectory = QDir::home();
QString file = homeDirectory.filePath(QLatin1String(".astylerc"));
if (QFile::exists(file)) {
cfgFileName = file;
} else {
file = homeDirectory.filePath(QLatin1String("astylerc"));
if (QFile::exists(file))
cfgFileName = file;
}
if (QFile::exists(file))
return file;
file = homeDirectory.filePath(QLatin1String("astylerc"));
if (QFile::exists(file))
return file;
}
if (m_settings->useCustomStyle())
cfgFileName = m_settings->styleFileName(m_settings->customStyle());
return QString();
}
if (cfgFileName.isEmpty()) {
BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(
QLatin1String(Constants::ArtisticStyle::DISPLAY_NAME)));
Command ArtisticStyle::command(const QString &cfgFile) const
{
Command command;
command.setExecutable(m_settings->command());
command.addOption(QLatin1String("-q"));
command.addOption(QLatin1String("--options=") + cfgFile);
if (m_settings->version() > ArtisticStyleSettings::Version_2_03) {
command.setProcessing(Command::PipeProcessing);
command.setPipeAddsNewline(true);
command.setReturnsCRLF(Utils::HostOsInfo::isWindowsHost());
} else {
Command command;
command.setExecutable(m_settings->command());
command.addOption(QLatin1String("-q"));
command.addOption(QLatin1String("--options=") + cfgFileName);
if (m_settings->version() > ArtisticStyleSettings::Version_2_03) {
command.setProcessing(Command::PipeProcessing);
command.setPipeAddsNewline(true);
command.setReturnsCRLF(Utils::HostOsInfo::isWindowsHost());
} else {
command.addOption(QLatin1String("%file"));
}
m_beautifierPlugin->formatCurrentFile(command);
command.addOption(QLatin1String("%file"));
}
return command;
}
} // namespace ArtisticStyle
......
......@@ -32,7 +32,7 @@
#define BEAUTIFIER_ARTISTICSTYLE_H
#include "../beautifierabstracttool.h"
#include "../command.h"
QT_FORWARD_DECLARE_CLASS(QAction)
......@@ -63,6 +63,8 @@ private:
BeautifierPlugin *m_beautifierPlugin;
QAction *m_formatFile;
ArtisticStyleSettings *m_settings;
QString configurationFile() const;
Command command(const QString &cfgFile) const;
};
} // namespace ArtisticStyle
......
......@@ -102,7 +102,19 @@ QList<QObject *> Uncrustify::autoReleaseObjects()
void Uncrustify::formatFile()
{
QString cfgFileName;
const QString cfgFileName = configurationFile();
if (cfgFileName.isEmpty()) {
BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(
QLatin1String(Constants::Uncrustify::DISPLAY_NAME)));
} else {
m_beautifierPlugin->formatCurrentFile(command(cfgFileName));
}
}
QString Uncrustify::configurationFile() const
{
if (m_settings->useCustomStyle())
return m_settings->styleFileName(m_settings->customStyle());
if (m_settings->useOtherFiles()) {
if (const ProjectExplorer::Project *project
......@@ -113,38 +125,33 @@ void Uncrustify::formatFile()
if (!file.endsWith(QLatin1String("cfg")))
continue;
const QFileInfo fi(file);
if (fi.isReadable() && fi.fileName() == QLatin1String("uncrustify.cfg")) {
cfgFileName = file;
break;
}
if (fi.isReadable() && fi.fileName() == QLatin1String("uncrustify.cfg"))
return file;
}
}
}
if (cfgFileName.isEmpty() && m_settings->useHomeFile()) {
if (m_settings->useHomeFile()) {
const QString file = QDir::home().filePath(QLatin1String("uncrustify.cfg"));
if (QFile::exists(file))
cfgFileName = file;
return file;
}
if (m_settings->useCustomStyle())
cfgFileName = m_settings->styleFileName(m_settings->customStyle());
return QString();
}
if (cfgFileName.isEmpty()) {
BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(
QLatin1String(Constants::Uncrustify::DISPLAY_NAME)));
} else {
Command command;
command.setExecutable(m_settings->command());
command.setProcessing(Command::PipeProcessing);
command.addOption(QLatin1String("-l"));
command.addOption(QLatin1String("cpp"));
command.addOption(QLatin1String("-L"));
command.addOption(QLatin1String("1-2"));
command.addOption(QLatin1String("-c"));
command.addOption(cfgFileName);
m_beautifierPlugin->formatCurrentFile(command);
}
Command Uncrustify::command(const QString &cfgFile) const
{
Command command;
command.setExecutable(m_settings->command());
command.setProcessing(Command::PipeProcessing);
command.addOption(QLatin1String("-l"));
command.addOption(QLatin1String("cpp"));
command.addOption(QLatin1String("-L"));
command.addOption(QLatin1String("1-2"));
command.addOption(QLatin1String("-c"));
command.addOption(cfgFile);
return command;
}
} // namespace Uncrustify
......
......@@ -32,7 +32,7 @@
#define BEAUTIFIER_UNCRUSTIFY_H
#include "../beautifierabstracttool.h"
#include "../command.h"
QT_FORWARD_DECLARE_CLASS(QAction)
......@@ -63,6 +63,8 @@ private:
BeautifierPlugin *m_beautifierPlugin;
QAction *m_formatFile;
UncrustifySettings *m_settings;
QString configurationFile() const;
Command command(const QString &cfgFile) const;
};
} // namespace Uncrustify
......
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