1 11 12 package org.eclipse.jface.text.formatter; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 import org.eclipse.jface.text.BadLocationException; 20 import org.eclipse.jface.text.DefaultPositionUpdater; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.IRegion; 23 import org.eclipse.jface.text.ITypedRegion; 24 import org.eclipse.jface.text.TextUtilities; 25 import org.eclipse.jface.text.TypedPosition; 26 27 48 public class MultiPassContentFormatter implements IContentFormatter, IContentFormatterExtension { 49 50 55 protected class NonDeletingPositionUpdater extends DefaultPositionUpdater { 56 57 62 public NonDeletingPositionUpdater(final String category) { 63 super(category); 64 } 65 66 69 protected final boolean notDeleted() { 70 71 if (fOffset < fPosition.offset && (fPosition.offset + fPosition.length < fOffset + fLength)) { 72 73 int offset= fOffset + fLength; 74 if (offset < fDocument.getLength()) { 75 76 try { 77 78 boolean moved= false; 79 char character= fDocument.getChar(offset); 80 81 while (offset < fDocument.getLength() && Character.isWhitespace(character)) { 82 83 moved= true; 84 character= fDocument.getChar(offset++); 85 } 86 87 if (moved) 88 offset--; 89 90 } catch (BadLocationException exception) { 91 } 93 94 fPosition.offset= offset; 95 fPosition.length= 0; 96 } 97 } 98 return true; 99 } 100 } 101 102 103 private IFormattingStrategyExtension fMaster= null; 104 105 private final String fPartitioning; 106 107 private final Map fSlaves= new HashMap (); 108 109 private final String fType; 110 111 117 public MultiPassContentFormatter(final String partitioning, final String type) { 118 fPartitioning= partitioning; 119 fType= type; 120 } 121 122 125 public final void format(final IDocument medium, final IFormattingContext context) { 126 127 context.setProperty(FormattingContextProperties.CONTEXT_MEDIUM, medium); 128 129 final Boolean document= (Boolean )context.getProperty(FormattingContextProperties.CONTEXT_DOCUMENT); 130 if (document == null || !document.booleanValue()) { 131 132 final IRegion region= (IRegion)context.getProperty(FormattingContextProperties.CONTEXT_REGION); 133 if (region != null) { 134 try { 135 formatMaster(context, medium, region.getOffset(), region.getLength()); 136 } finally { 137 formatSlaves(context, medium, region.getOffset(), region.getLength()); 138 } 139 } 140 } else { 141 try { 142 formatMaster(context, medium, 0, medium.getLength()); 143 } finally { 144 formatSlaves(context, medium, 0, medium.getLength()); 145 } 146 } 147 } 148 149 152 public final void format(final IDocument medium, final IRegion region) { 153 154 final FormattingContext context= new FormattingContext(); 155 156 context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE); 157 context.setProperty(FormattingContextProperties.CONTEXT_REGION, region); 158 159 format(medium, context); 160 } 161 162 176 protected void formatMaster(final IFormattingContext context, final IDocument document, int offset, int length) { 177 178 try { 179 180 final int delta= offset - document.getLineInformationOfOffset(offset).getOffset(); 181 offset -= delta; 182 length += delta; 183 184 } catch (BadLocationException exception) { 185 } 187 188 if (fMaster != null) { 189 190 context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, new TypedPosition(offset, length, fType)); 191 192 fMaster.formatterStarts(context); 193 fMaster.format(); 194 fMaster.formatterStops(); 195 } 196 } 197 198 213 protected void formatSlave(final IFormattingContext context, final IDocument document, final int offset, final int length, final String type) { 214 215 final IFormattingStrategyExtension strategy= (IFormattingStrategyExtension)fSlaves.get(type); 216 if (strategy != null) { 217 218 context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, new TypedPosition(offset, length, type)); 219 220 strategy.formatterStarts(context); 221 strategy.format(); 222 strategy.formatterStops(); 223 } 224 } 225 226 241 protected void formatSlaves(final IFormattingContext context, final IDocument document, final int offset, final int length) { 242 243 Map partitioners= new HashMap (0); 244 try { 245 246 final ITypedRegion[] partitions= TextUtilities.computePartitioning(document, fPartitioning, offset, length, false); 247 248 if (!fType.equals(partitions[0].getType())) 249 partitions[0]= TextUtilities.getPartition(document, fPartitioning, partitions[0].getOffset(), false); 250 251 if (partitions.length > 1) { 252 253 if (!fType.equals(partitions[partitions.length - 1].getType())) 254 partitions[partitions.length - 1]= TextUtilities.getPartition(document, fPartitioning, partitions[partitions.length - 1].getOffset(), false); 255 } 256 257 String type= null; 258 ITypedRegion partition= null; 259 260 partitioners= TextUtilities.removeDocumentPartitioners(document); 261 262 for (int index= partitions.length - 1; index >= 0; index--) { 263 264 partition= partitions[index]; 265 type= partition.getType(); 266 267 if (!fType.equals(type)) 268 formatSlave(context, document, partition.getOffset(), partition.getLength(), type); 269 } 270 271 } catch (BadLocationException exception) { 272 } finally { 274 TextUtilities.addDocumentPartitioners(document, partitioners); 275 } 276 } 277 278 281 public final IFormattingStrategy getFormattingStrategy(final String type) { 282 return null; 283 } 284 285 296 public final void setMasterStrategy(final IFormattingStrategy strategy) { 297 Assert.isTrue(strategy instanceof IFormattingStrategyExtension); 298 fMaster= (IFormattingStrategyExtension) strategy; 299 } 300 301 314 public final void setSlaveStrategy(final IFormattingStrategy strategy, final String type) { 315 Assert.isTrue(strategy instanceof IFormattingStrategyExtension); 316 if (!fType.equals(type)) 317 fSlaves.put(type, strategy); 318 } 319 } 320 | Popular Tags |