Search in sources :

Example 71 with ITextFileBufferManager

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer)

Example 72 with ITextFileBufferManager

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;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) CoreException(org.eclipse.core.runtime.CoreException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 73 with ITextFileBufferManager

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);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) CoreException(org.eclipse.core.runtime.CoreException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 74 with ITextFileBufferManager

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) CoreException(org.eclipse.core.runtime.CoreException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IDocument(org.eclipse.jface.text.IDocument)

Example 75 with ITextFileBufferManager

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;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) LocationKind(org.eclipse.core.filebuffers.LocationKind) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer)

Aggregations

ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)87 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)66 IDocument (org.eclipse.jface.text.IDocument)54 IPath (org.eclipse.core.runtime.IPath)52 CoreException (org.eclipse.core.runtime.CoreException)36 BadLocationException (org.eclipse.jface.text.BadLocationException)26 IFile (org.eclipse.core.resources.IFile)22 Test (org.junit.Test)17 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)11 IOException (java.io.IOException)10 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)10 IResource (org.eclipse.core.resources.IResource)8 Path (org.eclipse.core.runtime.Path)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 TextEdit (org.eclipse.text.edits.TextEdit)7 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 UndoEdit (org.eclipse.text.edits.UndoEdit)6 FileNotFoundException (java.io.FileNotFoundException)5 MalformedURLException (java.net.MalformedURLException)5