From b7263a90a96014def6e634e98c6b3e4eed7f5267 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 1 Sep 2010 18:00:02 +0200 Subject: [PATCH] - created DefaultSnippetElementProvider for standard SnippetElements - introduced SnippetInfo and changed signature of ISnippetElementProvider - added SnippetAnchorElement - refactored InsertCtor to insert ctor body at the same time as the dialog --- .../ICSharpCode.AvalonEdit.csproj | 1 + .../Snippets/InsertionContext.cs | 9 +++ .../Snippets/SnippetAnchorElement.cs | 78 +++++++++++++++++++ .../Snippets/SnippetBoundElement.cs | 1 - 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 ICSharpCode.AvalonEdit/Snippets/SnippetAnchorElement.cs diff --git a/ICSharpCode.AvalonEdit/ICSharpCode.AvalonEdit.csproj b/ICSharpCode.AvalonEdit/ICSharpCode.AvalonEdit.csproj index c5b4df4..929867a 100644 --- a/ICSharpCode.AvalonEdit/ICSharpCode.AvalonEdit.csproj +++ b/ICSharpCode.AvalonEdit/ICSharpCode.AvalonEdit.csproj @@ -286,6 +286,7 @@ VisualLine.cs + diff --git a/ICSharpCode.AvalonEdit/Snippets/InsertionContext.cs b/ICSharpCode.AvalonEdit/Snippets/InsertionContext.cs index 4eb1b8a..5db7e9d 100644 --- a/ICSharpCode.AvalonEdit/Snippets/InsertionContext.cs +++ b/ICSharpCode.AvalonEdit/Snippets/InsertionContext.cs @@ -92,6 +92,15 @@ namespace ICSharpCode.AvalonEdit.Snippets AnchorSegment wholeSnippetAnchor; bool deactivateIfSnippetEmpty; + public int StartPosition { + get { + if (wholeSnippetAnchor != null) + return wholeSnippetAnchor.Offset; + else + return startPosition; + } + } + /// /// Inserts text at the insertion position and advances the insertion position. /// This method will add the current indentation to every line in and will diff --git a/ICSharpCode.AvalonEdit/Snippets/SnippetAnchorElement.cs b/ICSharpCode.AvalonEdit/Snippets/SnippetAnchorElement.cs new file mode 100644 index 0000000..df54f4d --- /dev/null +++ b/ICSharpCode.AvalonEdit/Snippets/SnippetAnchorElement.cs @@ -0,0 +1,78 @@ +// +// +// +// +// $Revision$ +// + +using System; +using ICSharpCode.AvalonEdit.Document; + +namespace ICSharpCode.AvalonEdit.Snippets +{ + public sealed class SnippetAnchorElement : SnippetElement + { + string textToInsert = ""; + + public string Name { get; private set; } + + public SnippetAnchorElement(string name) + { + this.Name = name; + } + + public override void Insert(InsertionContext context) + { + int start = context.InsertionPosition; + context.InsertText(""); + int end = context.InsertionPosition; + AnchorSegment segment = new AnchorSegment(context.Document, start, end - start); + context.RegisterActiveElement(this, new AnchorSnippetElement(segment, "", Name, context)); + } + } + + public sealed class AnchorSnippetElement : IActiveElement + { + public bool IsEditable { + get { return false; } + } + + AnchorSegment segment; + InsertionContext context; + + public ISegment Segment { + get { return segment; } + } + + public AnchorSnippetElement(AnchorSegment segment, string text, string name, InsertionContext context) + { + this.segment = segment; + this.context = context; + this.Text = text; + this.Name = name; + } + + public string Text { + get { return context.Document.GetText(segment); } + set { + int offset = segment.Offset; + int length = segment.Length; + context.Document.Replace(offset, length, value); + if (length == 0) { + // replacing an empty anchor segment with text won't enlarge it, so we have to recreate it + segment = new AnchorSegment(context.Document, offset, value.Length); + } + } + } + + public string Name { get; private set; } + + public void OnInsertionCompleted() + { + } + + public void Deactivate() + { + } + } +} diff --git a/ICSharpCode.AvalonEdit/Snippets/SnippetBoundElement.cs b/ICSharpCode.AvalonEdit/Snippets/SnippetBoundElement.cs index 7195552..11b1802 100644 --- a/ICSharpCode.AvalonEdit/Snippets/SnippetBoundElement.cs +++ b/ICSharpCode.AvalonEdit/Snippets/SnippetBoundElement.cs @@ -107,7 +107,6 @@ namespace ICSharpCode.AvalonEdit.Snippets { } - public bool IsEditable { get { return false; } }