use of org.eclipse.core.runtime.SubMonitor in project eclipse.platform.text by eclipse.
the class ConvertLineDelimitersOperation method computeTextEdit.
@Override
protected MultiTextEditWithProgress computeTextEdit(ITextFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException {
IDocument document = fileBuffer.getDocument();
int lineCount = document.getNumberOfLines();
SubMonitor subMonitor = SubMonitor.convert(progressMonitor, FileBuffersMessages.ConvertLineDelimitersOperation_task_generatingChanges, lineCount);
try {
MultiTextEditWithProgress multiEdit = new MultiTextEditWithProgress(FileBuffersMessages.ConvertLineDelimitersOperation_task_applyingChanges);
for (int i = 0; i < lineCount; i++) {
final String delimiter = document.getLineDelimiter(i);
if (delimiter != null && delimiter.length() > 0 && !delimiter.equals(fLineDelimiter)) {
IRegion region = document.getLineInformation(i);
multiEdit.addChild(new ReplaceEdit(region.getOffset() + region.getLength(), delimiter.length(), fLineDelimiter));
}
subMonitor.split(1);
}
return multiEdit.getChildrenSize() <= 0 ? null : multiEdit;
} catch (BadLocationException x) {
// $NON-NLS-1$
throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, "", x));
}
}
use of org.eclipse.core.runtime.SubMonitor in project eclipse.platform.text by eclipse.
the class GenericFileBufferOperationRunner method releaseFileBuffers.
private void releaseFileBuffers(IPath[] locations, IProgressMonitor progressMonitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(progressMonitor, FileBuffersMessages.FileBufferOperationRunner_task_disconnecting, locations.length);
final ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
for (IPath location : locations) {
fileBufferManager.disconnect(location, LocationKind.NORMALIZE, subMonitor.split(1));
}
}
use of org.eclipse.core.runtime.SubMonitor in project eclipse.platform.text by eclipse.
the class GenericFileBufferOperationRunner method createFileBuffers.
private IFileBuffer[] createFileBuffers(IPath[] locations, IProgressMonitor progressMonitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(progressMonitor, FileBuffersMessages.FileBufferOperationRunner_task_connecting, locations.length);
try {
IFileBuffer[] fileBuffers = new ITextFileBuffer[locations.length];
for (int i = 0; i < locations.length; i++) {
fFileBufferManager.connect(locations[i], LocationKind.NORMALIZE, subMonitor.split(1));
fileBuffers[i] = fFileBufferManager.getFileBuffer(locations[i], LocationKind.NORMALIZE);
}
return fileBuffers;
} catch (CoreException x) {
try {
releaseFileBuffers(locations, new NullProgressMonitor());
} catch (CoreException e) {
}
throw x;
}
}
use of org.eclipse.core.runtime.SubMonitor in project eclipse.platform.text by eclipse.
the class GenericFileBufferOperationRunner method execute.
/**
* Executes the given operation for all file buffers specified by the given locations.
*
* @param locations the file buffer locations
* @param operation the operation to be performed
* @param monitor the progress monitor, or <code>null</code> if progress reporting is not desired
* @throws CoreException in case of error
* @throws OperationCanceledException in case the execution get canceled
*/
public void execute(IPath[] locations, final IFileBufferOperation operation, IProgressMonitor monitor) throws CoreException, OperationCanceledException {
final int size = locations.length;
SubMonitor subMonitor = SubMonitor.convert(monitor, operation.getOperationName(), size * 200);
try {
IFileBuffer[] fileBuffers = createFileBuffers(locations, subMonitor.split(size * 10));
IFileBuffer[] fileBuffers2Save = findFileBuffersToSave(fileBuffers);
fFileBufferManager.validateState(fileBuffers2Save, subMonitor.split(size * 10), fValidationContext);
if (!isCommitable(fileBuffers2Save)) {
throw new OperationCanceledException();
}
IFileBuffer[] unsynchronizedFileBuffers = findUnsynchronizedFileBuffers(fileBuffers);
performOperation(unsynchronizedFileBuffers, operation, subMonitor.split(size * 40));
final IFileBuffer[] synchronizedFileBuffers = findSynchronizedFileBuffers(fileBuffers);
fIsCompleted = false;
fThrowable = null;
synchronized (fCompletionLock) {
executeInContext(new Runnable() {
@Override
public void run() {
synchronized (fCompletionLock) {
try {
SafeRunner.run(new ISafeRunnable() {
@Override
public void handleException(Throwable throwable) {
fThrowable = throwable;
}
@Override
public void run() throws Exception {
performOperation(synchronizedFileBuffers, operation, subMonitor.split(50));
}
});
} finally {
fIsCompleted = true;
fCompletionLock.notifyAll();
}
}
}
});
while (!fIsCompleted) {
try {
fCompletionLock.wait(500);
} catch (InterruptedException x) {
}
}
}
if (fThrowable != null) {
if (fThrowable instanceof CoreException)
throw (CoreException) fThrowable;
throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, fThrowable.getLocalizedMessage(), fThrowable));
}
commit(fileBuffers2Save, subMonitor.split(size * 80));
} finally {
releaseFileBuffers(locations, subMonitor.split(size * 10));
}
}
use of org.eclipse.core.runtime.SubMonitor in project eclipse.platform.text by eclipse.
the class RemoveTrailingWhitespaceOperation method computeTextEdit.
@Override
protected MultiTextEditWithProgress computeTextEdit(ITextFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException {
IDocument document = fileBuffer.getDocument();
int lineCount = document.getNumberOfLines();
SubMonitor subMonitor = SubMonitor.convert(progressMonitor, FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_generatingChanges, lineCount);
try {
MultiTextEditWithProgress multiEdit = new MultiTextEditWithProgress(FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_applyingChanges);
for (int i = 0; i < lineCount; i++) {
IRegion region = document.getLineInformation(i);
if (region.getLength() == 0)
continue;
int lineStart = region.getOffset();
int lineExclusiveEnd = lineStart + region.getLength();
int j = lineExclusiveEnd - 1;
while (j >= lineStart && Character.isWhitespace(document.getChar(j))) --j;
++j;
if (j < lineExclusiveEnd)
multiEdit.addChild(new DeleteEdit(j, lineExclusiveEnd - j));
subMonitor.split(1);
}
return multiEdit.getChildrenSize() <= 0 ? null : multiEdit;
} catch (BadLocationException x) {
// $NON-NLS-1$
throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, "", x));
}
}
Aggregations