Beispiel #1
0
        private bool InvokeZenCoding(DTE dte)
        {
            Span zenSpan = GetSyntaxSpan(out string syntax);

            if (zenSpan.IsEmpty || _view.Selection.SelectedSpans[0].Length > 0 || !IsValidTextBuffer())
            {
                return(false);
            }

            var    parser = new Parser();
            string result = parser.Parse(syntax, ZenType.HTML);

            if (!string.IsNullOrEmpty(result))
            {
                Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                {
                    using (var undo = _undoManager.TextBufferUndoHistory.CreateTransaction("ZenCoding"))
                    {
                        ITextSelection selection = UpdateTextBuffer(zenSpan, result);

                        Span newSpan = new Span(zenSpan.Start, selection.SelectedSpans[0].Length);

                        dte.ExecuteCommand("Edit.FormatSelection");
                        SetCaret(newSpan, false);

                        selection.Clear();
                        undo.Complete();
                    }
                }), DispatcherPriority.ApplicationIdle, null);

                return(true);
            }

            return(false);
        }
        internal override void Execute(EmacsCommandContext context)
        {
            ITextSelection selection = context.TextView.Selection;
            ClipboardRing  ring      = context.Manager.ClipboardRing;

            if (!ring.IsEmpty)
            {
                ITrackingSpan preInsertionSelectionSpan = null;

                if (!selection.IsEmpty)
                {
                    // Currently only support stream selection
                    if (selection.Mode == TextSelectionMode.Stream)
                    {
                        preInsertionSelectionSpan = context.TextView.TextSnapshot.CreateTrackingSpan(selection.StreamSelectionSpan.SnapshotSpan, SpanTrackingMode.EdgeInclusive);
                    }
                    else
                    {
                        selection.Clear();
                    }
                }
                else
                {
                    preInsertionSelectionSpan = context.TextView.TextSnapshot.CreateTrackingSpan(new Span(selection.AnchorPoint.Position, 0), SpanTrackingMode.EdgeInclusive);
                }

                context.EditorOperations.ReplaceSelection(ring.GetNext());

                // Select newly inserted text
                SnapshotSpan newSelectionRange = preInsertionSelectionSpan.GetSpan(context.TextView.TextSnapshot);
                context.EditorOperations.SelectAndMoveCaret(new VirtualSnapshotPoint(newSelectionRange.Start), new VirtualSnapshotPoint(newSelectionRange.End));
            }
        }
Beispiel #3
0
        private bool InvokeZenCoding()
        {
            Span zenSpan = GetText();

            if (zenSpan.Length == 0 || TextView.Selection.SelectedSpans[0].Length > 0 || !IsValidTextBuffer())
            {
                return(false);
            }

            string zenSyntax = TextView.TextBuffer.CurrentSnapshot.GetText(zenSpan);

            Parser parser = new Parser();
            string result = parser.Parse(zenSyntax, ZenType.HTML);

            if (!string.IsNullOrEmpty(result))
            {
                EditorExtensionsPackage.DTE.UndoContext.Open("ZenCoding");

                ITextSelection selection = UpdateTextBuffer(zenSpan, result);
                Span           newSpan   = new Span(zenSpan.Start, selection.SelectedSpans[0].Length);

                selection.Clear();

                EditorExtensionsPackage.DTE.UndoContext.Close();

                Dispatcher.CurrentDispatcher.BeginInvoke(
                    new Action(() => SetCaret(newSpan, false)), DispatcherPriority.Normal, null);

                return(true);
            }

            return(false);
        }
Beispiel #4
0
        private bool InvokeZenCoding()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            Span zenSpan = GetSyntaxSpan(out string syntax);

            if (zenSpan.IsEmpty || _view.Selection.SelectedSpans[0].Length > 0 || !IsValidTextBuffer())
            {
                return(false);
            }

            var    parser = new Parser();
            string result = parser.Parse(syntax, ZenType.HTML);

            if (!string.IsNullOrEmpty(result))
            {
                using (ITextUndoTransaction undo = _undoManager.TextBufferUndoHistory.CreateTransaction("ZenCoding"))
                {
                    ITextSelection selection = UpdateTextBuffer(zenSpan, result);

                    var newSpan = new Span(zenSpan.Start, selection.SelectedSpans[0].Length);

                    _dte.ExecuteCommand("Edit.FormatSelection");
                    SetCaret(newSpan, false);

                    selection.Clear();
                    undo.Complete();
                }

                return(true);
            }

            return(false);
        }
Beispiel #5
0
        private bool InvokeZenCoding()
        {
            Span zenSpan = GetText();

            if (zenSpan.Length == 0 || TextView.Selection.SelectedSpans[0].Length > 0 || !IsValidTextBuffer())
            {
                return(false);
            }

            string zenSyntax = TextView.TextBuffer.CurrentSnapshot.GetText(zenSpan);

            string result = XamlParser.Parse(zenSyntax);

            if (!string.IsNullOrEmpty(result))
            {
                Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                {
                    ITextSelection selection = UpdateTextBuffer(zenSpan, result);

                    Span newSpan = new Span(zenSpan.Start, selection.SelectedSpans[0].Length);

                    vsXenPackage.ExecuteCommand("Edit.FormatSelection");
                    SetCaret(newSpan, false);

                    selection.Clear();
                }), DispatcherPriority.ApplicationIdle, null);

                return(true);
            }

            return(false);
        }
Beispiel #6
0
 public static void Select(this ITextSelection selection, params SnapshotSpan[] spans)
 {
     if (spans.Length == 1)
     {
         selection.Mode = TextSelectionMode.Stream;
         selection.Clear();
         selection.Select(spans[0], false);
     }
     else
     {
         selection.Mode = TextSelectionMode.Box;
         foreach (var span in spans)
         {
             selection.Select(span, false);
         }
     }
 }
Beispiel #7
0
        /// <summary>
        /// Starts tab stops navigation inside currently selected text.
        /// </summary>
        /// <param name="originalPosition">
        /// Original position of the caret before generated markup was pasted.
        /// </param>
        internal void PostProcessSelection(int originalPosition)
        {
            ITextSelection selection = _view.Selection;

            if (selection.SelectedSpans.Count == 0)
            {
                _trackingSpan = null;
                return;
            }

            SnapshotSpan selectionSpan = selection.SelectedSpans[0];
            Span         insertedText  = new Span(originalPosition, selectionSpan.Length);

            _trackingSpan = _view.TextBuffer.CurrentSnapshot.CreateTrackingSpan(
                insertedText, SpanTrackingMode.EdgeExclusive);

            selection.Clear();
            Dispatcher.CurrentDispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                new Action(() => SetCaret(insertedText, false)));
        }
Beispiel #8
0
        /// <summary>
        /// 调用ZenCoding
        /// </summary>
        /// <returns></returns>
        private bool InvokeZenCoding()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            string syntax = string.Empty;
            //获取用户输入的标签
            Span zenSpan = GetSyntaxSpan(out syntax);

            if (zenSpan.IsEmpty || _view.Selection.SelectedSpans[0].Length > 0 || !IsValidTextBuffer())
            {
                return(false);
            }

            ////用户输入html语法标签
            //string syntax1 = _view.TextBuffer.CurrentSnapshot.GetText(zenSpan);

            var    parser = new Parser();
            string result = parser.Parse(syntax, ZenType.HTML);

            if (!string.IsNullOrEmpty(result))
            {
                //撤销事务使用
                using (ITextUndoTransaction undo = _undoManager.TextBufferUndoHistory.CreateTransaction("ZenCoding"))
                {
                    ITextSelection selection = UpdateTextBuffer(zenSpan, result);

                    var newSpan = new Span(zenSpan.Start, selection.SelectedSpans[0].Length);
                    //选区内容格式化命令
                    EditorExtensions.EditorExtensionsPackage.DTE.ExecuteCommand("Edit.FormatSelection");
                    SetCaret(newSpan, false);

                    selection.Clear();
                    undo.Complete();
                }

                return(true);
            }

            return(false);
        }
Beispiel #9
0
        internal override void Execute(EmacsCommandContext context)
        {
            ITextSelection selection         = context.TextView.Selection;
            bool           trackCaret        = true;
            bool           markSessionActive = context.MarkSession.IsActive;

            // Return immediately if the buffer is read-only.
            if (context.TextBuffer.IsReadOnly(selection.Start.Position.GetContainingLine().Extent))
            {
                return;
            }

            // If there's not a multi-line selection, then clear it and setup for line indentation
            if (!selection.IsEmpty)
            {
                if (selection.Mode == TextSelectionMode.Box)
                {
                    return;
                }
                else
                {
                    VirtualSnapshotSpan selectionSpan = selection.StreamSelectionSpan;

                    if (selectionSpan.Start.Position.GetContainingLine().LineNumber != selectionSpan.End.Position.GetContainingLine().LineNumber)
                    {
                        return;
                    }

                    selection.Clear();

                    // Since there was a selection on the line before the format, we are not obligated to place the caret at a specific place
                    // after the format operation is done
                    trackCaret = false;
                }
            }

            // Strip any existing whitespace to setup the line for formatting
            this.StripWhiteSpace(context.TextView.GetCaretPosition().GetContainingLine());

            int?indentation = context.Manager.SmartIndentationService.GetDesiredIndentation(context.TextView, context.TextView.GetCaretPosition().GetContainingLine());

            if (indentation.HasValue)
            {
                // Insert the desired indentation level
                context.TextBuffer.Insert(context.TextView.GetCaretPosition().GetContainingLine().Start, new string(' ', indentation.Value));

                // Finally, are any tab/spaces conversions necessary?
                if (!context.TextView.Options.IsConvertTabsToSpacesEnabled())
                {
                    context.EditorOperations.ConvertSpacesToTabs();
                }
            }
            else
            {
                // We couldn't find any indentation level for the line, try executing the format command as the last resort

                // Remember caret position
                int caretOffsetFromStart = 0;

                if (trackCaret)
                {
                    CaretPosition positionBeforeChange = context.TextView.Caret.Position;
                    context.EditorOperations.MoveToStartOfLineAfterWhiteSpace(false);
                    caretOffsetFromStart = positionBeforeChange.BufferPosition.Position - context.TextView.GetCaretPosition();
                }

                // Format
                context.EditorOperations.SelectAndMoveCaret(
                    new VirtualSnapshotPoint(context.TextView.GetCaretPosition().GetContainingLine().Start, 0),
                    new VirtualSnapshotPoint(context.TextView.GetCaretPosition().GetContainingLine().End, 0));

                context.CommandRouter.ExecuteDTECommand("Edit.FormatSelection");

                // Move to beginning of newly indented line after format operation is done
                context.EditorOperations.MoveToStartOfLineAfterWhiteSpace(false);

                // Restore the position of the caret
                if (caretOffsetFromStart > 0)
                {
                    context.EditorOperations.MoveCaret(context.TextView.Caret.Position.BufferPosition + caretOffsetFromStart, false);
                }
            }

            // Ensure we restore the state of the mark session after the formatting operation (changing selection activates
            // the mark session automatically and we change the selection during our formatting operation).
            if (!markSessionActive)
            {
                context.MarkSession.Deactivate();
            }
        }
Beispiel #10
0
        internal override void Execute(EmacsCommandContext context)
        {
            ITextSelection selection = context.TextView.Selection;
            bool           flag      = true;
            bool           isActive  = context.MarkSession.IsActive;

            if (context.TextBuffer.IsReadOnly(selection.Start.Position.GetContainingLine().Extent))
            {
                return;
            }
            if (!selection.IsEmpty)
            {
                if (selection.Mode == TextSelectionMode.Box)
                {
                    return;
                }
                VirtualSnapshotSpan streamSelectionSpan = selection.StreamSelectionSpan;
                if (streamSelectionSpan.Start.Position.GetContainingLine().LineNumber !=
                    streamSelectionSpan.End.Position.GetContainingLine().LineNumber)
                {
                    return;
                }
                selection.Clear();
                flag = false;
            }
            StripWhiteSpace(context.TextView.GetCaretPosition().GetContainingLine());
            int?desiredIndentation = context.Manager.SmartIndentationService.GetDesiredIndentation(context.TextView,
                                                                                                   context.TextView.GetCaretPosition().GetContainingLine());

            if (desiredIndentation.HasValue)
            {
                context.TextBuffer.Insert(context.TextView.GetCaretPosition().GetContainingLine().Start,
                                          new string(' ', desiredIndentation.Value));
                if (!context.TextView.Options.IsConvertTabsToSpacesEnabled())
                {
                    context.EditorOperations.ConvertSpacesToTabs();
                }
            }
            else
            {
                int num = 0;
                if (flag)
                {
                    CaretPosition position = context.TextView.Caret.Position;
                    context.EditorOperations.MoveToStartOfLineAfterWhiteSpace(false);
                    num = position.BufferPosition.Position - context.TextView.GetCaretPosition();
                }
                context.EditorOperations.SelectAndMoveCaret(
                    new VirtualSnapshotPoint(context.TextView.GetCaretPosition().GetContainingLine().Start, 0),
                    new VirtualSnapshotPoint(context.TextView.GetCaretPosition().GetContainingLine().End, 0));
                context.CommandRouter.ExecuteDTECommand("Edit.FormatSelection");
                context.EditorOperations.MoveToStartOfLineAfterWhiteSpace(false);
                if (num > 0)
                {
                    context.EditorOperations.MoveCaret(context.TextView.Caret.Position.BufferPosition + num, false);
                }
            }
            if (isActive)
            {
                return;
            }
            context.MarkSession.Deactivate(true);
        }
Beispiel #11
0
        /// <summary>
        /// Indicates that the drag and drop operation has completed, and that the final tasks, if any, should be performed now.
        /// </summary>
        /// <param name="dragDropInfo">
        /// Information about the drag and drop operation in progress.
        /// </param>
        /// <returns>
        /// The drag and drop effects of this drop operation. For example, if the drop operation has moved data,
        /// DragDropPointerEffects.Move should be returned.
        /// </returns>
        /// <remarks>This method is called when the user drops the data onto the editor.
        /// This marks the end of a drag and drop operation.
        /// The <see cref="IDropHandler"/> is expected to perform the final tasks of the operation.
        /// </remarks>
        public virtual DragDropPointerEffects HandleDataDropped(DragDropInfo dragDropInfo)
        {
            if (dragDropInfo == null)
            {
                throw new ArgumentNullException(nameof(dragDropInfo));
            }

            ITextSelection selection = _cocoaTextView.Selection;
            //keeps track of the result of this operation
            DragDropPointerEffects result = DragDropPointerEffects.None;
            //tracks the location at which the data was dropped
            VirtualSnapshotPoint dropLocation = dragDropInfo.VirtualBufferPosition;
            //convert the drag/drop data to text
            string dragDropText  = this.ExtractText(dragDropInfo);
            bool   isReversed    = selection.IsReversed;
            bool   copyRequested = (dragDropInfo.KeyStates & NSEventModifierMask.AlternateKeyMask) == NSEventModifierMask.AlternateKeyMask;
            bool   copyAllowed   = (dragDropInfo.AllowedEffects & NSDragOperation.Copy) == NSDragOperation.Copy;

            ITextSnapshot preEditSnapshot = _cocoaTextView.TextSnapshot;

            // track the point where the data will be inserted
            ITrackingPoint insertionPoint = preEditSnapshot.CreateTrackingPoint(dropLocation.Position, PointTrackingMode.Negative);

            // track the currently selected spans before any edits are performed on the buffer
            List <ITrackingSpan> selectionSpans = new List <ITrackingSpan>();

            foreach (SnapshotSpan selectedSpan in selection.SelectedSpans)
            {
                selectionSpans.Add(preEditSnapshot.CreateTrackingSpan(selectedSpan, SpanTrackingMode.EdgeExclusive));
            }

            // perform any necessary pre edit actions
            this.PerformPreEditActions(dragDropInfo);

            // clear selection before data operations
            if (!selection.IsEmpty)
            {
                selection.Clear();
            }

            // a reference to the snapshot resulting from the edits
            bool successfulEdit = false;

            // if the data is being dropped in virtual space, calculate how many whitespace characters will be inserted
            // to fill the gap between the dropped point and the closest buffer position
            int virtualSpaceLength = 0;

            if (dragDropInfo.VirtualBufferPosition.IsInVirtualSpace)
            {
                virtualSpaceLength = _editorOperations.GetWhitespaceForVirtualSpace(dragDropInfo.VirtualBufferPosition).Length;
            }

            if (copyRequested && copyAllowed)
            {
                //copy the data by inserting it in the buffer
                successfulEdit = this.InsertText(dropLocation, dragDropText);
                if (successfulEdit)
                {
                    result = DragDropPointerEffects.Copy;
                }
            }
            else
            {
                //the data needs to be moved
                if (dragDropInfo.IsInternal)
                {
                    //delete the existing selection, and add the data to the new location
                    successfulEdit = this.MoveText(dropLocation, selectionSpans, dragDropText);
                }
                else
                {
                    //the drag is not from this text view, just insert the data at dropLocation
                    successfulEdit = this.InsertText(dropLocation, dragDropText);
                }

                //set the pointer effect to move if the edit was successful since that implies that the data was moved successfully
                if (successfulEdit)
                {
                    result = DragDropPointerEffects.Move;
                }
            }

            // finally select the newly inserted data if the operation was successful
            if (result != DragDropPointerEffects.None)
            {
                SnapshotPoint textInsertionPoint = insertionPoint.GetPoint(_cocoaTextView.TextSnapshot);

                // if the data was inserted in virtual space, offset the selection's anchor point by the whitespace that was inserted
                // in virtual space
                if (virtualSpaceLength != 0)
                {
                    textInsertionPoint = textInsertionPoint.Add(virtualSpaceLength);
                }

                this.SelectText(textInsertionPoint, dragDropText.Length, dragDropInfo, isReversed);
            }

            // perform any post edit actions as necessary
            this.PerformPostEditActions(dragDropInfo, successfulEdit);

            return(result);
        }