1 11 package org.eclipse.ltk.internal.core.refactoring; 12 13 import org.eclipse.jface.text.BadPositionCategoryException; 14 import org.eclipse.jface.text.DocumentEvent; 15 import org.eclipse.jface.text.IPositionUpdater; 16 import org.eclipse.jface.text.Position; 17 18 19 public final class NonDeletingPositionUpdater implements IPositionUpdater { 20 21 22 private final String fCategory; 23 24 29 public NonDeletingPositionUpdater(String category) { 30 fCategory= category; 31 } 32 33 36 public void update(DocumentEvent event) { 37 38 int eventOffset= event.getOffset(); 39 int eventOldEndOffset= eventOffset + event.getLength(); 40 int eventNewLength= event.getText() == null ? 0 : event.getText().length(); 41 int eventNewEndOffset= eventOffset + eventNewLength; 42 int deltaLength= eventNewLength - event.getLength(); 43 44 try { 45 Position[] positions= event.getDocument().getPositions(fCategory); 46 47 for (int i= 0; i != positions.length; i++) { 48 49 Position position= positions[i]; 50 51 if (position.isDeleted()) 52 continue; 53 54 int offset= position.getOffset(); 55 int length= position.getLength(); 56 int end= offset + length; 57 58 if (offset > eventOldEndOffset) { 59 position.setOffset(offset + deltaLength); 61 } else if (end < eventOffset) { 62 } else if (offset <= eventOffset && end >= eventOldEndOffset) { 64 position.setLength(length + deltaLength); 66 } else if (offset < eventOffset) { 67 position.setLength(eventNewEndOffset - offset); 70 } else if (end > eventOldEndOffset) { 71 position.setOffset(eventOffset); 75 int deleted= eventOldEndOffset - offset; 76 position.setLength(length - deleted + eventNewLength); 77 } else { 78 int newOffset= Math.min(offset, eventNewEndOffset); 81 int newEndOffset= Math.min(end, eventNewEndOffset); 82 position.setOffset(newOffset); 83 position.setLength(newEndOffset - newOffset); 84 } 85 } 86 } catch (BadPositionCategoryException e) { 87 } 89 } 90 91 96 public String getCategory() { 97 return fCategory; 98 } 99 } 100 | Popular Tags |