Browse Source

Improved C#<->VB converter.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.0@1468 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 19 years ago
parent
commit
06680fb191
  1. 1
      src/Libraries/NRefactory/Project/Src/Output/CSharp/CSharpOutputVisitor.cs
  2. 20
      src/Libraries/NRefactory/Project/Src/Parser/Visitors/ToCSharpConvertVisitor.cs
  3. 26
      src/Libraries/NRefactory/Project/Src/Parser/Visitors/ToVBNetConvertVisitor.cs
  4. 3
      src/Libraries/NRefactory/Project/Src/Parser/Visitors/VBNetConstructsConvertVisitor.cs
  5. 15
      src/Libraries/NRefactory/Test/Output/CSharp/VBToCSharpConverterTest.cs
  6. 16
      src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs

1
src/Libraries/NRefactory/Project/Src/Output/CSharp/CSharpOutputVisitor.cs

@ -436,6 +436,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -436,6 +436,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public object Visit(DelegateDeclaration delegateDeclaration, object data)
{
VisitAttributes(delegateDeclaration.Attributes, data);
outputFormatter.Indent();
OutputModifier(delegateDeclaration.Modifier);
outputFormatter.PrintToken(Tokens.Delegate);
outputFormatter.Space();

20
src/Libraries/NRefactory/Project/Src/Parser/Visitors/ToCSharpConvertVisitor.cs

@ -23,8 +23,26 @@ namespace ICSharpCode.NRefactory.Parser @@ -23,8 +23,26 @@ namespace ICSharpCode.NRefactory.Parser
/// </summary>
public class ToCSharpConvertVisitor : AbstractAstTransformer
{
// The following conversions should be implemented in the future:
// The following conversions are implemented:
// Public Event EventName(param As String) -> automatic delegate declaration
public override object Visit(EventDeclaration eventDeclaration, object data)
{
if (!eventDeclaration.HasAddRegion && !eventDeclaration.HasRaiseRegion && !eventDeclaration.HasRemoveRegion) {
if (eventDeclaration.TypeReference.IsNull) {
DelegateDeclaration dd = new DelegateDeclaration(eventDeclaration.Modifier, null);
dd.Name = eventDeclaration.Name + "EventHandler";
dd.Parameters = eventDeclaration.Parameters;
dd.ReturnType = new TypeReference("System.Void");
dd.Parent = eventDeclaration.Parent;
eventDeclaration.Parameters = null;
int index = eventDeclaration.Parent.Children.IndexOf(eventDeclaration);
// inserting before current position is not allowed in a Transformer
eventDeclaration.Parent.Children.Insert(index + 1, dd);
eventDeclaration.TypeReference = new TypeReference(dd.Name);
}
}
return base.Visit(eventDeclaration, data);
}
}
}

26
src/Libraries/NRefactory/Project/Src/Parser/Visitors/ToVBNetConvertVisitor.cs

@ -28,6 +28,29 @@ namespace ICSharpCode.NRefactory.Parser @@ -28,6 +28,29 @@ namespace ICSharpCode.NRefactory.Parser
// Conflicting field/property names -> m_field
// Anonymous methods are put into new methods
// Simple event handler creation is replaced with AddressOfExpression
// Move Imports-statements out of namespaces
List<INode> nodesToMoveToCompilationUnit = new List<INode>();
public override object Visit(CompilationUnit compilationUnit, object data)
{
base.Visit(compilationUnit, data);
for (int i = 0; i < nodesToMoveToCompilationUnit.Count; i++) {
compilationUnit.Children.Insert(i, nodesToMoveToCompilationUnit[i]);
nodesToMoveToCompilationUnit[i].Parent = compilationUnit;
}
return null;
}
public override object Visit(UsingDeclaration usingDeclaration, object data)
{
base.Visit(usingDeclaration, data);
if (usingDeclaration.Parent is NamespaceDeclaration) {
nodesToMoveToCompilationUnit.Add(usingDeclaration);
RemoveCurrentNode();
}
return null;
}
TypeDeclaration currentType;
@ -261,7 +284,8 @@ namespace ICSharpCode.NRefactory.Parser @@ -261,7 +284,8 @@ namespace ICSharpCode.NRefactory.Parser
public override object Visit(ConstructorDeclaration constructorDeclaration, object data)
{
if ((constructorDeclaration.Modifier & Modifier.Visibility) == 0)
// make constructor private if visiblity is not set (unless constructor is static)
if ((constructorDeclaration.Modifier & (Modifier.Visibility | Modifier.Static)) == 0)
constructorDeclaration.Modifier |= Modifier.Private;
return base.Visit(constructorDeclaration, data);
}

3
src/Libraries/NRefactory/Project/Src/Parser/Visitors/VBNetConstructsConvertVisitor.cs

@ -66,7 +66,8 @@ namespace ICSharpCode.NRefactory.Parser @@ -66,7 +66,8 @@ namespace ICSharpCode.NRefactory.Parser
public override object Visit(ConstructorDeclaration constructorDeclaration, object data)
{
if ((constructorDeclaration.Modifier & Modifier.Visibility) == 0)
// make constructor public if visiblity is not set (unless constructor is static)
if ((constructorDeclaration.Modifier & (Modifier.Visibility | Modifier.Static)) == 0)
constructorDeclaration.Modifier = Modifier.Public;
// MyBase.New() and MyClass.New() calls inside the constructor are converted to :base() and :this()

15
src/Libraries/NRefactory/Test/Output/CSharp/VBToCSharpConverterTest.cs

@ -124,6 +124,14 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -124,6 +124,14 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
"a = null;\nb = null;");
}
[Test]
public void VBEvent()
{
TestMember("Friend Event CancelNow(ByVal a As String)",
"internal event CancelNowEventHandler CancelNow;\n" +
"internal delegate void CancelNowEventHandler(string a);");
}
[Test]
public void StaticMethod()
{
@ -172,6 +180,13 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -172,6 +180,13 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
"public tmp1() : this(1)\n{\n}");
}
[Test]
public void StaticConstructor()
{
TestMember("Shared Sub New()\nEnd Sub",
"static tmp1()\n{\n}");
}
[Test]
public void Destructor()
{

16
src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs

@ -62,6 +62,15 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -62,6 +62,15 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
TestProgram("class tmp1 { void tmp2() {\n" + input + "\n}}", b.ToString());
}
[Test]
public void MoveImportsStatement()
{
TestProgram("namespace test { using SomeNamespace; }",
"Imports SomeNamespace\r\n" +
"Namespace test\r\n" +
"End Namespace\r\n");
}
[Test]
public void ForWithUnknownConditionAndSingleStatement()
{
@ -225,6 +234,13 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -225,6 +234,13 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
"Public Sub New()\n\tMe.New(1)\nEnd Sub");
}
[Test]
public void StaticConstructor()
{
TestMember("static tmp1() { }",
"Shared Sub New()\nEnd Sub");
}
[Test]
public void Destructor()
{

Loading…
Cancel
Save