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.projection.FragmentUpdater;
|
|
14
|
131
|
15 import dwtx.jface.text.projection.ProjectionMapping; // packageimport
|
|
16 import dwtx.jface.text.projection.ChildDocumentManager; // packageimport
|
|
17 import dwtx.jface.text.projection.SegmentUpdater; // packageimport
|
|
18 import dwtx.jface.text.projection.Segment; // packageimport
|
|
19 import dwtx.jface.text.projection.ProjectionDocument; // packageimport
|
|
20 import dwtx.jface.text.projection.ProjectionDocumentEvent; // packageimport
|
|
21 import dwtx.jface.text.projection.ChildDocument; // packageimport
|
|
22 import dwtx.jface.text.projection.IMinimalMapping; // packageimport
|
|
23 import dwtx.jface.text.projection.Fragment; // packageimport
|
|
24 import dwtx.jface.text.projection.ProjectionTextStore; // packageimport
|
|
25 import dwtx.jface.text.projection.ProjectionDocumentManager; // packageimport
|
|
26
|
|
27
|
129
|
28 import dwt.dwthelper.utils;
|
|
29
|
|
30
|
|
31 import dwtx.jface.text.BadLocationException;
|
|
32 import dwtx.jface.text.BadPositionCategoryException;
|
|
33 import dwtx.jface.text.DefaultPositionUpdater;
|
|
34 import dwtx.jface.text.DocumentEvent;
|
|
35 import dwtx.jface.text.IDocument;
|
|
36 import dwtx.jface.text.Position;
|
|
37
|
|
38
|
|
39 /**
|
|
40 * The position updater used to adapt the fragments of a master document. If an
|
|
41 * insertion happens at a fragment's offset, the fragment is extended rather
|
|
42 * than shifted. Also, the last fragment is extended if an insert operation
|
|
43 * happens at the end of the fragment.
|
|
44 *
|
|
45 * @since 3.0
|
|
46 */
|
|
47 class FragmentUpdater : DefaultPositionUpdater {
|
|
48
|
|
49 /** Indicates whether the position being updated represents the last fragment. */
|
|
50 private bool fIsLast= false;
|
|
51
|
|
52 /**
|
|
53 * Creates the fragment updater for the given category.
|
|
54 *
|
|
55 * @param fragmentCategory the position category used for managing the fragments of a document
|
|
56 */
|
162
|
57 /+protected+/ this(String fragmentCategory) {
|
129
|
58 super(fragmentCategory);
|
|
59 }
|
|
60
|
|
61 /*
|
|
62 * @see dwtx.jface.text.IPositionUpdater#update(dwtx.jface.text.DocumentEvent)
|
|
63 */
|
|
64 public void update(DocumentEvent event) {
|
|
65
|
|
66 try {
|
|
67
|
|
68 Position[] category= event.getDocument().getPositions(getCategory());
|
|
69
|
|
70 fOffset= event.getOffset();
|
|
71 fLength= event.getLength();
|
|
72 fReplaceLength= (event.getText() is null ? 0 : event.getText().length());
|
|
73 fDocument= event.getDocument();
|
|
74
|
|
75 for (int i= 0; i < category.length; i++) {
|
|
76
|
|
77 fPosition= category[i];
|
|
78 fIsLast= (i is category.length -1);
|
|
79
|
|
80 fOriginalPosition.offset= fPosition.offset;
|
|
81 fOriginalPosition.length= fPosition.length;
|
|
82
|
|
83 if (notDeleted())
|
|
84 adaptToReplace();
|
|
85 }
|
|
86
|
|
87 } catch (BadPositionCategoryException x) {
|
|
88 // do nothing
|
|
89 }
|
|
90 }
|
|
91
|
|
92 /*
|
|
93 * @see dwtx.jface.text.DefaultPositionUpdater#adaptToInsert()
|
|
94 */
|
|
95 protected void adaptToInsert() {
|
|
96 int myStart= fPosition.offset;
|
|
97 int myEnd= Math.max(myStart, fPosition.offset + fPosition.length - (fIsLast || isAffectingReplace() ? 0 : 1));
|
|
98
|
|
99 if (myEnd < fOffset)
|
|
100 return;
|
|
101
|
|
102 if (fLength <= 0) {
|
|
103
|
|
104 if (myStart <= fOffset)
|
|
105 fPosition.length += fReplaceLength;
|
|
106 else
|
|
107 fPosition.offset += fReplaceLength;
|
|
108
|
|
109 } else {
|
|
110
|
|
111 if (myStart <= fOffset && fOriginalPosition.offset <= fOffset)
|
|
112 fPosition.length += fReplaceLength;
|
|
113 else
|
|
114 fPosition.offset += fReplaceLength;
|
|
115 }
|
|
116 }
|
|
117
|
|
118 /**
|
|
119 * Returns whether this updater considers any position affected by the given document event. A
|
|
120 * position is affected if <code>event</code> {@link Position#overlapsWith(int, int) overlaps}
|
|
121 * with it but not if the position is only shifted.
|
162
|
122 *
|
129
|
123 * @param event the event
|
|
124 * @return <code>true</code> if there is any affected position, <code>false</code> otherwise
|
|
125 */
|
|
126 public bool affectsPositions(DocumentEvent event) {
|
|
127 IDocument document= event.getDocument();
|
|
128 try {
|
|
129
|
|
130 int index= document.computeIndexInCategory(getCategory(), event.getOffset());
|
|
131 Position[] fragments= document.getPositions(getCategory());
|
|
132
|
|
133 if (0 < index) {
|
|
134 Position fragment= fragments[index - 1];
|
|
135 if (fragment.overlapsWith(event.getOffset(), event.getLength()))
|
|
136 return true;
|
|
137 if (index is fragments.length && fragment.offset + fragment.length is event.getOffset())
|
|
138 return true;
|
|
139 }
|
|
140
|
|
141 if (index < fragments.length) {
|
|
142 Position fragment= fragments[index];
|
|
143 return fragment.overlapsWith(event.getOffset(), event.getLength());
|
|
144 }
|
|
145
|
|
146 } catch (BadLocationException x) {
|
|
147 } catch (BadPositionCategoryException x) {
|
|
148 }
|
|
149
|
|
150 return false;
|
|
151 }
|
|
152 }
|