Exemplo n.º 1
0
        public async Task <IEnumerable <TextChange> > GetTextChangesAsync(Document oldDocument, Document newDocument, CancellationToken cancellationToken)
        {
            var oldText = await oldDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var newText = await newDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var diffService = _differenceSelectorService.GetTextDifferencingService(oldDocument.Project.LanguageServices.GetService <IContentTypeLanguageService>().GetDefaultContentType())
                              ?? _differenceSelectorService.DefaultTextDifferencingService;

            var differenceOptions = new StringDifferenceOptions()
            {
                DifferenceType = StringDifferenceTypes.Word
            };

            var oldTextSnapshot = oldText.FindCorrespondingEditorTextSnapshot();
            var newTextSnapshot = newText.FindCorrespondingEditorTextSnapshot();
            var useSnapshots    = oldTextSnapshot != null && newTextSnapshot != null;

            var diffResult = useSnapshots
                ? diffService.DiffSnapshotSpans(oldTextSnapshot.GetFullSpan(), newTextSnapshot.GetFullSpan(), differenceOptions)
                : diffService.DiffStrings(oldText.ToString(), newText.ToString(), differenceOptions);

            return(diffResult.Differences.Select(d =>
                                                 new TextChange(
                                                     diffResult.LeftDecomposition.GetSpanInOriginal(d.Left).ToTextSpan(),
                                                     newText.GetSubText(diffResult.RightDecomposition.GetSpanInOriginal(d.Right).ToTextSpan()).ToString())));
        }
Exemplo n.º 2
0
        private IHierarchicalDifferenceCollection DiffStrings(string leftTextWithReplacement, string rightTextWithReplacement)
        {
            var diffService = _differenceSelectorService.GetTextDifferencingService(
                _workspace.Services.GetLanguageServices(this.Project.Language).GetService <IContentTypeLanguageService>().GetDefaultContentType());

            diffService = diffService ?? _differenceSelectorService.DefaultTextDifferencingService;
            return(diffService.DiffStrings(leftTextWithReplacement, rightTextWithReplacement, s_venusEditOptions.DifferenceOptions));
        }
Exemplo n.º 3
0
        private IHierarchicalDifferenceCollection DiffStrings(string leftTextWithReplacement, string rightTextWithReplacement)
        {
            var document = LanguageBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChangesSafe();

            var diffService = _differenceSelectorService.GetTextDifferencingService(
                _workspace.Services.GetLanguageServices(document.Project.Language).GetService <IContentTypeLanguageService> ().GetDefaultContentType());

            diffService = diffService ?? _differenceSelectorService.DefaultTextDifferencingService;
            return(diffService.DiffStrings(leftTextWithReplacement, rightTextWithReplacement, s_venusEditOptions.DifferenceOptions));
        }
Exemplo n.º 4
0
        private IHierarchicalDifferenceCollection ComputeEditDifferences(TextDocument oldDocument, TextDocument newDocument, CancellationToken cancellationToken)
        {
            // Get the text that's actually in the editor.
            var oldText = oldDocument.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var newText = newDocument.GetTextAsync(cancellationToken).WaitAndGetResult(cancellationToken);

            // Defer to the editor to figure out what changes the client made.
            var diffService = _differenceSelectorService.GetTextDifferencingService(
                oldDocument.Project.LanguageServices.GetService <IContentTypeLanguageService>().GetDefaultContentType());

            diffService = diffService ?? _differenceSelectorService.DefaultTextDifferencingService;
            return(diffService.DiffStrings(oldText.ToString(), newText.ToString(), new StringDifferenceOptions()
            {
                DifferenceType = StringDifferenceTypes.Word | StringDifferenceTypes.Line,
            }));
        }