From 6997465bff95875eacf12c58944fc183a50d30c1 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Mon, 25 Aug 2008 16:03:44 +0000 Subject: [PATCH] PythonConsole now uses a read-only text marker for the read-only region and stops the undo action affecting text before the prompt. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3443 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../PythonBinding/Project/Src/ITextEditor.cs | 5 +++ .../Project/Src/PythonConsole.cs | 1 + .../PythonBinding/Project/Src/TextEditor.cs | 35 ++++++++++--------- .../Test/Console/MockTextEditor.cs | 10 ++++++ ...PythonConsoleReadOnlyRegionsTestFixture.cs | 6 ++++ .../Test/Console/TextEditorTestFixture.cs | 32 +++++++++++++---- 6 files changed, 66 insertions(+), 23 deletions(-) diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ITextEditor.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ITextEditor.cs index 7dbd6dde48..73d5d2b8b0 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ITextEditor.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/ITextEditor.cs @@ -87,5 +87,10 @@ namespace ICSharpCode.PythonBinding /// Shows the code completion window. /// void ShowCompletionWindow(); + + /// + /// Makes the current text content read only. Text can be entered at the end. + /// + void MakeCurrentContentReadOnly(); } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonConsole.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonConsole.cs index 0eb79468f0..dca4ff8709 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonConsole.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonConsole.cs @@ -97,6 +97,7 @@ namespace ICSharpCode.PythonBinding if (style == Style.Prompt) { promptLength = text.Length; + textEditor.MakeCurrentContentReadOnly(); } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/TextEditor.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/TextEditor.cs index 459eb1d4af..a5c45d4f0b 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/TextEditor.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/TextEditor.cs @@ -20,11 +20,11 @@ namespace ICSharpCode.PythonBinding TextEditorControl textEditorControl; TextArea textArea; Color customLineColour = Color.LightGray; + TextMarker readOnlyMarker; public TextEditor(TextEditorControl textEditorControl) { this.textEditorControl = textEditorControl; - this.textEditorControl.Document.LineCountChanged += LineCountChanged; this.textArea = textEditorControl.ActiveTextAreaControl.TextArea; textEditorControl.TextEditorProperties.SupportReadOnlySegments = true; } @@ -69,11 +69,6 @@ namespace ICSharpCode.PythonBinding } } } - - void AddCustomLine(int line) - { - //textEditorControl.Document.CustomLineManager.AddCustomLine(line, customLineColour, false); - } public int Column { get { return textEditorControl.ActiveTextAreaControl.Caret.Column; } @@ -124,6 +119,22 @@ namespace ICSharpCode.PythonBinding return textEditorControl.Document.GetText(lineSegment); } } + + /// + /// Makes the current text read only. Text can still be entered at the end. + /// + public void MakeCurrentContentReadOnly() + { + IDocument doc = textEditorControl.Document; + if (readOnlyMarker == null) { + readOnlyMarker = new TextMarker(0, doc.TextLength, TextMarkerType.Invisible); + readOnlyMarker.IsReadOnly = true; + doc.MarkerStrategy.AddMarker(readOnlyMarker); + } + readOnlyMarker.Offset = 0; + readOnlyMarker.Length = doc.TextLength; + doc.UndoStack.ClearAll(); + } public void ShowCompletionWindow() { @@ -145,16 +156,6 @@ namespace ICSharpCode.PythonBinding } else { textEditorControl.IndentStyle = style; } - } - - void LineCountChanged(object source, LineCountChangeEventArgs e) - { - IDocument doc = textEditorControl.Document; - int totalLines = doc.TotalNumberOfLines; - //doc.CustomLineManager.Clear(); - for (int line = 0; line < totalLines - 1; ++line) { - // doc.CustomLineManager.AddCustomLine(line, customLineColour, false); - } - } + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/MockTextEditor.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/MockTextEditor.cs index 2c5c337908..4886f66d84 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/MockTextEditor.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/MockTextEditor.cs @@ -30,6 +30,7 @@ namespace PythonBinding.Tests.Console int totalLines = 1; List textColors = new List(); bool showCompletionWindowCalled; + bool makeReadOnlyCalled; public MockTextEditor() { @@ -64,6 +65,10 @@ namespace PythonBinding.Tests.Console public bool IsShowCompletionWindowCalled { get { return showCompletionWindowCalled; } } + + public bool IsMakeCurrentContentReadOnlyCalled { + get { return makeReadOnlyCalled; } + } public string Text { get { return previousLines.ToString() + lineBuilder.ToString(); } @@ -192,6 +197,11 @@ namespace PythonBinding.Tests.Console showCompletionWindowCalled = true; } + public void MakeCurrentContentReadOnly() + { + makeReadOnlyCalled = true; + } + public List WrittenTextColors { get { return textColors; } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleReadOnlyRegionsTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleReadOnlyRegionsTestFixture.cs index 9f6a5c0e1c..edfaeb90c9 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleReadOnlyRegionsTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/PythonConsoleReadOnlyRegionsTestFixture.cs @@ -32,6 +32,12 @@ namespace PythonBinding.Tests.Console console = new PythonConsole(textEditor); console.Write(prompt, Style.Prompt); } + + [Test] + public void MakeCurrentContentReadOnlyIsCalled() + { + Assert.IsTrue(textEditor.IsMakeCurrentContentReadOnlyCalled); + } [Test] public void LeftArrowThenInsertNewCharacterInsertsText() diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/TextEditorTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/TextEditorTestFixture.cs index 556104cca6..4729d9ef3f 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/TextEditorTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Console/TextEditorTestFixture.cs @@ -121,15 +121,17 @@ namespace PythonBinding.Tests.Console } [Test] - [Ignore("Change to use read-only marker.")] - public void AfterEnterKeyPreviousLineIsCustomLine() + public void MakeCurrentTextEditorContent() { - //textEditorControl.Document.CustomLineManager.Clear(); textEditorControl.Text = String.Empty; textEditor.Write("abc" + Environment.NewLine); - //Assert.AreEqual(1, textEditorControl.Document.CustomLineManager.CustomLines.Count); - //Assert.AreEqual(textEditor.CustomLineColour, textEditorControl.Document.CustomLineManager.GetCustomColor(0, Color.Empty)); - Assert.Fail("Implement..."); + textEditor.MakeCurrentContentReadOnly(); + + TextMarker readOnlyTextMarker = GetReadOnlyTextMarker(textEditorControl); + Assert.IsNotNull(readOnlyTextMarker); + Assert.AreEqual(0, readOnlyTextMarker.Offset); + Assert.AreEqual(textEditorControl.Text.Length, readOnlyTextMarker.Length); + Assert.IsFalse(textEditorControl.Document.UndoStack.CanUndo); } [Test] @@ -368,5 +370,23 @@ namespace PythonBinding.Tests.Console keyPressed = ch; return false; } + + /// + /// Used to remove all text markers from the text editor. + /// + bool AllMarkersMatch(TextMarker marker) + { + return true; + } + + TextMarker GetReadOnlyTextMarker(TextEditorControl textEditorControl) + { + foreach (TextMarker marker in textEditorControl.Document.MarkerStrategy.TextMarker) { + if (marker.IsReadOnly) { + return marker; + } + } + return null; + } } }