diff --git a/src/shared/cplusplus/PrettyPrinter.cpp b/src/shared/cplusplus/PrettyPrinter.cpp index c97de44140057596285fdaf14684fe843fffa12e..da116f7e7d3ca3e1065ae411baa76e1cf1b3a31f 100644 --- a/src/shared/cplusplus/PrettyPrinter.cpp +++ b/src/shared/cplusplus/PrettyPrinter.cpp @@ -30,10 +30,14 @@ #include "PrettyPrinter.h" #include "AST.h" #include "Token.h" + #include <iostream> #include <string> +#include <sstream> #include <cassert> +#include <QString> + CPLUSPLUS_USE_NAMESPACE PrettyPrinter::PrettyPrinter(Control *control, std::ostream &out) @@ -73,13 +77,38 @@ void PrettyPrinter::outToken(unsigned token) const unsigned end = t.begin(); _lastToken = token; + std::ostringstream oss; + // Preserve non-AST text QByteArray ba(_contents.constData() + start, end - start); - _out << ba.constData(); + oss << ba.constData(); // Print the token itself QByteArray tt(_contents.constData() + t.begin(), t.length); - _out << tt.constData(); + oss << tt.constData(); + + QString stuff = QString::fromUtf8(oss.str().c_str()); + QString indent = QString(_depth * 4, QLatin1Char(' ')); + + int from = 0; + int index = 0; + while ((index = stuff.indexOf(QLatin1Char('\n'), from)) != -1) { + from = index + 1; + int firstNonWhitespace = from; + + while (firstNonWhitespace < stuff.length()) { + const QChar c = stuff.at(firstNonWhitespace); + if (c.isSpace() && c != QLatin1Char('\n')) + ++firstNonWhitespace; + else + break; + } + + if (firstNonWhitespace != from) + stuff.replace(from, firstNonWhitespace - from, indent); + } + + _out << stuff.toUtf8().constData(); } bool PrettyPrinter::visit(AccessDeclarationAST *ast)