Browse Source

Improvements to C#->VB code converter (see forum-10353).

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@5448 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Daniel Grunwald 16 years ago
parent
commit
a7764d92d1
  1. 16
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs
  2. 8
      src/Libraries/NRefactory/Project/Src/Visitors/ToVBNetConvertVisitor.cs
  3. 33
      src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBNetConverterTest.cs

16
src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
// </file>
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
@ -2664,11 +2665,13 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -2664,11 +2665,13 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public override object TrackedVisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, object data)
{
outputFormatter.PrintToken(Tokens.New);
outputFormatter.Space();
TrackedVisit(objectCreateExpression.CreateType, data);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
AppendCommaSeparatedList(objectCreateExpression.Parameters);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
if (!objectCreateExpression.IsAnonymousType) {
outputFormatter.Space();
TrackedVisit(objectCreateExpression.CreateType, data);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
AppendCommaSeparatedList(objectCreateExpression.Parameters);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
}
CollectionInitializerExpression initializer = objectCreateExpression.ObjectInitializer;
if (!initializer.IsNull) {
outputFormatter.Space();
@ -2682,6 +2685,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -2682,6 +2685,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.PrintToken(Tokens.Comma);
outputFormatter.PrintLineContinuation();
outputFormatter.Indent();
//outputFormatter.PrintText("Key "); TODO "Key" cannot be represented in AST
NamedArgumentExpression nae = expr as NamedArgumentExpression;
if (nae != null) {
outputFormatter.PrintToken(Tokens.Dot);
@ -2690,6 +2694,8 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -2690,6 +2694,8 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
TrackedVisit(nae.Expression, data);
} else {
TrackedVisit(expr, data);
}
}
outputFormatter.IndentationLevel--;

8
src/Libraries/NRefactory/Project/Src/Visitors/ToVBNetConvertVisitor.cs

@ -30,6 +30,7 @@ namespace ICSharpCode.NRefactory.Visitors @@ -30,6 +30,7 @@ namespace ICSharpCode.NRefactory.Visitors
// Decrease array creation size - VB specifies upper bound instead of array length
// Automatic properties are converted to explicit implementation
// base[index] - VB requires MyBase.Item
// var i = 0; -> Dim i = 0 (remove typereference for 'var')
List<INode> nodesToMoveToCompilationUnit = new List<INode>();
@ -388,5 +389,12 @@ namespace ICSharpCode.NRefactory.Visitors @@ -388,5 +389,12 @@ namespace ICSharpCode.NRefactory.Visitors
ReplaceCurrentNode(new MemberReferenceExpression(baseReferenceExpression, "Item"));
return null;
}
public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data)
{
if (localVariableDeclaration.TypeReference.Type == "var")
localVariableDeclaration.TypeReference = TypeReference.Null;;
return base.VisitLocalVariableDeclaration(localVariableDeclaration, data);
}
}
}

33
src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBNetConverterTest.cs

@ -594,5 +594,38 @@ Private m_Name As String"); @@ -594,5 +594,38 @@ Private m_Name As String");
{
TestMember("public new void Remove() {}", "Public Shadows Sub Remove()\nEnd Sub");
}
[Test]
public void TestImplicitlyTypedVariable()
{
TestStatement("var i = 0;", "Dim i = 0");
}
[Test]
public void TestAnonymousType()
{
// TODO test is strictly incorrect, the converter should add "Key"
TestStatement("var x = new { i = 0, abc, abc.Test };",
"Dim x = New With { _\n" +
" .i = 0, _\n" +
" abc, _\n" +
" abc.Test _\n" +
"}");
}
[Test]
[Ignore]
public void CollectionAndObjectInitializer()
{
TestStatement("List<X> l = new List<X> { new X { A = 1 }, new X { A = 2 } };",
"Dim l As New List(Of X)() From { _\n" +
" New X() With { _\n" +
" .A = 1 _\n" +
" }, _\n" +
" New X() With { _\n" +
" .A = 2 _\n" +
" } _\n" +
"}");
}
}
}

Loading…
Cancel
Save