Browse Source

AvalonEdit: Add TextDocument constructor that takes ITextSource.

This allows creating a document from a RopeTextDocument or from another TextDocument while sharing unchanged parts of the document text.
pull/1/head
Daniel Grunwald 15 years ago
parent
commit
fde21e0f16
  1. 25
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs
  2. 14
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/TextEditor.cs

25
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs

@ -105,6 +105,31 @@ namespace ICSharpCode.AvalonEdit.Document
undoStack.AttachToDocument(this); undoStack.AttachToDocument(this);
FireChangeEvents(); FireChangeEvents();
} }
/// <summary>
/// Create a new text document with the specified initial text.
/// </summary>
public TextDocument(ITextSource initialText)
: this(GetTextFromTextSource(initialText))
{
}
// gets the text from a text source, directly retrieving the underlying rope where possible
static IEnumerable<char> GetTextFromTextSource(ITextSource textSource)
{
if (textSource == null)
throw new ArgumentNullException("textSource");
RopeTextSource rts = textSource as RopeTextSource;
if (rts != null)
return rts.GetRope();
TextDocument doc = textSource as TextDocument;
if (doc != null)
return doc.rope;
return textSource.Text;
}
#endregion #endregion
#region Text #region Text

14
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/TextEditor.cs

@ -58,18 +58,16 @@ namespace ICSharpCode.AvalonEdit
textArea.TextView.Services.AddService(typeof(TextEditor), this); textArea.TextView.Services.AddService(typeof(TextEditor), this);
SetCurrentPropertyValue(OptionsProperty, textArea.Options); SetCurrentValue(OptionsProperty, textArea.Options);
SetCurrentPropertyValue(DocumentProperty, new TextDocument()); SetCurrentValue(DocumentProperty, new TextDocument());
} }
void SetCurrentPropertyValue(DependencyProperty property, object value) #if !DOTNET4
void SetCurrentValue(DependencyProperty property, object value)
{ {
#if DOTNET4
SetCurrentValue(property, value);
#else
SetValue(property, value); SetValue(property, value);
#endif
} }
#endif
#endregion #endregion
/// <inheritdoc/> /// <inheritdoc/>
@ -440,7 +438,7 @@ namespace ICSharpCode.AvalonEdit
if (e.PropertyName == "IsOriginalFile") { if (e.PropertyName == "IsOriginalFile") {
TextDocument document = this.Document; TextDocument document = this.Document;
if (document != null) { if (document != null) {
SetCurrentPropertyValue(IsModifiedProperty, Boxes.Box(!document.UndoStack.IsOriginalFile)); SetCurrentValue(IsModifiedProperty, Boxes.Box(!document.UndoStack.IsOriginalFile));
} }
return true; return true;
} else { } else {

Loading…
Cancel
Save