diff org.eclipse.text/src/org/eclipse/jface/text/link/InclusivePositionUpdater.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 5feec68b4556
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.eclipse.text/src/org/eclipse/jface/text/link/InclusivePositionUpdater.d	Sat Mar 14 18:23:29 2009 +0100
@@ -0,0 +1,126 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 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.text.link.InclusivePositionUpdater;
+
+import org.eclipse.jface.text.link.LinkedModeModel; // packageimport
+import org.eclipse.jface.text.link.LinkedPosition; // packageimport
+import org.eclipse.jface.text.link.ILinkedModeListener; // packageimport
+import org.eclipse.jface.text.link.TabStopIterator; // packageimport
+import org.eclipse.jface.text.link.LinkedModeUI; // packageimport
+import org.eclipse.jface.text.link.LinkedPositionGroup; // packageimport
+import org.eclipse.jface.text.link.LinkedModeManager; // packageimport
+import org.eclipse.jface.text.link.LinkedPositionAnnotations; // packageimport
+import org.eclipse.jface.text.link.ProposalPosition; // packageimport
+
+
+import java.lang.all;
+import java.util.Set;
+
+import org.eclipse.jface.text.BadPositionCategoryException;
+import org.eclipse.jface.text.DocumentEvent;
+import org.eclipse.jface.text.IPositionUpdater;
+import org.eclipse.jface.text.Position;
+
+
+/**
+ * Position updater that considers any change in
+ * <code>[p.offset,&nbsp;p.offset&nbsp;+&nbsp;p.length]</code> of a {@link Position}
+ * <code>p</code> as belonging to the position.
+ * <p>
+ * Internal class. Do not use. Public for testing purposes only.
+ * </p>
+ *
+ * @since 3.0
+ * @noinstantiate This class is not intended to be instantiated by clients.
+ * @noextend This class is not intended to be subclassed by clients.
+ */
+public class InclusivePositionUpdater : IPositionUpdater {
+
+    /** The position category. */
+    private const String fCategory;
+
+    /**
+     * Creates a new updater for the given <code>category</code>.
+     *
+     * @param category the new category.
+     */
+    public this(String category) {
+        fCategory= category;
+    }
+
+    /*
+     * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
+     */
+    public void update(DocumentEvent event) {
+
+        int eventOffset= event.getOffset();
+        int eventOldLength= event.getLength();
+        int eventNewLength= event.getText() is null ? 0 : event.getText().length();
+        int deltaLength= eventNewLength - eventOldLength;
+
+        try {
+            Position[] positions= event.getDocument().getPositions(fCategory);
+
+            for (int i= 0; i !is positions.length; i++) {
+
+                Position position= positions[i];
+
+                if (position.isDeleted())
+                    continue;
+
+                int offset= position.getOffset();
+                int length= position.getLength();
+                int end= offset + length;
+
+                if (offset > eventOffset + eventOldLength)
+                    // position comes way
+                    // after change - shift
+                    position.setOffset(offset + deltaLength);
+                else if (end < eventOffset) {
+                    // position comes way before change -
+                    // leave alone
+                } else if (offset <= eventOffset && end >= eventOffset + eventOldLength) {
+                    // event completely internal to the position - adjust length
+                    position.setLength(length + deltaLength);
+                } else if (offset < eventOffset) {
+                    // event extends over end of position - adjust length
+                    int newEnd= eventOffset + eventNewLength;
+                    position.setLength(newEnd - offset);
+                } else if (end > eventOffset + eventOldLength) {
+                    // event extends from before position into it - adjust offset
+                    // and length
+                    // offset becomes end of event, length adjusted accordingly
+                    // we want to recycle the overlapping part
+                    position.setOffset(eventOffset);
+                    int deleted= eventOffset + eventOldLength - offset;
+                    position.setLength(length - deleted + eventNewLength);
+                } else {
+                    // event consumes the position - delete it
+                    position.delete_();
+                }
+            }
+        } catch (BadPositionCategoryException e) {
+            // ignore and return
+        }
+    }
+
+    /**
+     * Returns the position category.
+     *
+     * @return the position category
+     */
+    public String getCategory() {
+        return fCategory;
+    }
+
+}