Mercurial > projects > dwt-addons
annotate dwtx/jface/text/reconciler/Reconciler.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | f70d9508c95c |
children |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 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.reconciler.Reconciler; | |
14 | |
131 | 15 import dwtx.jface.text.reconciler.IReconciler; // packageimport |
16 import dwtx.jface.text.reconciler.DirtyRegionQueue; // packageimport | |
17 import dwtx.jface.text.reconciler.IReconcilingStrategy; // packageimport | |
18 import dwtx.jface.text.reconciler.AbstractReconcileStep; // packageimport | |
19 import dwtx.jface.text.reconciler.IReconcilingStrategyExtension; // packageimport | |
20 import dwtx.jface.text.reconciler.MonoReconciler; // packageimport | |
21 import dwtx.jface.text.reconciler.IReconcileStep; // packageimport | |
22 import dwtx.jface.text.reconciler.AbstractReconciler; // packageimport | |
23 import dwtx.jface.text.reconciler.IReconcilableModel; // packageimport | |
24 import dwtx.jface.text.reconciler.DirtyRegion; // packageimport | |
25 import dwtx.jface.text.reconciler.IReconcileResult; // packageimport | |
26 import dwtx.jface.text.reconciler.IReconcilerExtension; // packageimport | |
27 | |
28 | |
129 | 29 import dwt.dwthelper.utils; |
30 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
31 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
32 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
33 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
34 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
35 |
129 | 36 |
37 import dwtx.core.runtime.Assert; | |
38 import dwtx.core.runtime.IProgressMonitor; | |
39 import dwtx.jface.text.BadLocationException; | |
40 import dwtx.jface.text.IDocument; | |
41 import dwtx.jface.text.IDocumentExtension3; | |
42 import dwtx.jface.text.IRegion; | |
43 import dwtx.jface.text.ITypedRegion; | |
44 import dwtx.jface.text.Region; | |
45 import dwtx.jface.text.TextUtilities; | |
46 import dwtx.jface.text.TypedRegion; | |
47 | |
48 | |
49 /** | |
50 * Standard implementation of {@link dwtx.jface.text.reconciler.IReconciler}. | |
51 * The reconciler is configured with a set of {@linkplain dwtx.jface.text.reconciler.IReconcilingStrategy reconciling strategies} | |
52 * each of which is responsible for a particular content type. | |
53 * <p> | |
54 * Usually, clients instantiate this class and configure it before using it. | |
55 * </p> | |
56 * | |
57 * @see dwtx.jface.text.IDocumentListener | |
58 * @see dwtx.jface.text.ITextInputListener | |
59 * @see dwtx.jface.text.reconciler.DirtyRegion | |
60 */ | |
61 public class Reconciler : AbstractReconciler , IReconcilerExtension { | |
62 | |
63 /** The map of reconciling strategies. */ | |
64 private Map fStrategies; | |
65 | |
66 /** | |
67 * The partitioning this reconciler uses. | |
68 *@since 3.0 | |
69 */ | |
70 private String fPartitioning; | |
71 | |
72 /** | |
73 * Creates a new reconciler with the following configuration: it is | |
74 * an incremental reconciler with a standard delay of 500 milliseconds. There | |
75 * are no predefined reconciling strategies. The partitioning it uses | |
76 * is the default partitioning {@link IDocumentExtension3#DEFAULT_PARTITIONING}. | |
77 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
78 public this() { |
129 | 79 super(); |
80 fPartitioning= IDocumentExtension3.DEFAULT_PARTITIONING; | |
81 } | |
82 | |
83 /** | |
84 * Sets the document partitioning for this reconciler. | |
85 * | |
86 * @param partitioning the document partitioning for this reconciler | |
87 * @since 3.0 | |
88 */ | |
89 public void setDocumentPartitioning(String partitioning) { | |
90 Assert.isNotNull(partitioning); | |
91 fPartitioning= partitioning; | |
92 } | |
93 | |
94 /* | |
95 * @see dwtx.jface.text.reconciler.IReconcilerExtension#getDocumentPartitioning() | |
96 * @since 3.0 | |
97 */ | |
98 public String getDocumentPartitioning() { | |
99 return fPartitioning; | |
100 } | |
101 | |
102 /** | |
103 * Registers a given reconciling strategy for a particular content type. | |
104 * If there is already a strategy registered for this type, the new strategy | |
105 * is registered instead of the old one. | |
106 * | |
107 * @param strategy the reconciling strategy to register, or <code>null</code> to remove an existing one | |
108 * @param contentType the content type under which to register | |
109 */ | |
110 public void setReconcilingStrategy(IReconcilingStrategy strategy, String contentType) { | |
111 | |
112 Assert.isNotNull(contentType); | |
113 | |
114 if (fStrategies is null) | |
115 fStrategies= new HashMap(); | |
116 | |
117 if (strategy is null) | |
118 fStrategies.remove(contentType); | |
119 else { | |
162 | 120 fStrategies.put(contentType, cast(Object)strategy); |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
121 if (cast(IReconcilingStrategyExtension )strategy && getProgressMonitor() !is null) { |
134 | 122 IReconcilingStrategyExtension extension= cast(IReconcilingStrategyExtension) strategy; |
129 | 123 extension.setProgressMonitor(getProgressMonitor()); |
124 } | |
125 } | |
126 } | |
127 | |
128 /* | |
129 * @see IReconciler#getReconcilingStrategy(String) | |
130 */ | |
131 public IReconcilingStrategy getReconcilingStrategy(String contentType) { | |
132 | |
133 Assert.isNotNull(contentType); | |
134 | |
135 if (fStrategies is null) | |
136 return null; | |
137 | |
134 | 138 return cast(IReconcilingStrategy) fStrategies.get(contentType); |
129 | 139 } |
140 | |
141 /** | |
142 * Processes a dirty region. If the dirty region is <code>null</code> the whole | |
143 * document is consider being dirty. The dirty region is partitioned by the | |
144 * document and each partition is handed over to a reconciling strategy registered | |
145 * for the partition's content type. | |
146 * | |
147 * @param dirtyRegion the dirty region to be processed | |
148 * @see AbstractReconciler#process(DirtyRegion) | |
149 */ | |
150 protected void process(DirtyRegion dirtyRegion) { | |
151 | |
152 IRegion region= dirtyRegion; | |
153 | |
154 if (region is null) | |
155 region= new Region(0, getDocument().getLength()); | |
156 | |
157 ITypedRegion[] regions= computePartitioning(region.getOffset(), region.getLength()); | |
158 | |
159 for (int i= 0; i < regions.length; i++) { | |
160 ITypedRegion r= regions[i]; | |
161 IReconcilingStrategy s= getReconcilingStrategy(r.getType()); | |
162 if (s is null) | |
163 continue; | |
164 | |
165 if(dirtyRegion !is null) | |
166 s.reconcile(dirtyRegion, r); | |
167 else | |
168 s.reconcile(r); | |
169 } | |
170 } | |
171 | |
172 /* | |
173 * @see AbstractReconciler#reconcilerDocumentChanged(IDocument) | |
174 * @since 2.0 | |
175 */ | |
176 protected void reconcilerDocumentChanged(IDocument document) { | |
177 if (fStrategies !is null) { | |
178 Iterator e= fStrategies.values().iterator(); | |
179 while (e.hasNext()) { | |
134 | 180 IReconcilingStrategy strategy= cast(IReconcilingStrategy) e.next(); |
129 | 181 strategy.setDocument(document); |
182 } | |
183 } | |
184 } | |
185 | |
186 /* | |
187 * @see AbstractReconciler#setProgressMonitor(IProgressMonitor) | |
188 * @since 2.0 | |
189 */ | |
190 public void setProgressMonitor(IProgressMonitor monitor) { | |
191 super.setProgressMonitor(monitor); | |
192 | |
193 if (fStrategies !is null) { | |
194 Iterator e= fStrategies.values().iterator(); | |
195 while (e.hasNext()) { | |
134 | 196 IReconcilingStrategy strategy= cast(IReconcilingStrategy) e.next(); |
138 | 197 if ( cast(IReconcilingStrategyExtension)strategy ) { |
134 | 198 IReconcilingStrategyExtension extension= cast(IReconcilingStrategyExtension) strategy; |
129 | 199 extension.setProgressMonitor(monitor); |
200 } | |
201 } | |
202 } | |
203 } | |
204 | |
205 /* | |
206 * @see AbstractReconciler#initialProcess() | |
207 * @since 2.0 | |
208 */ | |
209 protected void initialProcess() { | |
210 ITypedRegion[] regions= computePartitioning(0, getDocument().getLength()); | |
211 List contentTypes= new ArrayList(regions.length); | |
212 for (int i= 0; i < regions.length; i++) { | |
213 String contentType= regions[i].getType(); | |
214 if( contentTypes.contains(contentType)) | |
215 continue; | |
216 contentTypes.add(contentType); | |
217 IReconcilingStrategy s= getReconcilingStrategy(contentType); | |
138 | 218 if ( cast(IReconcilingStrategyExtension)s ) { |
134 | 219 IReconcilingStrategyExtension e= cast(IReconcilingStrategyExtension) s; |
129 | 220 e.initialReconcile(); |
221 } | |
222 } | |
223 } | |
224 | |
225 /** | |
226 * Computes and returns the partitioning for the given region of the input document | |
227 * of the reconciler's connected text viewer. | |
228 * | |
229 * @param offset the region offset | |
230 * @param length the region length | |
231 * @return the computed partitioning | |
232 * @since 3.0 | |
233 */ | |
234 private ITypedRegion[] computePartitioning(int offset, int length) { | |
235 ITypedRegion[] regions= null; | |
236 try { | |
237 regions= TextUtilities.computePartitioning(getDocument(), getDocumentPartitioning(), offset, length, false); | |
238 } catch (BadLocationException x) { | |
162 | 239 regions= new ITypedRegion[0]; |
129 | 240 } |
241 return regions; | |
242 } | |
243 } |