Example #1
0
        private async Task<Solution> GetFix(TextDocument publicSurfaceAreaDocument, string newSymbolName, CancellationToken cancellationToken)
        {
            var sourceText = await publicSurfaceAreaDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
            var newSourceText = AddSymbolNamesToSourceText(sourceText, new[] { newSymbolName });

            return publicSurfaceAreaDocument.Project.Solution.WithAdditionalDocumentText(publicSurfaceAreaDocument.Id, newSourceText);
        }
Example #2
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 #3
0
        public FileChange(TextDocument left,
            TextDocument right,
            IComponentModel componentModel,
            AbstractChange parent,
            PreviewEngine engine,
            IVsImageService2 imageService) : base(engine)
        {
            Contract.ThrowIfFalse(left != null || right != null);

            this.Id = left != null ? left.Id : right.Id;
            _left = left;
            _right = right;
            _imageService = imageService;

            _componentModel = componentModel;
            var bufferFactory = componentModel.GetService<ITextBufferFactoryService>();
            var bufferText = left != null ?
                left.GetTextAsync(CancellationToken.None).WaitAndGetResult(CancellationToken.None) :
                right.GetTextAsync(CancellationToken.None).WaitAndGetResult(CancellationToken.None);
            _buffer = bufferFactory.CreateTextBuffer(bufferText.ToString(), bufferFactory.InertContentType);
            _encoding = bufferText.Encoding;

            this.Children = ComputeChildren(left, right, CancellationToken.None);
            this.parent = parent;
        }
Example #4
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(diffService, left, right, cancellationToken);
            if (diff.Differences.Count == 0)
            {
                // There are no changes.
                return ChangeList.Empty;
            }

            return GetChangeList(diff, right.Id, oldText, newText);
        }
        private async Task<Solution> GetFix(TextDocument publicSurfaceAreaDocument, string newSymbolName, ImmutableHashSet<string> siblingSymbolNamesToRemove, CancellationToken cancellationToken)
        {
            SourceText sourceText = await publicSurfaceAreaDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
            SourceText newSourceText = AddSymbolNamesToSourceText(sourceText, new[] { newSymbolName });
            newSourceText = RemoveSymbolNamesFromSourceText(newSourceText, siblingSymbolNamesToRemove);

            return publicSurfaceAreaDocument.Project.Solution.WithAdditionalDocumentText(publicSurfaceAreaDocument.Id, newSourceText);
        }
Example #6
0
        private static void AppendAssetMap(TextDocument document, Dictionary<Checksum, object> map)
        {
            DocumentStateChecksums documentChecksums;
            Assert.True(document.State.TryGetStateChecksums(out documentChecksums));

            documentChecksums.Find(document.State, Flatten(documentChecksums), map, CancellationToken.None);

            // fix up due to source text can't be obtained synchronously in product code
            map[documentChecksums.Text] = document.State.GetTextSynchronously(CancellationToken.None);
        }
 public void OpenDocument(TextDocument document)
 {
     if (document is Document)
     {
         OpenDocument(document.Id);
     }
     else
     {
         OpenAdditionalDocument(document.Id);
     }
 }
 public void CloseDocument(TextDocument document, SourceText text)
 {
     if (document is Document)
     {
         OnDocumentClosed(document.Id, new PreviewTextLoader(text));
     }
     else
     {
         OnAdditionalDocumentClosed(document.Id, new PreviewTextLoader(text));
     }
 }
        private static IOrderedEnumerable<Diagnostic> GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer,
                                                                                        TextDocument document,
                                                                                        AnalyzerOptions options)
        {
            var diagnostics = document.Project.GetCompilationAsync()
                                      .Result
                                      .WithAnalyzers(ImmutableArray.Create(analyzer),
                                                     options)
                                      .GetAnalyzerDiagnosticsAsync().Result;

            return SortDiagnostics(diagnostics);
        }
Example #10
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 #11
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 #12
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);
        }
 public TestAdditionalDocument(TextDocument textDocument)
     : this(textDocument.FilePath, textDocument.Name, textDocument.GetTextAsync(CancellationToken.None).Result.ToString())
 {
 }
Example #14
0
        private static bool ContainsBetterDiff(TextDocument left, TextDocument right, IHierarchicalDifferenceCollection diffResult, CancellationToken cancellationToken)
        {
            var textDiffCount = diffResult.Differences.Count;

            var leftDocument = left as Document;
            var rightDocument = right as Document;
            if (leftDocument == null || rightDocument == null)
            {
                return false;
            }

            var syntaxDiffCount = rightDocument.GetTextChangesAsync(leftDocument, cancellationToken).WaitAndGetResult(cancellationToken).Count();
            return syntaxDiffCount > textDiffCount;
        }
Example #15
0
        private static IHierarchicalDifferenceCollection ComputeDiffSpans(ITextDifferencingService diffService, TextDocument left, TextDocument right, CancellationToken cancellationToken)
        {
            // TODO: it would be nice to have a syntax based differ for presentation here, 
            //       current way of just using text differ has its own issue, and using syntax differ in compiler that are for incremental parser
            //       has its own drawbacks.

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

            var oldString = oldText.ToString();
            var newString = newText.ToString();

            return diffService.DiffStrings(oldString, newString, new StringDifferenceOptions()
            {
                DifferenceType = StringDifferenceTypes.Line,
            });
        }
Example #16
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 });
 }
 private static bool IsStyleCopSettingsDocument(TextDocument document)
 {
     return string.Equals(document.Name, StyleCopSettingsFileName, StringComparison.OrdinalIgnoreCase);
 }
Example #18
0
 public TextDocumentEventArgs(TextDocument document)
 {
     this.Document = document ?? throw new ArgumentNullException(nameof(document));
 }
Example #19
0
 /// <summary>
 /// Apply the inputted CodeAction to the inputted document.
 /// Meant to be used to apply codefixes.
 /// </summary>
 /// <param name="document">The Document to apply the fix on</param>
 /// <param name="codeAction">A CodeAction that will be applied to the Document.</param>
 /// <returns>A Document with the changes from the CodeAction</returns>
 private static Document ApplyFix(TextDocument document, CodeAction codeAction)
 {
     var operations = codeAction.GetOperationsAsync(CancellationToken.None).Result;
     var solution = operations.OfType<ApplyChangesOperation>().Single().ChangedSolution;
     return solution.GetDocument(document.Id);
 }