Beautifier: Handle CRLF for Artistic Style on Windows correctly

Artistic Style will return CRLF on Windows if you are using a pipe. Thus
we have to convert it.

Change-Id: Id24cac2f35cc3c1978d709fb6c66cee8c8814e34
Reviewed-by: David Schulz <david.schulz@digia.com>
This commit is contained in:
Lorenz Haas
2014-07-09 08:59:38 +02:00
parent 45195b331b
commit e392014baa
4 changed files with 25 additions and 2 deletions

View File

@@ -51,6 +51,7 @@
#include <projectexplorer/projectexplorer.h> #include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <QAction> #include <QAction>
#include <QMenu> #include <QMenu>
@@ -148,6 +149,7 @@ void ArtisticStyle::formatFile()
if (m_settings->version() > ArtisticStyleSettings::Version_2_03) { if (m_settings->version() > ArtisticStyleSettings::Version_2_03) {
command.setProcessing(Command::PipeProcessing); command.setProcessing(Command::PipeProcessing);
command.setPipeAddsNewline(true); command.setPipeAddsNewline(true);
command.setReturnsCRLF(Utils::HostOsInfo::isWindowsHost());
} else { } else {
command.addOption(QLatin1String("%file")); command.addOption(QLatin1String("%file"));
} }

View File

@@ -198,9 +198,14 @@ QString BeautifierPlugin::format(const QString &text, const Command &command,
return QString(); return QString();
} }
if (command.pipeAddsNewline()) { const bool addsNewline = command.pipeAddsNewline();
const bool returnsCRLF = command.returnsCRLF();
if (addsNewline || returnsCRLF) {
QString formatted = QString::fromUtf8(process.readAllStandardOutput()); QString formatted = QString::fromUtf8(process.readAllStandardOutput());
formatted.remove(QRegExp(QLatin1String("(\\r\\n|\\n)$"))); if (addsNewline)
formatted.remove(QRegExp(QLatin1String("(\\r\\n|\\n)$")));
if (returnsCRLF)
formatted.replace(QLatin1String("\r\n"), QLatin1String("\n"));
return formatted; return formatted;
} }
return QString::fromUtf8(process.readAllStandardOutput()); return QString::fromUtf8(process.readAllStandardOutput());

View File

@@ -35,6 +35,7 @@ namespace Internal {
Command::Command() Command::Command()
: m_processing(FileProcessing) : m_processing(FileProcessing)
, m_pipeAddsNewline(false) , m_pipeAddsNewline(false)
, m_returnsCRLF(false)
{ {
} }
@@ -78,5 +79,16 @@ void Command::setPipeAddsNewline(bool pipeAddsNewline)
m_pipeAddsNewline = pipeAddsNewline; m_pipeAddsNewline = pipeAddsNewline;
} }
bool Command::returnsCRLF() const
{
return m_returnsCRLF;
}
void Command::setReturnsCRLF(bool returnsCRLF)
{
m_returnsCRLF = returnsCRLF;
}
} // namespace Internal } // namespace Internal
} // namespace Beautifier } // namespace Beautifier

View File

@@ -58,11 +58,15 @@ public:
bool pipeAddsNewline() const; bool pipeAddsNewline() const;
void setPipeAddsNewline(bool pipeAddsNewline); void setPipeAddsNewline(bool pipeAddsNewline);
bool returnsCRLF() const;
void setReturnsCRLF(bool returnsCRLF);
private: private:
QString m_executable; QString m_executable;
QStringList m_options; QStringList m_options;
Processing m_processing; Processing m_processing;
bool m_pipeAddsNewline; bool m_pipeAddsNewline;
bool m_returnsCRLF;
}; };
} // namespace Internal } // namespace Internal