示例#1
0
        public static void StartOfWord(this EnvDTE80.DTE2 dte2, Boolean extendSelection)
        {
            if (!dte2.TryGetTextView(out var vsTextView))
            {
                return;
            }

            var wpfTextView = vsTextView.GetWpfTextView();

            if (!extendSelection && !wpfTextView.Selection.IsEmpty)
            {
                wpfTextView.Selection.Select(wpfTextView.Selection.ActivePoint, wpfTextView.Selection.ActivePoint);
            }

            var caret      = wpfTextView.Caret;
            var operations = dte2.GetEditorOperations().GetEditorOperations(wpfTextView);

            if (caret.InVirtualSpace)
            {
                operations.MoveToEndOfLine(extendSelection);
                return;
            }

            var point = caret.Position.BufferPosition;

            if (point.Position == 0)
            {
                return;
            }

            var navigator = dte2.GetTextStructureNavigatorSelector().GetTextStructureNavigator(wpfTextView.TextBuffer);

            if (point == caret.ContainingTextViewLine.Start)
            {
                operations.MoveLineUp(extendSelection);
                operations.MoveToLastNonWhiteSpaceCharacter(extendSelection);

                if (navigator.GetExtentOfWord(caret.Position.BufferPosition).IsSignificant)
                {
                    operations.MoveToNextCharacter(extendSelection);
                }

                return;
            }

            var extent = navigator.GetExtentOfWord(point - 1);

            for (Int32 i = point; i > extent.Span.Start; i--)
            {
                operations.MoveToPreviousCharacter(extendSelection);
            }
        }
示例#2
0
        public static void EndOfWord(this EnvDTE80.DTE2 dte2, Boolean extendSelection)
        {
            if (!dte2.TryGetTextView(out var vsTextView))
            {
                return;
            }

            var wpfTextView = vsTextView.GetWpfTextView();

            if (!extendSelection && !wpfTextView.Selection.IsEmpty)
            {
                wpfTextView.Selection.Select(wpfTextView.Selection.ActivePoint, wpfTextView.Selection.ActivePoint);
            }

            var caret = wpfTextView.Caret;
            var point = caret.Position.BufferPosition;

            if (point.Position == wpfTextView.TextSnapshot.Length)
            {
                return;
            }

            var operations = dte2.GetEditorOperations().GetEditorOperations(wpfTextView);

            if (point == caret.ContainingTextViewLine.End)
            {
                operations.MoveToStartOfNextLineAfterWhiteSpace(extendSelection);
                return;
            }

            var navigator = dte2.GetTextStructureNavigatorSelector().GetTextStructureNavigator(wpfTextView.TextBuffer);
            var extent    = navigator.GetExtentOfWord(point);

            if (!extent.IsSignificant)
            {
                for (int i = 0; i < extent.Span.Length; i++)
                {
                    operations.MoveToNextCharacter(extendSelection);
                }

                EndOfWord(dte2, extendSelection);
                return;
            }

            for (Int32 i = point; i < extent.Span.End && i < caret.ContainingTextViewLine.End; i++)
            {
                operations.MoveToNextCharacter(extendSelection);
            }
        }