From c918d66e9df85d99780fa9cf0d98bfcb961d894d Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 15 Sep 2017 08:39:12 +0200 Subject: [PATCH] Reimplement folding for C#. --- .../OutputVisitor/CSharpOutputVisitor.cs | 2 +- .../Output/TextTokenWriter.cs | 55 +++++++++---------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs b/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs index dd1cc31bd..076ae703b 100644 --- a/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs +++ b/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs @@ -1373,8 +1373,8 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor foreach (var node in blockStatement.Statements) { node.AcceptVisitor(this); } - EndNode(blockStatement); CloseBrace(style); + EndNode(blockStatement); } public virtual void VisitBreakStatement(BreakStatement breakStatement) diff --git a/ICSharpCode.Decompiler/Output/TextTokenWriter.cs b/ICSharpCode.Decompiler/Output/TextTokenWriter.cs index 5b671076a..86564efea 100644 --- a/ICSharpCode.Decompiler/Output/TextTokenWriter.cs +++ b/ICSharpCode.Decompiler/Output/TextTokenWriter.cs @@ -206,13 +206,32 @@ namespace ICSharpCode.Decompiler public override void WriteToken(Role role, string token) { - // Attach member reference to token only if there's no identifier in the current node. - var member = GetCurrentMemberReference(); - var node = nodeStack.Peek(); - if (member != null && node.GetChildByRole(Roles.Identifier).IsNull) - output.WriteReference(token, member); - else - output.Write(token); + switch (token) { + case "{": + if (braceLevelWithinType >= 0 || nodeStack.Peek() is TypeDeclaration) + braceLevelWithinType++; + if (nodeStack.OfType().Count() <= 1 || FoldBraces) { + output.MarkFoldStart(defaultCollapsed: braceLevelWithinType == 1); + } + output.Write("{"); + break; + case "}": + output.Write('}'); + if (nodeStack.OfType().Count() <= 1 || FoldBraces) + output.MarkFoldEnd(); + if (braceLevelWithinType >= 0) + braceLevelWithinType--; + break; + default: + // Attach member reference to token only if there's no identifier in the current node. + var member = GetCurrentMemberReference(); + var node = nodeStack.Peek(); + if (member != null && node.GetChildByRole(Roles.Identifier).IsNull) + output.WriteReference(token, member); + else + output.Write(token); + break; + } } public override void Space() @@ -220,28 +239,6 @@ namespace ICSharpCode.Decompiler output.Write(' '); } - public void OpenBrace(BraceStyle style) - { - if (braceLevelWithinType >= 0 || nodeStack.Peek() is TypeDeclaration) - braceLevelWithinType++; - if (nodeStack.OfType().Count() <= 1 || FoldBraces) { - output.MarkFoldStart(defaultCollapsed: braceLevelWithinType == 1); - } - output.WriteLine(); - output.WriteLine("{"); - output.Indent(); - } - - public void CloseBrace(BraceStyle style) - { - output.Unindent(); - output.Write('}'); - if (nodeStack.OfType().Count() <= 1 || FoldBraces) - output.MarkFoldEnd(); - if (braceLevelWithinType >= 0) - braceLevelWithinType--; - } - public override void Indent() { output.Indent();