Browse Source

fixed SD2-1497 - Converting VB.NET With statement to C#

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/vbnet@6171 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Siegfried Pammer 16 years ago
parent
commit
364dea4182
  1. 84
      src/Libraries/NRefactory/Project/Src/Visitors/VBNetConstructsConvertVisitor.cs
  2. 32
      src/Libraries/NRefactory/Test/Output/CSharp/VBNetToCSharpConverterTest.cs

84
src/Libraries/NRefactory/Project/Src/Visitors/VBNetConstructsConvertVisitor.cs

@ -48,11 +48,13 @@ namespace ICSharpCode.NRefactory.Visitors @@ -48,11 +48,13 @@ namespace ICSharpCode.NRefactory.Visitors
Dictionary<string, string> usings;
List<UsingDeclaration> addedUsings;
TypeDeclaration currentTypeDeclaration;
int withStatementCount;
public override object VisitCompilationUnit(CompilationUnit compilationUnit, object data)
{
usings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
addedUsings = new List<UsingDeclaration>();
withStatementCount = 0;
base.VisitCompilationUnit(compilationUnit, data);
int i;
for (i = 0; i < compilationUnit.Children.Count; i++) {
@ -680,5 +682,87 @@ namespace ICSharpCode.NRefactory.Visitors @@ -680,5 +682,87 @@ namespace ICSharpCode.NRefactory.Visitors
{
return new List<Expression>(exprs.Select(expr => new PrimitiveExpression(expr)));
}
public override object VisitWithStatement(WithStatement withStatement, object data)
{
withStatementCount++;
string varName = "_with" + withStatementCount;
WithConvertVisitor converter = new WithConvertVisitor(varName);
LocalVariableDeclaration withVariable = new LocalVariableDeclaration(new VariableDeclaration(varName, withStatement.Expression, new TypeReference("var", true)));
withStatement.Body.AcceptVisitor(converter, null);
base.VisitWithStatement(withStatement, data);
var statements = withStatement.Body.Children;
statements.Insert(0, withVariable);
withVariable.Parent = withStatement.Body;
statements.Reverse();
foreach (var stmt in statements) {
InsertAfterSibling(withStatement, stmt);
}
RemoveCurrentNode();
return null;
}
class WithConvertVisitor : AbstractAstTransformer
{
string withAccessor;
public WithConvertVisitor(string withAccessor)
{
this.withAccessor = withAccessor;
}
public override object VisitWithStatement(WithStatement withStatement, object data)
{
// skip any nested with statement
var block = withStatement.Body;
withStatement.Body = BlockStatement.Null;
base.VisitWithStatement(withStatement, data);
withStatement.Body = block;
return null;
}
public override object VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data)
{
if (memberReferenceExpression.TargetObject.IsNull) {
IdentifierExpression id = new IdentifierExpression(withAccessor);
memberReferenceExpression.TargetObject = id;
id.Parent = memberReferenceExpression;
}
return base.VisitMemberReferenceExpression(memberReferenceExpression, data);
}
public override object VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data)
{
if (binaryOperatorExpression.Left.IsNull && binaryOperatorExpression.Op == BinaryOperatorType.DictionaryAccess) {
IdentifierExpression id = new IdentifierExpression(withAccessor);
binaryOperatorExpression.Left = id;
id.Parent = binaryOperatorExpression;
}
return base.VisitBinaryOperatorExpression(binaryOperatorExpression, data);
}
public override object VisitXmlMemberAccessExpression(XmlMemberAccessExpression xmlMemberAccessExpression, object data)
{
if (xmlMemberAccessExpression.TargetObject.IsNull) {
IdentifierExpression id = new IdentifierExpression(withAccessor);
xmlMemberAccessExpression.TargetObject = id;
id.Parent = xmlMemberAccessExpression;
}
return base.VisitXmlMemberAccessExpression(xmlMemberAccessExpression, data);
}
}
}
}

32
src/Libraries/NRefactory/Test/Output/CSharp/VBNetToCSharpConverterTest.cs

@ -628,7 +628,7 @@ static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.Stat @@ -628,7 +628,7 @@ static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.Stat
TestStatement("With Ejes\n" +
" .AddLine(p1, p2)\n" +
"End With",
"{\n Ejes.AddLine(p1, p2);\n}");
"var _with1 = Ejes;\n_with1.AddLine(p1, p2);");
}
[Test]
@ -641,7 +641,7 @@ static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.Stat @@ -641,7 +641,7 @@ static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.Stat
" End With\n" +
"End With",
"{\n {\n tb1.Font.Italic = true;\n }\n}");
"var _with1 = tb1;\nvar _with2 = _with1.Font;\n_with2.Italic = true;");
}
[Test]
@ -654,7 +654,7 @@ static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.Stat @@ -654,7 +654,7 @@ static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.Stat
" End With\n" +
"End With",
"{\n {\n tb1.Something.Font.Italic = true;\n }\n}");
"var _with1 = tb1;\nvar _with2 = _with1.Something.Font;\n_with2.Italic = true;");
}
[Test]
@ -989,5 +989,31 @@ End Function", @@ -989,5 +989,31 @@ End Function",
}"
);
}
[Test]
public void SD2_1497()
{
TestMember(
@"Public Function Bug() As String
With New System.Text.StringBuilder(""Hi! "")
.Append(""you "")
.Append(""folks "")
.Append(""from "")
.Append(""sharpdevelop!"")
Return .ToString()
End With
End Function",
@"public string Bug()
{
var _with1 = new System.Text.StringBuilder(""Hi! "");
_with1.Append(""you "");
_with1.Append(""folks "");
_with1.Append(""from "");
_with1.Append(""sharpdevelop!"");
return _with1.ToString();
}
"
);
}
}
}

Loading…
Cancel
Save