// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Windows.Input; using ICSharpCode.AvalonEdit.Document; namespace ICSharpCode.AvalonEdit.Snippets { /// /// Sets the caret position after interactive mode has finished. /// [Serializable] public class SnippetCaretElement : SnippetElement { /// public override void Insert(InsertionContext context) { if (!string.IsNullOrEmpty(context.SelectedText)) SetCaret(context); } internal static void SetCaret(InsertionContext context) { TextAnchor pos = context.Document.CreateAnchor(context.InsertionPosition); pos.MovementType = AnchorMovementType.BeforeInsertion; pos.SurviveDeletion = true; context.Deactivated += (sender, e) => { if (e.Reason == DeactivateReason.ReturnPressed || e.Reason == DeactivateReason.NoActiveElements) { context.TextArea.Caret.Offset = pos.Offset; } }; } } }