1 11 package org.eclipse.jdt.internal.corext.textmanipulation; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 21 import org.eclipse.core.resources.IFile; 22 23 import org.eclipse.jface.util.Assert; 24 25 import org.eclipse.jface.text.BadLocationException; 26 import org.eclipse.jface.text.DefaultLineTracker; 27 import org.eclipse.jface.text.IDocument; 28 import org.eclipse.jface.text.ILineTracker; 29 import org.eclipse.jface.text.IRegion; 30 31 import org.eclipse.jdt.internal.corext.util.Messages; 32 import org.eclipse.jdt.internal.corext.util.Strings; 33 34 import org.eclipse.jdt.internal.ui.IJavaStatusConstants; 35 import org.eclipse.jdt.internal.ui.JavaPlugin; 36 37 43 public class TextBuffer { 44 45 48 private static class DocumentRegion extends TextRegion { 49 IRegion fRegion; 50 public DocumentRegion(IRegion region) { 51 fRegion= region; 52 } 53 public int getOffset() { 54 return fRegion.getOffset(); 55 } 56 public int getLength() { 57 return fRegion.getLength(); 58 } 59 } 60 61 64 public class Block { 65 public String content; 66 public int offsetDelta; 67 } 68 69 private IDocument fDocument; 70 71 private static final TextBufferFactory fgFactory= new TextBufferFactory(); 72 73 public TextBuffer(IDocument document) { 74 fDocument= document; 75 Assert.isNotNull(fDocument); 76 } 77 78 public IDocument getDocument() { 79 return fDocument; 80 } 81 82 87 public int getLength() { 88 return fDocument.getLength(); 89 } 90 91 96 public int getNumberOfLines() { 97 return fDocument.getNumberOfLines(); 98 } 99 100 108 public char getChar(int offset) { 109 try { 110 return fDocument.getChar(offset); 111 } catch (BadLocationException e) { 112 throw new ArrayIndexOutOfBoundsException (e.getMessage()); 113 } 114 } 115 116 121 public String getContent() { 122 return fDocument.get(); 123 } 124 125 131 public String getContent(int start, int length) { 132 try { 133 return fDocument.get(start, length); 134 } catch (BadLocationException e) { 135 return null; 136 } 137 } 138 139 public Block getBlockContent(int start, int length, int tabWidth) { 140 Block result= new Block(); 141 StringBuffer buffer= new StringBuffer (); 142 int lineOffset= getLineInformationOfOffset(start).getOffset(); 143 if (start > lineOffset) { 144 String line= getContent(lineOffset, start - lineOffset); 145 String indent= Strings.getIndentString(line, tabWidth); 146 result.offsetDelta= -indent.length(); 147 buffer.append(indent); 148 } 149 final int end= start + length; 150 TextRegion region= getLineInformationOfOffset(end); 151 lineOffset= region.getOffset(); 152 if (lineOffset == end) { 154 int lineNumber= getLineOfOffset(lineOffset); 155 if (lineNumber > 0) { 156 length= length - getLineDelimiter(lineNumber - 1).length(); 157 } 158 } 159 if (buffer.length() == 0) { 160 result.content= getContent(start, length); 161 } else { 162 buffer.append(getContent(start, length)); 163 result.content= buffer.toString(); 164 } 165 return result; 166 } 167 168 173 public String getLineDelimiter() { 174 String lineDelimiter= getLineDelimiter(0); 175 if (lineDelimiter == null) 176 lineDelimiter= System.getProperty("line.separator", "\n"); return lineDelimiter; 178 } 179 180 186 public String getLineDelimiter(int line) { 187 try { 188 return fDocument.getLineDelimiter(line); 189 } catch (BadLocationException e) { 190 return null; 191 } 192 } 193 194 201 public String getLineContent(int line) { 202 try { 203 IRegion region= fDocument.getLineInformation(line); 204 return fDocument.get(region.getOffset(), region.getLength()); 205 } catch (BadLocationException e) { 206 return null; 207 } 208 } 209 210 216 public int getLineIndent(int lineNumber, int tabWidth) { 217 String lineContent= getLineContent(lineNumber); 218 if (lineContent == null) 219 return -1; 220 return Strings.computeIndent(lineContent, tabWidth); 221 } 222 223 232 public TextRegion getLineInformation(int line) { 233 try { 234 return new DocumentRegion(fDocument.getLineInformation(line)); 235 } catch (BadLocationException e) { 236 return null; 237 } 238 } 239 240 249 public TextRegion getLineInformationOfOffset(int offset) { 250 try { 251 return new DocumentRegion(fDocument.getLineInformationOfOffset(offset)); 252 } catch (BadLocationException e) { 253 return null; 254 } 255 } 256 257 265 public int getLineOfOffset(int offset) { 266 try { 267 return fDocument.getLineOfOffset(offset); 268 } catch (BadLocationException e) { 269 return -1; 270 } 271 } 272 273 281 public String getLineContentOfOffset(int offset) { 282 try { 283 IRegion region= fDocument.getLineInformationOfOffset(offset); 284 return fDocument.get(region.getOffset(), region.getLength()); 285 } catch (BadLocationException e) { 286 return null; 287 } 288 } 289 290 298 public String [] convertIntoLines(int offset, int length, boolean lastNewLineCreateEmptyLine) { 299 try { 300 String text= fDocument.get(offset, length); 301 ILineTracker tracker= new DefaultLineTracker(); 302 tracker.set(text); 303 int size= tracker.getNumberOfLines(); 304 int lastLine= size - 1; 305 List result= new ArrayList (size); 306 for (int i= 0; i < size; i++) { 307 IRegion region= tracker.getLineInformation(i); 308 String line= getContent(offset + region.getOffset(), region.getLength()); 309 if (i < lastLine || !"".equals(line) || lastNewLineCreateEmptyLine) result.add(line); 311 } 312 return (String []) result.toArray(new String [result.size()]); 313 } catch (BadLocationException e) { 314 return null; 315 } 316 } 317 318 326 public void replace(int offset, int length, String text) throws CoreException { 327 try { 328 fDocument.replace(offset, length, text); 329 } catch (BadLocationException e) { 330 IStatus s= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, 331 Messages.format( 332 TextManipulationMessages.TextBuffer_wrongRange, new Object [] {new Integer (offset), new Integer (length) } ), e); 334 throw new CoreException(s); 335 } 336 } 337 338 public void replace(IRegion range, String text) throws CoreException { 339 replace(range.getOffset(), range.getLength(), text); 340 } 341 342 344 362 public IStatus makeCommittable(Object context) { 363 return fgFactory.makeCommittable(this, context); 364 } 365 366 368 371 public String toString() { 372 return fDocument.get(); 373 } 374 375 377 380 void release() { 381 } 382 383 385 394 public static TextBuffer acquire(IFile file) throws CoreException { 395 return fgFactory.acquire(file); 396 } 397 398 403 public static void release(TextBuffer buffer) { 404 fgFactory.release(buffer); 405 } 406 407 420 public static void commitChanges(TextBuffer buffer, boolean force, IProgressMonitor pm) throws CoreException { 421 fgFactory.commitChanges(buffer, force, pm); 422 } 423 424 437 public static TextBuffer create(IFile file) throws CoreException { 438 return fgFactory.create(file); 439 } 440 441 449 public static TextBuffer create(String content) { 450 return fgFactory.create(content); 451 } 452 453 456 public static void save(TextBuffer buffer, IProgressMonitor pm) throws CoreException { 457 fgFactory.save(buffer, pm); 458 } 459 460 public static void aboutToChange(TextBuffer buffer) throws CoreException { 461 fgFactory.aboutToChange(buffer); 462 } 463 464 public static void changed(TextBuffer buffer) throws CoreException { 465 fgFactory.changed(buffer); 466 } 467 } 468 | Popular Tags |