Java Code Examples for org.eclipse.jdt.core.ISourceReference#getSource()

The following examples show how to use org.eclipse.jdt.core.ISourceReference#getSource() . 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: TypedSource.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static String getSourceOfDeclararationNode(IJavaElement elem, ICompilationUnit cu) throws JavaModelException, CoreException {
	Assert.isTrue(elem.getElementType() != IJavaElement.IMPORT_CONTAINER);
	if (elem instanceof ISourceReference) {
		ISourceReference reference= (ISourceReference) elem;
		String source= reference.getSource();
		if (source != null)
			return Strings.trimIndentation(source, cu.getJavaProject(), false);
	}
	return ""; //$NON-NLS-1$
}
 
Example 2
Source File: JavaCompareAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String getExtendedSource(ISourceReference ref) throws JavaModelException {

		// get parent
		if (ref instanceof IJavaElement) {
			IJavaElement parent= ((IJavaElement) ref).getParent();
			if (parent instanceof ISourceReference) {
				ISourceReference sr= (ISourceReference) parent;
				String parentContent= sr.getSource();
				if (parentContent != null) {
					ISourceRange parentRange= sr.getSourceRange();
					ISourceRange childRange= ref.getSourceRange();

					int start= childRange.getOffset() - parentRange.getOffset();
					int end= start + childRange.getLength();

					// search backwards for beginning of line
					while (start > 0) {
						char c= parentContent.charAt(start-1);
						if (c == '\n' || c == '\r')
							break;
						start--;
					}

					return parentContent.substring(start, end);
				}
			}
		}

		return ref.getSource();
	}
 
Example 3
Source File: SourceView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected Object computeInput(Object input) {

	if (fViewer == null || !(input instanceof ISourceReference))
		return null;

	ISourceReference sourceRef= (ISourceReference)input;

	if (fLastOpenedElement != null && input instanceof IJavaElement && ((IJavaElement)input).getHandleIdentifier().equals(fLastOpenedElement.getHandleIdentifier())) {
		fLastOpenedElement= null;
		return null;
	} else {
		fLastOpenedElement= null;
	}

	String source;
	try {
		source= sourceRef.getSource();
	} catch (JavaModelException ex) {
		return ""; //$NON-NLS-1$
	}

	if (source == null)
		return ""; //$NON-NLS-1$

	source= removeLeadingComments(source);
	String delim= StubUtility.getLineDelimiterUsed((IJavaElement) input);

	String[] sourceLines= Strings.convertIntoLines(source);
	if (sourceLines == null || sourceLines.length == 0)
		return ""; //$NON-NLS-1$

	String firstLine= sourceLines[0];
	boolean firstCharNotWhitespace= firstLine != null && firstLine.length() > 0 && !Character.isWhitespace(firstLine.charAt(0));
	if (firstCharNotWhitespace)
		sourceLines[0]= ""; //$NON-NLS-1$
	IJavaProject project;
	if (input instanceof IJavaElement)
		project= ((IJavaElement) input).getJavaProject();
	else
		project= null;
	Strings.trimIndentation(sourceLines, project);

	if (firstCharNotWhitespace)
		sourceLines[0]= firstLine;

	return Strings.concatenate(sourceLines, delim);
}