From abc43d22a17103c3be7833c63a6ce6a07c15ea6b Mon Sep 17 00:00:00 2001 From: mrward Date: Sun, 12 Sep 2010 20:32:11 +0100 Subject: [PATCH] Move the code to save text sent before the scripting console window is active into a separate class. --- .../Project/ICSharpCode.Scripting.csproj | 1 + .../Scripting/Project/Src/ScriptingConsole.cs | 73 +++++++------------ .../Project/Src/TextSentToScriptingConsole.cs | 71 ++++++++++++++++++ 3 files changed, 99 insertions(+), 46 deletions(-) create mode 100644 src/AddIns/BackendBindings/Scripting/Project/Src/TextSentToScriptingConsole.cs diff --git a/src/AddIns/BackendBindings/Scripting/Project/ICSharpCode.Scripting.csproj b/src/AddIns/BackendBindings/Scripting/Project/ICSharpCode.Scripting.csproj index 0975ee52a7..c13e55a77a 100644 --- a/src/AddIns/BackendBindings/Scripting/Project/ICSharpCode.Scripting.csproj +++ b/src/AddIns/BackendBindings/Scripting/Project/ICSharpCode.Scripting.csproj @@ -97,6 +97,7 @@ + diff --git a/src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingConsole.cs b/src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingConsole.cs index 7f239f1ad1..fd952b8437 100644 --- a/src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingConsole.cs +++ b/src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingConsole.cs @@ -18,7 +18,8 @@ namespace ICSharpCode.Scripting int promptLength; bool firstPromptDisplayed; - List savedTextLines = new List(); + + TextSentToScriptingConsole textSent = new TextSentToScriptingConsole(); public event EventHandler LineReceived; @@ -70,18 +71,19 @@ namespace ICSharpCode.Scripting { textEditor.Write(text); if (style == ScriptingStyle.Prompt) { - WriteSavedTextAfterFirstPrompt(); + firstPromptDisplayed = true; promptLength = text.Length; + + WriteFirstLineOfSentText(); + textEditor.MakeCurrentContentReadOnly(); } } - void WriteSavedTextAfterFirstPrompt() + void WriteFirstLineOfSentText() { - firstPromptDisplayed = true; - if (savedTextLines.Count > 0) { - string line = GetFirstLineOfText(savedTextLines); - savedTextLines.RemoveAt(0); + if (textSent.HasLine) { + string line = textSent.RemoveFirstLine(); textEditor.Write(line); } } @@ -286,7 +288,12 @@ namespace ICSharpCode.Scripting unreadLines.AddLine(line); FireLineReceivedEvent(); MoveCursorToEndOfLastTextEditorLine(); - WriteTextIfFirstPromptHasBeenDisplayed(line + "\r\n"); + + if (firstPromptDisplayed) { + WriteLine(line, ScriptingStyle.Out); + } else { + SaveLineToDisplayAfterFirstPromptDisplayed(line); + } } protected virtual void FireLineReceivedEvent() @@ -294,23 +301,10 @@ namespace ICSharpCode.Scripting OnLineReceived(); } - void WriteTextIfFirstPromptHasBeenDisplayed(string text) + void SaveLineToDisplayAfterFirstPromptDisplayed(string line) { - if (firstPromptDisplayed) { - Write(text, ScriptingStyle.Out); - } else { - savedTextLines = GetLines(text); - } - } - - void WriteFirstLineIfFirstPromptHasBeenDisplayed(List lines) - { - string firstLine = GetFirstLineOfText(lines); - if (firstPromptDisplayed) { - Write(firstLine, ScriptingStyle.Out); - lines.RemoveAt(0); - } - savedTextLines = lines; + string text = line + "\r\n"; + textSent.AddText(text); } public virtual IList GetMemberNames(string name) @@ -325,34 +319,21 @@ namespace ICSharpCode.Scripting public void SendText(string text) { + textSent.AddText(text); MoveCursorToEndOfLastTextEditorLine(); - WriteFirstLineOfTextIfFirstPromptHasBeenDisplayed(text); - } - - void WriteFirstLineOfTextIfFirstPromptHasBeenDisplayed(string text) - { - List lines = GetLines(text); - if (lines.Count > 1) { - unreadLines.AddAllLinesExceptLast(lines); - FireLineReceivedEvent(); + SaveUnreadLinesOfSentText(); + + if (firstPromptDisplayed) { + WriteFirstLineOfSentText(); } - WriteFirstLineIfFirstPromptHasBeenDisplayed(lines); } - List GetLines(string text) + void SaveUnreadLinesOfSentText() { - text = text.Replace("\r\n", "\n"); - string[] lines = text.Split('\n'); - return new List(lines); - } - - string GetFirstLineOfText(List lines) - { - if (lines.Count > 1) { - string firstLine = lines[0] + "\r\n"; - return firstLine; + if (textSent.HasAtLeastOneLine) { + unreadLines.AddAllLinesExceptLast(textSent.lines); + FireLineReceivedEvent(); } - return lines[0]; } public string ReadFirstUnreadLine() diff --git a/src/AddIns/BackendBindings/Scripting/Project/Src/TextSentToScriptingConsole.cs b/src/AddIns/BackendBindings/Scripting/Project/Src/TextSentToScriptingConsole.cs new file mode 100644 index 0000000000..5a6b5cdfa7 --- /dev/null +++ b/src/AddIns/BackendBindings/Scripting/Project/Src/TextSentToScriptingConsole.cs @@ -0,0 +1,71 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using System.Collections.Generic; + +namespace ICSharpCode.Scripting +{ + public class TextSentToScriptingConsole + { + public List lines = new List(); + + public TextSentToScriptingConsole() + { + } + + public void AddText(string text) + { + GetLines(text); + } + + public void AddLines(IList linesToAdd) + { + lines.AddRange(linesToAdd); + } + + void GetLines(string text) + { + string[] linesToAdd = ConvertTextToLines(text); + lines.AddRange(linesToAdd); + } + + string[] ConvertTextToLines(string text) + { + text = text.Replace("\r\n", "\n"); + return text.Split('\n'); + } + + public bool HasLine { + get { return lines.Count > 0; } + } + + public bool HasAtLeastOneLine { + get { return lines.Count > 1; } + } + + /// + /// Returns line with '\r\n' if not the last line of text. + /// + public string RemoveFirstLine() + { + string line = GetFirstLine(); + if (line != null) { + lines.RemoveAt(0); + } + return line; + } + + public string GetFirstLine() + { + if (HasLine) { + if (lines.Count > 1) { + string firstLine = lines[0] + "\r\n"; + return firstLine; + } + return lines[0]; + } + return null; + } + } +}