Class DiffHelper


  • public class DiffHelper
    extends java.lang.Object
    Utility class for computing differences between strings with ANSI attribute awareness.

    The DiffHelper class provides methods for computing the differences between two strings while being aware of ANSI escape sequences and text attributes. This allows for proper diffing of styled text without breaking the ANSI escape sequences.

    Unlike standard diff algorithms, this implementation ensures that any text in a Diff object is a valid ANSI string with properly balanced escape sequences. This is particularly important when diffing AttributedStrings or other text with embedded styling information.

    The diff algorithm identifies three types of operations:

    • DELETE - Text that exists in the first string but not in the second
    • INSERT - Text that exists in the second string but not in the first
    • EQUAL - Text that is common to both strings

    This class is particularly useful for implementing features like change highlighting in terminal applications, where differences between versions of text need to be displayed with proper styling.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  DiffHelper.Diff
      Class representing one diff operation.
      static class  DiffHelper.Operation
      The data structure representing a diff is a Linked list of Diff objects: {Diff(Operation.DELETE, "Hello"), Diff(Operation.INSERT, "Goodbye"), Diff(Operation.EQUAL, " world.")} which means: delete "Hello", add "Goodbye" and keep " world."
    • Method Detail

      • diff

        public static java.util.List<DiffHelper.Diff> diff​(AttributedString text1,
                                                           AttributedString text2)
        Compute the differences between two attributed strings.

        Returns a list of DiffHelper.Diff operations (EQUAL, INSERT, DELETE) that transform text1 into text2. Hidden character ranges are kept intact ? they are never split across diff segments.

        Parameters:
        text1 - the original text
        text2 - the modified text
        Returns:
        a list of diff operations