Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/NonDeletingPositionUpdater.d @ 133:7d818bd32d63
Fix ctors to this with gvim regexp
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 24 Aug 2008 01:29:22 +0200 |
parents | c4fb132a086c |
children | 75302ef3f92f |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 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.internal.text.NonDeletingPositionUpdater; | |
14 | |
131 | 15 import dwtx.jface.internal.text.InternalAccessor; // packageimport |
16 import dwtx.jface.internal.text.StickyHoverManager; // packageimport | |
17 import dwtx.jface.internal.text.InformationControlReplacer; // packageimport | |
18 import dwtx.jface.internal.text.TableOwnerDrawSupport; // packageimport | |
19 import dwtx.jface.internal.text.DelayedInputChangeListener; // packageimport | |
20 | |
21 | |
129 | 22 import dwt.dwthelper.utils; |
23 | |
24 import dwtx.jface.text.BadPositionCategoryException; | |
25 import dwtx.jface.text.DocumentEvent; | |
26 import dwtx.jface.text.IPositionUpdater; | |
27 import dwtx.jface.text.Position; | |
28 | |
29 | |
30 /** | |
31 * A position updater that never deletes a position. If the region containing | |
32 * the position is deleted, the position is moved to the beginning/end (falling | |
33 * together) of the change. If the region containing the position is replaced, | |
34 * the position is placed at the same location inside the replacement text, but | |
35 * always inside the replacement text. | |
36 * | |
37 * @since 3.1 | |
38 */ | |
39 public final class NonDeletingPositionUpdater : IPositionUpdater { | |
40 /** The position category. */ | |
41 private final String fCategory; | |
42 | |
43 /** | |
44 * Creates a new updater for the given <code>category</code>. | |
45 * | |
46 * @param category the new category. | |
47 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
48 public this(String category) { |
129 | 49 fCategory= category; |
50 } | |
51 | |
52 /* | |
53 * @see dwtx.jface.text.IPositionUpdater#update(dwtx.jface.text.DocumentEvent) | |
54 */ | |
55 public void update(DocumentEvent event) { | |
56 | |
57 int eventOffset= event.getOffset(); | |
58 int eventOldEndOffset= eventOffset + event.getLength(); | |
59 int eventNewLength= event.getText() is null ? 0 : event.getText().length(); | |
60 int eventNewEndOffset= eventOffset + eventNewLength; | |
61 int deltaLength= eventNewLength - event.getLength(); | |
62 | |
63 try { | |
64 Position[] positions= event.getDocument().getPositions(fCategory); | |
65 | |
66 for (int i= 0; i !is positions.length; i++) { | |
67 | |
68 Position position= positions[i]; | |
69 | |
70 if (position.isDeleted()) | |
71 continue; | |
72 | |
73 int offset= position.getOffset(); | |
74 int length= position.getLength(); | |
75 int end= offset + length; | |
76 | |
77 if (offset > eventOldEndOffset) { | |
78 // position comes way after change - shift | |
79 position.setOffset(offset + deltaLength); | |
80 } else if (end < eventOffset) { | |
81 // position comes way before change - leave alone | |
82 } else if (offset <= eventOffset && end >= eventOldEndOffset) { | |
83 // event completely internal to the position - adjust length | |
84 position.setLength(length + deltaLength); | |
85 } else if (offset < eventOffset) { | |
86 // event extends over end of position - include the | |
87 // replacement text into the position | |
88 position.setLength(eventNewEndOffset - offset); | |
89 } else if (end > eventOldEndOffset) { | |
90 // event extends from before position into it - adjust | |
91 // offset and length, including the replacement text into | |
92 // the position | |
93 position.setOffset(eventOffset); | |
94 int deleted= eventOldEndOffset - offset; | |
95 position.setLength(length - deleted + eventNewLength); | |
96 } else { | |
97 // event comprises the position - keep it at the same | |
98 // position, but always inside the replacement text | |
99 int newOffset= Math.min(offset, eventNewEndOffset); | |
100 int newEndOffset= Math.min(end, eventNewEndOffset); | |
101 position.setOffset(newOffset); | |
102 position.setLength(newEndOffset - newOffset); | |
103 } | |
104 } | |
105 } catch (BadPositionCategoryException e) { | |
106 // ignore and return | |
107 } | |
108 } | |
109 | |
110 /** | |
111 * Returns the position category. | |
112 * | |
113 * @return the position category | |
114 */ | |
115 public String getCategory() { | |
116 return fCategory; | |
117 } | |
118 } |