📄 csharpoutputvisitor.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
// <version>$Revision: 2522 $</version>
// </file>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Parser.CSharp;
namespace ICSharpCode.NRefactory.PrettyPrinter
{
public sealed class CSharpOutputVisitor : IOutputAstVisitor
{
Errors errors = new Errors();
CSharpOutputFormatter outputFormatter;
PrettyPrintOptions prettyPrintOptions = new PrettyPrintOptions();
NodeTracker nodeTracker;
bool printFullSystemType;
public string Text {
get {
return outputFormatter.Text;
}
}
public Errors Errors {
get {
return errors;
}
}
AbstractPrettyPrintOptions IOutputAstVisitor.Options {
get { return prettyPrintOptions; }
}
public PrettyPrintOptions Options {
get { return prettyPrintOptions; }
}
public IOutputFormatter OutputFormatter {
get {
return outputFormatter;
}
}
public NodeTracker NodeTracker {
get {
return nodeTracker;
}
}
public CSharpOutputVisitor()
{
outputFormatter = new CSharpOutputFormatter(prettyPrintOptions);
nodeTracker = new NodeTracker(this);
}
void Error(INode node, string message)
{
outputFormatter.PrintText(" // ERROR: " + message + Environment.NewLine);
errors.Error(node.StartLocation.Y, node.StartLocation.X, message);
}
void NotSupported(INode node)
{
Error(node, "Not supported in C#: " + node.GetType().Name);
}
#region ICSharpCode.NRefactory.Parser.IASTVisitor interface implementation
public object VisitCompilationUnit(CompilationUnit compilationUnit, object data)
{
nodeTracker.TrackedVisitChildren(compilationUnit, data);
outputFormatter.EndFile();
return null;
}
/// <summary>
/// Converts type name to primitive type name. Returns null if typeString is not
/// a primitive type.
/// </summary>
static string ConvertTypeString(string typeString)
{
string primitiveType;
if (TypeReference.PrimitiveTypesCSharpReverse.TryGetValue(typeString, out primitiveType))
return primitiveType;
else
return typeString;
}
void PrintTemplates(List<TemplateDefinition> templates)
{
if (templates.Count == 0) return;
outputFormatter.PrintToken(Tokens.LessThan);
for (int i = 0; i < templates.Count; i++) {
if (i > 0) PrintFormattedComma();
outputFormatter.PrintIdentifier(templates[i].Name);
}
outputFormatter.PrintToken(Tokens.GreaterThan);
}
public object VisitTypeReference(TypeReference typeReference, object data)
{
if (typeReference == TypeReference.ClassConstraint) {
outputFormatter.PrintToken(Tokens.Class);
} else if (typeReference == TypeReference.StructConstraint) {
outputFormatter.PrintToken(Tokens.Struct);
} else if (typeReference == TypeReference.NewConstraint) {
outputFormatter.PrintToken(Tokens.New);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
} else {
PrintTypeReferenceWithoutArray(typeReference);
if (typeReference.IsArrayType) {
PrintArrayRank(typeReference.RankSpecifier, 0);
}
}
return null;
}
void PrintArrayRank(int[] rankSpecifier, int startRankIndex)
{
for (int i = startRankIndex; i < rankSpecifier.Length; ++i) {
outputFormatter.PrintToken(Tokens.OpenSquareBracket);
if (this.prettyPrintOptions.SpacesWithinBrackets) {
outputFormatter.Space();
}
for (int j = 0; j < rankSpecifier[i]; ++j) {
outputFormatter.PrintToken(Tokens.Comma);
}
if (this.prettyPrintOptions.SpacesWithinBrackets) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.CloseSquareBracket);
}
}
void PrintTypeReferenceWithoutArray(TypeReference typeReference)
{
if (typeReference.IsGlobal) {
outputFormatter.PrintText("global::");
}
if (typeReference.Type == null || typeReference.Type.Length == 0) {
outputFormatter.PrintText("void");
} else if (typeReference.SystemType == "System.Nullable" && typeReference.GenericTypes != null
&& typeReference.GenericTypes.Count == 1 && !typeReference.IsGlobal)
{
nodeTracker.TrackedVisit(typeReference.GenericTypes[0], null);
outputFormatter.PrintText("?");
} else {
if (typeReference.SystemType.Length > 0) {
if (printFullSystemType || typeReference.IsGlobal) {
outputFormatter.PrintIdentifier(typeReference.SystemType);
} else {
outputFormatter.PrintText(ConvertTypeString(typeReference.SystemType));
}
} else {
outputFormatter.PrintText(typeReference.Type);
}
if (typeReference.GenericTypes != null && typeReference.GenericTypes.Count > 0) {
outputFormatter.PrintToken(Tokens.LessThan);
AppendCommaSeparatedList(typeReference.GenericTypes);
outputFormatter.PrintToken(Tokens.GreaterThan);
}
}
for (int i = 0; i < typeReference.PointerNestingLevel; ++i) {
outputFormatter.PrintToken(Tokens.Times);
}
}
public object VisitInnerClassTypeReference(InnerClassTypeReference innerClassTypeReference, object data)
{
nodeTracker.TrackedVisit(innerClassTypeReference.BaseType, data);
outputFormatter.PrintToken(Tokens.Dot);
return VisitTypeReference((TypeReference)innerClassTypeReference, data);
}
#region Global scope
void VisitAttributes(ICollection attributes, object data)
{
if (attributes == null || attributes.Count <= 0) {
return;
}
foreach (AttributeSection section in attributes) {
nodeTracker.TrackedVisit(section, data);
}
}
void PrintFormattedComma()
{
if (this.prettyPrintOptions.SpacesBeforeComma) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Comma);
if (this.prettyPrintOptions.SpacesAfterComma) {
outputFormatter.Space();
}
}
public object VisitAttributeSection(AttributeSection attributeSection, object data)
{
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.OpenSquareBracket);
if (this.prettyPrintOptions.SpacesWithinBrackets) {
outputFormatter.Space();
}
if (!string.IsNullOrEmpty(attributeSection.AttributeTarget)) {
outputFormatter.PrintText(attributeSection.AttributeTarget);
outputFormatter.PrintToken(Tokens.Colon);
outputFormatter.Space();
}
Debug.Assert(attributeSection.Attributes != null);
for (int j = 0; j < attributeSection.Attributes.Count; ++j) {
nodeTracker.TrackedVisit((INode)attributeSection.Attributes[j], data);
if (j + 1 < attributeSection.Attributes.Count) {
PrintFormattedComma();
}
}
if (this.prettyPrintOptions.SpacesWithinBrackets) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.CloseSquareBracket);
outputFormatter.NewLine();
return null;
}
public object VisitAttribute(ICSharpCode.NRefactory.Ast.Attribute attribute, object data)
{
outputFormatter.PrintIdentifier(attribute.Name);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
this.AppendCommaSeparatedList(attribute.PositionalArguments);
if (attribute.NamedArguments != null && attribute.NamedArguments.Count > 0) {
if (attribute.PositionalArguments.Count > 0) {
PrintFormattedComma();
}
for (int i = 0; i < attribute.NamedArguments.Count; ++i) {
nodeTracker.TrackedVisit((INode)attribute.NamedArguments[i], data);
if (i + 1 < attribute.NamedArguments.Count) {
PrintFormattedComma();
}
}
}
outputFormatter.PrintToken(Tokens.CloseParenthesis);
return null;
}
public object VisitNamedArgumentExpression(NamedArgumentExpression namedArgumentExpression, object data)
{
outputFormatter.PrintIdentifier(namedArgumentExpression.Name);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
nodeTracker.TrackedVisit(namedArgumentExpression.Expression, data);
return null;
}
public object VisitUsing(Using @using, object data)
{
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Using);
outputFormatter.Space();
outputFormatter.PrintIdentifier(@using.Name);
if (@using.IsAlias) {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
printFullSystemType = true;
nodeTracker.TrackedVisit(@using.Alias, data);
printFullSystemType = false;
}
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine();
return null;
}
public object VisitUsingDeclaration(UsingDeclaration usingDeclaration, object data)
{
foreach (Using u in usingDeclaration.Usings) {
nodeTracker.TrackedVisit(u, data);
}
return null;
}
public object VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration, object data)
{
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Namespace);
outputFormatter.Space();
outputFormatter.PrintIdentifier(namespaceDeclaration.Name);
outputFormatter.BeginBrace(this.prettyPrintOptions.NamespaceBraceStyle);
nodeTracker.TrackedVisitChildren(namespaceDeclaration, data);
outputFormatter.EndBrace();
return null;
}
void OutputEnumMembers(TypeDeclaration typeDeclaration, object data)
{
for (int i = 0; i < typeDeclaration.Children.Count; i++) {
FieldDeclaration fieldDeclaration = (FieldDeclaration)typeDeclaration.Children[i];
nodeTracker.BeginNode(fieldDeclaration);
VariableDeclaration f = (VariableDeclaration)fieldDeclaration.Fields[0];
VisitAttributes(fieldDeclaration.Attributes, data);
outputFormatter.Indent();
outputFormatter.PrintIdentifier(f.Name);
if (f.Initializer != null && !f.Initializer.IsNull) {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
nodeTracker.TrackedVisit(f.Initializer, data);
}
if (i < typeDeclaration.Children.Count - 1) {
outputFormatter.PrintToken(Tokens.Comma);
}
outputFormatter.NewLine();
nodeTracker.EndNode(fieldDeclaration);
}
}
TypeDeclaration currentType = null;
public object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data)
{
VisitAttributes(typeDeclaration.Attributes, data);
outputFormatter.Indent();
OutputModifier(typeDeclaration.Modifier);
switch (typeDeclaration.Type) {
case ClassType.Enum:
outputFormatter.PrintToken(Tokens.Enum);
break;
case ClassType.Interface:
outputFormatter.PrintToken(Tokens.Interface);
break;
case ClassType.Struct:
outputFormatter.PrintToken(Tokens.Struct);
break;
default:
outputFormatter.PrintToken(Tokens.Class);
break;
}
outputFormatter.Space();
outputFormatter.PrintIdentifier(typeDeclaration.Name);
PrintTemplates(typeDeclaration.Templates);
if (typeDeclaration.BaseTypes != null && typeDeclaration.BaseTypes.Count > 0) {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Colon);
outputFormatter.Space();
for (int i = 0; i < typeDeclaration.BaseTypes.Count; ++i) {
if (i > 0) {
PrintFormattedComma();
}
nodeTracker.TrackedVisit(typeDeclaration.BaseTypes[i], data);
}
}
foreach (TemplateDefinition templateDefinition in typeDeclaration.Templates) {
nodeTracker.TrackedVisit(templateDefinition, data);
}
switch (typeDeclaration.Type) {
case ClassType.Enum:
outputFormatter.BeginBrace(this.prettyPrintOptions.EnumBraceStyle);
break;
case ClassType.Interface:
outputFormatter.BeginBrace(this.prettyPrintOptions.InterfaceBraceStyle);
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -