diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs index 66834ca7f3..5868d0be23 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs @@ -34,6 +34,9 @@ namespace ICSharpCode.PythonBinding // references to fields or parameters. List methodParameters = new List(); MethodDeclaration currentMethod; + + // Holds the names of any parameters defined for this class. + List propertyNames = new List(); SupportedLanguage language; List entryPointMethods; @@ -664,10 +667,13 @@ namespace ICSharpCode.PythonBinding public override object TrackedVisitIdentifierExpression(IdentifierExpression identifierExpression, object data) { - if (IsField(identifierExpression.Identifier)) { - Append("self._" + identifierExpression.Identifier); + string name = identifierExpression.Identifier; + if (IsField(name)) { + Append("self._" + name); + } else if (IsProperty(name) && !IsMethodParameter(name)) { + Append("self." + name); } else { - Append(identifierExpression.Identifier); + Append(name); } return null; } @@ -949,6 +955,7 @@ namespace ICSharpCode.PythonBinding public override object TrackedVisitPropertyDeclaration(PropertyDeclaration propertyDeclaration, object data) { string propertyName = propertyDeclaration.Name; + propertyNames.Add(propertyName); // Add get statements. if (propertyDeclaration.HasGetRegion) { @@ -1265,7 +1272,7 @@ namespace ICSharpCode.PythonBinding public override object TrackedVisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data) { memberReferenceExpression.TargetObject.AcceptVisitor(this, data); - if (memberReferenceExpression.TargetObject is ThisReferenceExpression) { + if ((memberReferenceExpression.TargetObject is ThisReferenceExpression) && !IsProperty(memberReferenceExpression.MemberName)) { Append("._"); } else { Append("."); @@ -1513,10 +1520,8 @@ namespace ICSharpCode.PythonBinding bool IsField(string name) { // Check the current method's parameters. - foreach (ParameterDeclarationExpression param in methodParameters) { - if (param.ParameterName == name) { - return false; - } + if (IsMethodParameter(name)) { + return false; } // Check the current class's fields. @@ -1530,6 +1535,21 @@ namespace ICSharpCode.PythonBinding 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); + } + /// /// Creates an attach statement (i.e. button.Click += ButtonClick) /// or remove statement (i.e. button.Click -= ButtonClick) diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/PropertyReferenceConversionTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/PropertyReferenceConversionTestFixture.cs new file mode 100644 index 0000000000..248472fcb7 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/PropertyReferenceConversionTestFixture.cs @@ -0,0 +1,62 @@ +// +// +// +// +// $Revision$ +// + +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); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index ea8ad5737d..de16d4dc5d 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -141,6 +141,7 @@ +