From c16eaaff79928048b55ffa2af061232d967c80e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Fri, 13 Mar 2009 16:11:41 +0000 Subject: [PATCH] * NRefactory.csproj: * Src/PrettyPrinter/CSharp/OutputFormatter.cs: * Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs: * Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs: Added some options. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3849 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../NRefactory/Project/NRefactory.csproj | 43 +++--- .../CSharp/CSharpOutputVisitor.cs | 71 +++++++--- .../PrettyPrinter/CSharp/OutputFormatter.cs | 25 ++-- .../CSharp/PrettyPrintOptions.cs | 129 +++++++++++++++++- 4 files changed, 214 insertions(+), 54 deletions(-) diff --git a/src/Libraries/NRefactory/Project/NRefactory.csproj b/src/Libraries/NRefactory/Project/NRefactory.csproj index db7767e1f3..f1b8d3b441 100644 --- a/src/Libraries/NRefactory/Project/NRefactory.csproj +++ b/src/Libraries/NRefactory/Project/NRefactory.csproj @@ -1,4 +1,5 @@ - + + Debug AnyCPU @@ -26,18 +27,19 @@ v3.5 - False - True + false + true TEST;DEBUG;NET35 ..\..\..\..\bin\ - false + none + 4 - True - False + true TEST;NET35 ..\..\..\..\bin\ - false + none + 4 Full @@ -77,9 +79,6 @@ - - Configuration\GlobalAssemblyInfo.cs - @@ -137,26 +136,24 @@ - - + + - + - + - - - - - - - - - + + + + + + + diff --git a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs index c675ad8bf8..5db0a810f9 100644 --- a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs +++ b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs @@ -315,11 +315,11 @@ namespace ICSharpCode.NRefactory.PrettyPrinter outputFormatter.Space(); outputFormatter.PrintIdentifier(namespaceDeclaration.Name); - outputFormatter.BeginBrace(this.prettyPrintOptions.NamespaceBraceStyle); + outputFormatter.BeginBrace (this.prettyPrintOptions.NamespaceBraceStyle, this.prettyPrintOptions.IndentNamespaceBody); namespaceDeclaration.AcceptChildren(this, data); - outputFormatter.EndBrace(); + outputFormatter.EndBrace(this.prettyPrintOptions.IndentNamespaceBody); return null; } @@ -392,16 +392,16 @@ namespace ICSharpCode.NRefactory.PrettyPrinter switch (typeDeclaration.Type) { case ClassType.Enum: - outputFormatter.BeginBrace(this.prettyPrintOptions.EnumBraceStyle); + outputFormatter.BeginBrace(this.prettyPrintOptions.EnumBraceStyle, this.prettyPrintOptions.IndentEnumBody); break; case ClassType.Interface: - outputFormatter.BeginBrace(this.prettyPrintOptions.InterfaceBraceStyle); + outputFormatter.BeginBrace(this.prettyPrintOptions.InterfaceBraceStyle, this.prettyPrintOptions.IndentInterfaceBody); break; case ClassType.Struct: - outputFormatter.BeginBrace(this.prettyPrintOptions.StructBraceStyle); + outputFormatter.BeginBrace(this.prettyPrintOptions.StructBraceStyle, this.prettyPrintOptions.IndentStructBody); break; default: - outputFormatter.BeginBrace(this.prettyPrintOptions.ClassBraceStyle); + outputFormatter.BeginBrace(this.prettyPrintOptions.ClassBraceStyle, this.prettyPrintOptions.IndentClassBody); break; } @@ -413,7 +413,20 @@ namespace ICSharpCode.NRefactory.PrettyPrinter typeDeclaration.AcceptChildren(this, data); } currentType = oldType; - outputFormatter.EndBrace(); + switch (typeDeclaration.Type) { + case ClassType.Enum: + outputFormatter.EndBrace(this.prettyPrintOptions.IndentEnumBody); + break; + case ClassType.Interface: + outputFormatter.EndBrace(this.prettyPrintOptions.IndentInterfaceBody); + break; + case ClassType.Struct: + outputFormatter.EndBrace(this.prettyPrintOptions.IndentStructBody); + break; + default: + outputFormatter.EndBrace(this.prettyPrintOptions.IndentCaseBody); + break; + } return null; } @@ -536,12 +549,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter } outputFormatter.PrintIdentifier(propertyDeclaration.Name); - outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle); + outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle, this.prettyPrintOptions.IndentPropertyBody); TrackVisit(propertyDeclaration.GetRegion, data); TrackVisit(propertyDeclaration.SetRegion, data); - outputFormatter.EndBrace(); + outputFormatter.EndBrace(this.prettyPrintOptions.IndentPropertyBody); return null; } @@ -601,10 +614,10 @@ namespace ICSharpCode.NRefactory.PrettyPrinter outputFormatter.PrintToken(Tokens.Semicolon); outputFormatter.NewLine(); } else { - outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle); + outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle, this.prettyPrintOptions.IndentEventBody); TrackVisit(eventDeclaration.AddRegion, data); TrackVisit(eventDeclaration.RemoveRegion, data); - outputFormatter.EndBrace(); + outputFormatter.EndBrace(this.prettyPrintOptions.IndentEventBody); } return null; } @@ -924,7 +937,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter outputFormatter.PrintToken(Tokens.Semicolon); outputFormatter.NewLine(); } else { - outputFormatter.BeginBrace(braceStyle); + outputFormatter.BeginBrace(braceStyle, this.prettyPrintOptions.IndentBlocks); foreach (Statement stmt in blockStatement.Children) { outputFormatter.Indent(); if (stmt is BlockStatement) { @@ -935,7 +948,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter if (!outputFormatter.LastCharacterIsNewLine) outputFormatter.NewLine(); } - outputFormatter.EndBrace(); + outputFormatter.EndBrace(this.prettyPrintOptions.IndentBlocks); } EndVisit(blockStatement); } @@ -1035,7 +1048,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter outputFormatter.PrintToken(Tokens.Null); outputFormatter.PrintToken(Tokens.CloseParenthesis); - outputFormatter.BeginBrace(BraceStyle.EndOfLine); + outputFormatter.BeginBrace(BraceStyle.EndOfLine, this.prettyPrintOptions.IndentBlocks); outputFormatter.Indent(); outputFormatter.PrintIdentifier(raiseEventStatement.EventName); @@ -1045,7 +1058,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter outputFormatter.PrintToken(Tokens.Semicolon); outputFormatter.NewLine(); - outputFormatter.EndBrace(); + outputFormatter.EndBrace(this.prettyPrintOptions.IndentBlocks); return null; } @@ -1321,11 +1334,15 @@ namespace ICSharpCode.NRefactory.PrettyPrinter outputFormatter.Space(); outputFormatter.PrintToken(Tokens.OpenCurlyBrace); outputFormatter.NewLine(); - ++outputFormatter.IndentationLevel; + if (prettyPrintOptions.IndentSwitchBody) + ++outputFormatter.IndentationLevel; + foreach (SwitchSection section in switchStatement.SwitchSections) { TrackVisit(section, data); } - --outputFormatter.IndentationLevel; + + if (prettyPrintOptions.IndentSwitchBody) + --outputFormatter.IndentationLevel; outputFormatter.Indent(); outputFormatter.PrintToken(Tokens.CloseCurlyBrace); return null; @@ -1336,14 +1353,26 @@ namespace ICSharpCode.NRefactory.PrettyPrinter foreach (CaseLabel label in switchSection.SwitchLabels) { TrackVisit(label, data); } - - ++outputFormatter.IndentationLevel; - foreach (Statement stmt in switchSection.Children) { + int standardIndentLevel = outputFormatter.IndentationLevel; + if (prettyPrintOptions.IndentCaseBody) + ++outputFormatter.IndentationLevel; + for (int i = 0; i < switchSection.Children.Count; i++) { + Statement stmt = switchSection.Children[i] as Statement; + int oldIndent = outputFormatter.IndentationLevel; + if (i == switchSection.Children.Count - 1) { + if (prettyPrintOptions.IndentBreakStatements) + outputFormatter.IndentationLevel = standardIndentLevel + 1; + else + outputFormatter.IndentationLevel = standardIndentLevel; + } outputFormatter.Indent(); TrackVisit(stmt, data); outputFormatter.NewLine(); + outputFormatter.IndentationLevel = oldIndent; } - --outputFormatter.IndentationLevel; + + if (prettyPrintOptions.IndentCaseBody) + --outputFormatter.IndentationLevel; return null; } diff --git a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/OutputFormatter.cs b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/OutputFormatter.cs index 3bd4868a00..d3edca2b68 100644 --- a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/OutputFormatter.cs +++ b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/OutputFormatter.cs @@ -40,7 +40,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter Stack braceStack = new Stack(); - public void BeginBrace(BraceStyle style) + public void BeginBrace(BraceStyle style, bool indent) { switch (style) { case BraceStyle.EndOfLine: @@ -49,25 +49,29 @@ namespace ICSharpCode.NRefactory.PrettyPrinter } PrintToken(Tokens.OpenCurlyBrace); NewLine(); - ++IndentationLevel; + if (indent) + ++IndentationLevel; break; case BraceStyle.NextLine: NewLine(); Indent(); PrintToken(Tokens.OpenCurlyBrace); NewLine(); - ++IndentationLevel; + if (indent) + ++IndentationLevel; break; case BraceStyle.NextLineShifted: NewLine(); - ++IndentationLevel; + if (indent) + ++IndentationLevel; Indent(); PrintToken(Tokens.OpenCurlyBrace); NewLine(); break; case BraceStyle.NextLineShifted2: NewLine(); - ++IndentationLevel; + if (indent) + ++IndentationLevel; Indent(); PrintToken(Tokens.OpenCurlyBrace); NewLine(); @@ -77,13 +81,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter braceStack.Push(style); } - public void EndBrace() + public void EndBrace(bool indent) { BraceStyle style = (BraceStyle)braceStack.Pop(); switch (style) { case BraceStyle.EndOfLine: case BraceStyle.NextLine: - --IndentationLevel; + if (indent) + --IndentationLevel; Indent(); PrintToken(Tokens.CloseCurlyBrace); NewLine(); @@ -92,10 +97,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter Indent(); PrintToken(Tokens.CloseCurlyBrace); NewLine(); - --IndentationLevel; + if (indent) + --IndentationLevel; break; case BraceStyle.NextLineShifted2: - --IndentationLevel; + if (indent) + --IndentationLevel; Indent(); PrintToken(Tokens.CloseCurlyBrace); NewLine(); diff --git a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs index 7a4cb2ccab..4ebe9242ee 100644 --- a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs +++ b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs @@ -213,6 +213,132 @@ namespace ICSharpCode.NRefactory.PrettyPrinter } #endregion + #region Indentation + bool indentNamespaceBody = true; + bool indentClassBody = true; + bool indentInterfaceBody = true; + bool indentStructBody = true; + bool indentEnumBody = true; + + bool indentMethodBody = true; + bool indentPropertyBody = true; + bool indentEventBody = true; + bool indentBlocks = true; + + bool indentSwitchBody = true; + bool indentCaseBody = true; + bool indentBreakStatements = true; + + public bool IndentBlocks { + get { + return indentBlocks; + } + set { + indentBlocks = value; + } + } + + public bool IndentClassBody { + get { + return indentClassBody; + } + set { + indentClassBody = value; + } + } + + public bool IndentStructBody { + get { + return indentStructBody; + } + set { + indentStructBody = value; + } + } + + public bool IndentPropertyBody { + get { + return indentPropertyBody; + } + set { + indentPropertyBody = value; + } + } + + public bool IndentNamespaceBody { + get { + return indentNamespaceBody; + } + set { + indentNamespaceBody = value; + } + } + + public bool IndentMethodBody { + get { + return indentMethodBody; + } + set { + indentMethodBody = value; + } + } + + public bool IndentInterfaceBody { + get { + return indentInterfaceBody; + } + set { + indentInterfaceBody = value; + } + } + + public bool IndentEventBody { + get { + return indentEventBody; + } + set { + indentEventBody = value; + } + } + + public bool IndentEnumBody { + get { + return indentEnumBody; + } + set { + indentEnumBody = value; + } + } + + public bool IndentBreakStatements { + get { + return indentBreakStatements; + } + set { + indentBreakStatements = value; + } + } + + public bool IndentSwitchBody { + get { + return indentSwitchBody; + } + set { + indentSwitchBody = value; + } + } + + public bool IndentCaseBody { + get { + return indentCaseBody; + } + set { + indentCaseBody = value; + } + } + + #endregion + #region Before Parentheses bool beforeMethodCallParentheses = false; bool beforeDelegateDeclarationParentheses = false; @@ -553,5 +679,6 @@ namespace ICSharpCode.NRefactory.PrettyPrinter } } #endregion + } -} +} \ No newline at end of file