From e392014baadf98fb8cf38a50bf5c4621236b3aab Mon Sep 17 00:00:00 2001
From: Lorenz Haas <lykurg@gmail.com>
Date: Wed, 9 Jul 2014 08:59:38 +0200
Subject: [PATCH] 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>
---
 .../beautifier/artisticstyle/artisticstyle.cpp       |  2 ++
 src/plugins/beautifier/beautifierplugin.cpp          |  9 +++++++--
 src/plugins/beautifier/command.cpp                   | 12 ++++++++++++
 src/plugins/beautifier/command.h                     |  4 ++++
 4 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/plugins/beautifier/artisticstyle/artisticstyle.cpp b/src/plugins/beautifier/artisticstyle/artisticstyle.cpp
index 7aed71f2885..bed8e72d410 100644
--- a/src/plugins/beautifier/artisticstyle/artisticstyle.cpp
+++ b/src/plugins/beautifier/artisticstyle/artisticstyle.cpp
@@ -51,6 +51,7 @@
 #include <projectexplorer/projectexplorer.h>
 #include <projectexplorer/project.h>
 #include <utils/fileutils.h>
+#include <utils/hostosinfo.h>
 
 #include <QAction>
 #include <QMenu>
@@ -148,6 +149,7 @@ void ArtisticStyle::formatFile()
         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"));
         }
diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp
index 5aae5577855..f0a21aa89ad 100644
--- a/src/plugins/beautifier/beautifierplugin.cpp
+++ b/src/plugins/beautifier/beautifierplugin.cpp
@@ -198,9 +198,14 @@ QString BeautifierPlugin::format(const QString &text, const Command &command,
             return QString();
         }
 
-        if (command.pipeAddsNewline()) {
+        const bool addsNewline = command.pipeAddsNewline();
+        const bool returnsCRLF = command.returnsCRLF();
+        if (addsNewline || returnsCRLF) {
             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 QString::fromUtf8(process.readAllStandardOutput());
diff --git a/src/plugins/beautifier/command.cpp b/src/plugins/beautifier/command.cpp
index 834f469a58c..55206c3b0f8 100644
--- a/src/plugins/beautifier/command.cpp
+++ b/src/plugins/beautifier/command.cpp
@@ -35,6 +35,7 @@ namespace Internal {
 Command::Command()
     : m_processing(FileProcessing)
     , m_pipeAddsNewline(false)
+    , m_returnsCRLF(false)
 {
 }
 
@@ -78,5 +79,16 @@ void Command::setPipeAddsNewline(bool pipeAddsNewline)
     m_pipeAddsNewline = pipeAddsNewline;
 }
 
+bool Command::returnsCRLF() const
+{
+    return m_returnsCRLF;
+}
+
+void Command::setReturnsCRLF(bool returnsCRLF)
+{
+    m_returnsCRLF = returnsCRLF;
+}
+
+
 } // namespace Internal
 } // namespace Beautifier
diff --git a/src/plugins/beautifier/command.h b/src/plugins/beautifier/command.h
index e6774f38fae..102e9f91c3f 100644
--- a/src/plugins/beautifier/command.h
+++ b/src/plugins/beautifier/command.h
@@ -58,11 +58,15 @@ public:
     bool pipeAddsNewline() const;
     void setPipeAddsNewline(bool pipeAddsNewline);
 
+    bool returnsCRLF() const;
+    void setReturnsCRLF(bool returnsCRLF);
+
 private:
     QString m_executable;
     QStringList m_options;
     Processing m_processing;
     bool m_pipeAddsNewline;
+    bool m_returnsCRLF;
 };
 
 } // namespace Internal
-- 
GitLab