Browse Source

Python code converter now attempts to detect property references and adds a this reference.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4763 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 16 years ago
parent
commit
636ccb5f6e
  1. 36
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs
  2. 62
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/PropertyReferenceConversionTestFixture.cs
  3. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

36
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs

@ -34,6 +34,9 @@ namespace ICSharpCode.PythonBinding
// references to fields or parameters. // references to fields or parameters.
List<ParameterDeclarationExpression> methodParameters = new List<ParameterDeclarationExpression>(); List<ParameterDeclarationExpression> methodParameters = new List<ParameterDeclarationExpression>();
MethodDeclaration currentMethod; MethodDeclaration currentMethod;
// Holds the names of any parameters defined for this class.
List<string> propertyNames = new List<string>();
SupportedLanguage language; SupportedLanguage language;
List<MethodDeclaration> entryPointMethods; List<MethodDeclaration> entryPointMethods;
@ -664,10 +667,13 @@ namespace ICSharpCode.PythonBinding
public override object TrackedVisitIdentifierExpression(IdentifierExpression identifierExpression, object data) public override object TrackedVisitIdentifierExpression(IdentifierExpression identifierExpression, object data)
{ {
if (IsField(identifierExpression.Identifier)) { string name = identifierExpression.Identifier;
Append("self._" + identifierExpression.Identifier); if (IsField(name)) {
Append("self._" + name);
} else if (IsProperty(name) && !IsMethodParameter(name)) {
Append("self." + name);
} else { } else {
Append(identifierExpression.Identifier); Append(name);
} }
return null; return null;
} }
@ -949,6 +955,7 @@ namespace ICSharpCode.PythonBinding
public override object TrackedVisitPropertyDeclaration(PropertyDeclaration propertyDeclaration, object data) public override object TrackedVisitPropertyDeclaration(PropertyDeclaration propertyDeclaration, object data)
{ {
string propertyName = propertyDeclaration.Name; string propertyName = propertyDeclaration.Name;
propertyNames.Add(propertyName);
// Add get statements. // Add get statements.
if (propertyDeclaration.HasGetRegion) { if (propertyDeclaration.HasGetRegion) {
@ -1265,7 +1272,7 @@ namespace ICSharpCode.PythonBinding
public override object TrackedVisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data) public override object TrackedVisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data)
{ {
memberReferenceExpression.TargetObject.AcceptVisitor(this, data); memberReferenceExpression.TargetObject.AcceptVisitor(this, data);
if (memberReferenceExpression.TargetObject is ThisReferenceExpression) { if ((memberReferenceExpression.TargetObject is ThisReferenceExpression) && !IsProperty(memberReferenceExpression.MemberName)) {
Append("._"); Append("._");
} else { } else {
Append("."); Append(".");
@ -1513,10 +1520,8 @@ namespace ICSharpCode.PythonBinding
bool IsField(string name) bool IsField(string name)
{ {
// Check the current method's parameters. // Check the current method's parameters.
foreach (ParameterDeclarationExpression param in methodParameters) { if (IsMethodParameter(name)) {
if (param.ParameterName == name) { return false;
return false;
}
} }
// Check the current class's fields. // Check the current class's fields.
@ -1530,6 +1535,21 @@ namespace ICSharpCode.PythonBinding
return false; return false;
} }
bool IsMethodParameter(string name)
{
foreach (ParameterDeclarationExpression param in methodParameters) {
if (param.ParameterName == name) {
return true;
}
}
return false;
}
bool IsProperty(string name)
{
return propertyNames.Contains(name);
}
/// <summary> /// <summary>
/// Creates an attach statement (i.e. button.Click += ButtonClick) /// Creates an attach statement (i.e. button.Click += ButtonClick)
/// or remove statement (i.e. button.Click -= ButtonClick) /// or remove statement (i.e. button.Click -= ButtonClick)

62
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/PropertyReferenceConversionTestFixture.cs

@ -0,0 +1,62 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using ICSharpCode.NRefactory;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
namespace PythonBinding.Tests.Converter
{
[TestFixture]
public class PropertyReferenceConversionTestFixture
{
string csharp = "class Foo\r\n" +
"{\r\n" +
" int count = 0;\r\n" +
" public int Count {\r\n" +
" get {\r\n" +
" return count;\r\n" +
" }\r\n" +
" }\r\n" +
"\r\n" +
" public void Increment()\r\n" +
" {\r\n" +
" Count++;\r\n" +
" }\r\n" +
"\r\n" +
" public void SetCount(int Count)\r\n" +
" {\r\n" +
" this.Count = Count;\r\n" +
" }\r\n" +
"}";
[Test]
public void ConvertedPythonCode()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string python = converter.Convert(csharp);
string expectedPython = "class Foo(object):\r\n" +
" def __init__(self):\r\n" +
" self._count = 0\r\n" +
"\r\n" +
" def get_Count(self):\r\n" +
" return self._count\r\n" +
"\r\n" +
" Count = property(fget=get_Count)\r\n" +
"\r\n" +
" def Increment(self):\r\n" +
" self.Count += 1\r\n" +
"\r\n" +
" def SetCount(self, Count):\r\n" +
" self.Count = Count";
Assert.AreEqual(expectedPython, python, python);
}
}
}

1
src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

@ -141,6 +141,7 @@
<Compile Include="Converter\ObjectReferenceEqualsConversionTestFixture.cs" /> <Compile Include="Converter\ObjectReferenceEqualsConversionTestFixture.cs" />
<Compile Include="Converter\ProjectHasStartupObjectTestFixture.cs" /> <Compile Include="Converter\ProjectHasStartupObjectTestFixture.cs" />
<Compile Include="Converter\PropertyConversionTestFixture.cs" /> <Compile Include="Converter\PropertyConversionTestFixture.cs" />
<Compile Include="Converter\PropertyReferenceConversionTestFixture.cs" />
<Compile Include="Converter\PropertyWithGetSetStatementsTestfixture.cs" /> <Compile Include="Converter\PropertyWithGetSetStatementsTestfixture.cs" />
<Compile Include="Converter\PropertyWithGetterTestFixture.cs" /> <Compile Include="Converter\PropertyWithGetterTestFixture.cs" />
<Compile Include="Converter\PropertyWithSetterTestFixture.cs" /> <Compile Include="Converter\PropertyWithSetterTestFixture.cs" />

Loading…
Cancel
Save