Java Code Examples for org.eclipse.jdt.core.IOpenable#getBuffer()

The following examples show how to use org.eclipse.jdt.core.IOpenable#getBuffer() . 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: LapseView.java    From lapse-plus with GNU General Public License v3.0 6 votes vote down vote up
private CompilationUnit internalSetInput(IOpenable input) throws CoreException {
	IBuffer buffer = input.getBuffer();
	if (buffer == null) {
		JavaPlugin.logErrorMessage("Input has no buffer"); //$NON-NLS-1$
	}
	if (input instanceof ICompilationUnit) {
		fParser.setSource((ICompilationUnit) input);
	} else {
		fParser.setSource((IClassFile) input);
	}

	try {
		CompilationUnit root = (CompilationUnit) fParser.createAST(null);
		log("Recomputed the AST for " + buffer.getUnderlyingResource().getName());
						
		if (root == null) {
			JavaPlugin.logErrorMessage("Could not create AST"); //$NON-NLS-1$
		}

		return root;
	} catch (RuntimeException e) {
		JavaPlugin.logErrorMessage("Could not create AST:\n" + e.getMessage()); //$NON-NLS-1$
		return null;
	}
}
 
Example 2
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a range for the given offset and length for an {@link IOpenable}
 *
 * @param openable
 * @param offset
 * @param length
 * @return
 * @throws JavaModelException
 */
public static Range toRange(IOpenable openable, int offset, int length) throws JavaModelException{
	Range range = newRange();
	if (offset > 0 || length > 0) {
		int[] loc = null;
		int[] endLoc = null;
		IBuffer buffer = openable.getBuffer();
		if (buffer != null) {
			loc = JsonRpcHelpers.toLine(buffer, offset);
			endLoc = JsonRpcHelpers.toLine(buffer, offset + length);
		}
		if (loc == null) {
			loc = new int[2];
		}
		if (endLoc == null) {
			endLoc = new int[2];
		}
		setPosition(range.getStart(), loc);
		setPosition(range.getEnd(), endLoc);
	}
	return range;
}
 
Example 3
Source File: JsonRpcHelpers.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static <T> T convert(IOpenable openable, Function<IDocument, T> consumer) throws JavaModelException {
	Assert.isNotNull(openable, "openable");
	boolean mustClose = false;
	try {
		if (!openable.isOpen()) {
			openable.open(new NullProgressMonitor());
			mustClose = openable.isOpen();
		}
		IBuffer buffer = openable.getBuffer();
		return consumer.apply(toDocument(buffer));
	} finally {
		if (mustClose) {
			try {
				openable.close();
			} catch (JavaModelException e) {
				JavaLanguageServerPlugin.logException("Error when closing openable: " + openable, e);
			}
		}
	}
}
 
Example 4
Source File: CallLocation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the IBuffer for the IMember represented by this CallLocation.
 *
 * @return IBuffer for the IMember or null if the member doesn't have a buffer (for
 *          example if it is a binary file without source attachment).
 */
private IBuffer getBufferForMember() {
    IBuffer buffer = null;
    try {
        IOpenable openable = fMember.getOpenable();
        if (openable != null && fMember.exists()) {
            buffer = openable.getBuffer();
        }
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
    }
    return buffer;
}
 
Example 5
Source File: JavaDocLocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the reason for why the Javadoc of the Java element could not be retrieved.
 * 
 * @param element whose Javadoc could not be retrieved
 * @param root the root of the Java element
 * @return the String message for why the Javadoc could not be retrieved for the Java element or
 *         <code>null</code> if the Java element is from a source container
 * @since 3.9
 */
public static String getExplanationForMissingJavadoc(IJavaElement element, IPackageFragmentRoot root) {
	String message= null;
	try {
		boolean isBinary= (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY);
		if (isBinary) {
			boolean hasAttachedJavadoc= JavaDocLocations.getJavadocBaseLocation(element) != null;
			boolean hasAttachedSource= root.getSourceAttachmentPath() != null;
			IOpenable openable= element.getOpenable();
			boolean hasSource= openable.getBuffer() != null;

			// Provide hint why there's no Java doc
			if (!hasAttachedSource && !hasAttachedJavadoc)
				message= CorextMessages.JavaDocLocations_noAttachments;
			else if (!hasAttachedJavadoc && !hasSource)
				message= CorextMessages.JavaDocLocations_noAttachedJavadoc;
			else if (!hasAttachedSource)
				message= CorextMessages.JavaDocLocations_noAttachedSource;
			else if (!hasSource)
				message= CorextMessages.JavaDocLocations_noInformation;

		}
	} catch (JavaModelException e) {
		message= CorextMessages.JavaDocLocations_error_gettingJavadoc;
		JavaPlugin.log(e);
	}
	return message;
}
 
Example 6
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Evaluates the indentation used by a Java element. (in tabulators)
 * 
 * @param elem the element to get the indent of
 * @return return the indent unit
 * @throws JavaModelException thrown if the element could not be accessed
 */
public static int getIndentUsed(IJavaElement elem) throws JavaModelException {
	IOpenable openable= elem.getOpenable();
	if (openable instanceof ITypeRoot) {
		IBuffer buf= openable.getBuffer();
		if (buf != null) {
			int offset= ((ISourceReference)elem).getSourceRange().getOffset();
			return getIndentUsed(buf, offset, elem.getJavaProject());
		}
	}
	return 0;
}