Java Code Examples for org.eclipse.jdt.core.ISourceRange#getOffset()

The following examples show how to use org.eclipse.jdt.core.ISourceRange#getOffset() . 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: MethodsSourcePositionComparator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private int compareInTheSameType(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) {
	try {
		IMethod firstMethod= (IMethod)firstMethodBinding.getJavaElement();
		IMethod secondMethod= (IMethod)secondMethodBinding.getJavaElement();
		if (firstMethod == null || secondMethod == null) {
			return 0;
		}
		ISourceRange firstSourceRange= firstMethod.getSourceRange();
		ISourceRange secondSourceRange= secondMethod.getSourceRange();

		if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) {
			return firstMethod.getElementName().compareTo(secondMethod.getElementName());
		} else {
			return firstSourceRange.getOffset() - secondSourceRange.getOffset();
		}
	} catch (JavaModelException e) {
		return 0;
	}
}
 
Example 2
Source File: StructureSelectNextAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
ISourceRange internalGetNewSelectionRange(ISourceRange oldSourceRange, ISourceReference sr, SelectionAnalyzer selAnalyzer) throws JavaModelException{
	if (oldSourceRange.getLength() == 0 && selAnalyzer.getLastCoveringNode() != null) {
		ASTNode previousNode= NextNodeAnalyzer.perform(oldSourceRange.getOffset(), selAnalyzer.getLastCoveringNode());
		if (previousNode != null)
			return getSelectedNodeSourceRange(sr, previousNode);
	}
	ASTNode first= selAnalyzer.getFirstSelectedNode();
	if (first == null)
		return getLastCoveringNodeRange(oldSourceRange, sr, selAnalyzer);

	ASTNode parent= first.getParent();
	if (parent == null)
		return getLastCoveringNodeRange(oldSourceRange, sr, selAnalyzer);

	ASTNode lastSelectedNode= selAnalyzer.getSelectedNodes()[selAnalyzer.getSelectedNodes().length - 1];
	ASTNode nextNode= getNextNode(parent, lastSelectedNode);
	if (nextNode == parent)
		return getSelectedNodeSourceRange(sr, first.getParent());
	int offset= oldSourceRange.getOffset();
	int end= Math.min(sr.getSourceRange().getLength(), nextNode.getStartPosition() + nextNode.getLength() - 1);
	return StructureSelectionAction.createSourceRange(offset, end);
}
 
Example 3
Source File: MethodsSourcePositionComparator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private int compareInTheSameType(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) {
	try {
		IMethod firstMethod= (IMethod)firstMethodBinding.getJavaElement();
		IMethod secondMethod= (IMethod)secondMethodBinding.getJavaElement();
		if (firstMethod == null || secondMethod == null) {
			return 0;
		}
		ISourceRange firstSourceRange= firstMethod.getSourceRange();
		ISourceRange secondSourceRange= secondMethod.getSourceRange();

		if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) {
			return firstMethod.getElementName().compareTo(secondMethod.getElementName());
		} else {
			return firstSourceRange.getOffset() - secondSourceRange.getOffset();
		}
	} catch (JavaModelException e) {
		return 0;
	}
}
 
Example 4
Source File: JavadocContentAccess.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Gets a reader for an IMember's Javadoc comment content from the source attachment.
 * The content does contain only the text from the comment without the Javadoc leading star characters.
 * Returns <code>null</code> if the member does not contain a Javadoc comment or if no source is available.
 * @param member The member to get the Javadoc of.
 * @return Returns a reader for the Javadoc comment content or <code>null</code> if the member
 * does not contain a Javadoc comment or if no source is available
 * @throws JavaModelException is thrown when the elements javadoc can not be accessed
 * @since 3.4
 */
private static Reader internalGetContentReader(IMember member) throws JavaModelException {
	IBuffer buf= member.getOpenable().getBuffer();
	if (buf == null) {
		return null; // no source attachment found
	}

	ISourceRange javadocRange= member.getJavadocRange();
	if (javadocRange != null) {
		JavaDocCommentReader reader= new JavaDocCommentReader(buf, javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength() - 1);
		if (!containsOnlyInheritDoc(reader, javadocRange.getLength())) {
			reader.reset();
			return reader;
		}
	}

	return null;
}
 
Example 5
Source File: JavadocContentAccess.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets a reader for an IMember's Javadoc comment content from the source attachment.
 * The content does contain only the text from the comment without the Javadoc leading star characters.
 * Returns <code>null</code> if the member does not contain a Javadoc comment or if no source is available.
 * @param member The member to get the Javadoc of.
 * @return Returns a reader for the Javadoc comment content or <code>null</code> if the member
 * does not contain a Javadoc comment or if no source is available
 * @throws JavaModelException is thrown when the elements javadoc can not be accessed
 * @since 3.4
 */
private static Reader internalGetContentReader(IMember member) throws JavaModelException {
	IBuffer buf= member.getOpenable().getBuffer();
	if (buf == null) {
		return null; // no source attachment found
	}

	ISourceRange javadocRange= member.getJavadocRange();
	if (javadocRange != null) {
		JavaDocCommentReader reader= new JavaDocCommentReader(buf, javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength() - 1);
		if (!containsOnlyInheritDoc(reader, javadocRange.getLength())) {
			reader.reset();
			return reader;
		}
	}

	return null;
}
 
Example 6
Source File: MatchLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void reportBinaryMemberDeclaration(IResource resource, IMember binaryMember, Binding binaryMemberBinding, IBinaryType info, int accuracy) throws CoreException {
	ClassFile classFile = (ClassFile) binaryMember.getClassFile();
	ISourceRange range = classFile.isOpen() ? binaryMember.getNameRange() : SourceMapper.UNKNOWN_RANGE;
	if (range.getOffset() == -1) {
		BinaryType type = (BinaryType) classFile.getType();
		String sourceFileName = type.sourceFileName(info);
		if (sourceFileName != null) {
			SourceMapper mapper = classFile.getSourceMapper();
			if (mapper != null) {
				char[] contents = mapper.findSource(type, sourceFileName);
				if (contents != null)
					range = mapper.mapSource(type, contents, info, binaryMember);
			}
		}
	}
	if (resource == null) resource =  this.currentPossibleMatch.resource;
	SearchMatch match = newDeclarationMatch(binaryMember, binaryMemberBinding, accuracy, range.getOffset(), range.getLength(), getParticipant(), resource);
	report(match);
}
 
Example 7
Source File: Utils.java    From jdt-codemining with Eclipse Public License 1.0 5 votes vote down vote up
public static ILineRange getLineRange(IJavaElement element, IDocument document)
		throws JavaModelException, BadLocationException {
	ISourceRange r = ((ISourceReference) element).getSourceRange();
	int offset = r.getOffset();
	int startLine = document.getLineOfOffset(offset);
	int endLine = document.getLineOfOffset(offset + r.getLength());
	return new LineRange(startLine, endLine - startLine);
}
 
Example 8
Source File: DOMFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public ASTNode search() throws JavaModelException {
	ISourceRange range = null;
	if (this.element instanceof IMember && !(this.element instanceof IInitializer))
		range = ((IMember) this.element).getNameRange();
	else
		range = this.element.getSourceRange();
	this.rangeStart = range.getOffset();
	this.rangeLength = range.getLength();
	this.ast.accept(this);
	return this.foundNode;
}
 
Example 9
Source File: RenameTypeWizardSimilarElementsPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public int category(Object element) {

	/*
	 * We'd like to present the elements in the same order as they
	 * appear in the source. This can be achieved by assigning a
	 * distinct category to every element; the category being derived
	 * from the source position of the element.
	 */

	ISourceRange sourceRange= null;
	if (element instanceof IMember) {
		IMember member= (IMember) element;
		try {
			sourceRange= member.getNameRange();
		} catch (JavaModelException e) {
			// fall through
		}
	}
	if (element instanceof ILocalVariable) {
		ILocalVariable var= (ILocalVariable) element;
		sourceRange= var.getNameRange();
	}

	if (sourceRange != null)
		return 100 + sourceRange.getOffset(); // +100: safe distance from all other categories.

	return super.category(element);
}
 
Example 10
Source File: GoToNextPreviousMemberAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final  void run() {
	ITextSelection selection= getTextSelection();
	ISourceRange newRange= getNewSelectionRange(createSourceRange(selection), null);
	// Check if new selection differs from current selection
	if (selection.getOffset() == newRange.getOffset() && selection.getLength() == newRange.getLength())
		return;
	fEditor.selectAndReveal(newRange.getOffset(), newRange.getLength());
}
 
Example 11
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 12
Source File: TopLevelTypeProblemsLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
	protected boolean isInside(int pos, ISourceReference sourceElement) throws CoreException {
//		XXX: Work in progress for problem decorator being a workbench decorator
//		IDecoratorManager decoratorMgr= PlatformUI.getWorkbench().getDecoratorManager();
//		if (!decoratorMgr.getEnabled("org.eclipse.jdt.ui.problem.decorator")) //$NON-NLS-1$
//			return false;

		if (!(sourceElement instanceof IType) || ((IType)sourceElement).getDeclaringType() != null)
			return false;

		ICompilationUnit cu= ((IType)sourceElement).getCompilationUnit();
		if (cu == null)
			return false;
		IType[] types= cu.getTypes();
		if (types.length < 1)
			return false;

		int firstTypeStartOffset= -1;
		ISourceRange range= types[0].getSourceRange();
		if (range != null)
			firstTypeStartOffset= range.getOffset();

		int lastTypeEndOffset= -1;
		range= types[types.length-1].getSourceRange();
		if (range != null)
			lastTypeEndOffset= range.getOffset() + range.getLength() - 1;

		return pos < firstTypeStartOffset || pos > lastTypeEndOffset || isInside(pos, sourceElement.getSourceRange());
	}
 
Example 13
Source File: DefaultJavaFoldingStructureProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public int computeCaptionOffset(IDocument document) throws BadLocationException {
	int nameStart= offset;
	try {
		// need a reconcile here?
		ISourceRange nameRange= fMember.getNameRange();
		if (nameRange != null)
			nameStart= nameRange.getOffset();
	} catch (JavaModelException e) {
		// ignore and use default
	}

	return nameStart - offset;
}
 
Example 14
Source File: RenameFieldProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void addDeclarationUpdate() throws CoreException {
	ISourceRange nameRange= fField.getNameRange();
	TextEdit textEdit= new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName());
	ICompilationUnit cu= fField.getCompilationUnit();
	String groupName= RefactoringCoreMessages.RenameFieldRefactoring_Update_field_declaration;
	addTextEdit(fChangeManager.get(cu), groupName, textEdit);
}
 
Example 15
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();
		}
	}
}
 
Example 16
Source File: AbstractJavaCodeMining.java    From jdt-codemining with Eclipse Public License 1.0 4 votes vote down vote up
private static int getLineNumber(IJavaElement element, IDocument document)
		throws JavaModelException, BadLocationException {
	ISourceRange r = ((ISourceReference) element).getNameRange();
	int offset = r.getOffset();
	return document.getLineOfOffset(offset);
}
 
Example 17
Source File: Util.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static boolean covers(ISourceRange thisRange, ISourceRange otherRange) {
	return thisRange.getOffset() <= otherRange.getOffset() && getEndInclusive(thisRange) >= getEndInclusive(otherRange);
}
 
Example 18
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Computes and returns the source reference that includes the caret and
 * serves as provider for the outline page selection and the editor range
 * indication.
 *
 * @return the computed source reference
 * @since 3.0
 */
protected ISourceReference computeHighlightRangeSourceReference() {
	ISourceViewer sourceViewer= getSourceViewer();
	if (sourceViewer == null)
		return null;

	StyledText styledText= sourceViewer.getTextWidget();
	if (styledText == null)
		return null;

	int caret= 0;
	if (sourceViewer instanceof ITextViewerExtension5) {
		ITextViewerExtension5 extension= (ITextViewerExtension5)sourceViewer;
		caret= extension.widgetOffset2ModelOffset(styledText.getCaretOffset());
	} else {
		int offset= sourceViewer.getVisibleRegion().getOffset();
		caret= offset + styledText.getCaretOffset();
	}

	IJavaElement element= getElementAt(caret, false);

	if ( !(element instanceof ISourceReference))
		return null;

	if (element.getElementType() == IJavaElement.IMPORT_DECLARATION) {

		IImportDeclaration declaration= (IImportDeclaration) element;
		IImportContainer container= (IImportContainer) declaration.getParent();
		ISourceRange srcRange= null;

		try {
			srcRange= container.getSourceRange();
		} catch (JavaModelException e) {
		}

		if (srcRange != null && srcRange.getOffset() == caret)
			return container;
	}

	return (ISourceReference) element;
}
 
Example 19
Source File: SelectionRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void acceptMethodTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector,int selectorStart, int selectorEnd, char[] typeParameterName, boolean isDeclaration, int start, int end) {
	IType type = resolveTypeByLocation(declaringTypePackageName, declaringTypeName,
			NameLookup.ACCEPT_ALL,
			selectorStart, selectorEnd);

	if(type != null) {
		IMethod method = null;

		String name = new String(selector);
		IMethod[] methods = null;

		try {
			methods = type.getMethods();
			done : for (int i = 0; i < methods.length; i++) {
				ISourceRange range = methods[i].getNameRange();
				if(range.getOffset() >= selectorStart
						&& range.getOffset() + range.getLength() <= selectorEnd
						&& methods[i].getElementName().equals(name)) {
					method = methods[i];
					break done;
				}
			}
		} catch (JavaModelException e) {
			//nothing to do
		}

		if(method == null) {
			addElement(type);
			if(SelectionEngine.DEBUG){
				System.out.print("SELECTION - accept type("); //$NON-NLS-1$
				System.out.print(type.toString());
				System.out.println(")"); //$NON-NLS-1$
			}
		} else {
			ITypeParameter typeParameter = method.getTypeParameter(new String(typeParameterName));
			if(typeParameter == null) {
				addElement(method);
				if(SelectionEngine.DEBUG){
					System.out.print("SELECTION - accept method("); //$NON-NLS-1$
					System.out.print(method.toString());
					System.out.println(")"); //$NON-NLS-1$
				}
			} else {
				addElement(typeParameter);
				if(SelectionEngine.DEBUG){
					System.out.print("SELECTION - accept method type parameter("); //$NON-NLS-1$
					System.out.print(typeParameter.toString());
					System.out.println(")"); //$NON-NLS-1$
				}
			}
		}
	}
}
 
Example 20
Source File: Util.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean covers(ISourceRange thisRange, ISourceRange otherRange) {
	return thisRange.getOffset() <= otherRange.getOffset()
			&& getEndInclusive(thisRange) >= getEndInclusive(otherRange);
}