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 @@
<Compile Include="Src\SendLineToScriptingConsoleCommand.cs" /> <Compile Include="Src\SendLineToScriptingConsoleCommand.cs" />
<Compile Include="Src\SendSelectedTextToScriptingConsoleCommand.cs" /> <Compile Include="Src\SendSelectedTextToScriptingConsoleCommand.cs" />
<Compile Include="Src\SendToScriptingConsoleCommand.cs" /> <Compile Include="Src\SendToScriptingConsoleCommand.cs" />
<Compile Include="Src\TextSentToScriptingConsole.cs" />
<Compile Include="Src\ThreadSafeScriptingConsole.cs" /> <Compile Include="Src\ThreadSafeScriptingConsole.cs" />
<Compile Include="Src\ThreadSafeScriptingConsoleEvents.cs" /> <Compile Include="Src\ThreadSafeScriptingConsoleEvents.cs" />
</ItemGroup> </ItemGroup>

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

@ -18,7 +18,8 @@ namespace ICSharpCode.Scripting
int promptLength; int promptLength;
bool firstPromptDisplayed; bool firstPromptDisplayed;
List<string> savedTextLines = new List<string>();
TextSentToScriptingConsole textSent = new TextSentToScriptingConsole();
public event EventHandler LineReceived; public event EventHandler LineReceived;
@ -70,18 +71,19 @@ namespace ICSharpCode.Scripting
{ {
textEditor.Write(text); textEditor.Write(text);
if (style == ScriptingStyle.Prompt) { if (style == ScriptingStyle.Prompt) {
WriteSavedTextAfterFirstPrompt(); firstPromptDisplayed = true;
promptLength = text.Length; promptLength = text.Length;
WriteFirstLineOfSentText();
textEditor.MakeCurrentContentReadOnly(); textEditor.MakeCurrentContentReadOnly();
} }
} }
void WriteSavedTextAfterFirstPrompt() void WriteFirstLineOfSentText()
{ {
firstPromptDisplayed = true; if (textSent.HasLine) {
if (savedTextLines.Count > 0) { string line = textSent.RemoveFirstLine();
string line = GetFirstLineOfText(savedTextLines);
savedTextLines.RemoveAt(0);
textEditor.Write(line); textEditor.Write(line);
} }
} }
@ -286,7 +288,12 @@ namespace ICSharpCode.Scripting
unreadLines.AddLine(line); unreadLines.AddLine(line);
FireLineReceivedEvent(); FireLineReceivedEvent();
MoveCursorToEndOfLastTextEditorLine(); MoveCursorToEndOfLastTextEditorLine();
WriteTextIfFirstPromptHasBeenDisplayed(line + "\r\n");
if (firstPromptDisplayed) {
WriteLine(line, ScriptingStyle.Out);
} else {
SaveLineToDisplayAfterFirstPromptDisplayed(line);
}
} }
protected virtual void FireLineReceivedEvent() protected virtual void FireLineReceivedEvent()
@ -294,23 +301,10 @@ namespace ICSharpCode.Scripting
OnLineReceived(); OnLineReceived();
} }
void WriteTextIfFirstPromptHasBeenDisplayed(string text) void SaveLineToDisplayAfterFirstPromptDisplayed(string line)
{ {
if (firstPromptDisplayed) { string text = line + "\r\n";
Write(text, ScriptingStyle.Out); textSent.AddText(text);
} else {
savedTextLines = GetLines(text);
}
}
void WriteFirstLineIfFirstPromptHasBeenDisplayed(List<string> lines)
{
string firstLine = GetFirstLineOfText(lines);
if (firstPromptDisplayed) {
Write(firstLine, ScriptingStyle.Out);
lines.RemoveAt(0);
}
savedTextLines = lines;
} }
public virtual IList<string> GetMemberNames(string name) public virtual IList<string> GetMemberNames(string name)
@ -325,34 +319,21 @@ namespace ICSharpCode.Scripting
public void SendText(string text) public void SendText(string text)
{ {
textSent.AddText(text);
MoveCursorToEndOfLastTextEditorLine(); MoveCursorToEndOfLastTextEditorLine();
WriteFirstLineOfTextIfFirstPromptHasBeenDisplayed(text); SaveUnreadLinesOfSentText();
}
if (firstPromptDisplayed) {
void WriteFirstLineOfTextIfFirstPromptHasBeenDisplayed(string text) WriteFirstLineOfSentText();
{
List<string> lines = GetLines(text);
if (lines.Count > 1) {
unreadLines.AddAllLinesExceptLast(lines);
FireLineReceivedEvent();
} }
WriteFirstLineIfFirstPromptHasBeenDisplayed(lines);
} }
List<string> GetLines(string text) void SaveUnreadLinesOfSentText()
{ {
text = text.Replace("\r\n", "\n"); if (textSent.HasAtLeastOneLine) {
string[] lines = text.Split('\n'); unreadLines.AddAllLinesExceptLast(textSent.lines);
return new List<string>(lines); FireLineReceivedEvent();
}
string GetFirstLineOfText(List<string> lines)
{
if (lines.Count > 1) {
string firstLine = lines[0] + "\r\n";
return firstLine;
} }
return lines[0];
} }
public string ReadFirstUnreadLine() public string ReadFirstUnreadLine()

71
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<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