Browse Source

Move the code to save text sent before the scripting console window is active into a separate class.

pull/1/head
mrward 15 years ago
parent
commit
abc43d22a1
  1. 1
      src/AddIns/BackendBindings/Scripting/Project/ICSharpCode.Scripting.csproj
  2. 73
      src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingConsole.cs
  3. 71
      src/AddIns/BackendBindings/Scripting/Project/Src/TextSentToScriptingConsole.cs

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

@ -97,6 +97,7 @@ @@ -97,6 +97,7 @@
<Compile Include="Src\SendLineToScriptingConsoleCommand.cs" />
<Compile Include="Src\SendSelectedTextToScriptingConsoleCommand.cs" />
<Compile Include="Src\SendToScriptingConsoleCommand.cs" />
<Compile Include="Src\TextSentToScriptingConsole.cs" />
<Compile Include="Src\ThreadSafeScriptingConsole.cs" />
<Compile Include="Src\ThreadSafeScriptingConsoleEvents.cs" />
</ItemGroup>

73
src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingConsole.cs

@ -18,7 +18,8 @@ namespace ICSharpCode.Scripting @@ -18,7 +18,8 @@ namespace ICSharpCode.Scripting
int promptLength;
bool firstPromptDisplayed;
List<string> savedTextLines = new List<string>();
TextSentToScriptingConsole textSent = new TextSentToScriptingConsole();
public event EventHandler LineReceived;
@ -70,18 +71,19 @@ namespace ICSharpCode.Scripting @@ -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 @@ -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 @@ -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<string> 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<string> GetMemberNames(string name)
@ -325,34 +319,21 @@ namespace ICSharpCode.Scripting @@ -325,34 +319,21 @@ namespace ICSharpCode.Scripting
public void SendText(string text)
{
textSent.AddText(text);
MoveCursorToEndOfLastTextEditorLine();
WriteFirstLineOfTextIfFirstPromptHasBeenDisplayed(text);
}
void WriteFirstLineOfTextIfFirstPromptHasBeenDisplayed(string text)
{
List<string> lines = GetLines(text);
if (lines.Count > 1) {
unreadLines.AddAllLinesExceptLast(lines);
FireLineReceivedEvent();
SaveUnreadLinesOfSentText();
if (firstPromptDisplayed) {
WriteFirstLineOfSentText();
}
WriteFirstLineIfFirstPromptHasBeenDisplayed(lines);
}
List<string> GetLines(string text)
void SaveUnreadLinesOfSentText()
{
text = text.Replace("\r\n", "\n");
string[] lines = text.Split('\n');
return new List<string>(lines);
}
string GetFirstLineOfText(List<string> 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()

71
src/AddIns/BackendBindings/Scripting/Project/Src/TextSentToScriptingConsole.cs

@ -0,0 +1,71 @@ @@ -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<string> lines = new List<string>();
public TextSentToScriptingConsole()
{
}
public void AddText(string text)
{
GetLines(text);
}
public void AddLines(IList<string> 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; }
}
/// <summary>
/// Returns line with '\r\n' if not the last line of text.
/// </summary>
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;
}
}
}
Loading…
Cancel
Save