From e729a02051236720ee3b70b89e0a9e6ed9fedc35 Mon Sep 17 00:00:00 2001 From: figment Date: Sun, 2 Feb 2014 22:46:17 -0800 Subject: [PATCH] Python Converter Enhancement - Remove ref and out parameters from method declarations --- .../Src/NRefactoryToPythonConverter.cs | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs index 317b3a1535..8d882ca9fa 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs @@ -30,6 +30,7 @@ namespace ICSharpCode.PythonBinding // references to fields or parameters. List methodParameters = new List(); MethodDeclaration currentMethod; + bool methodHasOutputParameters = false; // Holds the names of any parameters defined for this class. List propertyNames = new List(); @@ -1234,6 +1235,10 @@ namespace ICSharpCode.PythonBinding { // Add method name. currentMethod = methodDeclaration; + + var oldMethodHasOutputParameters = methodHasOutputParameters; + methodHasOutputParameters = false; + string methodName = methodDeclaration.Name; AppendIndented("def " + methodName); @@ -1263,6 +1268,7 @@ namespace ICSharpCode.PythonBinding } methodParameters = oldMethodParameters; + methodHasOutputParameters = oldMethodHasOutputParameters; currentMethod = null; return null; @@ -1474,7 +1480,25 @@ namespace ICSharpCode.PythonBinding returnStatement.Expression.AcceptVisitor(this, data); } else { AppendIndented("return "); + + // python returns out and ref generally as a return tuple + if (methodHasOutputParameters) + Append("("); + returnStatement.Expression.AcceptVisitor(this, data); + + if (methodHasOutputParameters) { + if (currentMethod != null) { + foreach (var param in currentMethod.Parameters) { + if (param.ParamModifier == ParameterModifiers.Out + || param.ParamModifier == ParameterModifiers.Ref) { + Append(", "); + Append(param.ParameterName); + } + } + } + Append(")"); + } AppendLine(); } return null; @@ -1753,7 +1777,7 @@ namespace ICSharpCode.PythonBinding if (!skipInitializer) usingStatement.ResourceAcquisition.AcceptVisitor(this, data); - + AppendIndentedLine("try:"); IncreaseIndent(); if (IsEmptyStatement(usingStatement.EmbeddedStatement)) @@ -2558,10 +2582,19 @@ namespace ICSharpCode.PythonBinding Append("self, "); } for (int i = 0; i < parameters.Count; ++i) { + var param = parameters[i]; + if (param.ParamModifier == ParameterModifiers.Out + || param.ParamModifier == ParameterModifiers.Ref) { + // track both ref and output for return statement + methodHasOutputParameters = true; + // drop output parameters from declarations + if (param.ParamModifier == ParameterModifiers.Out) + continue; + } if (i > 0) { Append(", "); } - Append(parameters[i].ParameterName); + Append(param.ParameterName); } } else { if (!IsStatic(method)) {