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
|
|
31 import java.util.ArrayList;
|
|
32 import java.util.HashMap;
|
|
33 import java.util.Iterator;
|
|
34 import java.util.List;
|
|
35 import java.util.Map;
|
|
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 */
|
|
78 public Reconciler() {
|
|
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 {
|
|
120 fStrategies.put(contentType, strategy);
|
|
121 if (strategy instanceof IReconcilingStrategyExtension && getProgressMonitor() !is null) {
|
|
122 IReconcilingStrategyExtension extension= (IReconcilingStrategyExtension) strategy;
|
|
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
|
|
138 return (IReconcilingStrategy) fStrategies.get(contentType);
|
|
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()) {
|
|
180 IReconcilingStrategy strategy= (IReconcilingStrategy) e.next();
|
|
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()) {
|
|
196 IReconcilingStrategy strategy= (IReconcilingStrategy) e.next();
|
|
197 if (strategy instanceof IReconcilingStrategyExtension) {
|
|
198 IReconcilingStrategyExtension extension= (IReconcilingStrategyExtension) strategy;
|
|
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);
|
|
218 if (s instanceof IReconcilingStrategyExtension) {
|
|
219 IReconcilingStrategyExtension e= (IReconcilingStrategyExtension) s;
|
|
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) {
|
|
239 regions= new TypedRegion[0];
|
|
240 }
|
|
241 return regions;
|
|
242 }
|
|
243 }
|