1 13 package org.eclipse.ltk.core.refactoring; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.text.edits.TextEdit; 20 import org.eclipse.text.edits.TextEditCopier; 21 import org.eclipse.text.edits.TextEditGroup; 22 import org.eclipse.text.edits.TextEditProcessor; 23 24 import org.eclipse.core.runtime.Assert; 25 import org.eclipse.core.runtime.CoreException; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 28 import org.eclipse.jface.text.BadLocationException; 29 import org.eclipse.jface.text.IDocument; 30 import org.eclipse.jface.text.IRegion; 31 32 import org.eclipse.ltk.internal.core.refactoring.Changes; 33 34 39 public abstract class TextEditBasedChange extends Change { 40 41 44 static final class LocalTextEditProcessor extends TextEditProcessor { 45 public static final int EXCLUDE= 1; 46 public static final int INCLUDE= 2; 47 48 private TextEdit[] fExcludes; 49 private TextEdit[] fIncludes; 50 51 protected LocalTextEditProcessor(IDocument document, TextEdit root, int flags) { 52 super(document, root, flags); 53 } 54 public void setIncludes(TextEdit[] includes) { 55 Assert.isNotNull(includes); 56 Assert.isTrue(fExcludes == null); 57 fIncludes= flatten(includes); 58 } 59 public void setExcludes(TextEdit[] excludes) { 60 Assert.isNotNull(excludes); 61 Assert.isTrue(fIncludes == null); 62 fExcludes= flatten(excludes); 63 } 64 protected boolean considerEdit(TextEdit edit) { 65 if (fExcludes != null) { 66 for (int i= 0; i < fExcludes.length; i++) { 67 if (edit.equals(fExcludes[i])) 68 return false; 69 } 70 return true; 71 } 72 if (fIncludes != null) { 73 for (int i= 0; i < fIncludes.length; i++) { 74 if (edit.equals(fIncludes[i])) 75 return true; 76 } 77 return false; 78 } 79 return true; 80 } 81 private TextEdit[] flatten(TextEdit[] edits) { 82 List result= new ArrayList (5); 83 for (int i= 0; i < edits.length; i++) { 84 flatten(result, edits[i]); 85 } 86 return (TextEdit[])result.toArray(new TextEdit[result.size()]); 87 } 88 private void flatten(List result, TextEdit edit) { 89 result.add(edit); 90 TextEdit[] children= edit.getChildren(); 91 for (int i= 0; i < children.length; i++) { 92 flatten(result, children[i]); 93 } 94 } 95 } 96 97 100 static final class PreviewAndRegion { 101 public IDocument document; 102 public IRegion region; 103 public PreviewAndRegion(IDocument d, IRegion r) { 104 document= d; 105 region= r; 106 } 107 } 108 109 113 static final TextEditBasedChangeGroup[] ALL_EDITS= new TextEditBasedChangeGroup[0]; 114 115 116 private List fChangeGroups; 117 private GroupCategorySet fCombiedGroupCategories; 118 119 120 private String fName; 121 122 123 private String fTextType; 124 125 126 private boolean fTrackEdits; 127 128 140 protected TextEditBasedChange(String name) { 141 Assert.isNotNull(name, "Name must not be null"); fChangeGroups= new ArrayList (5); 143 fName= name; 144 fTextType= "txt"; } 146 147 154 public void addChangeGroup(TextEditBasedChangeGroup group) { 155 Assert.isTrue(group != null); 156 fChangeGroups.add(group); 157 if (fCombiedGroupCategories != null) { 158 fCombiedGroupCategories= GroupCategorySet.union(fCombiedGroupCategories, group.getGroupCategorySet()); 159 } 160 } 161 162 169 public void addTextEditGroup(TextEditGroup group) { 170 addChangeGroup(new TextEditBasedChangeGroup(this, group)); 171 } 172 173 184 public boolean hasOneGroupCategory(List groupCategories) { 185 if (fCombiedGroupCategories == null) { 186 fCombiedGroupCategories= GroupCategorySet.NONE; 187 for (Iterator iter= fChangeGroups.iterator(); iter.hasNext();) { 188 TextEditBasedChangeGroup group= (TextEditBasedChangeGroup)iter.next(); 189 fCombiedGroupCategories= GroupCategorySet.union(fCombiedGroupCategories, group.getGroupCategorySet()); 190 } 191 } 192 return fCombiedGroupCategories.containsOneCategory(groupCategories); 193 } 194 195 201 public final TextEditBasedChangeGroup[] getChangeGroups() { 202 return (TextEditBasedChangeGroup[])fChangeGroups.toArray(new TextEditBasedChangeGroup[fChangeGroups.size()]); 203 } 204 205 String getContent(IDocument document, IRegion region, boolean expandRegionToFullLine, int surroundingLines) throws CoreException { 206 try { 207 if (expandRegionToFullLine) { 208 int startLine= Math.max(document.getLineOfOffset(region.getOffset()) - surroundingLines, 0); 209 int endLine; 210 if (region.getLength() == 0) { 211 if (surroundingLines == 0) { 214 return ""; } 217 218 endLine= Math.min( 219 document.getLineOfOffset(region.getOffset()) + surroundingLines - 1, 220 document.getNumberOfLines() - 1); 221 } else { 222 endLine= Math.min( 223 document.getLineOfOffset(region.getOffset() + region.getLength() - 1) + surroundingLines, 224 document.getNumberOfLines() - 1); 225 } 226 227 int offset= document.getLineInformation(startLine).getOffset(); 228 IRegion endLineRegion= document.getLineInformation(endLine); 229 int length = endLineRegion.getOffset() + endLineRegion.getLength() - offset; 230 return document.get(offset, length); 231 232 } else { 233 return document.get(region.getOffset(), region.getLength()); 234 } 235 } catch (BadLocationException e) { 236 throw Changes.asCoreException(e); 237 } 238 } 239 240 250 public abstract String getCurrentContent(IProgressMonitor pm) throws CoreException; 251 252 284 public abstract String getCurrentContent(IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException; 285 286 293 public boolean getKeepPreviewEdits() { 294 return fTrackEdits; 295 } 296 297 300 public String getName() { 301 return fName; 302 } 303 304 342 public abstract String getPreviewContent(TextEditBasedChangeGroup[] changeGroups, IRegion region, boolean expandRegionToFullLine, int surroundingLines, IProgressMonitor pm) throws CoreException; 343 344 353 public abstract String getPreviewContent(IProgressMonitor pm) throws CoreException; 354 355 360 public String getTextType() { 361 return fTextType; 362 } 363 364 TextEdit[] mapEdits(TextEdit[] edits, TextEditCopier copier) { 365 if (edits == null) 366 return null; 367 final List result= new ArrayList (edits.length); 368 for (int i= 0; i < edits.length; i++) { 369 TextEdit edit= copier.getCopy(edits[i]); 370 if (edit != null) 371 result.add(edit); 372 } 373 return (TextEdit[]) result.toArray(new TextEdit[result.size()]); 374 } 375 376 379 public void setEnabled(boolean enabled) { 380 super.setEnabled(enabled); 381 for (Iterator iter= fChangeGroups.iterator(); iter.hasNext();) { 382 TextEditBasedChangeGroup element= (TextEditBasedChangeGroup) iter.next(); 383 element.setEnabled(enabled); 384 } 385 } 386 387 393 public void setKeepPreviewEdits(boolean keep) { 394 fTrackEdits= keep; 395 } 396 397 409 public void setTextType(String type) { 410 if (type == null) 411 type= "txt"; fTextType= type; 413 } 414 } 415 | Popular Tags |