129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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
|
131
|
14
|
|
15 import dwtx.jface.text.formatter.ContextBasedFormattingStrategy; // packageimport
|
|
16 import dwtx.jface.text.formatter.FormattingContext; // packageimport
|
|
17 import dwtx.jface.text.formatter.IFormattingStrategy; // packageimport
|
|
18 import dwtx.jface.text.formatter.IContentFormatterExtension; // packageimport
|
|
19 import dwtx.jface.text.formatter.IFormattingStrategyExtension; // packageimport
|
|
20 import dwtx.jface.text.formatter.IContentFormatter; // packageimport
|
|
21 import dwtx.jface.text.formatter.FormattingContextProperties; // packageimport
|
|
22 import dwtx.jface.text.formatter.ContentFormatter; // packageimport
|
|
23 import dwtx.jface.text.formatter.IFormattingContext; // packageimport
|
|
24
|
129
|
25 module dwtx.jface.text.formatter.MultiPassContentFormatter;
|
|
26
|
|
27 import dwt.dwthelper.utils;
|
|
28
|
|
29 import java.util.HashMap;
|
|
30 import java.util.Map;
|
|
31
|
|
32 import dwtx.core.runtime.Assert;
|
|
33 import dwtx.jface.text.BadLocationException;
|
|
34 import dwtx.jface.text.DefaultPositionUpdater;
|
|
35 import dwtx.jface.text.IDocument;
|
|
36 import dwtx.jface.text.IRegion;
|
|
37 import dwtx.jface.text.ITypedRegion;
|
|
38 import dwtx.jface.text.TextUtilities;
|
|
39 import dwtx.jface.text.TypedPosition;
|
|
40
|
|
41 /**
|
|
42 * Content formatter for edit-based formatting strategies.
|
|
43 * <p>
|
|
44 * Two kinds of formatting strategies can be registered with this formatter:
|
|
45 * <ul>
|
|
46 * <li>one master formatting strategy for the default content type</li>
|
|
47 * <li>one formatting strategy for each non-default content type</li>
|
|
48 * </ul>
|
|
49 * The master formatting strategy always formats the whole region to be
|
|
50 * formatted in the first pass. In a second pass, all partitions of the region
|
|
51 * to be formatted that are not of master content type are formatted using the
|
|
52 * slave formatting strategy registered for the underlying content type. All
|
|
53 * formatting strategies must implement {@link IFormattingStrategyExtension}.
|
|
54 * <p>
|
|
55 * Regions to be formatted with the master formatting strategy always have
|
|
56 * an offset aligned to the line start. Regions to be formatted with slave formatting
|
|
57 * strategies are aligned on partition boundaries.
|
|
58 *
|
|
59 * @see IFormattingStrategyExtension
|
|
60 * @since 3.0
|
|
61 */
|
|
62 public class MultiPassContentFormatter : IContentFormatter, IContentFormatterExtension {
|
|
63
|
|
64 /**
|
|
65 * Position updater that shifts otherwise deleted positions to the next
|
|
66 * non-whitespace character. The length of the positions are truncated to
|
|
67 * one if the position was shifted.
|
|
68 */
|
|
69 protected class NonDeletingPositionUpdater : DefaultPositionUpdater {
|
|
70
|
|
71 /**
|
|
72 * Creates a new non-deleting position updater.
|
|
73 *
|
|
74 * @param category The position category to update its positions
|
|
75 */
|
|
76 public NonDeletingPositionUpdater(final String category) {
|
|
77 super(category);
|
|
78 }
|
|
79
|
|
80 /*
|
|
81 * @see dwtx.jface.text.DefaultPositionUpdater#notDeleted()
|
|
82 */
|
|
83 protected final bool notDeleted() {
|
|
84
|
|
85 if (fOffset < fPosition.offset && (fPosition.offset + fPosition.length < fOffset + fLength)) {
|
|
86
|
|
87 int offset= fOffset + fLength;
|
|
88 if (offset < fDocument.getLength()) {
|
|
89
|
|
90 try {
|
|
91
|
|
92 bool moved= false;
|
|
93 char character= fDocument.getChar(offset);
|
|
94
|
|
95 while (offset < fDocument.getLength() && Character.isWhitespace(character)) {
|
|
96
|
|
97 moved= true;
|
|
98 character= fDocument.getChar(offset++);
|
|
99 }
|
|
100
|
|
101 if (moved)
|
|
102 offset--;
|
|
103
|
|
104 } catch (BadLocationException exception) {
|
|
105 // Can not happen
|
|
106 }
|
|
107
|
|
108 fPosition.offset= offset;
|
|
109 fPosition.length= 0;
|
|
110 }
|
|
111 }
|
|
112 return true;
|
|
113 }
|
|
114 }
|
|
115
|
|
116 /** The master formatting strategy */
|
|
117 private IFormattingStrategyExtension fMaster= null;
|
|
118 /** The partitioning of this content formatter */
|
|
119 private final String fPartitioning;
|
|
120 /** The slave formatting strategies */
|
|
121 private final Map fSlaves= new HashMap();
|
|
122 /** The default content type */
|
|
123 private final String fType;
|
|
124
|
|
125 /**
|
|
126 * Creates a new content formatter.
|
|
127 *
|
|
128 * @param partitioning the document partitioning for this formatter
|
|
129 * @param type the default content type
|
|
130 */
|
|
131 public MultiPassContentFormatter(final String partitioning, final String type) {
|
|
132 fPartitioning= partitioning;
|
|
133 fType= type;
|
|
134 }
|
|
135
|
|
136 /*
|
|
137 * @see dwtx.jface.text.formatter.IContentFormatterExtension#format(dwtx.jface.text.IDocument, dwtx.jface.text.formatter.IFormattingContext)
|
|
138 */
|
|
139 public final void format(final IDocument medium, final IFormattingContext context) {
|
|
140
|
|
141 context.setProperty(FormattingContextProperties.CONTEXT_MEDIUM, medium);
|
|
142
|
|
143 final Boolean document= (Boolean)context.getProperty(FormattingContextProperties.CONTEXT_DOCUMENT);
|
|
144 if (document is null || !document.booleanValue()) {
|
|
145
|
|
146 final IRegion region= (IRegion)context.getProperty(FormattingContextProperties.CONTEXT_REGION);
|
|
147 if (region !is null) {
|
|
148 try {
|
|
149 formatMaster(context, medium, region.getOffset(), region.getLength());
|
|
150 } finally {
|
|
151 formatSlaves(context, medium, region.getOffset(), region.getLength());
|
|
152 }
|
|
153 }
|
|
154 } else {
|
|
155 try {
|
|
156 formatMaster(context, medium, 0, medium.getLength());
|
|
157 } finally {
|
|
158 formatSlaves(context, medium, 0, medium.getLength());
|
|
159 }
|
|
160 }
|
|
161 }
|
|
162
|
|
163 /*
|
|
164 * @see dwtx.jface.text.formatter.IContentFormatter#format(dwtx.jface.text.IDocument, dwtx.jface.text.IRegion)
|
|
165 */
|
|
166 public final void format(final IDocument medium, final IRegion region) {
|
|
167
|
|
168 final FormattingContext context= new FormattingContext();
|
|
169
|
|
170 context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
|
|
171 context.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
|
|
172
|
|
173 format(medium, context);
|
|
174 }
|
|
175
|
|
176 /**
|
|
177 * Formats the document specified in the formatting context with the master
|
|
178 * formatting strategy.
|
|
179 * <p>
|
|
180 * The master formatting strategy covers all regions of the document. The
|
|
181 * offset of the region to be formatted is aligned on line start boundaries,
|
|
182 * whereas the end index of the region remains the same. For this formatting
|
|
183 * type the document partitioning is not taken into account.
|
|
184 *
|
|
185 * @param context The formatting context to use
|
|
186 * @param document The document to operate on
|
|
187 * @param offset The offset of the region to format
|
|
188 * @param length The length of the region to format
|
|
189 */
|
|
190 protected void formatMaster(final IFormattingContext context, final IDocument document, int offset, int length) {
|
|
191
|
|
192 try {
|
|
193
|
|
194 final int delta= offset - document.getLineInformationOfOffset(offset).getOffset();
|
|
195 offset -= delta;
|
|
196 length += delta;
|
|
197
|
|
198 } catch (BadLocationException exception) {
|
|
199 // Do nothing
|
|
200 }
|
|
201
|
|
202 if (fMaster !is null) {
|
|
203
|
|
204 context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, new TypedPosition(offset, length, fType));
|
|
205
|
|
206 fMaster.formatterStarts(context);
|
|
207 fMaster.format();
|
|
208 fMaster.formatterStops();
|
|
209 }
|
|
210 }
|
|
211
|
|
212 /**
|
|
213 * Formats the document specified in the formatting context with the
|
|
214 * formatting strategy registered for the content type.
|
|
215 * <p>
|
|
216 * For this formatting type only slave strategies are used. The region to be
|
|
217 * formatted is aligned on partition boundaries of the underlying content
|
|
218 * type. The exact formatting strategy is determined by the underlying
|
|
219 * content type of the document partitioning.
|
|
220 *
|
|
221 * @param context The formatting context to use
|
|
222 * @param document The document to operate on
|
|
223 * @param offset The offset of the region to format
|
|
224 * @param length The length of the region to format
|
|
225 * @param type The content type of the region to format
|
|
226 */
|
|
227 protected void formatSlave(final IFormattingContext context, final IDocument document, final int offset, final int length, final String type) {
|
|
228
|
|
229 final IFormattingStrategyExtension strategy= (IFormattingStrategyExtension)fSlaves.get(type);
|
|
230 if (strategy !is null) {
|
|
231
|
|
232 context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, new TypedPosition(offset, length, type));
|
|
233
|
|
234 strategy.formatterStarts(context);
|
|
235 strategy.format();
|
|
236 strategy.formatterStops();
|
|
237 }
|
|
238 }
|
|
239
|
|
240 /**
|
|
241 * Formats the document specified in the formatting context with the slave
|
|
242 * formatting strategies.
|
|
243 * <p>
|
|
244 * For each content type of the region to be formatted in the document
|
|
245 * partitioning, the registered slave formatting strategy is used to format
|
|
246 * that particular region. The region to be formatted is aligned on
|
|
247 * partition boundaries of the underlying content type. If the content type
|
|
248 * is the document's default content type, nothing happens.
|
|
249 *
|
|
250 * @param context The formatting context to use
|
|
251 * @param document The document to operate on
|
|
252 * @param offset The offset of the region to format
|
|
253 * @param length The length of the region to format
|
|
254 */
|
|
255 protected void formatSlaves(final IFormattingContext context, final IDocument document, final int offset, final int length) {
|
|
256
|
|
257 Map partitioners= new HashMap(0);
|
|
258 try {
|
|
259
|
|
260 final ITypedRegion[] partitions= TextUtilities.computePartitioning(document, fPartitioning, offset, length, false);
|
|
261
|
|
262 if (!fType.equals(partitions[0].getType()))
|
|
263 partitions[0]= TextUtilities.getPartition(document, fPartitioning, partitions[0].getOffset(), false);
|
|
264
|
|
265 if (partitions.length > 1) {
|
|
266
|
|
267 if (!fType.equals(partitions[partitions.length - 1].getType()))
|
|
268 partitions[partitions.length - 1]= TextUtilities.getPartition(document, fPartitioning, partitions[partitions.length - 1].getOffset(), false);
|
|
269 }
|
|
270
|
|
271 String type= null;
|
|
272 ITypedRegion partition= null;
|
|
273
|
|
274 partitioners= TextUtilities.removeDocumentPartitioners(document);
|
|
275
|
|
276 for (int index= partitions.length - 1; index >= 0; index--) {
|
|
277
|
|
278 partition= partitions[index];
|
|
279 type= partition.getType();
|
|
280
|
|
281 if (!fType.equals(type))
|
|
282 formatSlave(context, document, partition.getOffset(), partition.getLength(), type);
|
|
283 }
|
|
284
|
|
285 } catch (BadLocationException exception) {
|
|
286 // Should not happen
|
|
287 } finally {
|
|
288 TextUtilities.addDocumentPartitioners(document, partitioners);
|
|
289 }
|
|
290 }
|
|
291
|
|
292 /*
|
|
293 * @see dwtx.jface.text.formatter.IContentFormatter#getFormattingStrategy(java.lang.String)
|
|
294 */
|
|
295 public final IFormattingStrategy getFormattingStrategy(final String type) {
|
|
296 return null;
|
|
297 }
|
|
298
|
|
299 /**
|
|
300 * Registers a master formatting strategy.
|
|
301 * <p>
|
|
302 * The strategy may already be registered with a certain content type as
|
|
303 * slave strategy. The master strategy is registered for the default content
|
|
304 * type of documents. If a master strategy has already been registered, it
|
|
305 * is overridden by the new one.
|
|
306 *
|
|
307 * @param strategy The master formatting strategy, must implement
|
|
308 * {@link IFormattingStrategyExtension}
|
|
309 */
|
|
310 public final void setMasterStrategy(final IFormattingStrategy strategy) {
|
|
311 Assert.isTrue(strategy instanceof IFormattingStrategyExtension);
|
|
312 fMaster= (IFormattingStrategyExtension) strategy;
|
|
313 }
|
|
314
|
|
315 /**
|
|
316 * Registers a slave formatting strategy for a certain content type.
|
|
317 * <p>
|
|
318 * The strategy may already be registered as master strategy. An
|
|
319 * already registered slave strategy for the specified content type
|
|
320 * will be replaced. However, the same strategy may be registered with
|
|
321 * several content types. Slave strategies cannot be registered for the
|
|
322 * default content type of documents.
|
|
323 *
|
|
324 * @param strategy The slave formatting strategy
|
|
325 * @param type The content type to register this strategy with,
|
|
326 * must implement {@link IFormattingStrategyExtension}
|
|
327 */
|
|
328 public final void setSlaveStrategy(final IFormattingStrategy strategy, final String type) {
|
|
329 Assert.isTrue(strategy instanceof IFormattingStrategyExtension);
|
|
330 if (!fType.equals(type))
|
|
331 fSlaves.put(type, strategy);
|
|
332 }
|
|
333 }
|