8 changed files with 421 additions and 685 deletions
@ -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; |
||||
} |
||||
} |
||||
} |
@ -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()); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue