use of org.eclipse.core.filebuffers.ITextFileBufferManager in project webtools.sourceediting by eclipse.
the class TestModelsFromFiles method getTextBuffer.
private ITextFileBuffer getTextBuffer(IFile file) throws CoreException {
ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
IPath fileIPath = file.getFullPath();
IPath normalizedPath = FileBuffers.normalizeLocation(fileIPath);
fileBufferManager.connect(normalizedPath, null);
ITextFileBuffer buffer = fileBufferManager.getTextFileBuffer(normalizedPath);
return buffer;
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project webtools.sourceediting by eclipse.
the class FileBufferModelManager method disconnect.
/**
* Deregisters "interest" in a document, or rather the file buffer that
* backs it. Intentionally used to alter the reference count of the file
* buffer so that it knows it can safely be disposed of.
*/
public boolean disconnect(IDocument document) {
if (document == null) {
// $NON-NLS-1$
Exception iae = new IllegalArgumentException("can not disconnect() without a document");
Logger.logException(iae);
return false;
}
DocumentInfo info = (DocumentInfo) fDocumentMap.get(document);
if (info == null)
return false;
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
boolean isOK = true;
try {
if (info.buffer.getLocation() != null) {
bufferManager.disconnect(info.buffer.getLocation(), info.locationKind, null);
} else if (info.buffer.getFileStore() != null) {
bufferManager.disconnectFileStore(info.buffer.getFileStore(), null);
}
} catch (CoreException e) {
Logger.logException(e);
isOK = false;
}
return isOK;
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project webtools.sourceediting by eclipse.
the class FileBufferModelManager method releaseModel.
public void releaseModel(IDocument document) {
if (document == null) {
// $NON-NLS-1$
Exception iae = new IllegalArgumentException("can not release a model without a document reference");
Logger.logException(iae);
return;
}
DocumentInfo info = (DocumentInfo) fDocumentMap.get(document);
if (info != null) {
if (Logger.DEBUG_FILEBUFFERMODELMANAGEMENT) {
// $NON-NLS-1$ //$NON-NLS-2$
Logger.log(Logger.INFO, "FileBufferModelManager noticed full release of model for " + locationString(info.buffer) + " " + info.buffer.getDocument());
}
info.model = null;
info.modelReferenceCount--;
if (info.selfConnected) {
if (Logger.DEBUG_FILEBUFFERMODELMANAGEMENT) {
// $NON-NLS-1$ //$NON-NLS-2$
Logger.log(Logger.INFO, "FileBufferModelManager disconnecting from " + locationString(info.buffer) + " " + info.buffer.getDocument());
}
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
try {
if (info.buffer.getLocation() != null) {
bufferManager.disconnect(info.buffer.getLocation(), info.locationKind, null);
} else if (info.buffer.getFileStore() != null) {
bufferManager.disconnectFileStore(info.buffer.getFileStore(), null);
}
} catch (CoreException e) {
// $NON-NLS-1$
Logger.logException("Error releasing model for " + locationString(info.buffer), e);
}
}
// [265899]
// In some scenarios, a model can be held onto after the editor has been disposed even if the lifecycle is
// maintained properly (e.g., an editor being closed before a DirtyRegionProcessor has a chance to complete). Because of this,
// the manager cannot be reliant upon the FileBufferMapper having the sole responsibility of the fDocumentMap cleanup
checkReferenceCounts(info, document);
}
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project webtools.sourceediting by eclipse.
the class FileBufferModelManager method getModel.
IStructuredModel getModel(File file) {
if (file == null) {
// $NON-NLS-1$
Exception iae = new IllegalArgumentException("can not get/create a model without a java.io.File");
Logger.logException(iae);
return null;
}
IStructuredModel model = null;
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
try {
IPath location = new Path(file.getAbsolutePath());
if (Logger.DEBUG_FILEBUFFERMODELMANAGEMENT) {
// $NON-NLS-1$
Logger.log(Logger.INFO, "FileBufferModelManager connecting to File " + location);
}
bufferManager.connect(location, LocationKind.LOCATION, getProgressMonitor());
ITextFileBuffer buffer = bufferManager.getTextFileBuffer(location, LocationKind.LOCATION);
if (buffer != null) {
DocumentInfo info = (DocumentInfo) fDocumentMap.get(buffer.getDocument());
if (info != null) {
/*
* Note: "info" being null at this point is a slight
* error.
*
* The connect call from above (or at some time earlier in
* the session) would have notified the FileBufferMapper
* of the creation of the corresponding text buffer and
* created the DocumentInfo object for
* IStructuredDocuments.
*/
info.locationKind = LocationKind.LOCATION;
info.selfConnected = true;
}
/*
* Check the document type. Although returning null for
* unknown documents would be fair, try to get a model if
* the document is at least a valid type.
*/
IDocument bufferDocument = buffer.getDocument();
if (bufferDocument instanceof IStructuredDocument) {
model = getModel((IStructuredDocument) bufferDocument);
} else {
/*
* 190768 - Quick diff marks do not disappear in the
* vertical ruler of JavaScript editor and
*
* 193805 - Changes are not thrown away when close
* with no save for files with no structured model
* associated with them (text files, javascript files,
* etc) in web project
*/
bufferManager.disconnect(location, LocationKind.IFILE, getProgressMonitor());
}
}
} catch (CoreException e) {
// $NON-NLS-1$
Logger.logException("Error getting model for " + file.getPath(), e);
}
return model;
}
use of org.eclipse.core.filebuffers.ITextFileBufferManager in project eclipse.jdt.ls by eclipse.
the class JsonRpcHelpers method toDocument.
/**
* Returns an {@link IDocument} for the given {@link IFile}.
*
* @param file an {@link IFile}
* @return a document with the contents of the file,
* or <code>null</code> if the file can not be opened.
*/
public static IDocument toDocument(IFile file) {
if (file != null && file.isAccessible()) {
IPath path = file.getFullPath();
ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
LocationKind kind = LocationKind.IFILE;
try {
fileBufferManager.connect(path, kind, new NullProgressMonitor());
ITextFileBuffer fileBuffer = fileBufferManager.getTextFileBuffer(path, kind);
if (fileBuffer != null) {
return fileBuffer.getDocument();
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Failed to convert " + file + " to an IDocument", e);
} finally {
try {
fileBufferManager.disconnect(path, kind, new NullProgressMonitor());
} catch (CoreException slurp) {
// Don't care
}
}
}
return null;
}
Aggregations