mirror of https://github.com/icsharpcode/ILSpy.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.2 KiB
133 lines
3.2 KiB
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) |
|
// This code is distributed under MIT X11 license (for details please see \doc\license.txt) |
|
|
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using ICSharpCode.Decompiler; |
|
using ICSharpCode.NRefactory.CSharp; |
|
using Mono.Cecil; |
|
|
|
namespace Decompiler |
|
{ |
|
public class TextOutputFormatter : IOutputFormatter |
|
{ |
|
readonly ITextOutput output; |
|
readonly Stack<AstNode> nodeStack = new Stack<AstNode>(); |
|
int braceLevelWithinType = -1; |
|
|
|
public TextOutputFormatter(ITextOutput output) |
|
{ |
|
if (output == null) |
|
throw new ArgumentNullException("output"); |
|
this.output = output; |
|
} |
|
|
|
public void WriteIdentifier(string identifier) |
|
{ |
|
MemberReference memberRef = GetCurrentMemberReference(); |
|
|
|
if (memberRef != null) |
|
output.WriteReference(identifier, memberRef); |
|
else |
|
output.Write(identifier); |
|
} |
|
|
|
MemberReference GetCurrentMemberReference() |
|
{ |
|
AstNode node = nodeStack.Peek(); |
|
MemberReference memberRef = node.Annotation<MemberReference>(); |
|
if (memberRef == null && node.Role == AstNode.Roles.TargetExpression && (node.Parent is InvocationExpression || node.Parent is ObjectCreateExpression)) { |
|
memberRef = node.Parent.Annotation<MemberReference>(); |
|
} |
|
return memberRef; |
|
} |
|
|
|
public void WriteKeyword(string keyword) |
|
{ |
|
output.Write(keyword); |
|
} |
|
|
|
public void WriteToken(string token) |
|
{ |
|
// Attach member reference to token only if there's no identifier in the current node. |
|
MemberReference memberRef = GetCurrentMemberReference(); |
|
if (memberRef != null && nodeStack.Peek().GetChildByRole(AstNode.Roles.Identifier).IsNull) |
|
output.WriteReference(token, memberRef); |
|
else |
|
output.Write(token); |
|
} |
|
|
|
public void Space() |
|
{ |
|
output.Write(' '); |
|
} |
|
|
|
public void OpenBrace(BraceStyle style) |
|
{ |
|
if (braceLevelWithinType >= 0 || nodeStack.Peek() is TypeDeclaration) |
|
braceLevelWithinType++; |
|
if (nodeStack.OfType<BlockStatement>().Count() <= 1) { |
|
output.MarkFoldStart(defaultCollapsed: braceLevelWithinType == 1); |
|
} |
|
output.WriteLine(); |
|
output.WriteLine("{"); |
|
output.Indent(); |
|
} |
|
|
|
public void CloseBrace(BraceStyle style) |
|
{ |
|
output.Unindent(); |
|
output.Write('}'); |
|
if (nodeStack.OfType<BlockStatement>().Count() <= 1) |
|
output.MarkFoldEnd(); |
|
if (braceLevelWithinType >= 0) |
|
braceLevelWithinType--; |
|
} |
|
|
|
public void Indent() |
|
{ |
|
output.Indent(); |
|
} |
|
|
|
public void Unindent() |
|
{ |
|
output.Unindent(); |
|
} |
|
|
|
public void NewLine() |
|
{ |
|
output.WriteLine(); |
|
} |
|
|
|
public void WriteComment(CommentType commentType, string content) |
|
{ |
|
switch (commentType) { |
|
case CommentType.SingleLine: |
|
output.Write("//"); |
|
output.WriteLine(content); |
|
break; |
|
case CommentType.MultiLine: |
|
output.Write("/*"); |
|
output.Write(content); |
|
output.Write("*/"); |
|
break; |
|
case CommentType.Documentation: |
|
output.Write("///"); |
|
output.WriteLine(content); |
|
break; |
|
} |
|
} |
|
|
|
public void StartNode(AstNode node) |
|
{ |
|
nodeStack.Push(node); |
|
} |
|
|
|
public void EndNode(AstNode node) |
|
{ |
|
if (nodeStack.Pop() != node) |
|
throw new InvalidOperationException(); |
|
} |
|
} |
|
}
|
|
|