Example #1
0
        private void UpdateBuffer(TextDocument document, SpanChange spanSource, out SourceTextContainer container, out TextDocument documentBackedByTextBuffer)
        {
            if (_previewWorkspace != null)
            {
                var currentDocument = _previewWorkspace.CurrentSolution.GetTextDocument(_currentDocument);
                var currentDocumentText = currentDocument.GetTextAsync().Result;
                _previewWorkspace.CloseDocument(currentDocument, currentDocumentText);

                // Put the new document into the current preview solution.
                var updatedSolution = _previewWorkspace.CurrentSolution.WithTextDocumentText(document.Id, document.GetTextAsync().Result);
                var updatedDocument = updatedSolution.GetTextDocument(document.Id);

                ApplyDocumentToBuffer(updatedDocument, spanSource, out container, out documentBackedByTextBuffer);

                _previewWorkspace.TryApplyChanges(documentBackedByTextBuffer.Project.Solution);
                _previewWorkspace.OpenDocument(document.Id);
                _currentDocument = document.Id;
            }
            else
            {
                _currentDocument = document.Id;

                ApplyDocumentToBuffer(document, spanSource, out container, out documentBackedByTextBuffer);
                _previewWorkspace = new PreviewDialogWorkspace(documentBackedByTextBuffer.Project.Solution);
                _previewWorkspace.OpenDocument(document.Id);
            }
        }
Example #2
0
        private void UpdateBuffer(Document document, SpanChange spanSource, out SourceTextContainer container, out Document documentBackedByTextBuffer)
        {
            if (_previewWorkspace != null)
            {
                _previewWorkspace.CloseDocument(_currentDocument, _previewWorkspace.CurrentSolution.GetDocument(_currentDocument).GetTextAsync().Result);

                // Put the new document into the current preview solution
                var updatedSolution = _previewWorkspace.CurrentSolution.WithDocumentText(document.Id, document.GetTextAsync().Result);
                var updatedDocument = updatedSolution.GetDocument(document.Id);

                ApplyDocumentToBuffer(updatedDocument, spanSource, out container, out documentBackedByTextBuffer);

                _previewWorkspace.TryApplyChanges(documentBackedByTextBuffer.Project.Solution);
                _previewWorkspace.OpenDocument(document.Id);
                _currentDocument = document.Id;
            }
            else
            {
                _currentDocument = document.Id;

                ApplyDocumentToBuffer(document, spanSource, out container, out documentBackedByTextBuffer);

                _previewWorkspace = new PreviewDialogWorkspace(documentBackedByTextBuffer.Project.Solution);
                _previewWorkspace.OpenDocument(document.Id);
            }
        }
Example #3
0
        private ChangeList GetEntireDocumentAsSpanChange(TextDocument document)
        {
            // Show the whole document.
            var entireSpan      = _buffer.CurrentSnapshot.CreateTrackingSpan(0, _buffer.CurrentSnapshot.Length, SpanTrackingMode.EdgeInclusive);
            var text            = document.GetTextAsync().Result.ToString();
            var displayText     = GetDisplayText(text);
            var entireSpanChild = new SpanChange(entireSpan, _buffer, document.Id, displayText, text, text, isDeletion: false, parent: this, engine: engine);

            return(new ChangeList(new[] { entireSpanChild }));
        }
Example #4
0
        public void UpdatePreview(DocumentId documentId, SpanChange spanSource)
        {
            var updatedSolution = _topLevelChange.GetUpdatedSolution(applyingChanges: false);
            var document        = updatedSolution.GetTextDocument(documentId);

            if (document != null)
            {
                _updater.UpdateView(document, spanSource);
            }
        }
Example #5
0
        public void UpdateView(TextDocument document, SpanChange spanSource)
        {
            UpdateBuffer(document);

            // Picking a different span: no text change; update span anyway.
            _tagger.Span = spanSource.GetSpan();
            var spanInBuffer = new SnapshotSpan(_textView.TextBuffer.CurrentSnapshot, new Span(_tagger.Span.Start, 0));

            _textView.ViewScroller.EnsureSpanVisible(spanInBuffer, EnsureSpanVisibleOptions.None);
        }
Example #6
0
        private ChangeList ComputeChildren(TextDocument left, TextDocument right, CancellationToken cancellationToken)
        {
            if (left == null)
            {
                // Added document.
                return(GetEntireDocumentAsSpanChange(right));
            }
            else if (right == null)
            {
                // Removed document.
                return(GetEntireDocumentAsSpanChange(left));
            }

            var oldText = left.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var newText = right.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);

            var diffSelector = _componentModel.GetService <ITextDifferencingSelectorService>();
            var diffService  = diffSelector.GetTextDifferencingService(
                left.Project.LanguageServices.GetService <IContentTypeLanguageService>().GetDefaultContentType());

            diffService = diffService ?? diffSelector.DefaultTextDifferencingService;

            var diff = ComputeDiffSpans(oldText, newText, diffService, cancellationToken);

            if (!diff.Differences.Any())
            {
                // There are no changes. Just show the whole document.
                return(GetEntireDocumentAsSpanChange(left));
            }

            var spanChanges = new List <SpanChange>();

            foreach (var difference in diff)
            {
                var leftSpan  = diff.LeftDecomposition.GetSpanInOriginal(difference.Left);
                var rightSpan = diff.RightDecomposition.GetSpanInOriginal(difference.Right);

                var leftText  = oldText.GetSubText(leftSpan.ToTextSpan()).ToString();
                var rightText = newText.GetSubText(rightSpan.ToTextSpan()).ToString();

                var trackingSpan = _buffer.CurrentSnapshot.CreateTrackingSpan(leftSpan, SpanTrackingMode.EdgeInclusive);

                var isDeletion  = difference.DifferenceType == DifferenceType.Remove;
                var displayText = isDeletion ? GetDisplayText(leftText) : GetDisplayText(rightText);

                var spanChange = new SpanChange(trackingSpan, _buffer, right.Id, displayText, leftText, rightText, difference.DifferenceType == DifferenceType.Remove, this, engine);

                spanChanges.Add(spanChange);
            }

            return(new ChangeList(spanChanges.ToArray()));
        }
Example #7
0
        public void UpdateView(TextDocument document, SpanChange spanSource)
        {
            var documentText = document.GetTextAsync().Result.ToString();
            if (TextView.TextBuffer.CurrentSnapshot.GetText() != documentText)
            {
                UpdateBuffer(document, spanSource, out var container, out var documentBackedByTextBuffer);
            }

            // Picking a different span: no text change; update span anyway.
            SpanToShow = spanSource.GetSpan();
            var spanInBuffer = new SnapshotSpan(TextView.TextBuffer.CurrentSnapshot, new Span(SpanToShow.Start, 0));
            TextView.ViewScroller.EnsureSpanVisible(spanInBuffer, EnsureSpanVisibleOptions.None);
            Tagger.OnTextBufferChanged();
        }
Example #8
0
        public void UpdateView(TextDocument document, SpanChange spanSource)
        {
            var documentText = document.GetTextAsync().Result.ToString();

            if (TextView.TextBuffer.CurrentSnapshot.GetText() != documentText)
            {
                UpdateBuffer(document, spanSource, out _, out _);
            }

            // Picking a different span: no text change; update span anyway.
            SpanToShow = spanSource.GetSpan();
            var spanInBuffer = new SnapshotSpan(TextView.TextBuffer.CurrentSnapshot, new Span(SpanToShow.Start, 0));

            TextView.ViewScroller.EnsureSpanVisible(spanInBuffer, EnsureSpanVisibleOptions.None);
            Tagger.OnTextBufferChanged();
        }
Example #9
0
        private void ApplyDocumentToBuffer(TextDocument document, SpanChange spanSource, out SourceTextContainer container, out TextDocument documentBackedByTextBuffer)
        {
            var contentTypeService = document.Project.LanguageServices.GetService<IContentTypeLanguageService>();
            var contentType = contentTypeService.GetDefaultContentType();

            TextView.TextBuffer.ChangeContentType(contentType, null);

            var documentText = document.GetTextAsync().Result.ToString();
            SpanToShow = spanSource.GetSpan();

            using (var edit = TextView.TextBuffer.CreateEdit())
            {
                edit.Replace(new Span(0, TextView.TextBuffer.CurrentSnapshot.Length), documentText);
                edit.Apply();
            }

            container = TextView.TextBuffer.AsTextContainer();
            documentBackedByTextBuffer = document.WithText(container.CurrentText);
        }
Example #10
0
        private ChangeList GetChangeList(
            IHierarchicalDifferenceCollection diff,
            DocumentId id,
            SourceText oldText,
            SourceText newText
            )
        {
            var spanChanges = new List <SpanChange>();

            foreach (var difference in diff)
            {
                var leftSpan  = diff.LeftDecomposition.GetSpanInOriginal(difference.Left);
                var rightSpan = diff.RightDecomposition.GetSpanInOriginal(difference.Right);

                var leftText  = oldText.GetSubText(leftSpan.ToTextSpan()).ToString();
                var rightText = newText.GetSubText(rightSpan.ToTextSpan()).ToString();

                var trackingSpan = _buffer.CurrentSnapshot.CreateTrackingSpan(
                    leftSpan,
                    SpanTrackingMode.EdgeInclusive
                    );

                var isDeletion  = difference.DifferenceType == DifferenceType.Remove;
                var displayText = isDeletion ? GetDisplayText(leftText) : GetDisplayText(rightText);

                var spanChange = new SpanChange(
                    trackingSpan,
                    _buffer,
                    id,
                    displayText,
                    leftText,
                    rightText,
                    isDeletion,
                    this,
                    engine
                    );

                spanChanges.Add(spanChange);
            }

            return(new ChangeList(spanChanges.ToArray()));
        }
Example #11
0
        private void ApplyDocumentToBuffer(Document document, SpanChange spanSource, out SourceTextContainer container, out Document documentBackedByTextBuffer)
        {
            var contentTypeService = document.Project.LanguageServices.GetService <IContentTypeLanguageService>();
            var contentType        = contentTypeService.GetDefaultContentType();

            TextView.TextBuffer.ChangeContentType(contentType, null);

            var documentText = document.GetTextAsync().Result.ToString();

            SpanToShow = spanSource.GetSpan();

            using (var edit = TextView.TextBuffer.CreateEdit())
            {
                edit.Replace(new Span(0, TextView.TextBuffer.CurrentSnapshot.Length), documentText);
                edit.Apply();
            }

            container = TextView.TextBuffer.AsTextContainer();
            documentBackedByTextBuffer = document.WithText(container.CurrentText);
        }
Example #12
0
        private ChangeList GetChangeList(IHierarchicalDifferenceCollection diff, DocumentId id, SourceText oldText, SourceText newText)
        {
            var spanChanges = new List<SpanChange>();
            foreach (var difference in diff)
            {
                var leftSpan = diff.LeftDecomposition.GetSpanInOriginal(difference.Left);
                var rightSpan = diff.RightDecomposition.GetSpanInOriginal(difference.Right);

                var leftText = oldText.GetSubText(leftSpan.ToTextSpan()).ToString();
                var rightText = newText.GetSubText(rightSpan.ToTextSpan()).ToString();

                var trackingSpan = _buffer.CurrentSnapshot.CreateTrackingSpan(leftSpan, SpanTrackingMode.EdgeInclusive);

                var isDeletion = difference.DifferenceType == DifferenceType.Remove;
                var displayText = isDeletion ? GetDisplayText(leftText) : GetDisplayText(rightText);

                var spanChange = new SpanChange(trackingSpan, _buffer, id, displayText, leftText, rightText, isDeletion, this, engine);

                spanChanges.Add(spanChange);
            }

            return new ChangeList(spanChanges.ToArray());
        }
Example #13
0
 private ChangeList GetEntireDocumentAsSpanChange(TextDocument document)
 {
     // Show the whole document.
     var entireSpan = _buffer.CurrentSnapshot.CreateTrackingSpan(0, _buffer.CurrentSnapshot.Length, SpanTrackingMode.EdgeInclusive);
     var text = document.GetTextAsync().Result.ToString();
     var displayText = GetDisplayText(text);
     var entireSpanChild = new SpanChange(entireSpan, _buffer, document.Id, displayText, text, text, isDeletion: false, parent: this, engine: engine);
     return new ChangeList(new[] { entireSpanChild });
 }
Example #14
0
 public void UpdatePreview(DocumentId documentId, SpanChange spanSource)
 {
     var updatedSolution = _topLevelChange.GetUpdatedSolution(applyingChanges: false);
     var document = updatedSolution.GetTextDocument(documentId);
     if (document != null)
     {
         _updater.UpdateView(document, spanSource);
     }
 }
Example #15
0
        private ChangeList ComputeChildren(TextDocument left, TextDocument right, CancellationToken cancellationToken)
        {
            if (left == null)
            {
                // Added document.
                return GetEntireDocumentAsSpanChange(right);
            }
            else if (right == null)
            {
                // Removed document.
                return GetEntireDocumentAsSpanChange(left);
            }

            var oldText = left.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var newText = right.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);

            var diffSelector = _componentModel.GetService<ITextDifferencingSelectorService>();
            var diffService = diffSelector.GetTextDifferencingService(
                left.Project.LanguageServices.GetService<IContentTypeLanguageService>().GetDefaultContentType());

            diffService = diffService ?? diffSelector.DefaultTextDifferencingService;

            var diff = ComputeDiffSpans(oldText, newText, diffService, cancellationToken);
            if (!diff.Differences.Any())
            {
                // There are no changes. Just show the whole document.
                return GetEntireDocumentAsSpanChange(left);
            }

            var spanChanges = new List<SpanChange>();
            foreach (var difference in diff)
            {
                var leftSpan = diff.LeftDecomposition.GetSpanInOriginal(difference.Left);
                var rightSpan = diff.RightDecomposition.GetSpanInOriginal(difference.Right);

                var leftText = oldText.GetSubText(leftSpan.ToTextSpan()).ToString();
                var rightText = newText.GetSubText(rightSpan.ToTextSpan()).ToString();

                var trackingSpan = _buffer.CurrentSnapshot.CreateTrackingSpan(leftSpan, SpanTrackingMode.EdgeInclusive);

                var isDeletion = difference.DifferenceType == DifferenceType.Remove;
                var displayText = isDeletion ? GetDisplayText(leftText) : GetDisplayText(rightText);

                var spanChange = new SpanChange(trackingSpan, _buffer, right.Id, displayText, leftText, rightText, difference.DifferenceType == DifferenceType.Remove, this, engine);

                spanChanges.Add(spanChange);
            }

            return new ChangeList(spanChanges.ToArray());
        }