129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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.link.InclusivePositionUpdater;
|
|
14
|
131
|
15 import dwtx.jface.text.link.LinkedModeModel; // packageimport
|
|
16 import dwtx.jface.text.link.LinkedPosition; // packageimport
|
|
17 import dwtx.jface.text.link.ILinkedModeListener; // packageimport
|
|
18 import dwtx.jface.text.link.TabStopIterator; // packageimport
|
|
19 import dwtx.jface.text.link.LinkedModeUI; // packageimport
|
|
20 import dwtx.jface.text.link.LinkedPositionGroup; // packageimport
|
|
21 import dwtx.jface.text.link.LinkedModeManager; // packageimport
|
|
22 import dwtx.jface.text.link.LinkedPositionAnnotations; // packageimport
|
|
23 import dwtx.jface.text.link.ProposalPosition; // packageimport
|
|
24
|
|
25
|
129
|
26 import dwt.dwthelper.utils;
|
|
27
|
|
28 import dwtx.jface.text.BadPositionCategoryException;
|
|
29 import dwtx.jface.text.DocumentEvent;
|
|
30 import dwtx.jface.text.IPositionUpdater;
|
|
31 import dwtx.jface.text.Position;
|
|
32
|
|
33
|
|
34 /**
|
|
35 * Position updater that considers any change in
|
|
36 * <code>[p.offset, p.offset + p.length]</code> of a {@link Position}
|
|
37 * <code>p</code> as belonging to the position.
|
|
38 * <p>
|
|
39 * Internal class. Do not use. Public for testing purposes only.
|
|
40 * </p>
|
|
41 *
|
|
42 * @since 3.0
|
|
43 * @noinstantiate This class is not intended to be instantiated by clients.
|
|
44 * @noextend This class is not intended to be subclassed by clients.
|
|
45 */
|
|
46 public class InclusivePositionUpdater : IPositionUpdater {
|
|
47
|
|
48 /** The position category. */
|
|
49 private final String fCategory;
|
|
50
|
|
51 /**
|
|
52 * Creates a new updater for the given <code>category</code>.
|
|
53 *
|
|
54 * @param category the new category.
|
|
55 */
|
|
56 public InclusivePositionUpdater(String category) {
|
|
57 fCategory= category;
|
|
58 }
|
|
59
|
|
60 /*
|
|
61 * @see dwtx.jface.text.IPositionUpdater#update(dwtx.jface.text.DocumentEvent)
|
|
62 */
|
|
63 public void update(DocumentEvent event) {
|
|
64
|
|
65 int eventOffset= event.getOffset();
|
|
66 int eventOldLength= event.getLength();
|
|
67 int eventNewLength= event.getText() is null ? 0 : event.getText().length();
|
|
68 int deltaLength= eventNewLength - eventOldLength;
|
|
69
|
|
70 try {
|
|
71 Position[] positions= event.getDocument().getPositions(fCategory);
|
|
72
|
|
73 for (int i= 0; i !is positions.length; i++) {
|
|
74
|
|
75 Position position= positions[i];
|
|
76
|
|
77 if (position.isDeleted())
|
|
78 continue;
|
|
79
|
|
80 int offset= position.getOffset();
|
|
81 int length= position.getLength();
|
|
82 int end= offset + length;
|
|
83
|
|
84 if (offset > eventOffset + eventOldLength)
|
|
85 // position comes way
|
|
86 // after change - shift
|
|
87 position.setOffset(offset + deltaLength);
|
|
88 else if (end < eventOffset) {
|
|
89 // position comes way before change -
|
|
90 // leave alone
|
|
91 } else if (offset <= eventOffset && end >= eventOffset + eventOldLength) {
|
|
92 // event completely internal to the position - adjust length
|
|
93 position.setLength(length + deltaLength);
|
|
94 } else if (offset < eventOffset) {
|
|
95 // event extends over end of position - adjust length
|
|
96 int newEnd= eventOffset + eventNewLength;
|
|
97 position.setLength(newEnd - offset);
|
|
98 } else if (end > eventOffset + eventOldLength) {
|
|
99 // event extends from before position into it - adjust offset
|
|
100 // and length
|
|
101 // offset becomes end of event, length adjusted accordingly
|
|
102 // we want to recycle the overlapping part
|
|
103 position.setOffset(eventOffset);
|
|
104 int deleted= eventOffset + eventOldLength - offset;
|
|
105 position.setLength(length - deleted + eventNewLength);
|
|
106 } else {
|
|
107 // event consumes the position - delete it
|
|
108 position.delete();
|
|
109 }
|
|
110 }
|
|
111 } catch (BadPositionCategoryException e) {
|
|
112 // ignore and return
|
|
113 }
|
|
114 }
|
|
115
|
|
116 /**
|
|
117 * Returns the position category.
|
|
118 *
|
|
119 * @return the position category
|
|
120 */
|
|
121 public String getCategory() {
|
|
122 return fCategory;
|
|
123 }
|
|
124
|
|
125 }
|