diff options
author | Orgad Shaneh <orgad.shaneh@audiocodes.com> | 2013-01-08 03:32:53 +0200 |
---|---|---|
committer | hjk <qthjk@ovi.com> | 2013-01-08 10:48:18 +0100 |
commit | 29a93998df8405e8799ad23934a56cd99fb36403 (patch) | |
tree | c0e4a341efeef78fbe530a618caaa50254daad59 /src/libs/qmljs/qmljsreformatter.cpp | |
parent | 73a2717bed511cffd0163195c314f7d919e5128b (diff) | |
download | qt-creator-29a93998df8405e8799ad23934a56cd99fb36403.tar.gz |
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
Diffstat (limited to 'src/libs/qmljs/qmljsreformatter.cpp')
-rw-r--r-- | src/libs/qmljs/qmljsreformatter.cpp | 34 |
1 files changed, 14 insertions, 20 deletions
diff --git a/src/libs/qmljs/qmljsreformatter.cpp b/src/libs/qmljs/qmljsreformatter.cpp index bc7b78ab61..eb30eca766 100644 --- a/src/libs/qmljs/qmljsreformatter.cpp +++ b/src/libs/qmljs/qmljsreformatter.cpp @@ -166,11 +166,10 @@ protected: if (precededByEmptyLine(fixedLoc)) newLine(); out(toString(fixedLoc)); // don't use the sourceloc overload here - if (followedByNewLine(fixedLoc)) { + if (followedByNewLine(fixedLoc)) newLine(); - } else { + else out(" "); - } } void out(const QString &str, const SourceLocation &lastLoc = SourceLocation()) @@ -260,9 +259,8 @@ protected: qreal result = badnessFromSplits; foreach (const QString &line, lines) { // really long lines should be avoided at all cost - if (line.size() > strongMaxLineLength) { + if (line.size() > strongMaxLineLength) result += 50 + (line.size() - strongMaxLineLength); - } // having long lines is bad else if (line.size() > maxLineLength) { result += 3 + (line.size() - maxLineLength); @@ -446,22 +444,20 @@ protected: virtual bool preVisit(Node *ast) { SourceLocation firstLoc; - if (ExpressionNode *expr = ast->expressionCast()) { + if (ExpressionNode *expr = ast->expressionCast()) firstLoc = expr->firstSourceLocation(); - } else if (Statement *stmt = ast->statementCast()) { + else if (Statement *stmt = ast->statementCast()) firstLoc = stmt->firstSourceLocation(); - } else if (UiObjectMember *mem = ast->uiObjectMemberCast()) { + else if (UiObjectMember *mem = ast->uiObjectMemberCast()) firstLoc = mem->firstSourceLocation(); - } else if (UiImport *import = cast<UiImport *>(ast)) { + else if (UiImport *import = cast<UiImport *>(ast)) firstLoc = import->firstSourceLocation(); - } if (firstLoc.isValid() && int(firstLoc.offset) != _lastNewlineOffset) { _lastNewlineOffset = firstLoc.offset; - if (precededByEmptyLine(firstLoc) && !_result.endsWith(QLatin1String("\n\n"))) { + if (precededByEmptyLine(firstLoc) && !_result.endsWith(QLatin1String("\n\n"))) newLine(); - } } return true; @@ -470,15 +466,14 @@ protected: virtual void postVisit(Node *ast) { SourceLocation lastLoc; - if (ExpressionNode *expr = ast->expressionCast()) { + if (ExpressionNode *expr = ast->expressionCast()) lastLoc = expr->lastSourceLocation(); - } else if (Statement *stmt = ast->statementCast()) { + else if (Statement *stmt = ast->statementCast()) lastLoc = stmt->lastSourceLocation(); - } else if (UiObjectMember *mem = ast->uiObjectMemberCast()) { + else if (UiObjectMember *mem = ast->uiObjectMemberCast()) lastLoc = mem->lastSourceLocation(); - } else if (UiImport *import = cast<UiImport *>(ast)) { + else if (UiImport *import = cast<UiImport *>(ast)) lastLoc = import->lastSourceLocation(); - } if (lastLoc.isValid()) { const QList<SourceLocation> &comments = _doc->engine()->comments(); @@ -510,11 +505,10 @@ protected: virtual bool visit(UiImport *ast) { out("import ", ast->importToken); - if (!ast->fileName.isNull()) { + if (!ast->fileName.isNull()) out(QString::fromLatin1("\"%1\"").arg(ast->fileName.toString())); - } else { + else accept(ast->importUri); - } if (ast->versionToken.isValid()) { out(" "); out(ast->versionToken); |