Browse Source

Added support for multiline comments in the python code converter.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4312 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 17 years ago
parent
commit
067cfb1532
  1. 16
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs
  2. 55
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/MultiLineCommentTestFixture.cs
  3. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

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

@ -1395,12 +1395,13 @@ namespace ICSharpCode.PythonBinding @@ -1395,12 +1395,13 @@ namespace ICSharpCode.PythonBinding
} else {
codeBuilder.AppendToPreviousLine(" #" + comment.CommentText);
}
} else if (comment.CommentType == CommentType.Block) {
AppendMultilineComment(comment);
}
}
void IOutputFormatter.PrintPreprocessingDirective(PreprocessingDirective directive, bool forceWriteInPreviousBlock)
{
}
void IOutputFormatter.PrintBlankLine(bool forceWriteInPreviousBlock)
@ -1868,5 +1869,18 @@ namespace ICSharpCode.PythonBinding @@ -1868,5 +1869,18 @@ namespace ICSharpCode.PythonBinding
TypeDeclaration type = methodDeclaration.Parent as TypeDeclaration;
return type.Name;
}
void AppendMultilineComment(Comment comment)
{
string[] lines = comment.CommentText.Split(new char[] {'\n'});
for (int i = 0; i < lines.Length; ++i) {
string line = "# " + lines[i].Trim();
if ((i == 0) && !comment.CommentStartsLine) {
codeBuilder.AppendToPreviousLine(" " + line);
} else {
AppendIndentedLine(line);
}
}
}
}
}

55
src/AddIns/BackendBindings/Python/PythonBinding/Test/Converter/MultiLineCommentTestFixture.cs

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
// <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 MultiLineCommentConversionTestFixture
{
string csharp = "/* \r\n" +
"Class Foo\r\n" +
"*/ \r\n" +
"public class Foo\r\n" +
"{\r\n" +
" /* Initialize. */\r\n" +
" public Foo()\r\n" +
" {\r\n" +
" /* Initialize j.\r\n" +
" set to zero */\r\n" +
" j = 0; /* Set to zero */\r\n" +
" /* test */\r\n" +
" if (j == 0) j = 2;\r\n" +
" }\r\n" +
"}";
[Test]
public void ConvertedPythonCode()
{
NRefactoryToPythonConverter converter = new NRefactoryToPythonConverter(SupportedLanguage.CSharp);
converter.IndentString = " ";
string python = converter.Convert(csharp);
string expectedPython = "# \r\n" +
"# Class Foo\r\n" +
"# \r\n" +
"class Foo(object):\r\n" +
" # Initialize.\r\n"+
" def __init__(self):\r\n" +
" # Initialize j.\r\n" +
" # set to zero\r\n" +
" j = 0 # Set to zero\r\n" +
" # test\r\n" +
" if j == 0:\r\n" +
" j = 2";
Assert.AreEqual(expectedPython, python, python);
}
}
}

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

@ -130,6 +130,7 @@ @@ -130,6 +130,7 @@
<Compile Include="Converter\MethodWithBodyConversionTestFixture.cs" />
<Compile Include="Converter\IfStatementConversionTestFixture.cs" />
<Compile Include="Converter\ModulusOperatorConversionTestFixture.cs" />
<Compile Include="Converter\MultiLineCommentTestFixture.cs" />
<Compile Include="Converter\NestedClassConversionTestFixture.cs" />
<Compile Include="Converter\NestedIfStatementConversionTestFixture.cs" />
<Compile Include="Converter\NullConversionTestFixture.cs" />

Loading…
Cancel
Save