コード例 #1
0
ファイル: Snippet.cs プロジェクト: danwalmsley/PerspexEdit
        /// <summary>
        /// Inserts the snippet into the text area.
        /// </summary>
        public void Insert(TextArea textArea)
        {
            if (textArea == null)
            {
                throw new ArgumentNullException("textArea");
            }

            ISegment selection         = textArea.Selection.SurroundingSegment;
            int      insertionPosition = textArea.Caret.Offset;

            if (selection != null)             // if something is selected
            // use selection start instead of caret position,
            // because caret could be at end of selection or anywhere inside.
            // Removal of the selected text causes the caret position to be invalid.
            {
                insertionPosition = selection.Offset + TextUtilities.GetWhitespaceAfter(textArea.Document, selection.Offset).Length;
            }

            InsertionContext context = new InsertionContext(textArea, insertionPosition);

            using (context.Document.RunUpdate()) {
                if (selection != null)
                {
                    textArea.Document.Remove(insertionPosition, selection.EndOffset - insertionPosition);
                }
                Insert(context);
                context.RaiseInsertionCompleted(EventArgs.Empty);
            }
        }
コード例 #2
0
 public BoundActiveElement(InsertionContext context, SnippetReplaceableTextElement targetSnippetElement, SnippetBoundElement boundElement, AnchorSegment segment)
 {
     this.context = context;
     this.targetSnippetElement = targetSnippetElement;
     this.boundElement         = boundElement;
     this.segment = segment;
 }
コード例 #3
0
 /// <inheritdoc/>
 public override void Insert(InsertionContext context)
 {
     if (text != null)
     {
         context.InsertText(text);
     }
 }
コード例 #4
0
 /// <inheritdoc/>
 public override void Insert(InsertionContext context)
 {
     foreach (SnippetElement e in this.Elements)
     {
         e.Insert(context);
     }
 }
コード例 #5
0
 /// <inheritdoc/>
 public override void Insert(InsertionContext context)
 {
     if (!setCaretOnlyIfTextIsSelected || !string.IsNullOrEmpty(context.SelectedText))
     {
         SetCaret(context);
     }
 }
コード例 #6
0
        /// <inheritdoc/>
        public override void Insert(InsertionContext context)
        {
            int start = context.InsertionPosition;

            base.Insert(context);
            int end = context.InsertionPosition;

            context.RegisterActiveElement(this, new ReplaceableActiveElement(context, start, end));
        }
コード例 #7
0
        /// <inheritdoc />
        public override void Insert(InsertionContext context)
        {
            TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition);

            start.MovementType    = AnchorMovementType.BeforeInsertion;
            start.SurviveDeletion = true;
            AnchorSegment segment = new AnchorSegment(start, start);

            context.RegisterActiveElement(this, new AnchorElement(segment, Name, context));
        }
コード例 #8
0
        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;
                }
            };
        }
コード例 #9
0
 /// <inheritdoc/>
 public override void Insert(InsertionContext context)
 {
     if (targetElement != null)
     {
         TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition);
         start.MovementType    = AnchorMovementType.BeforeInsertion;
         start.SurviveDeletion = true;
         string inputText = targetElement.Text;
         if (inputText != null)
         {
             context.InsertText(ConvertText(inputText));
         }
         TextAnchor end = context.Document.CreateAnchor(context.InsertionPosition);
         end.MovementType    = AnchorMovementType.BeforeInsertion;
         end.SurviveDeletion = true;
         AnchorSegment segment = new AnchorSegment(start, end);
         context.RegisterActiveElement(this, new BoundActiveElement(context, targetElement, this, segment));
     }
 }
コード例 #10
0
        /// <inheritdoc/>
        public override void Insert(InsertionContext context)
        {
            StringBuilder tabString = new StringBuilder();

            for (int i = 0; i < Indentation; i++)
            {
                tabString.Append(context.Tab);
            }

            string indent = tabString.ToString();

            string text = context.SelectedText.TrimStart(' ', '\t');

            text = text.Replace(context.LineTerminator,
                                context.LineTerminator + indent);

            context.Document.Insert(context.InsertionPosition, text);
            context.InsertionPosition += text.Length;

            if (string.IsNullOrEmpty(context.SelectedText))
            {
                SnippetCaretElement.SetCaret(context);
            }
        }
コード例 #11
0
 public ReplaceableActiveElement(InsertionContext context, int startOffset, int endOffset)
 {
     this.context     = context;
     this.startOffset = startOffset;
     this.endOffset   = endOffset;
 }
コード例 #12
0
 /// <summary>
 /// Creates a new AnchorElement.
 /// </summary>
 public AnchorElement(AnchorSegment segment, string name, InsertionContext context)
 {
     this.segment = segment;
     this.context = context;
     this.Name    = name;
 }
コード例 #13
0
 public SnippetInputHandler(InsertionContext context)
     : base(context.TextArea)
 {
     this.context = context;
 }
コード例 #14
0
 /// <summary>
 /// Performs insertion of the snippet.
 /// </summary>
 public abstract void Insert(InsertionContext context);