1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.template.contentassist;
13 import org.eclipse.jface.text.BadPositionCategoryException;
14 import org.eclipse.jface.text.DocumentEvent;
15 import org.eclipse.jface.text.IPositionUpdater;
16 import org.eclipse.jface.text.Position;
19 * Position updater that takes any change in [position.offset, position.offset +
20 * position.length] as belonging to the position.
24 class InclusivePositionUpdater implements IPositionUpdater {
26 /** The position category. */
27 private final String fCategory;
30 * Creates a new updater for the given <code>category</code>.
35 public InclusivePositionUpdater(String category) {
40 * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
42 public void update(DocumentEvent event) {
44 int eventOffset = event.getOffset();
45 int eventOldLength = event.getLength();
46 int eventNewLength = event.getText() == null ? 0 : event.getText()
48 int deltaLength = eventNewLength - eventOldLength;
51 Position[] positions = event.getDocument().getPositions(fCategory);
53 for (int i = 0; i != positions.length; i++) {
55 Position position = positions[i];
57 if (position.isDeleted())
60 int offset = position.getOffset();
61 int length = position.getLength();
62 int end = offset + length;
64 if (offset > eventOffset + eventOldLength)
66 // after change - shift
67 position.setOffset(offset + deltaLength);
68 else if (end < eventOffset) {
69 // position comes way before change -
71 } else if (offset <= eventOffset
72 && end >= eventOffset + eventOldLength) {
73 // event completely internal to the position - adjust length
74 position.setLength(length + deltaLength);
75 } else if (offset < eventOffset) {
76 // event extends over end of position - adjust length
77 int newEnd = eventOffset + eventNewLength;
78 position.setLength(newEnd - offset);
79 } else if (end > eventOffset + eventOldLength) {
80 // event extends from before position into it - adjust
83 // offset becomes end of event, length ajusted acordingly
84 // we want to recycle the overlapping part
85 position.setOffset(eventOffset);
86 int deleted = eventOffset + eventOldLength - offset;
87 position.setLength(length - deleted + eventNewLength);
89 // event consumes the position - delete it
93 } catch (BadPositionCategoryException e) {
99 * Returns the position category.
101 * @return the position category
103 public String getCategory() {