From 0f3a0732fbe716ac90f8d7736a4dd9f07e2c0e50 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 22 Jul 2012 13:23:49 +0200 Subject: [PATCH] Snippets: make ${Caret} work as expected when ${Selection} isn't used. --- .../Snippets/DefaultSnippetElementProvider.cs | 10 ++++++-- .../Snippets/SnippetCaretElement.cs | 25 ++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Snippets/DefaultSnippetElementProvider.cs b/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Snippets/DefaultSnippetElementProvider.cs index ab1362efe9..d88c422f77 100644 --- a/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Snippets/DefaultSnippetElementProvider.cs +++ b/src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Snippets/DefaultSnippetElementProvider.cs @@ -13,8 +13,14 @@ namespace ICSharpCode.AvalonEdit.AddIn.Snippets { if ("Selection".Equals(snippetInfo.Tag, StringComparison.OrdinalIgnoreCase)) return new SnippetSelectionElement() { Indentation = GetWhitespaceBefore(snippetInfo.SnippetText, snippetInfo.Position).Length }; - if ("Caret".Equals(snippetInfo.Tag, StringComparison.OrdinalIgnoreCase)) - return new SnippetCaretElement(); + if ("Caret".Equals(snippetInfo.Tag, StringComparison.OrdinalIgnoreCase)) { + // If a ${Selection} exists, use the ${Caret} only if there is text selected + // (if no text is selected, ${Selection} will set the caret + if (snippetInfo.SnippetText.IndexOf("${Selection}", StringComparison.OrdinalIgnoreCase) >= 0) + return new SnippetCaretElement(setCaretOnlyIfTextIsSelected: true); + else + return new SnippetCaretElement(); + } return null; } diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs index d1060a6e87..fc9507c0e4 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs @@ -2,6 +2,7 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; +using System.Runtime.Serialization; using System.Windows.Input; using ICSharpCode.AvalonEdit.Document; @@ -13,10 +14,32 @@ namespace ICSharpCode.AvalonEdit.Snippets [Serializable] public class SnippetCaretElement : SnippetElement { + [OptionalField] + bool setCaretOnlyIfTextIsSelected; + + /// + /// Creates a new SnippetCaretElement. + /// + public SnippetCaretElement() + { + } + + /// + /// Creates a new SnippetCaretElement. + /// + /// + /// If set to true, the caret is set only when some text was selected. + /// This is useful + /// + public SnippetCaretElement(bool setCaretOnlyIfTextIsSelected) + { + this.setCaretOnlyIfTextIsSelected = setCaretOnlyIfTextIsSelected; + } + /// public override void Insert(InsertionContext context) { - if (!string.IsNullOrEmpty(context.SelectedText)) + if (!setCaretOnlyIfTextIsSelected || !string.IsNullOrEmpty(context.SelectedText)) SetCaret(context); }