1 11 package org.eclipse.jface.text; 12 13 14 import java.util.Iterator ; 15 import java.util.LinkedList ; 16 17 18 27 public class SequentialRewriteTextStore implements ITextStore { 28 29 32 private static class Replace { 33 public int newOffset; 34 public final int offset; 35 public final int length; 36 public final String text; 37 38 public Replace(int offset, int newOffset, int length, String text) { 39 this.newOffset= newOffset; 40 this.offset= offset; 41 this.length= length; 42 this.text= text; 43 } 44 } 45 46 47 private LinkedList fReplaceList; 48 49 private ITextStore fSource; 50 51 private static final boolean ASSERT_SEQUENTIALITY= false; 52 53 54 59 public SequentialRewriteTextStore(ITextStore source) { 60 fReplaceList= new LinkedList (); 61 fSource= source; 62 } 63 64 69 public ITextStore getSourceStore() { 70 commit(); 71 return fSource; 72 } 73 74 77 public void replace(int offset, int length, String text) { 78 if (text == null) 79 text= ""; 81 if (fReplaceList.size() == 0) { 82 fReplaceList.add(new Replace(offset, offset, length, text)); 83 84 } else { 85 Replace firstReplace= (Replace) fReplaceList.getFirst(); 86 Replace lastReplace= (Replace) fReplaceList.getLast(); 87 88 if (offset + length <= firstReplace.newOffset) { 90 int delta= text.length() - length; 91 if (delta != 0) { 92 for (Iterator i= fReplaceList.iterator(); i.hasNext(); ) { 93 Replace replace= (Replace) i.next(); 94 replace.newOffset += delta; 95 } 96 } 97 98 fReplaceList.addFirst(new Replace(offset, offset, length, text)); 99 100 } else if (offset >= lastReplace.newOffset + lastReplace.text.length()) { 102 int delta= getDelta(lastReplace); 103 fReplaceList.add(new Replace(offset - delta, offset, length, text)); 104 105 } else if (ASSERT_SEQUENTIALITY) { 106 throw new IllegalArgumentException (); 107 108 } else { 109 commit(); 110 fSource.replace(offset, length, text); 111 } 112 } 113 } 114 115 118 public void set(String text) { 119 fSource.set(text); 120 fReplaceList.clear(); 121 } 122 123 126 public String get(int offset, int length) { 127 128 if (fReplaceList.isEmpty()) 129 return fSource.get(offset, length); 130 131 132 Replace firstReplace= (Replace) fReplaceList.getFirst(); 133 Replace lastReplace= (Replace) fReplaceList.getLast(); 134 135 if (offset + length <= firstReplace.newOffset) { 137 return fSource.get(offset, length); 138 139 } else if (offset >= lastReplace.newOffset + lastReplace.text.length()) { 141 int delta= getDelta(lastReplace); 142 return fSource.get(offset - delta, length); 143 144 } else if (ASSERT_SEQUENTIALITY) { 145 throw new IllegalArgumentException (); 146 147 } else { 148 149 int delta= 0; 150 for (Iterator i= fReplaceList.iterator(); i.hasNext(); ) { 151 Replace replace= (Replace) i.next(); 152 153 if (offset + length < replace.newOffset) { 154 return fSource.get(offset - delta, length); 155 156 } else if (offset >= replace.newOffset && offset + length <= replace.newOffset + replace.text.length()) { 157 return replace.text.substring(offset - replace.newOffset, offset - replace.newOffset + length); 158 159 } else if (offset >= replace.newOffset + replace.text.length()) { 160 delta= getDelta(replace); 161 continue; 162 163 } else { 164 commit(); 165 return fSource.get(offset, length); 166 } 167 } 168 169 return fSource.get(offset - delta, length); 170 } 171 172 } 173 174 181 private static final int getDelta(Replace replace) { 182 return replace.newOffset - replace.offset + replace.text.length() - replace.length; 183 } 184 185 188 public char get(int offset) { 189 if (fReplaceList.isEmpty()) 190 return fSource.get(offset); 191 192 Replace firstReplace= (Replace) fReplaceList.getFirst(); 193 Replace lastReplace= (Replace) fReplaceList.getLast(); 194 195 if (offset < firstReplace.newOffset) { 197 return fSource.get(offset); 198 199 } else if (offset >= lastReplace.newOffset + lastReplace.text.length()) { 201 int delta= getDelta(lastReplace); 202 return fSource.get(offset - delta); 203 204 } else if (ASSERT_SEQUENTIALITY) { 205 throw new IllegalArgumentException (); 206 207 } else { 208 209 int delta= 0; 210 for (Iterator i= fReplaceList.iterator(); i.hasNext(); ) { 211 Replace replace= (Replace) i.next(); 212 213 if (offset < replace.newOffset) 214 return fSource.get(offset - delta); 215 216 else if (offset < replace.newOffset + replace.text.length()) 217 return replace.text.charAt(offset - replace.newOffset); 218 219 delta= getDelta(replace); 220 } 221 222 return fSource.get(offset - delta); 223 } 224 } 225 226 229 public int getLength() { 230 if (fReplaceList.isEmpty()) 231 return fSource.getLength(); 232 233 Replace lastReplace= (Replace) fReplaceList.getLast(); 234 return fSource.getLength() + getDelta(lastReplace); 235 } 236 237 240 public void dispose() { 241 fReplaceList= null; 242 fSource= null; 243 } 244 245 248 private void commit() { 249 250 if (fReplaceList.isEmpty()) 251 return; 252 253 StringBuffer buffer= new StringBuffer (); 254 255 int delta= 0; 256 for (Iterator i= fReplaceList.iterator(); i.hasNext(); ) { 257 Replace replace= (Replace) i.next(); 258 259 int offset= buffer.length() - delta; 260 buffer.append(fSource.get(offset, replace.offset - offset)); 261 buffer.append(replace.text); 262 delta= getDelta(replace); 263 } 264 265 int offset= buffer.length() - delta; 266 buffer.append(fSource.get(offset, fSource.getLength() - offset)); 267 268 fSource.set(buffer.toString()); 269 fReplaceList.clear(); 270 } 271 } 272 | Popular Tags |