diff options
author | Hugo Lima <hugo.lima@openbossa.org> | 2009-11-19 14:40:10 -0200 |
---|---|---|
committer | Hugo Lima <hugo.lima@openbossa.org> | 2009-11-19 14:46:38 -0200 |
commit | 2946030013ebe8f80a5596a41b5f183c1b2efa58 (patch) | |
tree | 93aaeb2543f85d653b6f0346bb450e11eae52516 | |
parent | 36b122bc1699f37f0354ce62e8de8e7ab866703b (diff) |
Fix the formatCode function.
Reviewed by Marcelo Lira <marcelo.lira@openbossa.org>
-rw-r--r-- | generator.cpp | 82 |
1 files changed, 18 insertions, 64 deletions
diff --git a/generator.cpp b/generator.cpp index cc7ef6f45..77ad6b098 100644 --- a/generator.cpp +++ b/generator.cpp @@ -340,75 +340,29 @@ FunctionModificationList Generator::functionModifications(const AbstractMetaFunc return mods; } -static QString formattedCodeHelper(QTextStream &s, Indentor &indentor, QStringList &lines) -{ - bool multilineComment = false; - bool lastEmpty = true; - QString lastLine; - while (!lines.isEmpty()) { - const QString line = lines.takeFirst().trimmed(); - if (line.isEmpty()) { - if (!lastEmpty) - s << endl; - lastEmpty = true; - continue; - } else - lastEmpty = false; - - if (line.startsWith("/*")) - multilineComment = true; - - if (multilineComment) { - s << indentor; - if (line.startsWith("*")) - s << " "; - s << line << endl; - if (line.endsWith("*/")) - multilineComment = false; - } else if (line.startsWith("}")) - return line; - else if (line.endsWith("")) { - s << indentor << line << endl; - return 0; - } else if (line.endsWith("{")) { - s << indentor << line << endl; - QString tmp; - { - Indentation indent(indentor); - tmp = formattedCodeHelper(s, indentor, lines); - } - if (!tmp.isNull()) - s << indentor << tmp << endl; - - lastLine = tmp; - continue; - } else { - s << indentor; - if (!lastLine.isEmpty() && - !lastLine.endsWith(";") && - !line.startsWith("@") && - !line.startsWith("//") && - !lastLine.startsWith("//") && - !lastLine.endsWith("}") && - !line.startsWith("{")) - s << " "; - s << line << endl; - } - lastLine = line; - } - return 0; -} - QTextStream& formatCode(QTextStream &s, const QString& code, Indentor &indentor) { + // detect number of spaces before the first character QStringList lst(code.split("\n")); - while (!lst.isEmpty()) { - QString tmp = formattedCodeHelper(s, indentor, lst); - if (!tmp.isNull()) - s << indentor << tmp << endl; + QRegExp nonSpaceRegex("[^\\s]"); + int spacesToRemove = 0; + foreach(QString line, lst) { + if (!line.trimmed().isEmpty()) { + spacesToRemove = line.indexOf(nonSpaceRegex); + if (spacesToRemove == -1) + spacesToRemove = 0; + break; + } + } + foreach(QString line, lst) { + int limit = 0; + for(int i = 0; i < spacesToRemove; ++i) { + if (line[i] == ' ') + limit++; + } + s << indentor << line.remove(0, limit) << endl; } - s.flush(); return s; } |