Browse Source

Snippets: make ${Caret} work as expected when ${Selection} isn't used.

pull/27/merge
Daniel Grunwald 13 years ago
parent
commit
0f3a0732fb
  1. 10
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Snippets/DefaultSnippetElementProvider.cs
  2. 25
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs

10
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Snippets/DefaultSnippetElementProvider.cs

@ -13,8 +13,14 @@ namespace ICSharpCode.AvalonEdit.AddIn.Snippets @@ -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;
}

25
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetCaretElement.cs

@ -2,6 +2,7 @@ @@ -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 @@ -13,10 +14,32 @@ namespace ICSharpCode.AvalonEdit.Snippets
[Serializable]
public class SnippetCaretElement : SnippetElement
{
[OptionalField]
bool setCaretOnlyIfTextIsSelected;
/// <summary>
/// Creates a new SnippetCaretElement.
/// </summary>
public SnippetCaretElement()
{
}
/// <summary>
/// Creates a new SnippetCaretElement.
/// </summary>
/// <param name="setCaretOnlyIfSelectionExists">
/// If set to true, the caret is set only when some text was selected.
/// This is useful
/// </param>
public SnippetCaretElement(bool setCaretOnlyIfTextIsSelected)
{
this.setCaretOnlyIfTextIsSelected = setCaretOnlyIfTextIsSelected;
}
/// <inheritdoc/>
public override void Insert(InsertionContext context)
{
if (!string.IsNullOrEmpty(context.SelectedText))
if (!setCaretOnlyIfTextIsSelected || !string.IsNullOrEmpty(context.SelectedText))
SetCaret(context);
}

Loading…
Cancel
Save