diff options
Diffstat (limited to 'src/Editor/Text/Util/TextUIUtil/ExtensionMethods.cs')
-rw-r--r-- | src/Editor/Text/Util/TextUIUtil/ExtensionMethods.cs | 85 |
1 files changed, 42 insertions, 43 deletions
diff --git a/src/Editor/Text/Util/TextUIUtil/ExtensionMethods.cs b/src/Editor/Text/Util/TextUIUtil/ExtensionMethods.cs index 9c1ba00..c22c853 100644 --- a/src/Editor/Text/Util/TextUIUtil/ExtensionMethods.cs +++ b/src/Editor/Text/Util/TextUIUtil/ExtensionMethods.cs @@ -7,47 +7,6 @@ namespace Microsoft.VisualStudio.Text.MultiSelection { public static class ExtensionMethods { - public static VirtualSnapshotPoint NormalizePoint(this ITextView view, VirtualSnapshotPoint point) - { - var line = view.GetTextViewLineContainingBufferPosition(point.Position); - - //If point is at the end of the line, return it (including any virtual space offset) - if (point.Position >= line.End) - { - return new VirtualSnapshotPoint(line.End, point.VirtualSpaces); - } - else - { - //Otherwise align it with the begining of the containing text element & - //return that (losing any virtual space). - SnapshotSpan element = line.GetTextElementSpan(point.Position); - return new VirtualSnapshotPoint(element.Start); - } - } - - public static Selection MapToSnapshot(this Selection region, ITextSnapshot snapshot, ITextView view) - { - var newInsertion = view.NormalizePoint(region.InsertionPoint.TranslateTo(snapshot)); - var newActive = view.NormalizePoint(region.ActivePoint.TranslateTo(snapshot)); - var newAnchor = view.NormalizePoint(region.AnchorPoint.TranslateTo(snapshot)); - PositionAffinity positionAffinity; - - if (region.Extent.Length == 0) - { - // Selection is just a caret, respect the caret's prefered affinity. - positionAffinity = region.InsertionPointAffinity; - } - else - { - // Selection is non-zero length, adjust affinity so that it is always toward the body of the selection. - // This attempts to ensure that the caret is always on the same line as the body of the selection in - // word wrap scenarios. - positionAffinity = newAnchor < newActive ? PositionAffinity.Predecessor : PositionAffinity.Successor; - } - - return new Selection(newInsertion, newAnchor, newActive, positionAffinity); - } - /// <summary> /// Remaps a given x-coordinate to a valid point. If the provided x-coordinate is past the right end of the line, it will /// be clipped to the correct position depending on the virtual space settings. If the ISmartIndent is providing indentation @@ -80,8 +39,7 @@ namespace Microsoft.VisualStudio.Text.MultiSelection { //The indentation specified by the smart indent service is desired column position of the caret. Find out how much virtual space //need to be at the end of the line to satisfy that. - // TODO: need a way to determine column width in xplat scenarios, bug https://devdiv.visualstudio.com/DevDiv/_workitems/edit/637741 - double columnWidth = (textView is ITextView3 textView3) ? textView3.FormattedLineSource.ColumnWidth : throw new NotSupportedException(); + double columnWidth = (textView.ViewScroller is IViewScroller2 viewScroller) ? viewScroller.ColumnWidth : 7; indentationWidth = Math.Max(0.0, (((double)indentation.Value) * columnWidth - textLine.TextWidth)); // if the coordinate is specified by the user and the user has selected a coordinate to the left @@ -98,6 +56,47 @@ namespace Microsoft.VisualStudio.Text.MultiSelection return xCoordinate; } + public static VirtualSnapshotPoint NormalizePoint(this ITextView view, VirtualSnapshotPoint point) + { + var line = view.GetTextViewLineContainingBufferPosition(point.Position); + + //If point is at the end of the line, return it (including any virtual space offset) + if (point.Position >= line.End) + { + return new VirtualSnapshotPoint(line.End, point.VirtualSpaces); + } + else + { + //Otherwise align it with the begining of the containing text element & + //return that (losing any virtual space). + SnapshotSpan element = line.GetTextElementSpan(point.Position); + return new VirtualSnapshotPoint(element.Start); + } + } + + public static Selection MapToSnapshot(this Selection region, ITextSnapshot snapshot, ITextView view) + { + var newInsertion = view.NormalizePoint(region.InsertionPoint.TranslateTo(snapshot)); + var newActive = view.NormalizePoint(region.ActivePoint.TranslateTo(snapshot)); + var newAnchor = view.NormalizePoint(region.AnchorPoint.TranslateTo(snapshot)); + PositionAffinity positionAffinity; + + if (region.Extent.Length == 0) + { + // Selection is just a caret, respect the caret's prefered affinity. + positionAffinity = region.InsertionPointAffinity; + } + else + { + // Selection is non-zero length, adjust affinity so that it is always toward the body of the selection. + // This attempts to ensure that the caret is always on the same line as the body of the selection in + // word wrap scenarios. + positionAffinity = newAnchor < newActive ? PositionAffinity.Predecessor : PositionAffinity.Successor; + } + + return new Selection(newInsertion, newAnchor, newActive, positionAffinity); + } + /// <summary> /// If you are looking at this, you're likely maintaining selection code, and should be aware that /// virtual whitespace allowances are not simply checking a flag. |