1 11 package org.eclipse.core.filebuffers.manipulation; 12 13 import org.eclipse.core.internal.filebuffers.FileBuffersPlugin; 14 import org.eclipse.core.internal.filebuffers.Progress; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.OperationCanceledException; 19 import org.eclipse.core.runtime.Status; 20 21 import org.eclipse.core.filebuffers.IFileBufferStatusCodes; 22 import org.eclipse.core.filebuffers.ITextFileBuffer; 23 24 import org.eclipse.text.edits.DeleteEdit; 25 26 import org.eclipse.jface.text.BadLocationException; 27 import org.eclipse.jface.text.DocumentRewriteSessionType; 28 import org.eclipse.jface.text.IDocument; 29 import org.eclipse.jface.text.IRegion; 30 31 36 public class RemoveTrailingWhitespaceOperation extends TextFileBufferOperation { 37 38 41 public RemoveTrailingWhitespaceOperation() { 42 super(FileBuffersMessages.RemoveTrailingWhitespaceOperation_name); 43 } 44 45 48 protected MultiTextEditWithProgress computeTextEdit(ITextFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException { 49 IDocument document= fileBuffer.getDocument(); 50 int lineCount= document.getNumberOfLines(); 51 52 progressMonitor= Progress.getMonitor(progressMonitor); 53 progressMonitor.beginTask(FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_generatingChanges, lineCount); 54 try { 55 56 MultiTextEditWithProgress multiEdit= new MultiTextEditWithProgress(FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_applyingChanges); 57 58 for (int i= 0; i < lineCount; i++) { 59 if (progressMonitor.isCanceled()) 60 throw new OperationCanceledException(); 61 62 IRegion region= document.getLineInformation(i); 63 if (region.getLength() == 0) 64 continue; 65 66 int lineStart= region.getOffset(); 67 int lineExclusiveEnd= lineStart + region.getLength(); 68 int j= lineExclusiveEnd -1; 69 while (j >= lineStart && Character.isWhitespace(document.getChar(j))) --j; 70 ++j; 71 if (j < lineExclusiveEnd) 72 multiEdit.addChild(new DeleteEdit(j, lineExclusiveEnd - j)); 73 progressMonitor.worked(1); 74 } 75 76 return multiEdit.getChildrenSize() <= 0 ? null : multiEdit; 77 78 } catch (BadLocationException x) { 79 throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, "", x)); } finally { 81 progressMonitor.done(); 82 } 83 } 84 85 88 protected DocumentRewriteSessionType getDocumentRewriteSessionType() { 89 return DocumentRewriteSessionType.SEQUENTIAL; 90 } 91 } 92 | Popular Tags |