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

            return(changes.ToImmutableArray());
        }
Exemplo n.º 2
0
        private StringDifferenceOptions GetDifferenceOptions(TextDifferenceTypes differenceTypes)
        {
            StringDifferenceTypes stringDifferenceTypes = default;

            if (differenceTypes.HasFlag(TextDifferenceTypes.Line))
            {
                stringDifferenceTypes |= StringDifferenceTypes.Line;
            }

            if (differenceTypes.HasFlag(TextDifferenceTypes.Word))
            {
                stringDifferenceTypes |= StringDifferenceTypes.Word;
            }

            if (differenceTypes.HasFlag(TextDifferenceTypes.Character))
            {
                stringDifferenceTypes |= StringDifferenceTypes.Character;
            }

            return(new StringDifferenceOptions()
            {
                DifferenceType = stringDifferenceTypes
            });
        }
Exemplo n.º 3
0
        public async Task <ImmutableArray <TextChange> > GetTextChangesAsync(Document oldDocument, Document newDocument, TextDifferenceTypes preferredDifferenceType, 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 = GetDifferenceOptions(preferredDifferenceType);

            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())).ToImmutableArray());
        }