129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.templates.InclusivePositionUpdater;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import dwtx.jface.text.BadPositionCategoryException;
|
|
18 import dwtx.jface.text.DocumentEvent;
|
|
19 import dwtx.jface.text.IPositionUpdater;
|
|
20 import dwtx.jface.text.Position;
|
|
21
|
|
22 /**
|
|
23 * Position updater that takes any change in [position.offset, position.offset + position.length] as
|
|
24 * belonging to the position.
|
|
25 *
|
|
26 * @since 3.0
|
|
27 */
|
|
28 class InclusivePositionUpdater : IPositionUpdater {
|
|
29
|
|
30 /** The position category. */
|
|
31 private final String fCategory;
|
|
32
|
|
33 /**
|
|
34 * Creates a new updater for the given <code>category</code>.
|
|
35 *
|
|
36 * @param category the new category.
|
|
37 */
|
|
38 public InclusivePositionUpdater(String category) {
|
|
39 fCategory= category;
|
|
40 }
|
|
41
|
|
42 /*
|
|
43 * @see dwtx.jface.text.IPositionUpdater#update(dwtx.jface.text.DocumentEvent)
|
|
44 */
|
|
45 public void update(DocumentEvent event) {
|
|
46
|
|
47 int eventOffset= event.getOffset();
|
|
48 int eventOldLength= event.getLength();
|
|
49 int eventNewLength= event.getText() is null ? 0 : event.getText().length();
|
|
50 int deltaLength= eventNewLength - eventOldLength;
|
|
51
|
|
52 try {
|
|
53 Position[] positions= event.getDocument().getPositions(fCategory);
|
|
54
|
|
55 for (int i= 0; i !is positions.length; i++) {
|
|
56
|
|
57 Position position= positions[i];
|
|
58
|
|
59 if (position.isDeleted())
|
|
60 continue;
|
|
61
|
|
62 int offset= position.getOffset();
|
|
63 int length= position.getLength();
|
|
64 int end= offset + length;
|
|
65
|
|
66 if (offset > eventOffset + eventOldLength)
|
|
67 // position comes way
|
|
68 // after change - shift
|
|
69 position.setOffset(offset + deltaLength);
|
|
70 else if (end < eventOffset) {
|
|
71 // position comes way before change -
|
|
72 // leave alone
|
|
73 } else if (offset <= eventOffset && end >= eventOffset + eventOldLength) {
|
|
74 // event completely internal to the position - adjust length
|
|
75 position.setLength(length + deltaLength);
|
|
76 } else if (offset < eventOffset) {
|
|
77 // event extends over end of position - adjust length
|
|
78 int newEnd= eventOffset + eventNewLength;
|
|
79 position.setLength(newEnd - offset);
|
|
80 } else if (end > eventOffset + eventOldLength) {
|
|
81 // event extends from before position into it - adjust offset
|
|
82 // and length
|
|
83 // offset becomes end of event, length adjusted accordingly
|
|
84 // we want to recycle the overlapping part
|
|
85 position.setOffset(eventOffset);
|
|
86 int deleted= eventOffset + eventOldLength - offset;
|
|
87 position.setLength(length - deleted + eventNewLength);
|
|
88 } else {
|
|
89 // event consumes the position - delete it
|
|
90 position.delete();
|
|
91 }
|
|
92 }
|
|
93 } catch (BadPositionCategoryException e) {
|
|
94 // ignore and return
|
|
95 }
|
|
96 }
|
|
97
|
|
98 /**
|
|
99 * Returns the position category.
|
|
100 *
|
|
101 * @return the position category
|
|
102 */
|
|
103 public String getCategory() {
|
|
104 return fCategory;
|
|
105 }
|
|
106
|
|
107 }
|