Class DocLineComparator

java.lang.Object
org.eclipse.compare.internal.DocLineComparator
All Implemented Interfaces:
ITokenComparator, IRangeComparator

public class DocLineComparator extends Object implements ITokenComparator
Implements the IRangeComparator interface for lines in a document. A DocLineComparator is used as the input for the RangeDifferencer engine to perform a line oriented compare on documents.

A DocLineComparator doesn't know anything about line separators because its notion of lines is solely defined in the underlying IDocument.

  • Constructor Details

    • DocLineComparator

      public DocLineComparator(IDocument document, IRegion region, boolean ignoreWhiteSpace)
      Creates a DocLineComparator for the given document range. ignoreWhiteSpace controls whether comparing lines (in method rangesEqual) should ignore whitespace.
      Parameters:
      document - the document from which the lines are taken
      region - if non-null only lines within this range are taken
      ignoreWhiteSpace - if true white space is ignored when comparing lines
    • DocLineComparator

      public DocLineComparator(IDocument document, IRegion region, boolean ignoreWhiteSpace, ICompareFilter[] compareFilters, char contributor, Optional<IIgnoreWhitespaceContributor> ignoreWhitespaceContributor)
      Creates a DocLineComparator for the given document range. ignoreWhiteSpace controls whether comparing lines (in method rangesEqual) should ignore whitespace. Compare filters may be used to affect the detection of line differences.
      Parameters:
      document - the document from which the lines are taken
      region - if non-null only lines within this range are taken
      ignoreWhiteSpace - if true white space is ignored when comparing lines
      compareFilters - the active compare filters for the compare
      contributor - contributor of document
      ignoreWhitespaceContributor - contributor for ignore whitespace logic, empty optional allowed, but null is not allowed
  • Method Details

    • getRangeCount

      public int getRangeCount()
      Returns the number of lines in the document.
      Specified by:
      getRangeCount in interface IRangeComparator
      Returns:
      number of lines
    • getTokenStart

      public int getTokenStart(int line)
      Description copied from interface: ITokenComparator
      Returns the start character position of the token with the given index. If the index is out of range (but not negative) the character position behind the last character (the length of the input string) is returned.
      Specified by:
      getTokenStart in interface ITokenComparator
      Parameters:
      line - index of the token for which to return the start position
      Returns:
      the start position of the token with the given index
    • getTokenLength

      public int getTokenLength(int line)
      Description copied from interface: ITokenComparator
      Returns the character length of the token with the given index. If the index is out of range (but not negative) the value 0 is returned.
      Specified by:
      getTokenLength in interface ITokenComparator
      Parameters:
      line - index of the token for which to return the start position
      Returns:
      the character length of the token with the given index
    • rangesEqual

      public boolean rangesEqual(int thisIndex, IRangeComparator otherComparator, int otherIndex)
      Returns true if a line given by the first index matches a line specified by the other IRangeComparator and index.
      Specified by:
      rangesEqual in interface IRangeComparator
      Parameters:
      thisIndex - the number of the line within this range comparator
      otherComparator - the range comparator to compare this with
      otherIndex - the number of the line within the other comparator
      Returns:
      true if the lines are equal
    • skipRangeComparison

      public boolean skipRangeComparison(int length, int maxLength, IRangeComparator other)
      Aborts the comparison if the number of tokens is too large.
      Specified by:
      skipRangeComparison in interface IRangeComparator
      Parameters:
      length - a number on which to base the decision whether to return true or false
      maxLength - another number on which to base the decision whether to return true or false
      other - the other IRangeComparator to compare with
      Returns:
      true to avoid a too lengthy range comparison