use of org.eclipse.core.filebuffers.ITextFileBufferManager in project eclipse-pmd by acanda.
the class JavaQuickFix method fixMarkersInFile.
/**
* Fixes all provided markers in a file.
*
* @param markers The markers to fix. There is at least one marker in this collection and all markers can be fixed
* by this quick fix.
*/
protected void fixMarkersInFile(final IFile file, final List<IMarker> markers, final IProgressMonitor monitor) {
monitor.subTask(file.getFullPath().toOSString());
final Optional<ICompilationUnit> optionalCompilationUnit = getCompilationUnit(file);
if (!optionalCompilationUnit.isPresent()) {
return;
}
final ICompilationUnit compilationUnit = optionalCompilationUnit.get();
ITextFileBufferManager bufferManager = null;
final IPath path = compilationUnit.getPath();
try {
bufferManager = FileBuffers.getTextFileBufferManager();
bufferManager.connect(path, LocationKind.IFILE, null);
final ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
final IDocument document = textFileBuffer.getDocument();
final IAnnotationModel annotationModel = textFileBuffer.getAnnotationModel();
final ASTParser astParser = ASTParser.newParser(AST.JLS4);
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
astParser.setResolveBindings(needsTypeResolution());
astParser.setSource(compilationUnit);
final SubProgressMonitor parserMonitor = new SubProgressMonitor(monitor, 100);
final CompilationUnit ast = (CompilationUnit) astParser.createAST(parserMonitor);
parserMonitor.done();
startFixingMarkers(ast);
final Map<?, ?> options = compilationUnit.getJavaProject().getOptions(true);
for (final IMarker marker : markers) {
try {
final MarkerAnnotation annotation = getMarkerAnnotation(annotationModel, marker);
// if the annotation is null it means that is was deleted by a previous quick fix
if (annotation != null) {
final Optional<T> node = getNodeFinder(annotationModel.getPosition(annotation)).findNode(ast);
if (node.isPresent()) {
final boolean isSuccessful = fixMarker(node.get(), document, options);
if (isSuccessful) {
marker.delete();
}
}
}
} finally {
monitor.worked(100);
}
}
finishFixingMarkers(ast, document, options);
// commit changes to underlying file if it is not opened in an editor
if (!isEditorOpen(file)) {
final SubProgressMonitor commitMonitor = new SubProgressMonitor(monitor, 100);
textFileBuffer.commit(commitMonitor, false);
commitMonitor.done();
} else {
monitor.worked(100);
}
} catch (CoreException | MalformedTreeException | BadLocationException e) {
// TODO: log error
// PMDPlugin.getDefault().error("Error processing quickfix", e);
} finally {
if (bufferManager != null) {
try {
bufferManager.disconnect(path, LocationKind.IFILE, null);
} catch (final CoreException e) {
// TODO: log error
// PMDPlugin.getDefault().error("Error processing quickfix", e);
}
}
}
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project che by eclipse.
the class DeletePackageFragmentRootChange method getFileContents.
private static String getFileContents(IFile file) throws CoreException {
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
IPath path = file.getFullPath();
manager.connect(path, LocationKind.IFILE, new NullProgressMonitor());
try {
return manager.getTextFileBuffer(path, LocationKind.IFILE).getDocument().get();
} finally {
manager.disconnect(path, LocationKind.IFILE, new NullProgressMonitor());
}
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project che by eclipse.
the class TextFileChange method releaseDocument.
/**
* {@inheritDoc}
*/
protected void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException {
Assert.isTrue(fAcquireCount > 0);
if (fAcquireCount == 1) {
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
manager.disconnect(fFile.getFullPath(), LocationKind.IFILE, pm);
}
fAcquireCount--;
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project che by eclipse.
the class UndoTextFileChange method perform.
/**
* {@inheritDoc}
*/
public Change perform(IProgressMonitor pm) throws CoreException {
if (pm == null)
pm = new NullProgressMonitor();
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
//$NON-NLS-1$
pm.beginTask("", 2);
ITextFileBuffer buffer = null;
try {
manager.connect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1));
buffer = manager.getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE);
IDocument document = buffer.getDocument();
ContentStamp currentStamp = ContentStamps.get(fFile, document);
boolean[] setContentStampSuccess = { false };
UndoEdit redo = performEdits(buffer, document, setContentStampSuccess);
if (needsSaving()) {
buffer.commit(pm, false);
if (!setContentStampSuccess[0]) {
// We weren't able to restore document stamp.
// Since we save restore the file stamp instead
ContentStamps.set(fFile, fContentStampToRestore);
}
}
return createUndoChange(redo, currentStamp);
} catch (BadLocationException e) {
if (fValidationState == null || !fValidationState.wasDerived())
throw Changes.asCoreException(e);
else
return new NullChange();
} catch (MalformedTreeException e) {
if (fValidationState == null || !fValidationState.wasDerived())
throw Changes.asCoreException(e);
else
return new NullChange();
} catch (CoreException e) {
if (fValidationState == null || !fValidationState.wasDerived())
throw e;
else
return new NullChange();
} finally {
if (buffer != null)
manager.disconnect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1));
}
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project che by eclipse.
the class ModificationStampValidationState method getBuffer.
protected static ITextFileBuffer getBuffer(IFile file) {
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
IPath path = file.getFullPath();
ITextFileBuffer buffer = manager.getTextFileBuffer(path, LocationKind.IFILE);
return buffer;
}
Aggregations