Browse Source

* Src/PrettyPrinter/CSharp/OutputFormatter.cs:

* Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs:
* Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs: Added place on new
  line options.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3856 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Mike Krüger 17 years ago
parent
commit
b978482653
  1. 54
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs
  2. 8
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/OutputFormatter.cs
  3. 42
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs

54
src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs

@ -986,6 +986,10 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
#region Statements #region Statements
void OutputBlock(BlockStatement blockStatement, BraceStyle braceStyle) void OutputBlock(BlockStatement blockStatement, BraceStyle braceStyle)
{
OutputBlock(blockStatement, braceStyle, true);
}
void OutputBlock(BlockStatement blockStatement, BraceStyle braceStyle, bool emitEndingNewLine)
{ {
BeginVisit(blockStatement); BeginVisit(blockStatement);
if (blockStatement.IsNull) { if (blockStatement.IsNull) {
@ -1003,7 +1007,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
if (!outputFormatter.LastCharacterIsNewLine) if (!outputFormatter.LastCharacterIsNewLine)
outputFormatter.NewLine(); outputFormatter.NewLine();
} }
outputFormatter.EndBrace(this.prettyPrintOptions.IndentBlocks); outputFormatter.EndBrace (this.prettyPrintOptions.IndentBlocks, emitEndingNewLine);
} }
EndVisit(blockStatement); EndVisit(blockStatement);
} }
@ -1261,7 +1265,11 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
if (ifElseStatement.HasElseStatements) { if (ifElseStatement.HasElseStatements) {
if (prettyPrintOptions.PlaceElseOnNewLine) {
outputFormatter.Indent(); outputFormatter.Indent();
} else {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Else); outputFormatter.PrintToken(Tokens.Else);
PrintIfSection(ifElseStatement.FalseStatement); PrintIfSection(ifElseStatement.FalseStatement);
} }
@ -1271,6 +1279,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
void PrintIfSection(List<Statement> statements) void PrintIfSection(List<Statement> statements)
{ {
if (statements.Count == 1 && (statements[0] is BlockStatement)) {
OutputBlock((BlockStatement)statements[0],
prettyPrintOptions.StatementBraceStyle,
prettyPrintOptions.PlaceElseOnNewLine);
return;
}
if (statements.Count != 1 || !(statements[0] is BlockStatement)) { if (statements.Count != 1 || !(statements[0] is BlockStatement)) {
outputFormatter.Space(); outputFormatter.Space();
} }
@ -1290,7 +1304,11 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public override object TrackedVisitElseIfSection(ElseIfSection elseIfSection, object data) public override object TrackedVisitElseIfSection(ElseIfSection elseIfSection, object data)
{ {
if (prettyPrintOptions.PlaceElseOnNewLine) {
outputFormatter.Indent(); outputFormatter.Indent();
} else {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Else); outputFormatter.PrintToken(Tokens.Else);
outputFormatter.Space(); outputFormatter.Space();
outputFormatter.PrintToken(Tokens.If); outputFormatter.PrintToken(Tokens.If);
@ -1307,7 +1325,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
outputFormatter.PrintToken(Tokens.CloseParenthesis); outputFormatter.PrintToken(Tokens.CloseParenthesis);
WriteEmbeddedStatement(elseIfSection.EmbeddedStatement); WriteEmbeddedStatement(elseIfSection.EmbeddedStatement, prettyPrintOptions.PlaceElseOnNewLine);
return null; return null;
} }
@ -1371,9 +1389,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
void WriteEmbeddedStatement (Statement statement) void WriteEmbeddedStatement (Statement statement)
{
WriteEmbeddedStatement (statement, true);
}
void WriteEmbeddedStatement (Statement statement, bool emitEndingNewLine)
{ {
if (statement is BlockStatement) { if (statement is BlockStatement) {
TrackVisit(statement, prettyPrintOptions.StatementBraceStyle); OutputBlock((BlockStatement)statement, prettyPrintOptions.StatementBraceStyle, emitEndingNewLine);
} else { } else {
++outputFormatter.IndentationLevel; ++outputFormatter.IndentationLevel;
outputFormatter.NewLine(); outputFormatter.NewLine();
@ -1593,10 +1616,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.PrintToken(Tokens.Do); outputFormatter.PrintToken(Tokens.Do);
} }
WriteEmbeddedStatement(doLoopStatement.EmbeddedStatement); WriteEmbeddedStatement(doLoopStatement.EmbeddedStatement, prettyPrintOptions.PlaceWhileOnNewLine);
if (doLoopStatement.ConditionPosition == ConditionPosition.End) { if (doLoopStatement.ConditionPosition == ConditionPosition.End) {
if (prettyPrintOptions.PlaceWhileOnNewLine) {
outputFormatter.Indent(); outputFormatter.Indent();
} else {
outputFormatter.Space();
}
PrintLoopCheck(doLoopStatement); PrintLoopCheck(doLoopStatement);
outputFormatter.PrintToken(Tokens.Semicolon); outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine(); outputFormatter.NewLine();
@ -1694,23 +1721,34 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public override object TrackedVisitTryCatchStatement(TryCatchStatement tryCatchStatement, object data) public override object TrackedVisitTryCatchStatement(TryCatchStatement tryCatchStatement, object data)
{ {
outputFormatter.PrintToken(Tokens.Try); outputFormatter.PrintToken(Tokens.Try);
WriteEmbeddedStatement(tryCatchStatement.StatementBlock);
foreach (CatchClause catchClause in tryCatchStatement.CatchClauses) { WriteEmbeddedStatement (tryCatchStatement.StatementBlock, prettyPrintOptions.PlaceCatchOnNewLine);
TrackVisit(catchClause, data); for (int i = 0 ; i < tryCatchStatement.CatchClauses.Count; i++) {
TrackVisit(tryCatchStatement.CatchClauses[i], i == tryCatchStatement.CatchClauses.Count - 1);
} }
if (!tryCatchStatement.FinallyBlock.IsNull) { if (!tryCatchStatement.FinallyBlock.IsNull) {
if (prettyPrintOptions.PlaceFinallyOnNewLine) {
// if (!prettyPrintOptions.PlaceCatchOnNewLine)
// outputFormatter.NewLine ();
outputFormatter.Indent(); outputFormatter.Indent();
} else {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Finally); outputFormatter.PrintToken(Tokens.Finally);
WriteEmbeddedStatement(tryCatchStatement.FinallyBlock); WriteEmbeddedStatement(tryCatchStatement.FinallyBlock);
} }
return null; return null;
} }
public override object TrackedVisitCatchClause(CatchClause catchClause, object data) public override object TrackedVisitCatchClause(CatchClause catchClause, object data)
{ {
if (prettyPrintOptions.PlaceCatchOnNewLine) {
outputFormatter.Indent(); outputFormatter.Indent();
} else {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Catch); outputFormatter.PrintToken(Tokens.Catch);
if (!catchClause.TypeReference.IsNull) { if (!catchClause.TypeReference.IsNull) {
@ -1731,7 +1769,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
outputFormatter.PrintToken(Tokens.CloseParenthesis); outputFormatter.PrintToken(Tokens.CloseParenthesis);
} }
WriteEmbeddedStatement(catchClause.StatementBlock); WriteEmbeddedStatement(catchClause.StatementBlock, ((bool)data) ? prettyPrintOptions.PlaceFinallyOnNewLine : prettyPrintOptions.PlaceCatchOnNewLine);
return null; return null;
} }

8
src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/OutputFormatter.cs

@ -88,6 +88,11 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
public void EndBrace (bool indent) public void EndBrace (bool indent)
{
EndBrace (indent, true);
}
public void EndBrace (bool indent, bool emitNewLine)
{ {
BraceStyle style = (BraceStyle)braceStack.Pop(); BraceStyle style = (BraceStyle)braceStack.Pop();
switch (style) { switch (style) {
@ -98,11 +103,13 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
--IndentationLevel; --IndentationLevel;
Indent(); Indent();
PrintToken(Tokens.CloseCurlyBrace); PrintToken(Tokens.CloseCurlyBrace);
if (emitNewLine)
NewLine(); NewLine();
break; break;
case BraceStyle.NextLineShifted: case BraceStyle.NextLineShifted:
Indent(); Indent();
PrintToken(Tokens.CloseCurlyBrace); PrintToken(Tokens.CloseCurlyBrace);
if (emitNewLine)
NewLine(); NewLine();
if (indent) if (indent)
--IndentationLevel; --IndentationLevel;
@ -112,6 +119,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
--IndentationLevel; --IndentationLevel;
Indent(); Indent();
PrintToken(Tokens.CloseCurlyBrace); PrintToken(Tokens.CloseCurlyBrace);
if (emitNewLine)
NewLine(); NewLine();
--IndentationLevel; --IndentationLevel;
break; break;

42
src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs

@ -340,6 +340,48 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
#endregion #endregion
#region NewLines
bool placeCatchOnNewLine = true;
public bool PlaceCatchOnNewLine {
get {
return placeCatchOnNewLine;
}
set {
placeCatchOnNewLine = value;
}
}
bool placeFinallyOnNewLine = true;
public bool PlaceFinallyOnNewLine {
get {
return placeFinallyOnNewLine;
}
set {
placeFinallyOnNewLine = value;
}
}
bool placeElseOnNewLine = true;
public bool PlaceElseOnNewLine {
get {
return placeElseOnNewLine;
}
set {
placeElseOnNewLine = value;
}
}
bool placeWhileOnNewLine = true;
public bool PlaceWhileOnNewLine {
get {
return placeWhileOnNewLine;
}
set {
placeWhileOnNewLine = value;
}
}
#endregion
#region Spaces #region Spaces
#region Before Parentheses #region Before Parentheses

Loading…
Cancel
Save