diff org.eclipse.jface.text/src/org/eclipse/jface/internal/text/revisions/ChangeRegion.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children 6f068362a363
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/internal/text/revisions/ChangeRegion.d	Sat Mar 14 18:23:29 2009 +0100
@@ -0,0 +1,177 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2006 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module org.eclipse.jface.internal.text.revisions.ChangeRegion;
+
+import org.eclipse.jface.internal.text.revisions.HunkComputer; // packageimport
+import org.eclipse.jface.internal.text.revisions.LineIndexOutOfBoundsException; // packageimport
+import org.eclipse.jface.internal.text.revisions.Hunk; // packageimport
+import org.eclipse.jface.internal.text.revisions.Colors; // packageimport
+import org.eclipse.jface.internal.text.revisions.Range; // packageimport
+import org.eclipse.jface.internal.text.revisions.RevisionPainter; // packageimport
+import org.eclipse.jface.internal.text.revisions.RevisionSelectionProvider; // packageimport
+
+import java.lang.all;
+import java.util.ListIterator;
+import java.util.List;
+import java.util.LinkedList;
+import tango.text.convert.Format;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.jface.text.revisions.Revision;
+import org.eclipse.jface.text.source.ILineRange;
+import org.eclipse.jface.text.source.LineRange;
+
+/**
+ * A change region describes a contiguous range of lines that was changed in the same revision of a
+ * document. Once it is adjusted to diff information, the originally contiguous range may be split
+ * into several ranges or even be empty.
+ *
+ * @since 3.2
+ */
+public final class ChangeRegion {
+    private const Revision fRevision;
+    private const ILineRange fLines;
+    private const List fAdjusted;
+
+    /**
+     * Creates a new change region for the given revision and line range.
+     *
+     * @param revision the revision of the new region
+     * @param lines the line range of the new region
+     * @throws IndexOutOfBoundsException if the line range is not well-formed
+     */
+    public this(Revision revision, ILineRange lines)  {
+        fAdjusted= new LinkedList();
+        Assert.isLegal(revision !is null);
+        Assert.isLegal(lines !is null);
+        fLines= Range.copy(lines);
+        fRevision=revision;
+        clearDiff();
+    }
+
+    /**
+     * Returns the revision that this region belongs to.
+     *
+     * @return the revision that this region belongs to
+     */
+    public Revision getRevision() {
+        return fRevision;
+    }
+
+    /**
+     * Returns the original (before applying diff information) line range of this change region.
+     *
+     * @return the original (before applying diff information) line range of this change region
+     */
+    public ILineRange getOriginalRange() {
+        return fLines;
+    }
+
+    /**
+     * Returns the list of {@link ILineRange}s of this change region for which the revision
+     * information is still valid.
+     *
+     * @return the list of adjusted line ranges
+     */
+    public List getAdjustedRanges() {
+        return fAdjusted;
+    }
+
+    /**
+     * Returns the line coverage of the adjusted ranges, an empty range if the coverage is empty.
+     *
+     * @return the line coverage of the adjusted ranges
+     */
+    public ILineRange getAdjustedCoverage() {
+        if (fAdjusted.isEmpty())
+            return new LineRange(fLines.getStartLine(), 0);
+
+        Range first= cast(Range) fAdjusted.get(0);
+        Range last= cast(Range) fAdjusted.get(fAdjusted.size() - 1);
+
+        return Range.createAbsolute(first.start(), last.end());
+    }
+
+    /**
+     * Clears any adjusted ranges, restoring the original range.
+     */
+    public void clearDiff() {
+        fAdjusted.clear();
+        fAdjusted.add(Range.copy(fLines));
+    }
+
+    /**
+     * Adjusts this change region to a diff hunk. This will change the adjusted ranges.
+     *
+     * @param hunk the diff hunk to adjust to
+     */
+    public void adjustTo(Hunk hunk) {
+        for (ListIterator it= fAdjusted.listIterator(); it.hasNext();) {
+            Range range= cast(Range) it.next();
+
+            // do we need a split?
+            int unchanged= getUnchanged(hunk, range.start());
+            if (unchanged > 0) {
+                if (unchanged >= range.length())
+                    continue;
+                range= range.split(unchanged);
+                it.add(range);
+                it.previous(); it.next(); // needed so we can remove below
+            }
+
+            int line= range.start();
+            Assert.isTrue(hunk.line <= line);
+
+            // by how much do we shrink?
+            int overlap= getOverlap(hunk, line);
+            if (overlap >= range.length()) {
+                it.remove();
+                continue;
+            }
+
+            // by how much do we move?
+            range.moveBy(hunk.delta + overlap);
+            range.resizeBy(-overlap);
+        }
+
+    }
+
+    private int getUnchanged(Hunk hunk, int line) {
+        return Math.max(0, hunk.line - line);
+    }
+
+    /*
+     * Returns the number of lines after line that the hunk reports as changed.
+     */
+    private int getOverlap(Hunk hunk, int line) {
+
+        int deltaLine= hunk.line + hunk.changed;
+        if (hunk.delta >= 0) {
+            if (deltaLine <= line)
+                return 0;
+            return deltaLine - line;
+        }
+
+        // hunk.delta < 0
+        int hunkEnd= deltaLine - hunk.delta;
+        int cutCount= hunkEnd - line;
+        return Math.max(0, cutCount);
+    }
+
+    /*
+     * @see java.lang.Object#toString()
+     */
+    public override String toString() {
+        return Format("ChangeRegion [{},[{}+{})]", fRevision.toString(), fLines.getStartLine(), fLines.getNumberOfLines() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+    }
+}