1 11 package org.eclipse.jface.text.projection; 12 13 14 import org.eclipse.core.runtime.Assert; 15 16 import org.eclipse.jface.text.BadPositionCategoryException; 17 import org.eclipse.jface.text.DefaultPositionUpdater; 18 import org.eclipse.jface.text.DocumentEvent; 19 import org.eclipse.jface.text.Position; 20 21 22 31 class SegmentUpdater extends DefaultPositionUpdater { 32 33 private Segment fNextSegment= null; 34 private boolean fIsProjectionChange= false; 35 36 41 protected SegmentUpdater(String segmentCategory) { 42 super(segmentCategory); 43 } 44 45 48 public void update(DocumentEvent event) { 49 50 Assert.isTrue(event instanceof ProjectionDocumentEvent); 51 fIsProjectionChange= ((ProjectionDocumentEvent) event).getChangeType() == ProjectionDocumentEvent.PROJECTION_CHANGE; 52 53 try { 54 55 Position[] category= event.getDocument().getPositions(getCategory()); 56 57 fOffset= event.getOffset(); 58 fLength= event.getLength(); 59 fReplaceLength= (event.getText() == null ? 0 : event.getText().length()); 60 fDocument= event.getDocument(); 61 62 for (int i= 0; i < category.length; i++) { 63 64 fPosition= category[i]; 65 Assert.isTrue(fPosition instanceof Segment); 66 67 if (i < category.length - 1) 68 fNextSegment= (Segment) category[i + 1]; 69 else 70 fNextSegment= null; 71 72 fOriginalPosition.offset= fPosition.offset; 73 fOriginalPosition.length= fPosition.length; 74 75 if (notDeleted()) 76 adaptToReplace(); 77 78 } 79 80 } catch (BadPositionCategoryException x) { 81 } 83 } 84 85 88 protected void adaptToInsert() { 89 90 Segment segment= (Segment) fPosition; 91 int myStart= segment.offset; 92 int myEnd= segment.offset + segment.length - (segment.isMarkedForStretch || fNextSegment == null || isAffectingReplace() ? 0 : 1); 93 myEnd= Math.max(myStart, myEnd); 94 int yoursStart= fOffset; 95 96 try { 97 98 if (myEnd < yoursStart) 99 return; 100 101 if (segment.isMarkedForStretch) { 102 Assert.isTrue(fIsProjectionChange); 103 segment.isMarkedForShift= false; 104 if (fNextSegment != null) { 105 fNextSegment.isMarkedForShift= true; 106 fNextSegment.isMarkedForStretch= false; 107 } 108 } 109 110 if (fLength <= 0) { 111 112 if (myStart < (yoursStart + (segment.isMarkedForShift ? 0 : 1))) 113 fPosition.length += fReplaceLength; 114 else 115 fPosition.offset += fReplaceLength; 116 117 } else { 118 119 if (myStart <= yoursStart && fOriginalPosition.offset <= yoursStart) 120 fPosition.length += fReplaceLength; 121 else 122 fPosition.offset += fReplaceLength; 123 } 124 125 } finally { 126 segment.clearMark(); 127 } 128 } 129 } 130 | Popular Tags |