Ejemplo n.º 1
0
        private void UpdateDiffDimensions(DiffViewModel diffViewModel, HunkRangeInfo hunkRangeInfo)
        {
            if (TextView.IsClosed)
                return;

            var startLineNumber = hunkRangeInfo.NewHunkRange.StartingLineNumber;
            var endLineNumber = startLineNumber + hunkRangeInfo.NewHunkRange.NumberOfLines - 1;

            var snapshot = TextView.TextBuffer.CurrentSnapshot;

            if (startLineNumber < 0
                || startLineNumber >= snapshot.LineCount
                || endLineNumber < 0
                || endLineNumber >= snapshot.LineCount)
            {
                return;
            }

            var startLine = snapshot.GetLineFromLineNumber(startLineNumber);
            var endLine = snapshot.GetLineFromLineNumber(endLineNumber);

            if (startLine == null || endLine == null) return;

            var mapTop = _scrollBar.Map.GetCoordinateAtBufferPosition(startLine.Start) - 0.5;
            var mapBottom = _scrollBar.Map.GetCoordinateAtBufferPosition(endLine.End) + 0.5;

            diffViewModel.Top = Math.Round(_scrollBar.GetYCoordinateOfScrollMapPosition(mapTop)) - 2.0;
            diffViewModel.Height = Math.Round(_scrollBar.GetYCoordinateOfScrollMapPosition(mapBottom)) - diffViewModel.Top + 2.0;
        }
Ejemplo n.º 2
0
        protected DiffViewModel(HunkRangeInfo hunkRangeInfo, IMarginCore marginCore, Action<DiffViewModel, HunkRangeInfo> updateDiffDimensions)
        {
            HunkRangeInfo = hunkRangeInfo;
            MarginCore = marginCore;
            _updateDiffDimensions = updateDiffDimensions;

            MarginCore.BrushesChanged += HandleBrushesChanged;
        }
Ejemplo n.º 3
0
        internal EditorDiffViewModel(HunkRangeInfo hunkRangeInfo, IMarginCore marginCore, Action<DiffViewModel, HunkRangeInfo> updateDiffDimensions)
            : base(hunkRangeInfo, marginCore, updateDiffDimensions)
        {
            ShowPopup = false;

            DiffText = GetDiffText();

            IsDiffTextVisible = GetIsDiffTextVisible();

            UpdateDimensions();
        }
Ejemplo n.º 4
0
        public void IsDeletion_3DeletedLines_ExpectTrue()
        {
            //Arrange
            var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-7,3", _contextLines), new HunkRange("+6,0", _contextLines), new List<string> { "-using Microsoft.VisualStudio.Shell;", "-using Microsoft.VisualStudio.Text;", "-using Microsoft.VisualStudio.Text.Editor;" }.ToArray());

            //Act
            var isDeletion = hunkRangeInfo.IsDeletion;

            //Assert
            isDeletion.ShouldBe(true);
        }
Ejemplo n.º 5
0
        public void IsAddition_NotAllDiffLinesStartsWithPlusSign_ExpectedFalse()
        {
            //Arrange
            var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0", _contextLines), new HunkRange("+42,20", _contextLines), new List<string> { "+ ", "- " }.ToArray());

            //Act
            bool isAddition = hunkRangeInfo.IsAddition;

            //Assert
            isAddition.ShouldBe(false);
        }
Ejemplo n.º 6
0
        public void OriginalText_1NewLineAnd1OriginalLineWithLeadingSpacesAndInvertedOrder_ExpectedOriginalText()
        {
            //Arrange
            var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-18"), new HunkRange("+18"), new List<string> { "-            it++; // this is just a comment", "+            it--;" }.ToArray());

            //Act
            var originalText = hunkRangeInfo.OriginalText;

            //Assert
            originalText[0].ShouldBe("            it++; // this is just a comment");
        }
Ejemplo n.º 7
0
        public void IsAddition_AllDiffLinesWithStartsWithPlusSign_ExpectedTrue()
        {
            //Arrange
            var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0"), new HunkRange("+42,20"), new List<string> { "+ ", "+ " }.ToArray());

            //Act
            bool isAddition = hunkRangeInfo.IsAddition;

            //Assert
            isAddition.ShouldBe(true);
        }
Ejemplo n.º 8
0
        public void IsModification_DiffLinesStartsWithPlusSignAndWithMinus_ExpectedTrue()
        {
            //Arrange
            var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0", _contextLines), new HunkRange("+42,20", _contextLines), new List<string> { "+ ", "- " }.ToArray());

            //Act
            bool isModification = hunkRangeInfo.IsModification;

            //Assert
            isModification.ShouldBe(true);
        }
Ejemplo n.º 9
0
        public DiffViewModel(HunkRangeInfo hunkRangeInfo, IWpfTextView textView)
        {
            _hunkRangeInfo = hunkRangeInfo;
            _textView = textView;

            ShowPopup = false;

            SetDisplayProperties();

            DiffBrush = GetDiffBrush();

            DiffText = GetDiffText();

            IsDiffTextVisible = GetIsDiffTextVisible();
        }
Ejemplo n.º 10
0
        internal DiffViewModel(GitDiffMargin margin, HunkRangeInfo hunkRangeInfo, IWpfTextView textView, IGitCommands gitCommands)
        {
            _margin = margin;
            _hunkRangeInfo = hunkRangeInfo;
            _textView = textView;
            _gitCommands = gitCommands;
            _margin.BrushesChanged += HandleBrushesChanged;

            ShowPopup = false;

            SetDisplayProperties();

            DiffText = GetDiffText();

            IsDiffTextVisible = GetIsDiffTextVisible();
        }
 protected override DiffViewModel CreateDiffViewModel(HunkRangeInfo hunkRangeInfo)
 {
     return new ScrollDiffViewModel(hunkRangeInfo, MarginCore, _updateDiffDimensions);
 }
Ejemplo n.º 12
0
        public void OriginalText_1NewLineAnd1OriginalLineWithLeadingSpaces_ExpectedOriginalText()
        {
            //Arrange
            var hunkRangeInfo = new HunkRangeInfo(new HunkRange("-41,0", _contextLines), new HunkRange("+42,20", _contextLines), new List<string> { "+ New Text", "-    Original Text" }.ToArray());

            //Act
            string originalText = hunkRangeInfo.OriginalText[0];

            //Assert
            originalText.ShouldBe("    Original Text");
        }
Ejemplo n.º 13
0
        private bool? UpdateNormalDiffDimensions(DiffViewModel diffViewModel, HunkRangeInfo hunkRangeInfo)
        {
            if (hunkRangeInfo.NewHunkRange.NumberOfLines <= 0)
            {
                // if visible, it would have been as a deletion
                return false;
            }

            var snapshot = TextView.TextBuffer.CurrentSnapshot;

            var startLineNumber = hunkRangeInfo.NewHunkRange.StartingLineNumber;
            var endLineNumber = startLineNumber + hunkRangeInfo.NewHunkRange.NumberOfLines - 1;
            if (startLineNumber < 0
                || startLineNumber >= snapshot.LineCount
                || endLineNumber < 0
                || endLineNumber >= snapshot.LineCount)
            {
                return false;
            }

            var startLine = snapshot.GetLineFromLineNumber(startLineNumber);
            var endLine = snapshot.GetLineFromLineNumber(endLineNumber);

            if (startLine == null || endLine == null)
                return null;

            var span = new SnapshotSpan(startLine.Start, endLine.End);
            if (!TextView.TextViewLines.FormattedSpan.IntersectsWith(span))
                return false;

            var startLineView = TextView.GetTextViewLineContainingBufferPosition(startLine.Start);
            var endLineView = TextView.GetTextViewLineContainingBufferPosition(endLine.Start);

            if (startLineView == null || endLineView == null)
                return false;

            if (TextView.TextViewLines.LastVisibleLine.EndIncludingLineBreak < startLineView.Start)
            {
                // starts after the last visible line
                return false;
            }

            if (TextView.TextViewLines.FirstVisibleLine.Start > endLineView.EndIncludingLineBreak)
            {
                // ends before the first visible line
                return false;
            }

            double startTop;
            switch (startLineView.VisibilityState)
            {
                case VisibilityState.FullyVisible:
                case VisibilityState.Hidden:
                case VisibilityState.PartiallyVisible:
                    startTop = startLineView.Top - TextView.ViewportTop;
                    break;

                case VisibilityState.Unattached:
                    // if the closest line was past the end we would have already returned
                    startTop = 0;
                    break;

                default:
                    // shouldn't be reachable, but definitely hide if this is the case
                    return false;
            }

            double stopBottom;
            switch (endLineView.VisibilityState)
            {
                case VisibilityState.FullyVisible:
                case VisibilityState.Hidden:
                case VisibilityState.PartiallyVisible:
                    stopBottom = endLineView.Bottom - TextView.ViewportTop;
                    break;

                case VisibilityState.Unattached:
                    // if the closest line was before the start we would have already returned
                    stopBottom = TextView.ViewportHeight;
                    break;

                default:
                    // shouldn't be reachable, but definitely hide if this is the case
                    return false;
            }

            diffViewModel.Top = startTop;
            diffViewModel.Height = stopBottom - startTop;
            return true;
        }
Ejemplo n.º 14
0
 protected abstract DiffViewModel CreateDiffViewModel(HunkRangeInfo hunkRangeInfo);
Ejemplo n.º 15
0
        private bool? UpdateDeletedDiffDimensions(DiffViewModel diffViewModel, HunkRangeInfo hunkRangeInfo)
        {
            if (hunkRangeInfo.NewHunkRange.NumberOfLines != 0)
            {
                // unexpected number of lines for a deletion hunk
                return false;
            }

            var snapshot = TextView.TextBuffer.CurrentSnapshot;

            var followingLineNumber = hunkRangeInfo.NewHunkRange.StartingLineNumber + 1;
            if (followingLineNumber < 0 || followingLineNumber >= snapshot.LineCount)
                return false;

            var followingLine = snapshot.GetLineFromLineNumber(followingLineNumber);
            if (followingLine == null)
                return null;

            var span = new SnapshotSpan(followingLine.Start, followingLine.End);
            if (!TextView.TextViewLines.FormattedSpan.IntersectsWith(span))
                return false;

            var followingLineView = TextView.GetTextViewLineContainingBufferPosition(followingLine.Start);
            if (followingLineView == null)
                return false;

            if (TextView.TextViewLines.LastVisibleLine.EndIncludingLineBreak < followingLineView.Start)
            {
                // starts after the last visible line
                return false;
            }

            if (TextView.TextViewLines.FirstVisibleLine.Start > followingLineView.EndIncludingLineBreak)
            {
                // ends before the first visible line
                return false;
            }

            double followingTop;
            switch (followingLineView.VisibilityState)
            {
                case VisibilityState.FullyVisible:
                case VisibilityState.Hidden:
                case VisibilityState.PartiallyVisible:
                    followingTop = followingLineView.Top - TextView.ViewportTop;
                    break;

                case VisibilityState.Unattached:
                    // if the closest line was past the end we would have already returned
                    followingTop = 0;
                    break;

                default:
                    // shouldn't be reachable, but definitely hide if this is the case
                    return false;
            }

            double center = followingTop;
            double height = TextView.LineHeight;
            diffViewModel.Top = center - (height / 2.0);
            diffViewModel.Height = TextView.LineHeight;
            return true;
        }
Ejemplo n.º 16
0
 internal ScrollDiffViewModel(HunkRangeInfo hunkRangeInfo, IMarginCore marginCore, Action<DiffViewModel, HunkRangeInfo> updateDiffDimensions)
     : base(hunkRangeInfo, marginCore, updateDiffDimensions)
 {
     UpdateDimensions();
 }
Ejemplo n.º 17
0
        public bool RollBack(HunkRangeInfo hunkRangeInfo)
        {
            if (hunkRangeInfo.SuppressRollback)
                return false;

            var snapshot = _textView.TextSnapshot;

            if (snapshot != snapshot.TextBuffer.CurrentSnapshot)
                return false;

            using (var edit = snapshot.TextBuffer.CreateEdit())
            {
                Span newSpan;
                if (hunkRangeInfo.IsDeletion)
                {
                    var startLine = snapshot.GetLineFromLineNumber(hunkRangeInfo.NewHunkRange.StartingLineNumber + 1);
                    newSpan = new Span(startLine.Start.Position, 0);
                }
                else
                {
                    var startLine = snapshot.GetLineFromLineNumber(hunkRangeInfo.NewHunkRange.StartingLineNumber);
                    var endLine = snapshot.GetLineFromLineNumber(hunkRangeInfo.NewHunkRange.StartingLineNumber + hunkRangeInfo.NewHunkRange.NumberOfLines - 1);
                    newSpan = Span.FromBounds(startLine.Start.Position, endLine.EndIncludingLineBreak.Position);
                }

                if (hunkRangeInfo.IsAddition)
                {
                    var startLine = snapshot.GetLineFromLineNumber(hunkRangeInfo.NewHunkRange.StartingLineNumber);
                    var endLine = snapshot.GetLineFromLineNumber(hunkRangeInfo.NewHunkRange.StartingLineNumber + hunkRangeInfo.NewHunkRange.NumberOfLines - 1);
                    edit.Delete(Span.FromBounds(startLine.Start.Position, endLine.EndIncludingLineBreak.Position));
                }
                else
                {
                    var lineBreak = snapshot.GetLineFromLineNumber(0).GetLineBreakText();
                    if (String.IsNullOrEmpty(lineBreak))
                        lineBreak = Environment.NewLine;

                    var originalText = String.Join(lineBreak, hunkRangeInfo.OriginalText);
                    if (hunkRangeInfo.NewHunkRange.StartingLineNumber + hunkRangeInfo.NewHunkRange.NumberOfLines != snapshot.LineCount)
                        originalText += lineBreak;

                    edit.Replace(newSpan, originalText);
                }

                edit.Apply();

                return true;
            }
        }
Ejemplo n.º 18
0
        private void UpdateDiffDimensions(DiffViewModel diffViewModel, HunkRangeInfo hunkRangeInfo)
        {
            if (TextView.IsClosed)
                return;

            bool? visible;
            if (diffViewModel.IsDeletion)
                visible = UpdateDeletedDiffDimensions(diffViewModel, hunkRangeInfo);
            else
                visible = UpdateNormalDiffDimensions(diffViewModel, hunkRangeInfo);

            if (visible.HasValue)
                diffViewModel.IsVisible = visible.Value;
        }