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

The following examples show how to use org.eclipse.jdt.core.ISourceReference#getSourceRange() . 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: ProblemsLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tests if a position is inside the source range of an element.
 * @param pos Position to be tested.
 * @param sourceElement Source element (must be a IJavaElement)
 * @return boolean Return <code>true</code> if position is located inside the source element.
 * @throws CoreException Exception thrown if element range could not be accessed.
 *
 * @since 2.1
 */
protected boolean isInside(int pos, ISourceReference sourceElement) throws CoreException {
	if (fCachedRange == null) {
		fCachedRange= sourceElement.getSourceRange();
	}
	ISourceRange range= fCachedRange;
	if (range != null) {
		int rangeOffset= range.getOffset();
		return (rangeOffset <= pos && rangeOffset + range.getLength() > pos);
	}
	return false;
}
 
Example 2
Source File: CompilationUnitEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the offset of the given Java element.
 *
 * @param element	Java element
 * @return Offset of the given Java element
 */
private int getOffset(IJavaElement element) {
	if (element instanceof ISourceReference) {
		ISourceReference sr= (ISourceReference) element;
		try {
			ISourceRange srcRange= sr.getSourceRange();
			if (srcRange != null)
				return srcRange.getOffset();
		} catch (JavaModelException e) {
		}
	}
	return -1;
}
 
Example 3
Source File: CompilationUnitEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the length of the given Java element.
 *
 * @param element	Java element
 * @return Length of the given Java element
 */
private int getLength(IJavaElement element) {
	if (element instanceof ISourceReference) {
		ISourceReference sr= (ISourceReference) element;
		try {
			ISourceRange srcRange= sr.getSourceRange();
			if (srcRange != null)
				return srcRange.getLength();
		} catch (JavaModelException e) {
		}
	}
	return -1;
}
 
Example 4
Source File: GoToNextPreviousMemberAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void update() {
	boolean enabled= false;
	ISourceReference ref= getSourceReference();
	if (ref != null) {
		ISourceRange range;
		try {
			range= ref.getSourceRange();
			enabled= range != null && range.getLength() > 0;
		} catch (JavaModelException e) {
			// enabled= false;
		}
	}
	setEnabled(enabled);
}
 
Example 5
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 6
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void run() {

	final JavaSourceViewer viewer= (JavaSourceViewer) getSourceViewer();
	if (viewer.isEditable() && ElementValidator.check(getInputJavaElement(), getSite().getShell(), JavaEditorMessages.JavaEditor_FormatElementDialog_label, true)) {

		final Point selection= viewer.rememberSelection();
		try {
			viewer.setRedraw(false);

			boolean emptySelection= selection.y == 0;
			if (emptySelection) {
				final ITypedRegion partition= TextUtilities.getPartition(viewer.getDocument(), IJavaPartitions.JAVA_PARTITIONING, selection.x, true);
				String type= partition.getType();
				if (IJavaPartitions.JAVA_DOC.equals(type) || IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(type) || IJavaPartitions.JAVA_SINGLE_LINE_COMMENT.equals(type)) {
					viewer.setSelectedRange(partition.getOffset(), partition.getLength());
					viewer.doOperation(ISourceViewer.FORMAT);
					return;
				}
			}
			final IJavaElement element= getElementAt(selection.x, true);
			if (element != null && element.exists()) {
				try {
					final int kind= element.getElementType();
					if (kind == IJavaElement.TYPE || kind == IJavaElement.METHOD || kind == IJavaElement.INITIALIZER) {

						final ISourceReference reference= (ISourceReference) element;
						final ISourceRange range= reference.getSourceRange();
						final ISourceRange nameRange= reference.getNameRange();
						final boolean seletionInNameRange= nameRange != null && selection.x >= nameRange.getOffset()
								&& selection.x + selection.y <= nameRange.getOffset() + nameRange.getLength();
						if (range != null && (emptySelection || seletionInNameRange))
							viewer.setSelectedRange(range.getOffset(), range.getLength());
					}
				} catch (JavaModelException exception) {
					// Should not happen
				}
			}
			viewer.doOperation(ISourceViewer.FORMAT);
		} catch (BadLocationException e) {
			// Cannot happen
		} finally {

			viewer.setRedraw(true);
			viewer.restoreSelection();
		}
	}
}