Browse Source

Add simple output formatter.

newNRvisualizers
Daniel Grunwald 15 years ago
parent
commit
e9bc0ec7fc
  1. 5
      ICSharpCode.NRefactory.Demo/MainForm.cs
  2. 4
      ICSharpCode.NRefactory/CSharp/OutputVisitor/IOutputFormatter.cs
  3. 64
      ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs
  4. 63
      ICSharpCode.NRefactory/CSharp/OutputVisitor/TextWriterOutputFormatter.cs
  5. 3
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

5
ICSharpCode.NRefactory.Demo/MainForm.cs

@ -125,7 +125,10 @@ namespace ICSharpCode.NRefactory.Demo @@ -125,7 +125,10 @@ namespace ICSharpCode.NRefactory.Demo
void CSharpGenerateCodeButtonClick(object sender, EventArgs e)
{
throw new NotImplementedException();
StringWriter w = new StringWriter();
OutputVisitor output = new OutputVisitor(w, new CSharpFormattingPolicy());
compilationUnit.AcceptVisitor(output, null);
csharpCodeTextBox.Text = w.ToString();
}
int GetOffset(TextBox textBox, AstLocation location)

4
ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputFormatter.cs → ICSharpCode.NRefactory/CSharp/OutputVisitor/IOutputFormatter.cs

@ -6,9 +6,9 @@ using System; @@ -6,9 +6,9 @@ using System;
namespace ICSharpCode.NRefactory.CSharp
{
/// <summary>
/// Output formatter for the output visitor.
/// Output formatter for the Output visitor.
/// </summary>
public class OutputFormatter
public interface IOutputFormatter
{
void WriteIdentifier(string ident);
void WriteKeyword(string keyword);

64
ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

@ -5,9 +5,9 @@ using System; @@ -5,9 +5,9 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp
@ -17,13 +17,24 @@ namespace ICSharpCode.NRefactory.CSharp @@ -17,13 +17,24 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary>
public class OutputVisitor : AstVisitor<object, object>
{
readonly OutputFormatter formatter;
readonly IOutputFormatter formatter;
readonly CSharpFormattingPolicy policy;
AstNode currentContainerNode;
readonly Stack<AstNode> positionStack = new Stack<AstNode>();
char lastChar;
public OutputVisitor(OutputFormatter formatter, CSharpFormattingPolicy formattingPolicy)
public OutputVisitor(TextWriter textWriter, CSharpFormattingPolicy formattingPolicy)
{
if (textWriter == null)
throw new ArgumentNullException("textWriter");
if (formattingPolicy == null)
throw new ArgumentNullException("formattingPolicy");
this.formatter = new TextWriterOutputFormatter(textWriter);
this.policy = formattingPolicy;
}
public OutputVisitor(IOutputFormatter formatter, CSharpFormattingPolicy formattingPolicy)
{
if (formatter == null)
throw new ArgumentNullException("formatter");
@ -119,7 +130,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -119,7 +130,8 @@ namespace ICSharpCode.NRefactory.CSharp
{
WriteSpecialsUpToRole(AstNode.Roles.Comma, nextNode);
Space(policy.SpacesBeforeComma);
formatter.WriteToken(",");
formatter.WriteToken(",");
lastChar = ',';
Space(policy.SpacesAfterComma);
}
@ -166,19 +178,26 @@ namespace ICSharpCode.NRefactory.CSharp @@ -166,19 +178,26 @@ namespace ICSharpCode.NRefactory.CSharp
void WriteKeyword(string keyword, Role<CSharpTokenNode> tokenRole = null)
{
WriteSpecialsUpToRole(tokenRole ?? AstNode.Roles.Keyword);
if (lastChar == 'a')
Space();
formatter.WriteKeyword(keyword);
lastChar = 'a';
}
void WriteIdentifier(string identifier, Role<Identifier> identifierRole = null)
{
WriteSpecialsUpToRole(identifierRole ?? AstNode.Roles.Identifier);
if (lastChar == 'a')
Space();
formatter.WriteIdentifier(identifier);
lastChar = 'a';
}
void WriteToken(string token, Role<CSharpTokenNode> tokenRole)
{
WriteSpecialsUpToRole(tokenRole);
formatter.WriteToken(token);
formatter.WriteToken(token);
lastChar = token[token.Length - 1];
}
void LPar()
@ -198,7 +217,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -198,7 +217,7 @@ namespace ICSharpCode.NRefactory.CSharp
{
if (!(currentContainerNode.Parent is ForStatement)) {
WriteToken(";", AstNode.Roles.Semicolon);
formatter.NewLine();
NewLine();
}
}
@ -207,20 +226,30 @@ namespace ICSharpCode.NRefactory.CSharp @@ -207,20 +226,30 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary>
void Space(bool addSpace = true)
{
if (addSpace)
if (addSpace) {
formatter.Space();
lastChar = ' ';
}
}
void NewLine()
{
formatter.NewLine();
lastChar = '\n';
}
void OpenBrace(BraceStyle style)
{
WriteSpecialsUpToRole(AstNode.Roles.LBrace);
formatter.OpenBrace();
lastChar = '{';
}
void CloseBrace(BraceStyle style)
{
WriteSpecialsUpToRole(AstNode.Roles.RBrace);
formatter.CloseBrace();
lastChar = '}';
}
#endregion
@ -262,6 +291,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -262,6 +291,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
WriteSpecialsUpToNode(ident);
formatter.WriteIdentifier(ident.Name);
lastChar = 'a';
}
}
@ -585,7 +615,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -585,7 +615,8 @@ namespace ICSharpCode.NRefactory.CSharp
public object VisitPrimitiveExpression(PrimitiveExpression primitiveExpression, object data)
{
StartNode(primitiveExpression);
formatter.WriteToken(ToCSharpString(primitiveExpression));
formatter.WriteToken(ToCSharpString(primitiveExpression));
lastChar = 'a';
return EndNode(primitiveExpression);
}
@ -785,7 +816,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -785,7 +816,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
WriteCommaSeparatedList(attributeSection.Attributes);
WriteToken("]", AstNode.Roles.RBracket);
formatter.NewLine();
NewLine();
return EndNode(attributeSection);
}
@ -816,7 +847,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -816,7 +847,7 @@ namespace ICSharpCode.NRefactory.CSharp
foreach (var member in namespaceDeclaration.Members)
member.AcceptVisitor(this, data);
CloseBrace(policy.NamespaceBraceStyle);
formatter.NewLine();
NewLine();
return EndNode(namespaceDeclaration);
}
@ -860,7 +891,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -860,7 +891,7 @@ namespace ICSharpCode.NRefactory.CSharp
member.AcceptVisitor(this, data);
}
CloseBrace(braceStyle);
formatter.NewLine();
NewLine();
return EndNode(typeDeclaration);
}
@ -919,7 +950,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -919,7 +950,7 @@ namespace ICSharpCode.NRefactory.CSharp
node.AcceptVisitor(this, data);
}
CloseBrace(style);
formatter.NewLine();
NewLine();
return EndNode(blockStatement);
}
@ -1060,7 +1091,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1060,7 +1091,7 @@ namespace ICSharpCode.NRefactory.CSharp
StartNode(labelStatement);
WriteIdentifier(labelStatement.Label);
WriteToken(":", LabelStatement.Roles.Colon);
formatter.NewLine();
NewLine();
return EndNode(labelStatement);
}
@ -1104,7 +1135,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1104,7 +1135,7 @@ namespace ICSharpCode.NRefactory.CSharp
foreach (var section in switchStatement.SwitchSections)
section.AcceptVisitor(this, data);
CloseBrace(policy.StatementBraceStyle);
formatter.NewLine();
NewLine();
return EndNode(switchStatement);
}
@ -1125,7 +1156,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1125,7 +1156,7 @@ namespace ICSharpCode.NRefactory.CSharp
Space();
caseLabel.Expression.AcceptVisitor(this, data);
WriteToken(":", CaseLabel.Roles.Colon);
formatter.NewLine();
NewLine();
return EndNode(caseLabel);
}
@ -1557,7 +1588,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1557,7 +1588,8 @@ namespace ICSharpCode.NRefactory.CSharp
WriteToken("[", ArraySpecifier.Roles.LBracket);
foreach (var comma in arraySpecifier.GetChildrenByRole(ArraySpecifier.Roles.Comma)) {
WriteSpecialsUpToNode(comma);
formatter.WriteToken(",");
formatter.WriteToken(",");
lastChar = ',';
}
WriteToken("]", ArraySpecifier.Roles.RBracket);
return EndNode(arraySpecifier);

63
ICSharpCode.NRefactory/CSharp/OutputVisitor/TextWriterOutputFormatter.cs

@ -0,0 +1,63 @@ @@ -0,0 +1,63 @@
// 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.IO;
namespace ICSharpCode.NRefactory.CSharp
{
/// <summary>
/// Writes C# code into a TextWriter.
/// </summary>
public class TextWriterOutputFormatter : IOutputFormatter
{
readonly TextWriter textWriter;
public TextWriterOutputFormatter(TextWriter textWriter)
{
if (textWriter == null)
throw new ArgumentNullException("textWriter");
this.textWriter = textWriter;
}
public void WriteIdentifier(string ident)
{
textWriter.Write(ident);
}
public void WriteKeyword(string keyword)
{
textWriter.Write(keyword);
}
public void WriteToken(string token)
{
textWriter.Write(token);
}
public void Space()
{
textWriter.Write(' ');
}
public void OpenBrace()
{
textWriter.Write('{');
}
public void CloseBrace()
{
textWriter.Write('}');
}
public void NewLine()
{
textWriter.WriteLine();
}
public void WriteComment(CommentType commentType, string content)
{
throw new NotImplementedException();
}
}
}

3
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -149,8 +149,9 @@ @@ -149,8 +149,9 @@
<Compile Include="CSharp\Formatter\DomIndentationVisitor.cs" />
<Compile Include="CSharp\Formatter\DomSpacingVisitor.cs" />
<Compile Include="CSharp\Formatter\Indent.cs" />
<Compile Include="CSharp\OutputVisitor\OutputFormatter.cs" />
<Compile Include="CSharp\OutputVisitor\IOutputFormatter.cs" />
<Compile Include="CSharp\OutputVisitor\OutputVisitor.cs" />
<Compile Include="CSharp\OutputVisitor\TextWriterOutputFormatter.cs" />
<Compile Include="CSharp\Parser\CSharpParser.cs" />
<Compile Include="CSharp\Parser\mcs\CryptoConvert.cs" />
<Compile Include="CSharp\Parser\mcs\MonoSymbolFile.cs" />

Loading…
Cancel
Save