1 11 package org.eclipse.jdt.internal.corext.refactoring.changes; 12 13 import org.eclipse.text.edits.MalformedTreeException; 14 import org.eclipse.text.edits.MultiTextEdit; 15 import org.eclipse.text.edits.TextEdit; 16 import org.eclipse.text.edits.TextEditGroup; 17 18 import org.eclipse.core.runtime.Assert; 19 20 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup; 21 import org.eclipse.ltk.core.refactoring.GroupCategorySet; 22 import org.eclipse.ltk.core.refactoring.TextChange; 23 import org.eclipse.ltk.core.refactoring.TextEditChangeGroup; 24 25 26 31 public class TextChangeCompatibility { 32 33 public static void addTextEdit(TextChange change, String name, TextEdit edit) throws MalformedTreeException { 34 Assert.isNotNull(change); 35 Assert.isNotNull(name); 36 Assert.isNotNull(edit); 37 TextEdit root= change.getEdit(); 38 if (root == null) { 39 root= new MultiTextEdit(); 40 change.setEdit(root); 41 } 42 insert(root, edit); 43 change.addTextEditGroup(new TextEditGroup(name, edit)); 44 } 45 46 public static void addTextEdit(TextChange change, String name, TextEdit edit, GroupCategorySet groupCategories) throws MalformedTreeException { 47 Assert.isNotNull(change); 48 Assert.isNotNull(name); 49 Assert.isNotNull(edit); 50 TextEdit root= change.getEdit(); 51 if (root == null) { 52 root= new MultiTextEdit(); 53 change.setEdit(root); 54 } 55 insert(root, edit); 56 change.addTextEditChangeGroup(new TextEditChangeGroup( 57 change, 58 new CategorizedTextEditGroup(name, edit, groupCategories))); 59 } 60 61 public static void insert(TextEdit parent, TextEdit edit) throws MalformedTreeException { 62 if (!parent.hasChildren()) { 63 parent.addChild(edit); 64 return; 65 } 66 TextEdit[] children= parent.getChildren(); 67 for (int i= 0; i < children.length; i++) { 69 TextEdit child= children[i]; 70 if (covers(child, edit)) { 71 insert(child, edit); 72 return; 73 } 74 } 75 int removed= 0; 78 for (int i= 0; i < children.length; i++) { 79 TextEdit child= children[i]; 80 if (covers(edit, child)) { 81 parent.removeChild(i - removed++); 82 edit.addChild(child); 83 } 84 } 85 parent.addChild(edit); 86 } 87 88 private static boolean covers(TextEdit thisEdit, TextEdit otherEdit) { 89 if (thisEdit.getLength() == 0) return false; 91 92 int thisOffset= thisEdit.getOffset(); 93 int thisEnd= thisEdit.getExclusiveEnd(); 94 if (otherEdit.getLength() == 0) { 95 int otherOffset= otherEdit.getOffset(); 96 return thisOffset < otherOffset && otherOffset < thisEnd; 97 } else { 98 int otherOffset= otherEdit.getOffset(); 99 int otherEnd= otherEdit.getExclusiveEnd(); 100 return thisOffset <= otherOffset && otherEnd <= thisEnd; 101 } 102 } 103 104 } 105 | Popular Tags |