Browse Source

Move common code from IronPython and IronRuby code builder class to Scripting project.

pull/1/head
mrward 15 years ago
parent
commit
d35e7f8feb
  1. 150
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeBuilder.cs
  2. 165
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonCodeBuilderTests.cs
  3. 183
      src/AddIns/BackendBindings/Ruby/RubyBinding/Project/Src/RubyCodeBuilder.cs
  4. 196
      src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Designer/RubyCodeBuilderTests.cs
  5. 1
      src/AddIns/BackendBindings/Scripting/Project/ICSharpCode.Scripting.csproj
  6. 185
      src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingCodeBuilder.cs
  7. 223
      src/AddIns/BackendBindings/Scripting/Test/Designer/ScriptingCodeBuilderTests.cs
  8. 1
      src/AddIns/BackendBindings/Scripting/Test/ICSharpCode.Scripting.Tests.csproj

150
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeBuilder.cs

@ -8,14 +8,12 @@ @@ -8,14 +8,12 @@
using System;
using System.ComponentModel;
using System.Text;
using ICSharpCode.Scripting;
namespace ICSharpCode.PythonBinding
{
public class PythonCodeBuilder
public class PythonCodeBuilder : ScriptingCodeBuilder
{
StringBuilder codeBuilder = new StringBuilder();
string indentString = "\t";
int indent;
bool insertedCreateComponentsContainer;
public PythonCodeBuilder()
@ -23,75 +21,8 @@ namespace ICSharpCode.PythonBinding @@ -23,75 +21,8 @@ namespace ICSharpCode.PythonBinding
}
public PythonCodeBuilder(int initialIndent)
: base(initialIndent)
{
indent = initialIndent;
}
/// <summary>
/// Gets or sets the string used for indenting.
/// </summary>
public string IndentString {
get { return indentString; }
set { indentString = value; }
}
/// <summary>
/// Returns the code.
/// </summary>
public override string ToString()
{
return codeBuilder.ToString();
}
/// <summary>
/// Returns true if the previous line contains code. If the previous line contains a
/// comment or is an empty line then it returns false;
/// </summary>
public bool PreviousLineIsCode {
get {
string code = ToString();
int end = MoveToPreviousLineEnd(code, code.Length - 1);
if (end > 0) {
int start = MoveToPreviousLineEnd(code, end);
string line = code.Substring(start + 1, end - start).Trim();
return (line.Length > 0) && (!line.Trim().StartsWith("#"));
}
return false;
}
}
/// <summary>
/// Appends text at the end of the current code.
/// </summary>
public void Append(string text)
{
codeBuilder.Append(text);
}
/// <summary>
/// Appends carriage return and line feed to the existing text.
/// </summary>
public void AppendLine()
{
Append("\r\n");
}
/// <summary>
/// Appends the text indented.
/// </summary>
public void AppendIndented(string text)
{
codeBuilder.Append(GetIndentString());
codeBuilder.Append(text);
}
/// <summary>
/// Inserts a new line at the start of the code before everything else.
/// </summary>
public void InsertIndentedLine(string text)
{
text = GetIndentString() + text + "\r\n";
codeBuilder.Insert(0, text, 1);
}
/// <summary>
@ -105,81 +36,10 @@ namespace ICSharpCode.PythonBinding @@ -105,81 +36,10 @@ namespace ICSharpCode.PythonBinding
public void InsertCreateComponentsContainer()
{
if (!insertedCreateComponentsContainer) {
InsertIndentedLine("self._components = " + typeof(Container).FullName + "()");
string text = String.Format("self._components = {0}()", typeof(Container).FullName);
InsertIndentedLine(text);
insertedCreateComponentsContainer = true;
}
}
/// <summary>
/// Inserts the text with a carriage return and newline at the end.
/// </summary>
public void AppendIndentedLine(string text)
{
AppendIndented(text + "\r\n");
}
public void AppendLineIfPreviousLineIsCode()
{
if (PreviousLineIsCode) {
codeBuilder.AppendLine();
}
}
/// <summary>
/// Appends the specified text to the end of the previous line.
/// </summary>
public void AppendToPreviousLine(string text)
{
string code = ToString();
int end = MoveToPreviousLineEnd(code, code.Length - 1);
if (end > 0) {
codeBuilder.Insert(end + 1, text);
}
}
public void IncreaseIndent()
{
indent++;
}
public void DecreaseIndent()
{
indent--;
}
public int Indent {
get { return indent; }
}
/// <summary>
/// Gets the length of the current code string.
/// </summary>
public int Length {
get { return codeBuilder.Length; }
}
string GetIndentString()
{
StringBuilder currentIndentString = new StringBuilder();
for (int i = 0; i < indent; ++i) {
currentIndentString.Append(indentString);
}
return currentIndentString.ToString();
}
/// <summary>
/// Returns the index of the end of the previous line.
/// </summary>
/// <param name="index">This is the index to start working backwards from.</param>
int MoveToPreviousLineEnd(string code, int index)
{
while (index >= 0) {
if (code[index] == '\r') {
return index - 1;
}
--index;
}
return -1;
}
}
}

165
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonCodeBuilderTests.cs

@ -23,93 +23,6 @@ namespace PythonBinding.Tests.Designer @@ -23,93 +23,6 @@ namespace PythonBinding.Tests.Designer
codeBuilder.IndentString = "\t";
}
[Test]
public void AppendNewLine()
{
codeBuilder.AppendLine();
Assert.AreEqual("\r\n", codeBuilder.ToString());
}
[Test]
public void AppendText()
{
codeBuilder.Append("abc");
Assert.AreEqual("abc", codeBuilder.ToString());
}
[Test]
public void AppendIndentedText()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndented("abc");
Assert.AreEqual("\tabc", codeBuilder.ToString());
}
[Test]
public void IncreaseIndentTwice()
{
codeBuilder.IncreaseIndent();
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndented("abc");
Assert.AreEqual("\t\tabc", codeBuilder.ToString());
}
[Test]
public void DecreaseIndent()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndented("abc");
codeBuilder.AppendLine();
codeBuilder.DecreaseIndent();
codeBuilder.AppendIndented("abc");
Assert.AreEqual("\tabc\r\nabc", codeBuilder.ToString());
}
[Test]
public void AppendIndentedLine()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndentedLine("abc");
Assert.AreEqual("\tabc\r\n", codeBuilder.ToString());
}
[Test]
public void InitialIndentWhenCodeBuilderCreatedIsZero()
{
Assert.AreEqual(0, codeBuilder.Indent);
}
[Test]
public void IncreaseIndentByOne()
{
codeBuilder.IncreaseIndent();
Assert.AreEqual(1, codeBuilder.Indent);
}
[Test]
public void LengthAfterAddingText()
{
codeBuilder.Append("abc");
Assert.AreEqual(3, codeBuilder.Length);
}
[Test]
public void IndentPassedToConstructor()
{
codeBuilder = new PythonCodeBuilder(2);
codeBuilder.AppendIndented("abc");
Assert.AreEqual("\t\tabc", codeBuilder.ToString());
}
[Test]
public void InsertIndentedLine()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndentedLine("def");
codeBuilder.InsertIndentedLine("abc");
Assert.AreEqual("\tabc\r\n\tdef\r\n", codeBuilder.ToString());
}
/// <summary>
/// Check that the "self._components = System.ComponentModel.Container()" line is generated
/// the once and before any other lines of code.
@ -123,85 +36,11 @@ namespace PythonBinding.Tests.Designer @@ -123,85 +36,11 @@ namespace PythonBinding.Tests.Designer
codeBuilder.InsertCreateComponentsContainer();
codeBuilder.InsertCreateComponentsContainer();
string expectedCode = " self._components = System.ComponentModel.Container()\r\n" +
string expectedCode =
" self._components = System.ComponentModel.Container()\r\n" +
" self._listView = System.Windows.Forms.ListView()\r\n";
Assert.AreEqual(expectedCode, codeBuilder.ToString());
}
[Test]
public void PreviousLineIsEmptyNewLine()
{
codeBuilder.AppendLine();
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsComment()
{
codeBuilder.AppendIndentedLine("# comment");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsNotEmptyNewLine()
{
codeBuilder.AppendIndentedLine("abc");
Assert.IsTrue(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineDoesNotExist()
{
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsEmptyAndCurrentLineHasText()
{
codeBuilder.AppendLine();
codeBuilder.Append("abc");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsMadeUpOfWhiteSpace()
{
codeBuilder.AppendIndentedLine(" \t ");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void TwoLinesWithPreviousLineMadeUpOfWhiteSpace()
{
codeBuilder.AppendIndentedLine("1st");
codeBuilder.AppendIndentedLine(" \t ");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void TwoLinesWithPreviousEmptyLine()
{
codeBuilder.AppendIndentedLine("1st");
codeBuilder.AppendLine();
codeBuilder.Append("abc");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void TwoLinesWithNoPreviousEmptyLine()
{
codeBuilder.AppendIndentedLine("First");
codeBuilder.AppendIndentedLine("Second");
Assert.IsTrue(codeBuilder.PreviousLineIsCode);
}
[Test]
public void AppendToPreviousLine()
{
codeBuilder.AppendIndentedLine("abc");
codeBuilder.AppendToPreviousLine(" # comment");
Assert.AreEqual("abc # comment\r\n", codeBuilder.ToString());
}
}
}

183
src/AddIns/BackendBindings/Ruby/RubyBinding/Project/Src/RubyCodeBuilder.cs

@ -8,132 +8,19 @@ @@ -8,132 +8,19 @@
using System;
using System.ComponentModel;
using System.Text;
using ICSharpCode.Scripting;
namespace ICSharpCode.RubyBinding
{
public class RubyCodeBuilder
public class RubyCodeBuilder : ScriptingCodeBuilder
{
StringBuilder codeBuilder = new StringBuilder();
string indentString = "\t";
int indent;
bool insertedCreateComponentsContainer;
public RubyCodeBuilder()
{
}
public RubyCodeBuilder(int initialIndent)
: base(initialIndent)
{
indent = initialIndent;
}
/// <summary>
/// Gets or sets the string used for indenting.
/// </summary>
public string IndentString {
get { return indentString; }
set { indentString = value; }
}
/// <summary>
/// Returns the code.
/// </summary>
public override string ToString()
{
return codeBuilder.ToString();
}
/// <summary>
/// Returns true if the previous line contains code. If the previous line contains a
/// comment or is an empty line then it returns false;
/// </summary>
public bool PreviousLineIsCode {
get {
string code = ToString();
int end = MoveToPreviousLineEnd(code, code.Length - 1);
if (end > 0) {
int start = MoveToPreviousLineEnd(code, end);
string line = code.Substring(start + 1, end - start).Trim();
return (line.Length > 0) && (!line.Trim().StartsWith("#"));
}
return false;
}
}
public string GetPreviousLine()
{
string code = ToString();
int end = MoveToPreviousLineEnd(code, code.Length - 1);
if (end > 0) {
int start = MoveToPreviousLineEnd(code, end);
return code.Substring(start + 1, end - start);
}
return String.Empty;
}
/// <summary>
/// Appends text at the end of the current code.
/// </summary>
public void Append(string text)
{
codeBuilder.Append(text);
}
/// <summary>
/// Appends carriage return and line feed to the existing text.
/// </summary>
public void AppendLine()
{
Append("\r\n");
}
/// <summary>
/// Appends the text indented.
/// </summary>
public void AppendIndented(string text)
{
codeBuilder.Append(GetIndentString());
codeBuilder.Append(text);
}
/// <summary>
/// Inserts a new line at the start of the code before everything else.
/// </summary>
public void InsertIndentedLine(string text)
{
text = GetIndentString() + text + "\r\n";
codeBuilder.Insert(0, text, 1);
}
/// <summary>
/// Inserts the following line of code before all the other lines of code:
///
/// "self._components = System.ComponentModel.Container()"
///
/// This line will only be inserted once. Multiple calls to this method will only result in one
/// line of code being inserted.
/// </summary>
public void InsertCreateComponentsContainer()
{
if (!insertedCreateComponentsContainer) {
InsertIndentedLine("self._components = " + typeof(Container).FullName + "()");
insertedCreateComponentsContainer = true;
}
}
/// <summary>
/// Inserts the text with a carriage return and newline at the end.
/// </summary>
public void AppendIndentedLine(string text)
{
AppendIndented(text + "\r\n");
}
public void AppendLineIfPreviousLineIsCode()
{
if (PreviousLineIsCode) {
codeBuilder.AppendLine();
}
}
public void AppendLineIfPreviousLineIsEndStatement()
@ -143,69 +30,5 @@ namespace ICSharpCode.RubyBinding @@ -143,69 +30,5 @@ namespace ICSharpCode.RubyBinding
AppendLine();
}
}
/// <summary>
/// Appends the specified text to the end of the previous line.
/// </summary>
public void AppendToPreviousLine(string text)
{
string code = ToString();
int end = MoveToPreviousLineEnd(code, code.Length - 1);
if (end > 0) {
codeBuilder.Insert(end + 1, text);
}
}
public void TrimEnd()
{
string trimmedText = codeBuilder.ToString().TrimEnd();
codeBuilder = new StringBuilder();
codeBuilder.Append(trimmedText);
}
public void IncreaseIndent()
{
indent++;
}
public void DecreaseIndent()
{
indent--;
}
public int Indent {
get { return indent; }
}
/// <summary>
/// Gets the length of the current code string.
/// </summary>
public int Length {
get { return codeBuilder.Length; }
}
string GetIndentString()
{
StringBuilder currentIndentString = new StringBuilder();
for (int i = 0; i < indent; ++i) {
currentIndentString.Append(indentString);
}
return currentIndentString.ToString();
}
/// <summary>
/// Returns the index of the end of the previous line.
/// </summary>
/// <param name="index">This is the index to start working backwards from.</param>
int MoveToPreviousLineEnd(string code, int index)
{
while (index >= 0) {
if (code[index] == '\r') {
return index - 1;
}
--index;
}
return -1;
}
}
}

196
src/AddIns/BackendBindings/Ruby/RubyBinding/Test/Designer/RubyCodeBuilderTests.cs

@ -23,187 +23,6 @@ namespace RubyBinding.Tests.Designer @@ -23,187 +23,6 @@ namespace RubyBinding.Tests.Designer
codeBuilder.IndentString = "\t";
}
[Test]
public void AppendNewLine()
{
codeBuilder.AppendLine();
Assert.AreEqual("\r\n", codeBuilder.ToString());
}
[Test]
public void AppendText()
{
codeBuilder.Append("abc");
Assert.AreEqual("abc", codeBuilder.ToString());
}
[Test]
public void AppendIndentedText()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndented("abc");
Assert.AreEqual("\tabc", codeBuilder.ToString());
}
[Test]
public void IncreaseIndentTwice()
{
codeBuilder.IncreaseIndent();
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndented("abc");
Assert.AreEqual("\t\tabc", codeBuilder.ToString());
}
[Test]
public void DecreaseIndent()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndented("abc");
codeBuilder.AppendLine();
codeBuilder.DecreaseIndent();
codeBuilder.AppendIndented("abc");
Assert.AreEqual("\tabc\r\nabc", codeBuilder.ToString());
}
[Test]
public void AppendIndentedLine()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndentedLine("abc");
Assert.AreEqual("\tabc\r\n", codeBuilder.ToString());
}
[Test]
public void InitialIndentWhenCodeBuilderCreatedIsZero()
{
Assert.AreEqual(0, codeBuilder.Indent);
}
[Test]
public void IncreaseIndentByOne()
{
codeBuilder.IncreaseIndent();
Assert.AreEqual(1, codeBuilder.Indent);
}
[Test]
public void LengthAfterAddingText()
{
codeBuilder.Append("abc");
Assert.AreEqual(3, codeBuilder.Length);
}
[Test]
public void IndentPassedToConstructor()
{
codeBuilder = new RubyCodeBuilder(2);
codeBuilder.AppendIndented("abc");
Assert.AreEqual("\t\tabc", codeBuilder.ToString());
}
[Test]
public void InsertIndentedLine()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndentedLine("def");
codeBuilder.InsertIndentedLine("abc");
Assert.AreEqual("\tabc\r\n\tdef\r\n", codeBuilder.ToString());
}
/// <summary>
/// Check that the "self._components = System.ComponentModel.Container()" line is generated
/// the once and before any other lines of code.
/// </summary>
[Test]
public void AppendCreateComponentsContainerTwice()
{
codeBuilder.IndentString = " ";
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndentedLine("self._listView = System.Windows.Forms.ListView()");
codeBuilder.InsertCreateComponentsContainer();
codeBuilder.InsertCreateComponentsContainer();
string expectedCode = " self._components = System.ComponentModel.Container()\r\n" +
" self._listView = System.Windows.Forms.ListView()\r\n";
Assert.AreEqual(expectedCode, codeBuilder.ToString());
}
[Test]
public void PreviousLineIsEmptyNewLine()
{
codeBuilder.AppendLine();
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsComment()
{
codeBuilder.AppendIndentedLine("# comment");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsNotEmptyNewLine()
{
codeBuilder.AppendIndentedLine("abc");
Assert.IsTrue(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineDoesNotExist()
{
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsEmptyAndCurrentLineHasText()
{
codeBuilder.AppendLine();
codeBuilder.Append("abc");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsMadeUpOfWhiteSpace()
{
codeBuilder.AppendIndentedLine(" \t ");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void TwoLinesWithPreviousLineMadeUpOfWhiteSpace()
{
codeBuilder.AppendIndentedLine("1st");
codeBuilder.AppendIndentedLine(" \t ");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void TwoLinesWithPreviousEmptyLine()
{
codeBuilder.AppendIndentedLine("1st");
codeBuilder.AppendLine();
codeBuilder.Append("abc");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void TwoLinesWithNoPreviousEmptyLine()
{
codeBuilder.AppendIndentedLine("First");
codeBuilder.AppendIndentedLine("Second");
Assert.IsTrue(codeBuilder.PreviousLineIsCode);
}
[Test]
public void AppendToPreviousLine()
{
codeBuilder.AppendIndentedLine("abc");
codeBuilder.AppendToPreviousLine(" # comment");
Assert.AreEqual("abc # comment\r\n", codeBuilder.ToString());
}
[Test]
public void TrimEnd()
{
@ -215,21 +34,6 @@ namespace RubyBinding.Tests.Designer @@ -215,21 +34,6 @@ namespace RubyBinding.Tests.Designer
Assert.AreEqual("abc\r\ndef", codeBuilder.ToString());
}
[Test]
public void GetFirstLineOfCode()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndentedLine("abc");
Assert.AreEqual("\tabc", codeBuilder.GetPreviousLine());
}
[Test]
public void GetPreviousLineReturnsEmptyStringIfNoPreviousLine()
{
codeBuilder.AppendIndented("def");
Assert.AreEqual(String.Empty, codeBuilder.GetPreviousLine());
}
[Test]
public void AppendLineIfPreviousLineIsEndStatementAppendsNewLineIfPreviousLineIsEndStatement()
{

1
src/AddIns/BackendBindings/Scripting/Project/ICSharpCode.Scripting.csproj

@ -72,6 +72,7 @@ @@ -72,6 +72,7 @@
<Compile Include="Src\IScriptingConsoleTextEditor.cs" />
<Compile Include="Src\IScriptingDesignerGenerator.cs" />
<Compile Include="Src\IScriptingFileService.cs" />
<Compile Include="Src\ScriptingCodeBuilder.cs" />
<Compile Include="Src\ScriptingConsoleCompletionData.cs" />
<Compile Include="Src\ScriptingConsoleCompletionDataProvider.cs" />
<Compile Include="Src\ScriptingConsoleTextEditor.cs" />

185
src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingCodeBuilder.cs

@ -0,0 +1,185 @@ @@ -0,0 +1,185 @@
// <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 System.Text;
namespace ICSharpCode.Scripting
{
public class ScriptingCodeBuilder
{
StringBuilder codeBuilder = new StringBuilder();
string indentString = "\t";
int indent;
public ScriptingCodeBuilder()
{
}
public ScriptingCodeBuilder(int initialIndent)
{
indent = initialIndent;
}
/// <summary>
/// Gets or sets the string used for indenting.
/// </summary>
public string IndentString {
get { return indentString; }
set { indentString = value; }
}
/// <summary>
/// Returns the code.
/// </summary>
public override string ToString()
{
return codeBuilder.ToString();
}
/// <summary>
/// Returns true if the previous line contains code. If the previous line contains a
/// comment or is an empty line then it returns false;
/// </summary>
public bool PreviousLineIsCode {
get {
string code = ToString();
int end = MoveToPreviousLineEnd(code, code.Length - 1);
if (end > 0) {
int start = MoveToPreviousLineEnd(code, end);
string line = code.Substring(start + 1, end - start).Trim();
return (line.Length > 0) && (!line.Trim().StartsWith("#"));
}
return false;
}
}
public string GetPreviousLine()
{
string code = ToString();
int end = MoveToPreviousLineEnd(code, code.Length - 1);
if (end > 0) {
int start = MoveToPreviousLineEnd(code, end);
return code.Substring(start + 1, end - start);
}
return String.Empty;
}
/// <summary>
/// Appends text at the end of the current code.
/// </summary>
public void Append(string text)
{
codeBuilder.Append(text);
}
/// <summary>
/// Appends carriage return and line feed to the existing text.
/// </summary>
public void AppendLine()
{
Append("\r\n");
}
/// <summary>
/// Appends the text indented.
/// </summary>
public void AppendIndented(string text)
{
codeBuilder.Append(GetIndentString());
codeBuilder.Append(text);
}
/// <summary>
/// Inserts a new line at the start of the code before everything else.
/// </summary>
public void InsertIndentedLine(string text)
{
text = GetIndentString() + text + "\r\n";
codeBuilder.Insert(0, text, 1);
}
/// <summary>
/// Inserts the text with a carriage return and newline at the end.
/// </summary>
public void AppendIndentedLine(string text)
{
AppendIndented(text + "\r\n");
}
public void AppendLineIfPreviousLineIsCode()
{
if (PreviousLineIsCode) {
codeBuilder.AppendLine();
}
}
/// <summary>
/// Appends the specified text to the end of the previous line.
/// </summary>
public void AppendToPreviousLine(string text)
{
string code = ToString();
int end = MoveToPreviousLineEnd(code, code.Length - 1);
if (end > 0) {
codeBuilder.Insert(end + 1, text);
}
}
public void TrimEnd()
{
string trimmedText = codeBuilder.ToString().TrimEnd();
codeBuilder = new StringBuilder();
codeBuilder.Append(trimmedText);
}
public void IncreaseIndent()
{
indent++;
}
public void DecreaseIndent()
{
indent--;
}
public int Indent {
get { return indent; }
}
/// <summary>
/// Gets the length of the current code string.
/// </summary>
public int Length {
get { return codeBuilder.Length; }
}
string GetIndentString()
{
StringBuilder currentIndentString = new StringBuilder();
for (int i = 0; i < indent; ++i) {
currentIndentString.Append(indentString);
}
return currentIndentString.ToString();
}
/// <summary>
/// Returns the index of the end of the previous line.
/// </summary>
/// <param name="index">This is the index to start working backwards from.</param>
int MoveToPreviousLineEnd(string code, int index)
{
while (index >= 0) {
if (code[index] == '\r') {
return index - 1;
}
--index;
}
return -1;
}
}
}

223
src/AddIns/BackendBindings/Scripting/Test/Designer/ScriptingCodeBuilderTests.cs

@ -0,0 +1,223 @@ @@ -0,0 +1,223 @@
// <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.Scripting;
using NUnit.Framework;
namespace ICSharpCode.Scripting.Tests.Designer
{
[TestFixture]
public class ScriptingCodeBuilderTests
{
ScriptingCodeBuilder codeBuilder;
[SetUp]
public void Init()
{
codeBuilder = new ScriptingCodeBuilder();
codeBuilder.IndentString = "\t";
}
[Test]
public void AppendNewLine()
{
codeBuilder.AppendLine();
string text = codeBuilder.ToString();
Assert.AreEqual("\r\n", text);
}
[Test]
public void AppendText()
{
codeBuilder.Append("abc");
string text = codeBuilder.ToString();
Assert.AreEqual("abc", text);
}
[Test]
public void AppendIndentedText()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndented("abc");
string text = codeBuilder.ToString();
Assert.AreEqual("\tabc", text);
}
[Test]
public void IncreaseIndentTwice()
{
codeBuilder.IncreaseIndent();
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndented("abc");
string text = codeBuilder.ToString();
Assert.AreEqual("\t\tabc", text);
}
[Test]
public void DecreaseIndent()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndented("abc");
codeBuilder.AppendLine();
codeBuilder.DecreaseIndent();
codeBuilder.AppendIndented("abc");
string text = codeBuilder.ToString();
Assert.AreEqual("\tabc\r\nabc", text);
}
[Test]
public void AppendIndentedLine()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndentedLine("abc");
string text = codeBuilder.ToString();
Assert.AreEqual("\tabc\r\n", text);
}
[Test]
public void InitialIndentWhenCodeBuilderCreatedIsZero()
{
Assert.AreEqual(0, codeBuilder.Indent);
}
[Test]
public void IncreaseIndentByOne()
{
codeBuilder.IncreaseIndent();
Assert.AreEqual(1, codeBuilder.Indent);
}
[Test]
public void LengthAfterAddingText()
{
codeBuilder.Append("abc");
Assert.AreEqual(3, codeBuilder.Length);
}
[Test]
public void IndentPassedToConstructor()
{
codeBuilder = new ScriptingCodeBuilder(2);
codeBuilder.AppendIndented("abc");
string text = codeBuilder.ToString();
Assert.AreEqual("\t\tabc", text);
}
[Test]
public void InsertIndentedLine()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndentedLine("def");
codeBuilder.InsertIndentedLine("abc");
string text = codeBuilder.ToString();
Assert.AreEqual("\tabc\r\n\tdef\r\n", text);
}
[Test]
public void PreviousLineIsEmptyNewLine()
{
codeBuilder.AppendLine();
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsComment()
{
codeBuilder.AppendIndentedLine("# comment");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsNotEmptyNewLine()
{
codeBuilder.AppendIndentedLine("abc");
Assert.IsTrue(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineDoesNotExist()
{
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsEmptyAndCurrentLineHasText()
{
codeBuilder.AppendLine();
codeBuilder.Append("abc");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void PreviousLineIsMadeUpOfWhiteSpace()
{
codeBuilder.AppendIndentedLine(" \t ");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void TwoLinesWithPreviousLineMadeUpOfWhiteSpace()
{
codeBuilder.AppendIndentedLine("1st");
codeBuilder.AppendIndentedLine(" \t ");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void TwoLinesWithPreviousEmptyLine()
{
codeBuilder.AppendIndentedLine("1st");
codeBuilder.AppendLine();
codeBuilder.Append("abc");
Assert.IsFalse(codeBuilder.PreviousLineIsCode);
}
[Test]
public void TwoLinesWithNoPreviousEmptyLine()
{
codeBuilder.AppendIndentedLine("First");
codeBuilder.AppendIndentedLine("Second");
Assert.IsTrue(codeBuilder.PreviousLineIsCode);
}
[Test]
public void AppendToPreviousLine()
{
codeBuilder.AppendIndentedLine("abc");
codeBuilder.AppendToPreviousLine(" # comment");
string text = codeBuilder.ToString();
Assert.AreEqual("abc # comment\r\n", text);
}
[Test]
public void TrimEnd()
{
codeBuilder.Append("abc");
codeBuilder.AppendLine();
codeBuilder.Append("def");
codeBuilder.AppendLine();
codeBuilder.TrimEnd();
Assert.AreEqual("abc\r\ndef", codeBuilder.ToString());
}
[Test]
public void GetFirstLineOfCode()
{
codeBuilder.IncreaseIndent();
codeBuilder.AppendIndentedLine("abc");
Assert.AreEqual("\tabc", codeBuilder.GetPreviousLine());
}
[Test]
public void GetPreviousLineReturnsEmptyStringIfNoPreviousLine()
{
codeBuilder.AppendIndented("def");
Assert.AreEqual(String.Empty, codeBuilder.GetPreviousLine());
}
}
}

1
src/AddIns/BackendBindings/Scripting/Test/ICSharpCode.Scripting.Tests.csproj

@ -76,6 +76,7 @@ @@ -76,6 +76,7 @@
<Compile Include="Console\ScriptingConsoleTextEditorTests.cs" />
<Compile Include="Console\ThreadSafeScriptingConsoleTextEditorTests.cs" />
<Compile Include="Designer\NameCreationServiceTests.cs" />
<Compile Include="Designer\ScriptingCodeBuilderTests.cs" />
<Compile Include="Testing\CreateTextWriterFromCreateTextWriterInfoTestFixture.cs" />
<Compile Include="Testing\CreateTextWriterInfoEqualsTestFixture.cs" />
<Compile Include="Utils\AddedComponent.cs" />

Loading…
Cancel
Save