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

The following examples show how to use org.eclipse.jdt.core.IMember#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: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static ISourceRange getNameRange(IJavaElement element) throws JavaModelException {
	ISourceRange nameRange = null;
	if (element instanceof IMember) {
		IMember member = (IMember) element;
		nameRange = member.getNameRange();
		if ((!SourceRange.isAvailable(nameRange))) {
			nameRange = member.getSourceRange();
		}
	} else if (element instanceof ITypeParameter || element instanceof ILocalVariable) {
		nameRange = ((ISourceReference) element).getNameRange();
	} else if (element instanceof ISourceReference) {
		nameRange = ((ISourceReference) element).getSourceRange();
	}
	if (!SourceRange.isAvailable(nameRange) && element.getParent() != null) {
		nameRange = getNameRange(element.getParent());
	}
	return nameRange;
}
 
Example 2
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean canEnable() throws JavaModelException {
	if (!super.canEnable() || fJavaElements.length == 0) {
		return false;
	}

	for (int i= 0; i < fJavaElements.length; i++) {
		if (fJavaElements[i] instanceof IMember) {
			IMember member= (IMember) fJavaElements[i];
			// we can copy some binary members, but not all
			if (member.isBinary() && member.getSourceRange() == null) {
				return false;
			}
		}
	}

	return true;
}
 
Example 3
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean canEnable() throws JavaModelException {
	if (!super.canEnable() || fJavaElements.length == 0)
		return false;

	for (int i= 0; i < fJavaElements.length; i++) {
		if (fJavaElements[i] instanceof IMember) {
			IMember member= (IMember) fJavaElements[i];
			// we can copy some binary members, but not all
			if (member.isBinary() && member.getSourceRange() == null)
				return false;
		}
	}

	return true;
}
 
Example 4
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ISourceRange getSourceRange(IJavaElement element) throws JavaModelException {
	ISourceRange sourceRange = null;
	if (element instanceof IMember) {
		IMember member = (IMember) element;
		sourceRange = member.getSourceRange();
	} else if (element instanceof ITypeParameter || element instanceof ILocalVariable) {
		sourceRange = ((ISourceReference) element).getSourceRange();
	} else if (element instanceof ISourceReference) {
		sourceRange = ((ISourceReference) element).getSourceRange();
	}
	if (!SourceRange.isAvailable(sourceRange) && element.getParent() != null) {
		sourceRange = getSourceRange(element.getParent());
	}
	return sourceRange;
}
 
Example 5
Source File: CalleeAnalyzerVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
CalleeAnalyzerVisitor(IMember member, CompilationUnit compilationUnit, IProgressMonitor progressMonitor) {
    fSearchResults = new CallSearchResultCollector();
    this.fMember = member;
    this.fCompilationUnit= compilationUnit;
    this.fProgressMonitor = progressMonitor;

    try {
        ISourceRange sourceRange = member.getSourceRange();
        this.fMethodStartPosition = sourceRange.getOffset();
        this.fMethodEndPosition = fMethodStartPosition + sourceRange.getLength();
    } catch (JavaModelException jme) {
        JavaPlugin.log(jme);
    }
}
 
Example 6
Source File: SourceActionDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int getElementAfterCursorPosition(CompilationUnitEditor editor, IJavaElement[] members) throws JavaModelException {
	if (editor == null) {
		return -1;
	}
	int offset= ((ITextSelection) editor.getSelectionProvider().getSelection()).getOffset();

	for (int i= 0; i < members.length; i++) {
		IMember curr= (IMember) members[i];
		ISourceRange range= curr.getSourceRange();
		if (offset < range.getOffset()) {
			return i;
		}
	}
	return members.length;
}
 
Example 7
Source File: MatchLocator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public MatchLocator(
	SearchPattern pattern,
	SearchRequestor requestor,
	IJavaSearchScope scope,
	IProgressMonitor progressMonitor) {

	this.pattern = pattern;
	this.patternLocator = PatternLocator.patternLocator(this.pattern);
	this.matchContainer = this.patternLocator == null ? 0 : this.patternLocator.matchContainer();
	this.requestor = requestor;
	this.scope = scope;
	this.progressMonitor = progressMonitor;
	if (pattern instanceof PackageDeclarationPattern) {
		this.searchPackageDeclaration = true;
	} else if (pattern instanceof OrPattern) {
		this.searchPackageDeclaration = ((OrPattern)pattern).hasPackageDeclaration();
	} else {
		this.searchPackageDeclaration = false;
	}
	if (pattern instanceof MethodPattern) {
	    IType type = ((MethodPattern) pattern).declaringType;
	    if (type != null && !type.isBinary()) {
	    	SourceType sourceType = (SourceType) type;
	    	IMember local = sourceType.getOuterMostLocalContext();
	    	if (local instanceof IMethod) { // remember this method's range so we don't purge its statements.
	    		try {
	    			ISourceRange range = local.getSourceRange();
	    			this.sourceStartOfMethodToRetain  = range.getOffset();
	    			this.sourceEndOfMethodToRetain = this.sourceStartOfMethodToRetain + range.getLength() - 1; // offset is 0 based.
	    		} catch (JavaModelException e) {
	    			// drop silently. 
	    		}
	    	}
	    }
	}
}
 
Example 8
Source File: ReorgUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static boolean hasSourceAvailable(IMember member) throws JavaModelException{
	return ! member.isBinary() ||
			(member.getSourceRange() != null && ! fgUnknownRange.equals(member.getSourceRange()));
}
 
Example 9
Source File: ReorgUtils.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean hasSourceAvailable(IMember member) throws JavaModelException{
	return ! member.isBinary() ||
			(member.getSourceRange() != null && ! fgUnknownRange.equals(member.getSourceRange()));
}
 
Example 10
Source File: CallHierarchyUI.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Opens the element in the editor or shows an error dialog if that fails.
 *
 * @param element the element to open
 * @param shell parent shell for error dialog
 * @param activateOnOpen <code>true</code> if the editor should be activated
 * @return <code>true</code> iff no error occurred while trying to open the editor,
 *         <code>false</code> iff an error dialog was raised.
 */
public static boolean openInEditor(Object element, Shell shell, boolean activateOnOpen) {
       CallLocation callLocation= CallHierarchy.getCallLocation(element);

       try {
        IMember enclosingMember;
        int selectionStart;
		int selectionLength;

        if (callLocation != null) {
			enclosingMember= callLocation.getMember();
			selectionStart= callLocation.getStart();
			selectionLength= callLocation.getEnd() - selectionStart;
        } else if (element instanceof MethodWrapper) {
        	enclosingMember= ((MethodWrapper) element).getMember();
        	ISourceRange selectionRange= enclosingMember.getNameRange();
        	if (selectionRange == null)
        		selectionRange= enclosingMember.getSourceRange();
        	if (selectionRange == null)
        		return true;
        	selectionStart= selectionRange.getOffset();
        	selectionLength= selectionRange.getLength();
        } else {
            return true;
        }

		IEditorPart methodEditor = JavaUI.openInEditor(enclosingMember, activateOnOpen, false);
           if (methodEditor instanceof ITextEditor) {
               ITextEditor editor = (ITextEditor) methodEditor;
			editor.selectAndReveal(selectionStart, selectionLength);
           }
           return true;
       } catch (JavaModelException e) {
           JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(),
                   IJavaStatusConstants.INTERNAL_ERROR,
                   CallHierarchyMessages.CallHierarchyUI_open_in_editor_error_message, e));

           ErrorDialog.openError(shell, CallHierarchyMessages.OpenLocationAction_error_title,
               CallHierarchyMessages.CallHierarchyUI_open_in_editor_error_message,
               e.getStatus());
           return false;
       } catch (PartInitException x) {
           String name;
       	if (callLocation != null)
       		name= callLocation.getCalledMember().getElementName();
       	else if (element instanceof MethodWrapper)
       		name= ((MethodWrapper) element).getName();
       	else
       		name= "";  //$NON-NLS-1$
           MessageDialog.openError(shell, CallHierarchyMessages.OpenLocationAction_error_title,
               Messages.format(
                   CallHierarchyMessages.CallHierarchyUI_open_in_editor_error_messageArgs,
                   new String[] { name, x.getMessage() }));
           return false;
       }
   }
 
Example 11
Source File: JavaReplaceWithEditionActionImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void performReplace(IMember input, IFile file,
		ITextFileBuffer textFileBuffer, IDocument document, ITypedElement ti)
		throws CoreException, JavaModelException,
		InvocationTargetException, InterruptedException {

	if (ti instanceof IStreamContentAccessor) {

		boolean inEditor= beingEdited(file);

		String content= JavaCompareUtilities.readString((IStreamContentAccessor)ti);
		String newContent= trimTextBlock(content, TextUtilities.getDefaultLineDelimiter(document), input.getJavaProject());
		if (newContent == null) {
			showError();
			return;
		}

		ICompilationUnit compilationUnit= input.getCompilationUnit();
		CompilationUnit root= parsePartialCompilationUnit(compilationUnit);


		ISourceRange nameRange= input.getNameRange();
		if (nameRange == null)
			nameRange= input.getSourceRange();
		// workaround for bug in getNameRange(): for AnnotationMembers length is negative
		int length= nameRange.getLength();
		if (length < 0)
			length= 1;
		ASTNode node2= NodeFinder.perform(root, new SourceRange(nameRange.getOffset(), length));
		ASTNode node;
		if (node2.getNodeType() == ASTNode.INITIALIZER)
			node= node2;
		else
			node= ASTNodes.getParent(node2, BodyDeclaration.class);
		if (node == null)
			node= ASTNodes.getParent(node2, AnnotationTypeDeclaration.class);
		if (node == null)
			node= ASTNodes.getParent(node2, EnumDeclaration.class);

		//ASTNode node= getBodyContainer(root, input);
		if (node == null) {
			showError();
			return;
		}

		ASTRewrite rewriter= ASTRewrite.create(root.getAST());
		rewriter.replace(node, rewriter.createStringPlaceholder(newContent, node.getNodeType()), null);

		if (inEditor) {
			JavaEditor je= getEditor(file);
			if (je != null)
				je.setFocus();
		}

		Map<String, String> options= null;
		IJavaProject javaProject= compilationUnit.getJavaProject();
		if (javaProject != null)
			options= javaProject.getOptions(true);
		applyChanges(rewriter, document, textFileBuffer, getShell(), inEditor, options);

	}
}
 
Example 12
Source File: JavaDocAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Returns <code>true</code> if the comment being inserted at
 * <code>command.offset</code> is the first comment (the first
 * Javadoc comment if <code>ignoreJavadoc</code> is
 * <code>true</code>) of the given member.
 * <p>
 * see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=55325 (don't add parameters if the member already has a comment)
 * </p>
 * @param document the document
 * @param command the document command
 * @param member the Java member
 * @param ignoreNonJavadoc <code>true</code> if non Javadoc should be ignored
 * @return <code>true</code> if it is the first comment
 * @throws JavaModelException if accessing the Java model fails
 * @throws BadLocationException if accessing the document fails
 */
private boolean isFirstComment(IDocument document, DocumentCommand command, IMember member, boolean ignoreNonJavadoc) throws BadLocationException, JavaModelException {
	IRegion partition= TextUtilities.getPartition(document, fPartitioning, command.offset, false);
	ISourceRange sourceRange= member.getSourceRange();
	if (sourceRange == null)
		return false;
	int srcOffset= sourceRange.getOffset();
	int srcLength= sourceRange.getLength();
	int nameRelativeOffset= member.getNameRange().getOffset() - srcOffset;
	int partitionRelativeOffset= partition.getOffset() - srcOffset;
	String token= ignoreNonJavadoc ? "/**" :  "/*"; //$NON-NLS-1$ //$NON-NLS-2$
	return document.get(srcOffset, srcLength).lastIndexOf(token, nameRelativeOffset) == partitionRelativeOffset;
}