Ejemplo n.º 1
0
        void TextBuffer_ChangedHighPriority(object sender, TextContentChangedEventArgs e)
        {
            var newAnchorPoint = anchorPoint.TranslateTo(TextView.TextSnapshot);
            var newActivePoint = activePoint.TranslateTo(TextView.TextSnapshot);

            Select(newAnchorPoint, newActivePoint);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Event Handler: Text View Layout Changed Event
        /// </summary>
        internal void LayoutChanged(ITextSnapshot oldSnapshot, ITextSnapshot newSnapshot)
        {
            Debug.Assert(_insertionPoint.Position.Snapshot == oldSnapshot);

            if (oldSnapshot != newSnapshot)
            {
                _insertionPoint = _insertionPoint.TranslateTo(newSnapshot);
            }

            this.UpdateBlinkTimer();

            //This should not cause a caret moved event to be raised but it will update position, bounds & overwrite mode.
            bool textChanges = AnyTextChanges(oldSnapshot.Version, newSnapshot.Version);

            this.RefreshCaret(textChanges);

            if (textChanges && _wpfTextView.Options.IsAutoScrollEnabled())
            {
                if (_insertionPoint.Position.GetContainingLine().LineNumber == newSnapshot.LineCount - 1)
                {
                    _wpfTextView.BeginInvokeOnMainThread(delegate
                    {
                        this.EnsureVisible();
                    });
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Inserts some textual data at the given position.
        /// </summary>
        /// <param name="position">Position at which the data is to be inserted</param>
        /// <param name="data">Text to be inserted</param>
        ///
        /// <returns>True if data insertion was successful, false otherwise.</returns>
        protected virtual bool InsertText(VirtualSnapshotPoint position, string data)
        {
            // move the caret to the place where data needs to be inserted
            _cocoaTextView.Caret.MoveTo(position.TranslateTo(this.TextView.TextSnapshot));

            return(_editorOperations.InsertText(data));
        }
Ejemplo n.º 4
0
        internal static void MoveCaret(this IEditorOperations editorOperations, SnapshotPoint bufferPosition, bool extendSelection)
        {
            if (extendSelection)
            {
                VirtualSnapshotPoint anchorPoint = editorOperations.TextView.Selection.AnchorPoint;
                editorOperations.TextView.Caret.MoveTo(bufferPosition);
                editorOperations.TextView.Selection.Select(anchorPoint.TranslateTo(editorOperations.TextView.TextSnapshot), editorOperations.TextView.Caret.Position.VirtualBufferPosition);
            }
            else
            {
                editorOperations.TextView.Selection.Clear();
                editorOperations.TextView.Caret.MoveTo(bufferPosition);
            }

            editorOperations.TextView.Caret.EnsureVisible();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Move the caret and restore the selection as part of the Redo operation.
        /// </summary>
        protected override void DoMoveCaretAndSelect(ITextView view, IMapEditToData map)
        {
            SnapshotPoint newCaret  = new SnapshotPoint(view.TextSnapshot, BeforeTextBufferChangeUndoPrimitive.MapToEdit(map, _newCaretIndex));
            SnapshotPoint newAnchor = new SnapshotPoint(view.TextSnapshot, BeforeTextBufferChangeUndoPrimitive.MapToEdit(map, _newSelectionAnchorIndex));
            SnapshotPoint newActive = new SnapshotPoint(view.TextSnapshot, BeforeTextBufferChangeUndoPrimitive.MapToEdit(map, _newSelectionActiveIndex));

            view.Caret.MoveTo(new VirtualSnapshotPoint(newCaret, _newCaretVirtualSpaces), (PositionAffinity)_newCaretAffinityByte);

            view.Selection.Mode = _newSelectionMode;

            var virtualAnchor = new VirtualSnapshotPoint(newAnchor, _newSelectionAnchorVirtualSpaces);
            var virtualActive = new VirtualSnapshotPoint(newActive, _newSelectionActiveVirtualSpaces);

            // Buffer may have been changed by one of the listeners on the caret move event.
            virtualAnchor = virtualAnchor.TranslateTo(view.TextSnapshot);
            virtualActive = virtualActive.TranslateTo(view.TextSnapshot);

            view.Selection.Select(virtualAnchor, virtualActive);
        }
 /// <summary>
 /// Update the selection based on the movement of the caret.
 /// </summary>
 private void UpdateSelection(bool extendSelection, VirtualSnapshotPoint oldPosition)
 {
     if (extendSelection)
     {
         if (TextView.Selection.IsEmpty)
         {
             //The provided old position might be stale
             this.TextView.AdvancedTextView.Selection.Select(oldPosition.TranslateTo(this.TextView.AdvancedTextView.TextSnapshot),
                                                             this.AdvancedCaret.Position.VirtualBufferPosition);
         }
         else
         {
             this.TextView.AdvancedTextView.Selection.Select(this.TextView.AdvancedTextView.Selection.AnchorPoint,
                                                             this.AdvancedCaret.Position.VirtualBufferPosition);
         }
     }
     else
     {
         TextView.AdvancedTextView.Selection.Clear();
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Moves the data from one location to another in the buffer by deleting the selection contents and inserting toInsert in insertionPoint.
        /// </summary>
        /// <param name="data">Text to be inserted</param>
        /// <param name="position">Position at which the data is to be inserted</param>
        /// <param name="selectionSpans">A list of <see cref="ITrackingSpan"/> tracking the selection of the user before the drop operation. This span collection should be deleted from the buffer</param>
        /// <returns>True if data insertion and removal was successful, false otherwise.</returns>
        protected virtual bool MoveText(VirtualSnapshotPoint position, IList <ITrackingSpan> selectionSpans, string data)
        {
            ITextSnapshot textSnapshot = _cocoaTextView.TextSnapshot;

            // update position to the latest snapshot
            position = position.TranslateTo(textSnapshot);

            // keep track of where the data needs to be inserted
            ITrackingPoint insertionLocation = textSnapshot.CreateTrackingPoint(position.Position, PointTrackingMode.Negative);

            // delete the selection
            if (!this.DeleteSpans(selectionSpans))
            {
                return(false);
            }

            // move the caret to the data insertion point
            _cocoaTextView.Caret.MoveTo(new VirtualSnapshotPoint(insertionLocation.GetPoint(this.TextView.TextSnapshot), position.VirtualSpaces));

            // finally insert the data
            return(_editorOperations.InsertText(data));
        }
Ejemplo n.º 8
0
 void OnImplicitCaretPositionChanged() => SetPositionCore(currentPosition.TranslateTo(textView.TextSnapshot));
Ejemplo n.º 9
0
        protected override int InternalExec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            int hr = OLECMDERR_E_NOTSUPPORTED;

            if (!WpfConsole.Host.IsCommandEnabled)
            {
                return(hr);
            }

            // if the console has not been successfully started, do not accept any key inputs
            if (!WpfConsole.Dispatcher.IsStartCompleted)
            {
                return(hr);
            }

            // if the console is in the middle of executing a command, do not accept any key inputs
            if (WpfConsole.Dispatcher.IsExecutingCommand)
            {
                return(hr);
            }

            if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
            {
                //Debug.Print("Exec: GUID_VSStandardCommandSet97: {0}", (VSConstants.VSStd97CmdID)nCmdID);

                switch ((VSConstants.VSStd97CmdID)nCmdID)
                {
                case VSConstants.VSStd97CmdID.Paste:
                    if (IsCaretInReadOnlyRegion || IsSelectionReadonly)
                    {
                        hr = VSConstants.S_OK;     // eat it
                    }
                    else
                    {
                        PasteText(ref hr);
                    }
                    break;
                }
            }
            else if (pguidCmdGroup == VSConstants.VSStd2K)
            {
                //Debug.Print("Exec: VSStd2K: {0}", (VSConstants.VSStd2KCmdID)nCmdID);

                switch ((VSConstants.VSStd2KCmdID)nCmdID)
                {
                case VSConstants.VSStd2KCmdID.TYPECHAR:
                    if (IsCompletionSessionActive)
                    {
                        char ch = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
                        if (IsCommitChar(ch))
                        {
                            if (_completionSession.SelectedCompletionSet.SelectionStatus.IsSelected)
                            {
                                _completionSession.Commit();
                            }
                            else
                            {
                                _completionSession.Dismiss();
                            }
                        }
                    }
                    else
                    {
                        if (IsSelectionReadonly)
                        {
                            WpfTextView.Selection.Clear();
                        }
                        if (IsCaretInReadOnlyRegion)
                        {
                            WpfTextView.Caret.MoveTo(WpfConsole.InputLineExtent.End);
                        }
                    }
                    break;

                case VSConstants.VSStd2KCmdID.LEFT:
                case VSConstants.VSStd2KCmdID.LEFT_EXT:
                case VSConstants.VSStd2KCmdID.LEFT_EXT_COL:
                case VSConstants.VSStd2KCmdID.WORDPREV:
                case VSConstants.VSStd2KCmdID.WORDPREV_EXT:
                case VSConstants.VSStd2KCmdID.WORDPREV_EXT_COL:
                    if (IsCaretAtInputLineStart)
                    {
                        //
                        // Note: This simple implementation depends on Prompt containing a trailing space.
                        // When caret is on the right of InputLineStart, editor will handle it correctly,
                        // and caret won't move left to InputLineStart because of the trailing space.
                        //
                        hr = VSConstants.S_OK;     // eat it
                    }
                    break;

                case VSConstants.VSStd2KCmdID.BOL:
                case VSConstants.VSStd2KCmdID.BOL_EXT:
                case VSConstants.VSStd2KCmdID.BOL_EXT_COL:
                    if (IsCaretOnInputLine)
                    {
                        VirtualSnapshotPoint oldCaretPoint = WpfTextView.Caret.Position.VirtualBufferPosition;

                        WpfTextView.Caret.MoveTo(WpfConsole.InputLineStart.Value);
                        WpfTextView.Caret.EnsureVisible();

                        if ((VSConstants.VSStd2KCmdID)nCmdID == VSConstants.VSStd2KCmdID.BOL)
                        {
                            WpfTextView.Selection.Clear();
                        }
                        else if ((VSConstants.VSStd2KCmdID)nCmdID != VSConstants.VSStd2KCmdID.BOL)     // extend selection
                        {
                            VirtualSnapshotPoint anchorPoint = WpfTextView.Selection.IsEmpty ?
                                                               oldCaretPoint.TranslateTo(WpfTextView.TextSnapshot) : WpfTextView.Selection.AnchorPoint;
                            WpfTextView.Selection.Select(anchorPoint, WpfTextView.Caret.Position.VirtualBufferPosition);
                        }

                        hr = VSConstants.S_OK;
                    }
                    break;

                case VSConstants.VSStd2KCmdID.UP:
                    if (!IsCompletionSessionActive)
                    {
                        if (IsCaretInReadOnlyRegion)
                        {
                            ExecuteCommand(VSConstants.VSStd2KCmdID.END);
                        }
                        WpfConsole.NavigateHistory(-1);
                        hr = VSConstants.S_OK;
                    }
                    break;

                case VSConstants.VSStd2KCmdID.DOWN:
                    if (!IsCompletionSessionActive)
                    {
                        if (IsCaretInReadOnlyRegion)
                        {
                            ExecuteCommand(VSConstants.VSStd2KCmdID.END);
                        }
                        WpfConsole.NavigateHistory(+1);
                        hr = VSConstants.S_OK;
                    }
                    break;

                case VSConstants.VSStd2KCmdID.RETURN:
                    if (IsCompletionSessionActive)
                    {
                        if (_completionSession.SelectedCompletionSet.SelectionStatus.IsSelected)
                        {
                            _completionSession.Commit();
                        }
                        else
                        {
                            _completionSession.Dismiss();
                        }
                    }
                    else if (IsCaretOnInputLine || !IsCaretInReadOnlyRegion)
                    {
                        ExecuteCommand(VSConstants.VSStd2KCmdID.END);
                        ExecuteCommand(VSConstants.VSStd2KCmdID.RETURN);

                        WpfConsole.EndInputLine();
                    }
                    hr = VSConstants.S_OK;
                    break;

                case VSConstants.VSStd2KCmdID.TAB:
                    if (!IsCaretInReadOnlyRegion)
                    {
                        if (IsCompletionSessionActive)
                        {
                            _completionSession.Commit();
                        }
                        else
                        {
                            TriggerCompletion();
                        }
                    }
                    hr = VSConstants.S_OK;
                    break;

                case VSConstants.VSStd2KCmdID.CANCEL:
                    if (IsCompletionSessionActive)
                    {
                        _completionSession.Dismiss();
                        hr = VSConstants.S_OK;
                    }
                    else if (!IsCaretInReadOnlyRegion)
                    {
                        // Delete all text after InputLineStart
                        WpfTextView.TextBuffer.Delete(WpfConsole.AllInputExtent);
                        hr = VSConstants.S_OK;
                    }
                    break;

                case VSConstants.VSStd2KCmdID.CUTLINE:
                    // clears the console when CutLine shortcut key is pressed,
                    // usually it is Ctrl + L
                    WpfConsole.ClearConsole();
                    hr = VSConstants.S_OK;
                    break;
                }
            }

            return(hr);
        }
Ejemplo n.º 10
0
        public override CommandResult Invoke(Guid group, int id, object inputArg, ref object outputArg)
        {
            if (_completionBroker.IsCompletionActive(TextView))
            {
                return(CommandResult.NotSupported);
            }
            var window   = _interactiveWorkflow.ActiveWindow;
            var curPoint = window.TextView.MapDownToBuffer(
                window.TextView.Caret.Position.BufferPosition,
                window.CurrentLanguageBuffer
                );

            bool extend = id == (int)VSConstants.VSStd2KCmdID.UP_EXT ||
                          id == (int)VSConstants.VSStd2KCmdID.DOWN_EXT ||
                          id == (int)VSConstants.VSStd2KCmdID.RIGHT_EXT ||
                          id == (int)VSConstants.VSStd2KCmdID.LEFT_EXT;

            var editorOps = _editorFactory.GetEditorOperations(TextView);

            if (curPoint != null)
            {
                var curLine = curPoint.Value.GetContainingLine();
                switch ((VSConstants.VSStd2KCmdID)id)
                {
                // history navigates if we're on the top or bottom line
                case VSConstants.VSStd2KCmdID.UP:
                    if (curLine.LineNumber == 0)
                    {
                        // this leaves the caret at the end which is what we want for up/down to work nicely
                        _history.PreviousEntry();
                        return(CommandResult.Executed);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.DOWN:
                    if (curLine.LineNumber == curPoint.Value.Snapshot.LineCount - 1)
                    {
                        _history.NextEntry();

                        // move the caret to the 1st line in history so down/up works nicely
                        var firstLine  = window.CurrentLanguageBuffer.CurrentSnapshot.GetLineFromLineNumber(0);
                        var upperPoint = MapUp(window, firstLine.Start);
                        window.TextView.Caret.MoveTo(upperPoint.Value);
                        return(CommandResult.Executed);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.LEFT:
                case VSConstants.VSStd2KCmdID.LEFT_EXT:
                    if (curLine.Start == curPoint.Value)
                    {
                        if (curLine.LineNumber != 0)
                        {
                            // move to the end of the previous line rather then navigating into the prompts
                            editorOps.MoveLineUp(extend);
                            window.InteractiveWindow.Operations.End(extend);
                        }
                        return(CommandResult.Executed);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.RIGHT:
                case VSConstants.VSStd2KCmdID.RIGHT_EXT:
                    if (curLine.End == curPoint.Value)
                    {
                        if (curLine.LineNumber != curPoint.Value.Snapshot.LineCount - 1)
                        {
                            // move to the beginning of the next line rather then navigating into the prompts
                            editorOps.MoveLineDown(extend);

                            curPoint = window.TextView.MapDownToBuffer(
                                window.TextView.Caret.Position.BufferPosition,
                                window.CurrentLanguageBuffer
                                );
                            Debug.Assert(curPoint != null);
                            var start = curPoint.Value.GetContainingLine().Start;

                            // Home would be nice here, but it goes to the beginning of the first non-whitespace char
                            if (extend)
                            {
                                VirtualSnapshotPoint anchor = TextView.Selection.AnchorPoint;
                                TextView.Caret.MoveTo(MapUp(window, start).Value);
                                TextView.Selection.Select(anchor.TranslateTo(TextView.TextSnapshot), TextView.Caret.Position.VirtualBufferPosition);
                            }
                            else
                            {
                                window.TextView.Caret.MoveTo(MapUp(window, start).Value);
                            }
                        }
                        return(CommandResult.Executed);
                    }
                    break;
                }
            }

            // if we're anywhere outside of the current language buffer then let the normal
            // editor behavior continue.
            switch ((VSConstants.VSStd2KCmdID)id)
            {
            case VSConstants.VSStd2KCmdID.UP:
            case VSConstants.VSStd2KCmdID.UP_EXT:
                editorOps.MoveLineUp(extend);
                break;

            case VSConstants.VSStd2KCmdID.DOWN:
            case VSConstants.VSStd2KCmdID.DOWN_EXT:
                editorOps.MoveLineDown(extend);
                break;

            case VSConstants.VSStd2KCmdID.LEFT:
            case VSConstants.VSStd2KCmdID.LEFT_EXT:
                editorOps.MoveToPreviousCharacter(extend);
                break;

            case VSConstants.VSStd2KCmdID.RIGHT:
            case VSConstants.VSStd2KCmdID.RIGHT_EXT:
                editorOps.MoveToNextCharacter(extend);
                break;
            }

            return(CommandResult.Executed);
        }
Ejemplo n.º 11
0
		bool Indent(VirtualSnapshotPoint vpos) {
			bool isOverwrite = Caret.OverwriteMode;
			if (!Selection.IsEmpty) {
				using (var ed = TextBuffer.CreateEdit()) {
					foreach (var span in Selection.SelectedSpans) {
						if (!ed.Delete(span))
							return false;
					}
					ed.Apply();
					if (ed.Canceled)
						return false;
				}
				vpos = vpos.TranslateTo(Snapshot, PointTrackingMode.Positive);
				Selection.Clear();
			}
			using (var ed = TextBuffer.CreateEdit()) {
				if (!IndentLine(ed, vpos, isOverwrite))
					return false;

				ed.Apply();
				if (ed.Canceled)
					return false;
			}
			Caret.MoveTo(vpos.Position.TranslateTo(Snapshot, PointTrackingMode.Positive));
			Caret.EnsureVisible();
			return true;
		}
Ejemplo n.º 12
0
		public void SelectAndMoveCaret(VirtualSnapshotPoint anchorPoint, VirtualSnapshotPoint activePoint, TextSelectionMode selectionMode, EnsureSpanVisibleOptions? scrollOptions) {
			anchorPoint = anchorPoint.TranslateTo(Snapshot);
			activePoint = activePoint.TranslateTo(Snapshot);
			if (anchorPoint == activePoint)
				Selection.Clear();
			else
				Selection.Select(anchorPoint, activePoint);
			Selection.Mode = selectionMode;
			activePoint = activePoint.TranslateTo(Snapshot);
			Caret.MoveTo(activePoint);
			if (scrollOptions == null)
				return;
			anchorPoint = anchorPoint.TranslateTo(Snapshot);
			activePoint = activePoint.TranslateTo(Snapshot);
			if (activePoint > anchorPoint)
				ViewScroller.EnsureSpanVisible(new SnapshotSpan(anchorPoint.Position, activePoint.Position), scrollOptions.Value & ~EnsureSpanVisibleOptions.ShowStart);
			else
				ViewScroller.EnsureSpanVisible(new SnapshotSpan(activePoint.Position, anchorPoint.Position), scrollOptions.Value | EnsureSpanVisibleOptions.ShowStart);
		}
Ejemplo n.º 13
0
        protected override int InternalExec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            int hr = OLECMDERR_E_NOTSUPPORTED;

            if (WpfConsole == null ||
                WpfConsole.Host == null ||
                WpfConsole.Dispatcher == null)
            {
                return(hr);
            }

            if (!WpfConsole.Host.IsCommandEnabled)
            {
                return(hr);
            }

            if (!WpfConsole.Dispatcher.IsExecutingReadKey)
            {
                // if the console has not been successfully started, do not accept any key inputs, unless
                // we are in the middle of a ReadKey call. This happens when the execution group policy setting
                // is set to AllSigned, and PS is asking user to trust the certificate.
                if (!WpfConsole.Dispatcher.IsStartCompleted)
                {
                    return(hr);
                }

                // if the console is in the middle of executing a command, do not accept any key inputs unless
                // we are in the middle of a ReadKey call.
                if (WpfConsole.Dispatcher.IsExecutingCommand)
                {
                    return(hr);
                }
            }

            if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
            {
                //Debug.Print("Exec: GUID_VSStandardCommandSet97: {0}", (VSConstants.VSStd97CmdID)nCmdID);

                switch ((VSConstants.VSStd97CmdID)nCmdID)
                {
                case VSConstants.VSStd97CmdID.Paste:
                    if (IsCaretInReadOnlyRegion || IsSelectionReadonly)
                    {
                        hr = VSConstants.S_OK;     // eat it
                    }
                    else
                    {
                        PasteText(ref hr);
                    }
                    break;
                }
            }
            else if (pguidCmdGroup == VSConstants.VSStd2K)
            {
                //Debug.Print("Exec: VSStd2K: {0}", (VSConstants.VSStd2KCmdID)nCmdID);

                var commandID = (VSConstants.VSStd2KCmdID)nCmdID;

                if (WpfConsole.Dispatcher.IsExecutingReadKey)
                {
                    switch (commandID)
                    {
                    case VSConstants.VSStd2KCmdID.TYPECHAR:
                    case VSConstants.VSStd2KCmdID.BACKSPACE:
                    case VSConstants.VSStd2KCmdID.RETURN:
                        var keyInfo = GetVsKeyInfo(pvaIn, commandID);
                        WpfConsole.Dispatcher.PostKey(keyInfo);
                        break;

                    case VSConstants.VSStd2KCmdID.CANCEL:     // Handle ESC
                        WpfConsole.Dispatcher.CancelWaitKey();
                        break;
                    }
                    hr = VSConstants.S_OK; // eat everything
                }
                else
                {
                    switch (commandID)
                    {
                    case VSConstants.VSStd2KCmdID.TYPECHAR:
                        if (IsCompletionSessionActive)
                        {
                            char ch = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
                            if (IsCommitChar(ch))
                            {
                                if (_completionSession.SelectedCompletionSet.SelectionStatus.IsSelected)
                                {
                                    _completionSession.Commit();
                                }
                                else
                                {
                                    _completionSession.Dismiss();
                                }
                            }
                        }
                        else
                        {
                            if (IsSelectionReadonly)
                            {
                                WpfTextView.Selection.Clear();
                            }
                            if (IsCaretInReadOnlyRegion)
                            {
                                WpfTextView.Caret.MoveTo(WpfConsole.InputLineExtent.End);
                            }
                        }
                        break;

                    case VSConstants.VSStd2KCmdID.LEFT:
                    case VSConstants.VSStd2KCmdID.LEFT_EXT:
                    case VSConstants.VSStd2KCmdID.LEFT_EXT_COL:
                    case VSConstants.VSStd2KCmdID.WORDPREV:
                    case VSConstants.VSStd2KCmdID.WORDPREV_EXT:
                    case VSConstants.VSStd2KCmdID.WORDPREV_EXT_COL:
                        if (IsCaretAtInputLineStart)
                        {
                            //
                            // Note: This simple implementation depends on Prompt containing a trailing space.
                            // When caret is on the right of InputLineStart, editor will handle it correctly,
                            // and caret won't move left to InputLineStart because of the trailing space.
                            //
                            hr = VSConstants.S_OK;     // eat it
                        }
                        break;

                    case VSConstants.VSStd2KCmdID.BOL:
                    case VSConstants.VSStd2KCmdID.BOL_EXT:
                    case VSConstants.VSStd2KCmdID.BOL_EXT_COL:
                        if (IsCaretOnInputLine)
                        {
                            VirtualSnapshotPoint oldCaretPoint = WpfTextView.Caret.Position.VirtualBufferPosition;

                            WpfTextView.Caret.MoveTo(WpfConsole.InputLineStart.Value);
                            WpfTextView.Caret.EnsureVisible();

                            if ((VSConstants.VSStd2KCmdID)nCmdID == VSConstants.VSStd2KCmdID.BOL)
                            {
                                WpfTextView.Selection.Clear();
                            }
                            else if ((VSConstants.VSStd2KCmdID)nCmdID != VSConstants.VSStd2KCmdID.BOL)
                            // extend selection
                            {
                                VirtualSnapshotPoint anchorPoint = WpfTextView.Selection.IsEmpty
                                        ? oldCaretPoint.TranslateTo(
                                    WpfTextView.TextSnapshot)
                                        : WpfTextView.Selection.AnchorPoint;
                                WpfTextView.Selection.Select(anchorPoint,
                                                             WpfTextView.Caret.Position.VirtualBufferPosition);
                            }

                            hr = VSConstants.S_OK;
                        }
                        break;

                    case VSConstants.VSStd2KCmdID.UP:
                        if (!IsCompletionSessionActive)
                        {
                            if (IsCaretInReadOnlyRegion)
                            {
                                ExecuteCommand(VSConstants.VSStd2KCmdID.END);
                            }
                            WpfConsole.NavigateHistory(-1);
                            hr = VSConstants.S_OK;
                        }
                        break;

                    case VSConstants.VSStd2KCmdID.DOWN:
                        if (!IsCompletionSessionActive)
                        {
                            if (IsCaretInReadOnlyRegion)
                            {
                                ExecuteCommand(VSConstants.VSStd2KCmdID.END);
                            }
                            WpfConsole.NavigateHistory(+1);
                            hr = VSConstants.S_OK;
                        }
                        break;

                    case VSConstants.VSStd2KCmdID.RETURN:
                        if (IsCompletionSessionActive)
                        {
                            if (_completionSession.SelectedCompletionSet.SelectionStatus.IsSelected)
                            {
                                _completionSession.Commit();
                            }
                            else
                            {
                                _completionSession.Dismiss();
                            }
                        }
                        else if (IsCaretOnInputLine || !IsCaretInReadOnlyRegion)
                        {
                            ExecuteCommand(VSConstants.VSStd2KCmdID.END);
                            ExecuteCommand(VSConstants.VSStd2KCmdID.RETURN);

                            NuGetUIThreadHelper.JoinableTaskFactory
                            .RunAsync(() => EndInputLineAsync(WpfConsole))
                            .PostOnFailure(nameof(WpfConsoleKeyProcessor));
                        }
                        hr = VSConstants.S_OK;
                        break;

                    case VSConstants.VSStd2KCmdID.TAB:
                        if (!IsCaretInReadOnlyRegion)
                        {
                            if (IsCompletionSessionActive)
                            {
                                _completionSession.Commit();
                            }
                            else
                            {
                                NuGetUIThreadHelper.JoinableTaskFactory.RunAsync(async delegate { await TriggerCompletionAsync(); })
                                .PostOnFailure(nameof(WpfConsoleKeyProcessor));
                            }
                        }
                        hr = VSConstants.S_OK;
                        break;

                    case VSConstants.VSStd2KCmdID.CANCEL:
                        if (IsCompletionSessionActive)
                        {
                            _completionSession.Dismiss();
                            hr = VSConstants.S_OK;
                        }
                        else if (!IsCaretInReadOnlyRegion)
                        {
                            // Delete all text after InputLineStart
                            WpfTextView.TextBuffer.Delete(WpfConsole.AllInputExtent);
                            hr = VSConstants.S_OK;
                        }
                        break;

                    case VSConstants.VSStd2KCmdID.CUTLINE:
                        // clears the console when CutLine shortcut key is pressed,
                        // usually it is Ctrl + L
                        WpfConsole.ClearConsole();
                        hr = VSConstants.S_OK;
                        break;
                    }
                }
            }
            return(hr);
        }
Ejemplo n.º 14
0
        // Helped by source of Microsoft.VisualStudio.Text.Editor.DragDrop.DropHandlerBase.cs in assembly Microsoft.VisualStudio.Text.UI.Wpf, Version=14.0.0.0
        public static int HandleCommand(IWpfTextView textView, IClassifier classifier, IOleCommandTarget commandTarget, IEditorOperations editorOperations, bool shiftPressed = false)
        {
            //Guid cmdGroup = VSConstants.VSStd2K;
            var            isSingleLine  = false;
            var            selectedText  = editorOperations.SelectedText;
            ITrackingPoint trackingPoint = null;

            if (selectedText.Length == 0)
            // if nothing is selected, we can consider the current line as a selection
            {
                var virtualBufferPosition = editorOperations.TextView.Caret.Position.VirtualBufferPosition;
                trackingPoint = textView.TextSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative);
                editorOperations.SelectLine(textView.Caret.ContainingTextViewLine, false);
                isSingleLine = true;
            }

            if (isSingleLine)
            {
                editorOperations.CopySelection();
                editorOperations.MoveToNextCharacter(false);
                editorOperations.Paste();
                editorOperations.MoveToPreviousCharacter(false);

                textView.Caret.MoveTo(new VirtualSnapshotPoint(trackingPoint.GetPoint(textView.TextSnapshot)).TranslateTo(textView.TextSnapshot));
                if (!shiftPressed)
                {
                    editorOperations.MoveLineDown(false);
                }
            }
            else
            {
                var selection    = textView.Selection;
                var isReversed   = selection.IsReversed;
                var text         = selectedText;
                var textSnapshot = textView.TextSnapshot;
                var list         = new List <ITrackingSpan>();
                //var shiftKeyPressed=textVie
                foreach (SnapshotSpan snapshotSpan in selection.SelectedSpans)
                {
                    list.Add(textSnapshot.CreateTrackingSpan(snapshotSpan, SpanTrackingMode.EdgeExclusive));
                }
                if (!selection.IsEmpty)
                {
                    selection.Clear();
                }


                if (list.Count < 2)
                {
                    var offset = 0;
                    var virtualBufferPosition = editorOperations.TextView.Caret.Position.VirtualBufferPosition;
                    var point = editorOperations.TextView.Caret.Position.BufferPosition;
                    virtualBufferPosition = isReversed && !shiftPressed ? new VirtualSnapshotPoint(point.Add(text.Length))
                       : !isReversed && shiftPressed ? new VirtualSnapshotPoint(point.Add(-text.Length)) : virtualBufferPosition;

                    trackingPoint = textSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative);
                    if (virtualBufferPosition.IsInVirtualSpace)
                    {
                        offset = editorOperations.GetWhitespaceForVirtualSpace(virtualBufferPosition).Length;
                    }
                    textView.Caret.MoveTo(virtualBufferPosition.TranslateTo(textView.TextSnapshot));
                    editorOperations.InsertText(text);
                    var insertionPoint = trackingPoint.GetPoint(textView.TextSnapshot);
                    if (offset != 0)
                    {
                        insertionPoint = insertionPoint.Add(offset);
                    }

                    var virtualSnapshotPoint1 = new VirtualSnapshotPoint(insertionPoint);
                    var virtualSnapshotPoint2 = new VirtualSnapshotPoint(insertionPoint.Add(text.Length));
                    if (isReversed)
                    {
                        editorOperations.SelectAndMoveCaret(virtualSnapshotPoint2, virtualSnapshotPoint1, TextSelectionMode.Stream);
                    }
                    else
                    {
                        editorOperations.SelectAndMoveCaret(virtualSnapshotPoint1, virtualSnapshotPoint2, TextSelectionMode.Stream);
                    }
                }
                else
                {
                    var trackingPointOffsetList = new List <Tuple <ITrackingPoint, int, int> >();
                    //Insert Text!
                    if (isReversed)
                    {
                        list.Reverse();
                    }
                    foreach (var trackingSpan in list)
                    {
                        var span = trackingSpan.GetSpan(textSnapshot);
                        text = trackingSpan.GetText(textSnapshot);
                        var offset         = 0;
                        var insertionPoint = !isReversed?trackingSpan.GetEndPoint(span.Snapshot) : trackingSpan.GetStartPoint(span.Snapshot);

                        var virtualBufferPosition = new VirtualSnapshotPoint(insertionPoint);
                        virtualBufferPosition = isReversed && !shiftPressed ? new VirtualSnapshotPoint(insertionPoint.Add(text.Length))
                           : !isReversed && shiftPressed ? new VirtualSnapshotPoint(insertionPoint.Add(-text.Length)) : virtualBufferPosition;


                        trackingPoint = textSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative);
                        if (virtualBufferPosition.IsInVirtualSpace)
                        {
                            offset = editorOperations.GetWhitespaceForVirtualSpace(virtualBufferPosition).Length;
                        }
                        trackingPointOffsetList.Add(new Tuple <ITrackingPoint, int, int>(trackingPoint, offset, text.Length));
                        textView.Caret.MoveTo(virtualBufferPosition.TranslateTo(textView.TextSnapshot));
                        editorOperations.InsertText(text);
                    }
                    //Make Selections
                    {
                        var trackingPointOffset = trackingPointOffsetList.First();
                        var insertionPoint      = trackingPointOffset.Item1.GetPoint(textView.TextSnapshot);
                        if (trackingPointOffset.Item2 != 0)
                        {
                            insertionPoint = insertionPoint.Add(trackingPointOffset.Item2);
                        }
                        var virtualSnapshotPoint1 = new VirtualSnapshotPoint(insertionPoint.Add(!isReversed ? 0 : trackingPointOffset.Item3));

                        trackingPointOffset = trackingPointOffsetList.Last();
                        insertionPoint      = trackingPointOffset.Item1.GetPoint(textView.TextSnapshot);
                        if (trackingPointOffset.Item2 != 0)
                        {
                            insertionPoint = insertionPoint.Add(trackingPointOffset.Item2);
                        }
                        var virtualSnapshotPoint2 = new VirtualSnapshotPoint(insertionPoint.Add(isReversed ? 0 : trackingPointOffset.Item3));
                        editorOperations.SelectAndMoveCaret(virtualSnapshotPoint1, virtualSnapshotPoint2, TextSelectionMode.Box);
                    }
                }
            }

            return(VSConstants.S_OK);
        }
Ejemplo n.º 15
0
        // Helped by source of Microsoft.VisualStudio.Text.Editor.DragDrop.DropHandlerBase.cs in assembly Microsoft.VisualStudio.Text.UI.Wpf, Version=14.0.0.0
        public static int HandleCommand(IWpfTextView textView, IClassifier classifier, IOleCommandTarget commandTarget, IEditorOperations editorOperations, bool shiftPressed = false)
        {
            //Guid cmdGroup = VSConstants.VSStd2K;
            var            selectedText  = editorOperations.SelectedText;
            ITrackingPoint trackingPoint = null;

            if (selectedText.Length == 0)
            {
                // if nothing is selected, we can consider the current line as a selection
                var virtualBufferPosition = editorOperations.TextView.Caret.Position.VirtualBufferPosition;
                trackingPoint = textView.TextSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative);

                // Select all the text on the current line. Leaves caret at the start of the next line or end of line if last line.
                editorOperations.SelectLine(textView.Caret.ContainingTextViewLine, false);
                var text = editorOperations.SelectedText;
                // Clear the selection so new inserts will not overwrite the selected line. Caret stays at start of next line.
                editorOperations.ResetSelection();

                // Hack for Last Line: If last line of file, introduce a new line character then delete it after duplicating the line.
                var endOfFile = !EndsWithNewLine(text);
                if (endOfFile)
                {
                    // We are on the last line. Will need to insert a new line. Will be removed later.
                    editorOperations.InsertNewLine();
                }

                // Now we are at the beginning of the line we can insert the duplicate text.
                editorOperations.InsertText(text);

                // Clean up any newline character introduced by earlier hack
                if (endOfFile)
                {
                    editorOperations.Delete();
                }

                // Return the cursor to its original position, then move it down one line (unless doing reverse)
                textView.Caret.MoveTo(new VirtualSnapshotPoint(trackingPoint.GetPoint(textView.TextSnapshot)).TranslateTo(textView.TextSnapshot));
                if (!shiftPressed)
                {
                    editorOperations.MoveLineDown(false);
                }
            }
            else
            {
                var selection    = textView.Selection;
                var isReversed   = selection.IsReversed;
                var text         = selectedText;
                var textSnapshot = textView.TextSnapshot;
                var list         = new List <ITrackingSpan>();
                //var shiftKeyPressed=textVie
                foreach (SnapshotSpan snapshotSpan in selection.SelectedSpans)
                {
                    list.Add(textSnapshot.CreateTrackingSpan(snapshotSpan, SpanTrackingMode.EdgeExclusive));
                }
                if (!selection.IsEmpty)
                {
                    selection.Clear();
                }


                if (list.Count < 2)
                {
                    var offset = 0;
                    var virtualBufferPosition = editorOperations.TextView.Caret.Position.VirtualBufferPosition;
                    var point = editorOperations.TextView.Caret.Position.BufferPosition;
                    virtualBufferPosition = isReversed && !shiftPressed ? new VirtualSnapshotPoint(point.Add(text.Length))
                       : !isReversed && shiftPressed ? new VirtualSnapshotPoint(point.Add(-text.Length)) : virtualBufferPosition;

                    trackingPoint = textSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative);
                    if (virtualBufferPosition.IsInVirtualSpace)
                    {
                        offset = editorOperations.GetWhitespaceForVirtualSpace(virtualBufferPosition).Length;
                    }
                    textView.Caret.MoveTo(virtualBufferPosition.TranslateTo(textView.TextSnapshot));
                    editorOperations.InsertText(text);
                    var insertionPoint = trackingPoint.GetPoint(textView.TextSnapshot);
                    if (offset != 0)
                    {
                        insertionPoint = insertionPoint.Add(offset);
                    }

                    var virtualSnapshotPoint1 = new VirtualSnapshotPoint(insertionPoint);
                    var virtualSnapshotPoint2 = new VirtualSnapshotPoint(insertionPoint.Add(text.Length));
                    if (isReversed)
                    {
                        editorOperations.SelectAndMoveCaret(virtualSnapshotPoint2, virtualSnapshotPoint1, TextSelectionMode.Stream);
                    }
                    else
                    {
                        editorOperations.SelectAndMoveCaret(virtualSnapshotPoint1, virtualSnapshotPoint2, TextSelectionMode.Stream);
                    }
                }
                else
                {
                    var trackingPointOffsetList = new List <Tuple <ITrackingPoint, int, int> >();
                    //Insert Text!
                    if (isReversed)
                    {
                        list.Reverse();
                    }
                    foreach (var trackingSpan in list)
                    {
                        var span = trackingSpan.GetSpan(textSnapshot);
                        text = trackingSpan.GetText(textSnapshot);
                        var offset         = 0;
                        var insertionPoint = !isReversed?trackingSpan.GetEndPoint(span.Snapshot) : trackingSpan.GetStartPoint(span.Snapshot);

                        var virtualBufferPosition = new VirtualSnapshotPoint(insertionPoint);
                        virtualBufferPosition = isReversed && !shiftPressed ? new VirtualSnapshotPoint(insertionPoint.Add(text.Length))
                           : !isReversed && shiftPressed ? new VirtualSnapshotPoint(insertionPoint.Add(-text.Length)) : virtualBufferPosition;


                        trackingPoint = textSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative);
                        if (virtualBufferPosition.IsInVirtualSpace)
                        {
                            offset = editorOperations.GetWhitespaceForVirtualSpace(virtualBufferPosition).Length;
                        }
                        trackingPointOffsetList.Add(new Tuple <ITrackingPoint, int, int>(trackingPoint, offset, text.Length));
                        textView.Caret.MoveTo(virtualBufferPosition.TranslateTo(textView.TextSnapshot));
                        editorOperations.InsertText(text);
                    }
                    //Make Selections
                    {
                        var trackingPointOffset = trackingPointOffsetList.First();
                        var insertionPoint      = trackingPointOffset.Item1.GetPoint(textView.TextSnapshot);
                        if (trackingPointOffset.Item2 != 0)
                        {
                            insertionPoint = insertionPoint.Add(trackingPointOffset.Item2);
                        }
                        var virtualSnapshotPoint1 = new VirtualSnapshotPoint(insertionPoint.Add(!isReversed ? 0 : trackingPointOffset.Item3));

                        trackingPointOffset = trackingPointOffsetList.Last();
                        insertionPoint      = trackingPointOffset.Item1.GetPoint(textView.TextSnapshot);
                        if (trackingPointOffset.Item2 != 0)
                        {
                            insertionPoint = insertionPoint.Add(trackingPointOffset.Item2);
                        }
                        var virtualSnapshotPoint2 = new VirtualSnapshotPoint(insertionPoint.Add(isReversed ? 0 : trackingPointOffset.Item3));
                        editorOperations.SelectAndMoveCaret(virtualSnapshotPoint1, virtualSnapshotPoint2, TextSelectionMode.Box);
                    }
                }
            }

            return(VSConstants.S_OK);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Event Handler: Text View's Layout Changed event
        /// </summary>
        internal void LayoutChanged(bool visualSnapshotChange, ITextSnapshot newEditSnapshot)
        {
            if (this.IsEmpty)
            {
                //For an empty selection, bring the active and anchor points to the new snapshot (so we don't pin the old one)
                //but any old point in the new snapshot will do.
                _activePoint = new VirtualSnapshotPoint(newEditSnapshot, 0);
                _anchorPoint = _activePoint;

                if (visualSnapshotChange)
                {
                    //Make sure the selection spans are on the correct snapshot.
                    _virtualSelectedSpans = null;
                    _selectedSpans        = null;
                }
            }
            else if (visualSnapshotChange)
            {
                //Even though the selection may not have changed the snapshots for these are out of date. Delete and let them be lazily recreated.
                var newActivePoint = _activePoint.TranslateTo(newEditSnapshot);
                var newAnchorPoint = _anchorPoint.TranslateTo(newEditSnapshot);

                var normalizedAnchorPoint = this.NormalizePoint(newAnchorPoint);
                var normalizedActivePoint = this.NormalizePoint(newActivePoint);

                if (normalizedActivePoint == normalizedAnchorPoint)
                {
                    //Something happened to collapse the selection (perhaps both endpoints were contained in an outlining region that was collapsed).
                    //Treat this as clearing the selection.
                    this.Clear(false);
                    return;
                }
                else if ((normalizedActivePoint != newActivePoint) || (normalizedAnchorPoint != newAnchorPoint) || (this.Mode == TextSelectionMode.Box))
                {
                    //Something happened to move one endpoint of the selection (outlining region collapsed?).
                    //Treat this as setting the selection (but use InnerSelect since we have the properly normalized endpoints).
                    //
                    //For box selection, we always assume a layout caused something to change (since a classification change could cause the x-coordinate
                    //of one of the endpoints to change, which would change the entire selection). Trying to determine if that is really the case is
                    //expensive (since you need to check each line of the selection). Instead, we pretend it changes so we'll always redraw a box selection
                    //(but the cost there is proportional to the number of visible lines, not selected lines) and hope none of the consumers of the
                    //selection change event do anything expensive.
                    this.InnerSelect(normalizedAnchorPoint, normalizedActivePoint);
                    return;
                }
                else
                {
                    //The selection didn't "change" but the endpoints need to be brought current with the new snapshot.
                    _anchorPoint = normalizedAnchorPoint;
                    _activePoint = normalizedActivePoint;

                    //The cached spans could be affected if there is a visual snapshot change (even if the underlying edit snapshot did not change: c.f. box selection).
                    _virtualSelectedSpans = null;
                    _selectedSpans        = null;
                }
            }
            //If the visual snapshot didn't change, it is still possible that a box selection would "change" -- a classifcation change could
            //affect the horizontal position of text on a line -- but we are not going to worry about that case here. A classification change
            //(that would have any effect of selection) without a corresponding change in the buffer is rare enough that we can ignore it.

            this.Painter.Update(false);
        }