Java Code Examples for org.eclipse.core.filebuffers.FileBuffers#getTextFileBufferManager()

The following examples show how to use org.eclipse.core.filebuffers.FileBuffers#getTextFileBufferManager() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModulaSearchUtils.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
private static void evaluateTextEditor(Map<IFile, IDocument> result, IEditorPart ep, IFile filter) {
    IEditorInput input = ep.getEditorInput();
    if (input instanceof IFileEditorInput) {
        IFile file = ((IFileEditorInput) input).getFile();
        if (filter == null || filter.equals(file)) { 
            if (!result.containsKey(file)) { // take the first editor found
                ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
                ITextFileBuffer textFileBuffer = bufferManager
                        .getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
                if (textFileBuffer != null) {
                    // file buffer has precedence
                    result.put(file, textFileBuffer.getDocument());
                }
                else {
                    // use document provider
                    IDocument document = ((ITextEditor) ep).getDocumentProvider().getDocument(input);
                    if (document != null) {
                        result.put(file, document);
                    }
                }
            }
        }
    }
}
 
Example 2
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Reads the content of the java.io.File.
 * 
 * @param file the file whose content has to be read
 * @return the content of the file
 * @throws CoreException if the file could not be successfully connected or disconnected
 */
private static String getFileContent(File file) throws CoreException {
	String content= null;
	ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();

	IPath fullPath= new Path(file.getAbsolutePath());
	manager.connect(fullPath, LocationKind.LOCATION, null);
	try {
		ITextFileBuffer buffer= manager.getTextFileBuffer(fullPath, LocationKind.LOCATION);
		if (buffer != null) {
			content= buffer.getDocument().get();
		}
	} finally {
		manager.disconnect(fullPath, LocationKind.LOCATION, null);
	}
	return content;
}
 
Example 3
Source File: EditorUtils.java    From typescript.java with MIT License 6 votes vote down vote up
public static Position getPosition(IFile file, TextSpan textSpan) throws BadLocationException {
	ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
	ITextFileBuffer buffer = bufferManager.getTextFileBuffer(file.getLocation(), LocationKind.IFILE);
	if (buffer != null) {
		return getPosition(buffer.getDocument(), textSpan);
	}
	IDocumentProvider provider = new TextFileDocumentProvider();
	try {
		provider.connect(file);
		IDocument document = provider.getDocument(file);
		if (document != null) {
			return getPosition(document, textSpan);
		}
	} catch (CoreException e) {
	} finally {
		provider.disconnect(file);
	}
	return null;
}
 
Example 4
Source File: JsonRpcHelpers.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: JavadocContentAccess2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Reads the content of the java.io.File.
 *
 * @param file
 *            the file whose content has to be read
 * @return the content of the file
 * @throws CoreException
 *             if the file could not be successfully connected or disconnected
 */
private static String getFileContent(File file) throws CoreException {
	String content = null;
	ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();

	IPath fullPath = new Path(file.getAbsolutePath());
	manager.connect(fullPath, LocationKind.LOCATION, null);
	try {
		ITextFileBuffer buffer = manager.getTextFileBuffer(fullPath, LocationKind.LOCATION);
		if (buffer != null) {
			content = buffer.getDocument().get();
		}
	} finally {
		manager.disconnect(fullPath, LocationKind.LOCATION, null);
	}
	return content;
}
 
Example 6
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Reads the content of the IFile.
 * 
 * @param file the file whose content has to be read
 * @return the content of the file
 * @throws CoreException if the file could not be successfully connected or disconnected
 */
private static String getIFileContent(IFile file) throws CoreException {
	String content= null;
	ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
	IPath fullPath= file.getFullPath();
	manager.connect(fullPath, LocationKind.IFILE, null);
	try {
		ITextFileBuffer buffer= manager.getTextFileBuffer(fullPath, LocationKind.IFILE);
		if (buffer != null) {
			content= buffer.getDocument().get();
		}
	} finally {
		manager.disconnect(fullPath, LocationKind.IFILE, null);
	}

	return content;
}
 
Example 7
Source File: DocumentAdapter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void setContents(String contents) {
	synchronized (lock) {
		if (fDocument == null) {
			if (fTextFileBuffer != null) {
				fDocument = fTextFileBuffer.getDocument();
			} else {
				ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
				fDocument =  manager.createEmptyDocument(fFile.getFullPath(), LocationKind.IFILE);
			}
			fDocument.addDocumentListener(this);
			((ISynchronizable)fDocument).setLockObject(lock);
		}
	}
	if (!contents.equals(fDocument.get())) {
		fDocument.set(contents);
	}
}
 
Example 8
Source File: DeletePackageFragmentRootChange.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
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());
	}
}
 
Example 9
Source File: DocumentAdapter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public DocumentAdapter(IOpenable owner, IFile file) {
	fOwner = owner;
	fFile = file;
	fBufferListeners = new ArrayList<>(3);
	fIsClosed = false;

	ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
	try {
		manager.connect(file.getFullPath(), LocationKind.IFILE, null);
		fTextFileBuffer= manager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
	} catch (CoreException e) {
	}
}
 
Example 10
Source File: TypeScriptHyperLinkDetector.java    From typescript.java with MIT License 5 votes vote down vote up
/**
 * Returns the {@link IResource} from the given text viewer and null otherwise.
 * 
 * @param textViewer
 * @return the {@link IResource} from the given text viewer and null otherwise.
 */
private IResource getResource(ITextViewer textViewer) {
	ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class);
	if (textEditor != null) {
		return EditorUtils.getResource(textEditor);
	}
	ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
	ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(textViewer.getDocument());
	if (textFileBuffer != null) {
		IPath location = textFileBuffer.getLocation();
		return ResourcesPlugin.getWorkspace().getRoot().findMember(location);
	}
	return null;
}
 
Example 11
Source File: TypeScriptResourceUtil.java    From typescript.java with MIT License 5 votes vote down vote up
/**
 * Returns the {@link IDocument} from the given file and null if it's not
 * possible.
 */
public static IDocument getDocument(IPath location) {
	ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();

	boolean connected = false;
	try {
		ITextFileBuffer buffer = manager.getTextFileBuffer(location, LocationKind.NORMALIZE);
		if (buffer == null) {
			// no existing file buffer..create one
			manager.connect(location, LocationKind.NORMALIZE, new NullProgressMonitor());
			connected = true;
			buffer = manager.getTextFileBuffer(location, LocationKind.NORMALIZE);
			if (buffer == null) {
				return null;
			}
		}

		return buffer.getDocument();
	} catch (CoreException ce) {
		TypeScriptCorePlugin.logError(ce, "Error while getting document from file");
		return null;
	} finally {
		if (connected) {
			try {
				manager.disconnect(location, LocationKind.NORMALIZE, new NullProgressMonitor());
			} catch (CoreException e) {
				TypeScriptCorePlugin.logError(e, "Error while getting document from file");
			}
		}
	}
}
 
Example 12
Source File: TypeScriptResourceUtil.java    From typescript.java with MIT License 5 votes vote down vote up
/**
 * Returns the file from the given {@link IDocument}.
 */
public static IFile getFile(IDocument document) {
	ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); // get
																					// the
																					// buffer
																					// manager
	ITextFileBuffer buffer = bufferManager.getTextFileBuffer(document);
	IPath location = buffer == null ? null : buffer.getLocation();
	if (location == null) {
		return null;
	}

	return ResourcesPlugin.getWorkspace().getRoot().getFile(location);
}
 
Example 13
Source File: DeletePackageFragmentRootChange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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());
	}
}
 
Example 14
Source File: NLSSearchResultRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private InputStream createInputStream(IFile propertiesFile) throws CoreException {
	ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
	if (manager != null) {
		ITextFileBuffer buffer= manager.getTextFileBuffer(propertiesFile.getFullPath(), LocationKind.IFILE);
		if (buffer != null) {
			return new ByteArrayInputStream(buffer.getDocument().get().getBytes());
		}
	}

	return propertiesFile.getContents();
}
 
Example 15
Source File: OpenTypeHistory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean isContainerDirty(TypeNameMatch match) {
	ICompilationUnit cu= match.getType().getCompilationUnit();
	if (cu == null) {
		return false;
	}
	IResource resource= cu.getResource();
	ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
	ITextFileBuffer textFileBuffer= manager.getTextFileBuffer(resource.getFullPath(), LocationKind.IFILE);
	if (textFileBuffer != null) {
		return textFileBuffer.isDirty();
	}
	return false;
}
 
Example 16
Source File: TexSpellingEngine.java    From texlipse with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the project that belongs to the given document.
 * Thanks to Nitin Dahyabhai for the tip.
 * 
 * @param document
 * @return the project or <code>null</code> if no project was found
 */
private static IProject getProject(IDocument document) {
    ITextFileBufferManager fileBufferMgr = FileBuffers.getTextFileBufferManager();
    ITextFileBuffer fileBuffer = fileBufferMgr.getTextFileBuffer(document);
    
    if (fileBuffer != null) {
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IResource res = workspace.getRoot().findMember(fileBuffer.getLocation());
        if (res != null) return res.getProject();        	
    }
    return null;
}
 
Example 17
Source File: AbstractAnnotationHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
	IPath path;
	IAnnotationModel model;
	if (textViewer instanceof ISourceViewer) {
		path= null;
		model= ((ISourceViewer)textViewer).getAnnotationModel();
	} else {
		// Get annotation model from file buffer manager
		path= getEditorInputPath();
		model= getAnnotationModel(path);
	}
	if (model == null)
		return null;

	try {
		Iterator<Annotation> parent;
		if (model instanceof IAnnotationModelExtension2)
			parent= ((IAnnotationModelExtension2)model).getAnnotationIterator(hoverRegion.getOffset(), hoverRegion.getLength(), true, true);
		else
			parent= model.getAnnotationIterator();
		Iterator<Annotation> e= new JavaAnnotationIterator(parent, fAllAnnotations);

		int layer= -1;
		Annotation annotation= null;
		Position position= null;
		while (e.hasNext()) {
			Annotation a= e.next();

			AnnotationPreference preference= getAnnotationPreference(a);
			if (preference == null || !(preference.getTextPreferenceKey() != null && fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey()))))
				continue;

			Position p= model.getPosition(a);

			int l= fAnnotationAccess.getLayer(a);

			if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
				String msg= a.getText();
				if (msg != null && msg.trim().length() > 0) {
					layer= l;
					annotation= a;
					position= p;
				}
			}
		}
		if (layer > -1)
			return createAnnotationInfo(annotation, position, textViewer);

	} finally {
		try {
			if (path != null) {
				ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
				manager.disconnect(path, LocationKind.NORMALIZE, null);
			}
		} catch (CoreException ex) {
			JavaPlugin.log(ex.getStatus());
		}
	}

	return null;
}
 
Example 18
Source File: DocumentReconcileManager.java    From goclipse with Eclipse Public License 1.0 4 votes vote down vote up
public DocumentReconcileManager() {
	this(FileBuffers.getTextFileBufferManager(), LangCore.getBuildManager());
}
 
Example 19
Source File: AbstractAnnotationHover.java    From typescript.java with MIT License 4 votes vote down vote up
@Override
public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
	IPath path;
	IAnnotationModel model;
	if (textViewer instanceof ISourceViewer) {
		path = null;
		model = ((ISourceViewer) textViewer).getAnnotationModel();
	} else {
		// Get annotation model from file buffer manager
		path = getEditorInputPath();
		model = getAnnotationModel(path);
	}
	if (model == null)
		return null;

	try {
		Iterator<Annotation> parent;
		if (model instanceof IAnnotationModelExtension2)
			parent = ((IAnnotationModelExtension2) model).getAnnotationIterator(hoverRegion.getOffset(),
					hoverRegion.getLength() > 0 ? hoverRegion.getLength() : 1, true, true);
		else
			parent = model.getAnnotationIterator();
		Iterator<Annotation> e = new TypeScriptAnnotationIterator(parent, fAllAnnotations);

		int layer = -1;
		Annotation annotation = null;
		Position position = null;
		while (e.hasNext()) {
			Annotation a = e.next();

			AnnotationPreference preference = getAnnotationPreference(a);
			if (preference == null || !(preference.getTextPreferenceKey() != null
			/*
			 * && fStore.getBoolean(preference.getTextPreferenceKey()) ||
			 * (preference.getHighlightPreferenceKey() != null &&
			 * fStore.getBoolean(preference.getHighlightPreferenceKey()))
			 */))
				continue;

			Position p = model.getPosition(a);

			int l = fAnnotationAccess.getLayer(a);

			if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
				String msg = a.getText();
				if (msg != null && msg.trim().length() > 0) {
					layer = l;
					annotation = a;
					position = p;
				}
			}
		}
		if (layer > -1)
			return createAnnotationInfo(annotation, position, textViewer);

	} finally {
		try {
			if (path != null) {
				ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
				manager.disconnect(path, LocationKind.NORMALIZE, null);
			}
		} catch (CoreException ex) {
			TypeScriptUIPlugin.log(ex.getStatus());
		}
	}

	return null;
}
 
Example 20
Source File: ContentTypeHelper.java    From tm4e with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Find the content types from the given {@link IDocument} by using
 * {@link ITextFileBufferManager} and null otherwise.
 * 
 * @param document
 * @return the content types from the given {@link IDocument} by using
 *         {@link ITextFileBufferManager} and null otherwise.
 * @throws CoreException
 */
private static ContentTypeInfo findContentTypesFromFileBuffers(IDocument document) throws CoreException {
	ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
	ITextFileBuffer buffer = bufferManager.getTextFileBuffer(document);
	if (buffer != null) {
		return getContentTypes(buffer);
	}
	return null;
}