diff options
Diffstat (limited to 'bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/DocLineComparator.java')
-rw-r--r-- | bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/DocLineComparator.java | 126 |
1 files changed, 111 insertions, 15 deletions
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/DocLineComparator.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/DocLineComparator.java index 8686bbbb3..c0a4e1abd 100644 --- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/DocLineComparator.java +++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/DocLineComparator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2009 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,9 +10,13 @@ *******************************************************************************/ package org.eclipse.compare.internal; -import org.eclipse.jface.text.*; +import org.eclipse.compare.ICompareFilter; import org.eclipse.compare.contentmergeviewer.ITokenComparator; import org.eclipse.compare.rangedifferencer.IRangeComparator; +import org.eclipse.core.internal.expressions.util.LRUCache; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; /** * Implements the <code>IRangeComparator</code> interface for lines in a document. @@ -29,6 +33,9 @@ public class DocLineComparator implements ITokenComparator { private int fLineCount; private int fLength; private boolean fIgnoreWhiteSpace; + private ICompareFilter[] fCompareFilters; + private char fContributor; + private LRUCache fCompareFilterCache; /** * Creates a <code>DocLineComparator</code> for the given document range. @@ -41,8 +48,47 @@ public class DocLineComparator implements ITokenComparator { */ public DocLineComparator(IDocument document, IRegion region, boolean ignoreWhiteSpace) { + this(document, region, ignoreWhiteSpace, null, '?'); + } + + /** + * Creates a <code>DocLineComparator</code> for the given document range. + * ignoreWhiteSpace controls whether comparing lines (in method + * <code>rangesEqual<code>) should ignore whitespace. Compare filters may be used + * to affect the detection of line differences. + * + * @param document + * the document from which the lines are taken + * @param region + * if non-<code>null</code> only lines within this range are + * taken + * @param ignoreWhiteSpace + * if <code>true</code> white space is ignored when comparing + * lines + * @param compareFilters + * the active compare filters for the compare + * @param contributor + * contributor of document + */ + public DocLineComparator(IDocument document, IRegion region, + boolean ignoreWhiteSpace, ICompareFilter[] compareFilters, + char contributor) { fDocument = document; fIgnoreWhiteSpace = ignoreWhiteSpace; + fCompareFilters = compareFilters; + fContributor = contributor; + + boolean cacheFilteredLines = false; + if (compareFilters != null && compareFilters.length > 0) { + cacheFilteredLines = true; + for (int i = 0; i < compareFilters.length; i++) { + if (!compareFilters[i].canCacheFilteredRegions()) { + cacheFilteredLines = false; + break; + } + } + } + fCompareFilterCache = (cacheFilteredLines) ? new LRUCache(1024) : null; fLineOffset = 0; if (region != null) { @@ -116,18 +162,18 @@ public class DocLineComparator implements ITokenComparator { DocLineComparator other= (DocLineComparator) otherComparator; if (fIgnoreWhiteSpace) { - String s1= extract(thisIndex); - String s2= other.extract(otherIndex); - //return s1.trim().equals(s2.trim()); - return compare(s1, s2); + String[] linesToCompare = extract(thisIndex, otherIndex, other, false); + return compare(linesToCompare[0], linesToCompare[1]); } int tlen= getTokenLength(thisIndex); int olen= other.getTokenLength(otherIndex); - if (tlen == olen) { - String s1= extract(thisIndex); - String s2= other.extract(otherIndex); - return s1.equals(s2); + if (fCompareFilters != null && fCompareFilters.length > 0) { + String[] linesToCompare = extract(thisIndex, otherIndex, other, true); + return linesToCompare[0].equals(linesToCompare[1]); + } else if (tlen == olen) { + String[] linesToCompare = extract(thisIndex, otherIndex, other, false); + return linesToCompare[0].equals(linesToCompare[1]); } } return false; @@ -149,24 +195,74 @@ public class DocLineComparator implements ITokenComparator { //---- private methods + private String[] extract(int thisIndex, int otherIndex, + DocLineComparator other, boolean includeSeparator) { + + String[] extracts = new String[2]; + if (fCompareFilters != null && fCompareFilters.length > 0) { + if (fCompareFilterCache != null + && other.fCompareFilterCache != null) { + extracts[0] = (String) fCompareFilterCache.get(new Integer( + thisIndex)); + if (extracts[0] == null) { + extracts[0] = Utilities.applyCompareFilters( + extract(thisIndex, includeSeparator), fContributor, + other.extract(otherIndex, includeSeparator), other.fContributor, + fCompareFilters); + fCompareFilterCache + .put(new Integer(thisIndex), extracts[0]); + } + + extracts[1] = (String) other.fCompareFilterCache + .get(new Integer(otherIndex)); + if (extracts[1] == null) { + extracts[1] = Utilities.applyCompareFilters( + other.extract(otherIndex, includeSeparator), other.fContributor, + extract(thisIndex, includeSeparator), fContributor, fCompareFilters); + other.fCompareFilterCache.put(new Integer(otherIndex), + extracts[1]); + } + } else { + String thisLine = extract(thisIndex, includeSeparator); + String otherLine = other.extract(otherIndex, includeSeparator); + extracts = new String[] { + Utilities.applyCompareFilters(thisLine, fContributor, + otherLine, other.fContributor, fCompareFilters), + Utilities.applyCompareFilters(otherLine, + other.fContributor, thisLine, fContributor, + fCompareFilters) }; + } + } else { + extracts = new String[] { extract(thisIndex, includeSeparator), + other.extract(otherIndex, includeSeparator) }; + } + return extracts; + } + /** - * Extract a single line from the underlying document without the line separator. + * Extract a single line from the underlying document. * * @param line the number of the line to extract + * @param whether to include the line separator * @return the contents of the line as a String */ - private String extract(int line) { + private String extract(int line, boolean includeSeparator) { if (line < fLineCount) { try { - IRegion r= fDocument.getLineInformation(fLineOffset + line); + if (includeSeparator) + return fDocument.get(fDocument.getLineOffset(line), + fDocument.getLineLength(line)); + + IRegion r = fDocument.getLineInformation(fLineOffset + line); return fDocument.get(r.getOffset(), r.getLength()); - } catch(BadLocationException e) { + + } catch (BadLocationException e) { // silently ignored } } return ""; //$NON-NLS-1$ } - + private boolean compare(String s1, String s2) { int l1= s1.length(); int l2= s2.length(); |