org.eclipse.jdt.core.ISourceRange Java Examples

The following examples show how to use org.eclipse.jdt.core.ISourceRange. 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: Annotation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public ISourceRange getNameRange() throws JavaModelException {
	SourceMapper mapper= getSourceMapper();
	if (mapper != null) {
		ClassFile classFile = (ClassFile)getClassFile();
		if (classFile != null) {
			// ensure the class file's buffer is open so that source ranges are computed
			classFile.getBuffer();
			return mapper.getNameRange(this);
		}
	}
	Object info = getElementInfo();
	if (info instanceof AnnotationInfo) {
		AnnotationInfo annotationInfo = (AnnotationInfo) info;
		return new SourceRange(annotationInfo.nameStart, annotationInfo.nameEnd - annotationInfo.nameStart + 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
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 #3
Source File: SelfEncapsulateFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
	if (fVisibility < 0)
		fVisibility= (fField.getFlags() & (Flags.AccPublic | Flags.AccProtected | Flags.AccPrivate));
	RefactoringStatus result=  new RefactoringStatus();
	result.merge(Checks.checkAvailability(fField));
	if (result.hasFatalError())
		return result;
	fRoot= new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(fField.getCompilationUnit(), true, pm);
	ISourceRange sourceRange= fField.getNameRange();
	ASTNode node= NodeFinder.perform(fRoot, sourceRange.getOffset(), sourceRange.getLength());
	if (node == null) {
		return mappingErrorFound(result, node);
	}
	fFieldDeclaration= (VariableDeclarationFragment)ASTNodes.getParent(node, VariableDeclarationFragment.class);
	if (fFieldDeclaration == null) {
		return mappingErrorFound(result, node);
	}
	if (fFieldDeclaration.resolveBinding() == null) {
		if (!processCompilerError(result, node))
			result.addFatalError(RefactoringCoreMessages.SelfEncapsulateField_type_not_resolveable);
		return result;
	}
	computeUsedNames();
	return result;
}
 
Example #4
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Evaluates the insertion position of a new node.
 *
 * @param listRewrite The list rewriter to which the new node will be added
 * @param sibling The Java element before which the new element should be added.
 * @return the AST node of the list to insert before or null to insert as last.
 * @throws JavaModelException thrown if accessing the Java element failed
 */

public static ASTNode getNodeToInsertBefore(ListRewrite listRewrite, IJavaElement sibling) throws JavaModelException {
	if (sibling instanceof IMember) {
		ISourceRange sourceRange= ((IMember) sibling).getSourceRange();
		if (sourceRange == null) {
			return null;
		}
		int insertPos= sourceRange.getOffset();

		List<? extends ASTNode> members= listRewrite.getOriginalList();
		for (int i= 0; i < members.size(); i++) {
			ASTNode curr= members.get(i);
			if (curr.getStartPosition() >= insertPos) {
				return curr;
			}
		}
	}
	return null;
}
 
Example #5
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 #6
Source File: CorrectPackageDeclarationProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
	super.addEdits(doc, root);

	ICompilationUnit cu= getCompilationUnit();

	IPackageFragment parentPack= (IPackageFragment) cu.getParent();
	IPackageDeclaration[] decls= cu.getPackageDeclarations();

	if (parentPack.isDefaultPackage() && decls.length > 0) {
		for (int i= 0; i < decls.length; i++) {
			ISourceRange range= decls[i].getSourceRange();
			root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
		}
		return;
	}
	if (!parentPack.isDefaultPackage() && decls.length == 0) {
		String lineDelim = "\n";
		String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$
		root.addChild(new InsertEdit(0, str));
		return;
	}

	root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
 
Example #7
Source File: RenameLocalVariableProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void initAST() {
	if (!fIsComposite)
		fCompilationUnitNode= RefactoringASTParser.parseWithASTProvider(fCu, true, null);
	ISourceRange sourceRange= fLocalVariable.getNameRange();
	ASTNode name= NodeFinder.perform(fCompilationUnitNode, sourceRange);
	if (name == null)
		return;
	if (name.getParent() instanceof VariableDeclaration)
		fTempDeclarationNode= (VariableDeclaration) name.getParent();
}
 
Example #8
Source File: ImportOrganizeTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected IChooseImportQuery createQuery(final String name, final String[] choices, final int[] nEntries) {
	return new IChooseImportQuery() {
		@Override
		public TypeNameMatch[] chooseImports(TypeNameMatch[][] openChoices, ISourceRange[] ranges) {
			assertTrue(name + "-query-nchoices1", choices.length == openChoices.length);
			assertTrue(name + "-query-nchoices2", nEntries.length == openChoices.length);
			for (int i= 0; i < nEntries.length; i++) {
				assertTrue(name + "-query-cnt" + i, openChoices[i].length == nEntries[i]);
			}
			TypeNameMatch[] res= new TypeNameMatch[openChoices.length];
			for (int i= 0; i < openChoices.length; i++) {
				TypeNameMatch[] selection= openChoices[i];
				assertNotNull(name + "-query-setset" + i, selection);
				assertTrue(name + "-query-setlen" + i, selection.length > 0);
				TypeNameMatch found= null;
				for (int k= 0; k < selection.length; k++) {
					if (selection[k].getFullyQualifiedName().equals(choices[i])) {
						found= selection[k];
					}
				}
				assertNotNull(name + "-query-notfound" + i, found);
				res[i]= found;
			}
			return res;
		}
	};
}
 
Example #9
Source File: StructureSelectionAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final  void run() {
	IJavaElement inputElement= EditorUtility.getEditorInputJavaElement(fEditor, false);
	if (!(inputElement instanceof ITypeRoot && inputElement.exists()))
		return;

	ITypeRoot typeRoot= (ITypeRoot) inputElement;
	ISourceRange sourceRange;
	try {
		sourceRange= typeRoot.getSourceRange();
		if (sourceRange == null || sourceRange.getLength() == 0) {
			MessageDialog.openInformation(fEditor.getEditorSite().getShell(),
				SelectionActionMessages.StructureSelect_error_title,
				SelectionActionMessages.StructureSelect_error_message);
			return;
		}
	} catch (JavaModelException e) {
	}
	ITextSelection selection= getTextSelection();
	ISourceRange newRange= getNewSelectionRange(createSourceRange(selection), typeRoot);
	// Check if new selection differs from current selection
	if (selection.getOffset() == newRange.getOffset() && selection.getLength() == newRange.getLength())
		return;
	fSelectionHistory.remember(new SourceRange(selection.getOffset(), selection.getLength()));
	try {
		fSelectionHistory.ignoreSelectionChanges();
		fEditor.selectAndReveal(newRange.getOffset(), newRange.getLength());
	} finally {
		fSelectionHistory.listenToSelectionChanges();
	}
}
 
Example #10
Source File: InlineTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus checkAssignments(VariableDeclaration decl) {
	TempAssignmentFinder assignmentFinder= new TempAssignmentFinder(decl);
	getASTRoot().accept(assignmentFinder);
	if (!assignmentFinder.hasAssignments())
		return new RefactoringStatus();
	ASTNode firstAssignment= assignmentFinder.getFirstAssignment();
	int start= firstAssignment.getStartPosition();
	int length= firstAssignment.getLength();
	ISourceRange range= new SourceRange(start, length);
	RefactoringStatusContext context= JavaStatusContext.create(fCu, range);
	String message= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_assigned_more_once, BasicElementLabels.getJavaElementName(decl.getName().getIdentifier()));
	return RefactoringStatus.createFatalErrorStatus(message, context);
}
 
Example #11
Source File: JavaStatusContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public ISourceRange getSourceRange() {
	try {
		return fMember.getSourceRange();
	} catch (JavaModelException e) {
		return new SourceRange(0,0);
	}
}
 
Example #12
Source File: ASTFragmentFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected IASTFragment createFragment(ASTNode node, ISourceRange range, ICompilationUnit cu) throws JavaModelException {
	fRange= range;
	fCu= cu;
	IASTFragment result= createFragment(node);
	if(javaModelException != null)
		throw javaModelException;
	return result;
}
 
Example #13
Source File: JvmOutlineNodeElementOpener.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void openEObject(EObject state) {
	try {
		if (state instanceof JvmIdentifiableElement && state.eResource() instanceof TypeResource) {
			IJavaElement javaElement = javaElementFinder.findElementFor((JvmIdentifiableElement) state);
			if (javaElement instanceof IMember) {
				IResource resource = javaElement.getResource();
				if (resource instanceof IStorage) {
					ITrace traceToSource = traceInformation.getTraceToSource((IStorage) resource);
					if (traceToSource != null) {
						ISourceRange sourceRange = ((IMember) javaElement).getSourceRange();
						ILocationInResource sourceInformation = traceToSource.getBestAssociatedLocation(new TextRegion(sourceRange.getOffset(), sourceRange.getLength()));
						if (sourceInformation != null) {
							globalURIEditorOpener.open(sourceInformation.getAbsoluteResourceURI().getURI(), javaElement, true);
							return;
						}
					}
				}
				globalURIEditorOpener.open(null, javaElement, true);
				return;
			}
		}
	} catch (Exception exc) {
		LOG.error("Error opening java editor", exc);
	}
	super.openEObject(state);
}
 
Example #14
Source File: SelectionEngine.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Object findMethodWithAttachedDocInHierarchy(final MethodBinding method) throws JavaModelException {
	ReferenceBinding type= method.declaringClass;
	final SelectionRequestor requestor1 = (SelectionRequestor) this.requestor;
	return new InheritDocVisitor() {
		public Object visit(ReferenceBinding currType) throws JavaModelException {
			MethodBinding overridden =  findOverriddenMethodInType(currType, method);
			if (overridden == null)
				return InheritDocVisitor.CONTINUE;
			TypeBinding args[] = overridden.parameters;
			String names[] = new String[args.length];
			for (int i = 0; i < args.length; i++) {
				names[i] = Signature.createTypeSignature(args[i].sourceName(), false);
			}
			IMember member = (IMember) requestor1.findMethodFromBinding(overridden, names, overridden.declaringClass);
			if (member == null)
				return InheritDocVisitor.CONTINUE;
			if (member.getAttachedJavadoc(null) != null ) {  
				// for binary methods with attached javadoc and no source attached
				return overridden;
			}
			IOpenable openable = member.getOpenable();
			if (openable == null)
				return InheritDocVisitor.CONTINUE;
			IBuffer buf= openable.getBuffer();
			if (buf == null) {
				// no source attachment found. This method maybe the one. Stop.
				return InheritDocVisitor.STOP_BRANCH;
			}

			ISourceRange javadocRange= member.getJavadocRange();
			if (javadocRange == null)
				return InheritDocVisitor.CONTINUE;	// this method doesn't have javadoc, continue to look.
			String rawJavadoc= buf.getText(javadocRange.getOffset(), javadocRange.getLength());
			if (rawJavadoc != null) {
				return overridden;
			}
			return InheritDocVisitor.CONTINUE;
		}
	}.visitInheritDoc(type);
}
 
Example #15
Source File: RenameFieldProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.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 #16
Source File: StructureSelectEnclosingAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
ISourceRange internalGetNewSelectionRange(ISourceRange oldSourceRange, ISourceReference sr, SelectionAnalyzer selAnalyzer) throws JavaModelException{
	ASTNode first= selAnalyzer.getFirstSelectedNode();
	if (first == null || first.getParent() == null)
		return getLastCoveringNodeRange(oldSourceRange, sr, selAnalyzer);

	return getSelectedNodeSourceRange(sr, first.getParent());
}
 
Example #17
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 #18
Source File: BinaryMember.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public ISourceRange getNameRange() throws JavaModelException {
	SourceMapper mapper= getSourceMapper();
	if (mapper != null) {
		// ensure the class file's buffer is open so that source ranges are computed
		((ClassFile)getClassFile()).getBuffer();

		return mapper.getNameRange(this);
	} else {
		return SourceMapper.UNKNOWN_RANGE;
	}
}
 
Example #19
Source File: ImportsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static ICleanUpFix createCleanUp(final CompilationUnit cu, CodeGenerationSettings settings, boolean organizeImports, RefactoringStatus status) throws CoreException {
if (!organizeImports)
	return null;

final boolean hasAmbiguity[]= new boolean[] { false };
IChooseImportQuery query= new IChooseImportQuery() {
	public TypeNameMatch[] chooseImports(TypeNameMatch[][] openChoices, ISourceRange[] ranges) {
		hasAmbiguity[0]= true;
		return new TypeNameMatch[0];
	}
};

final ICompilationUnit unit= (ICompilationUnit)cu.getJavaElement();
OrganizeImportsOperation op= new OrganizeImportsOperation(unit, cu, settings.importIgnoreLowercase, false, false, query);
final TextEdit edit= op.createTextEdit(null);
if (hasAmbiguity[0]) {
	status.addInfo(Messages.format(ActionMessages.OrganizeImportsAction_multi_error_unresolvable, getLocationString(cu)));
}

if (op.getParseError() != null) {
	status.addInfo(Messages.format(ActionMessages.OrganizeImportsAction_multi_error_parse, getLocationString(cu)));
	return null;
}

if (edit == null || (edit instanceof MultiTextEdit && edit.getChildrenSize() == 0))
	return null;

return new ImportsFix(edit, unit, FixMessages.ImportsFix_OrganizeImports_Description);
  }
 
Example #20
Source File: Util.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
static boolean rangeIncludesNonWhitespaceOutsideRange(ISourceRange selection, ISourceRange nodes, IBuffer buffer) {
	if (!covers(selection, nodes)) {
		return false;
	}

	//TODO: skip leading comments. Consider that leading line comment must be followed by newline!
	if (!isJustWhitespace(selection.getOffset(), nodes.getOffset(), buffer)) {
		return true;
	}
	if (!isJustWhitespaceOrComment(nodes.getOffset() + nodes.getLength(), selection.getOffset() + selection.getLength(), buffer)) {
		return true;
	}
	return false;
}
 
Example #21
Source File: SourceRange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Sorts the given ranges by offset (backwards).
 * Note: modifies the parameter.
 * @param ranges the ranges to sort
 * @return the sorted ranges, which are identical to the parameter ranges
 */
public static ISourceRange[] reverseSortByOffset(ISourceRange[] ranges){
	Comparator<ISourceRange> comparator= new Comparator<ISourceRange>(){
		public int compare(ISourceRange o1, ISourceRange o2){
			return o2.getOffset() - o1.getOffset();
		}
	};
	Arrays.sort(ranges, comparator);
	return ranges;
}
 
Example #22
Source File: CompilationUnitEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes the element line of a java element (the start of the element, or the line with
 * the element's name range).
 *
 * @param document the displayed document for line information
 * @param element the java element, may be <code>null</code>
 * @return the element's start line, or -1
 * @exception BadLocationException if the offset is invalid in this document
 * @throws JavaModelException if getting the name range of the element fails
 * @since 3.2
 */
private int getElementLine(IDocument document, IJavaElement element) throws BadLocationException, JavaModelException {
	if (element instanceof IMember) {
		ISourceRange range= ((IMember) element).getNameRange();
		if (range != null)
			return document.getLineOfOffset(range.getOffset());
	}
	int elementOffset= getOffset(element);
	if (elementOffset != -1)
		return document.getLineOfOffset(elementOffset);
	return -1;
}
 
Example #23
Source File: JavadocContentAccess2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param method
 *            the method
 * @return the Javadoc content access for the given method, or <code>null</code>
 *         if no Javadoc could be found in source
 * @throws JavaModelException
 *             unexpected problem
 */
private JavadocContentAccess2 getJavadocContentAccess(IMethod method) throws JavaModelException {
	Object cached = fContentAccesses.get(method);
	if (cached != null) {
		return (JavadocContentAccess2) cached;
	}
	if (fContentAccesses.containsKey(method)) {
		return null;
	}

	IBuffer buf = method.getOpenable().getBuffer();
	if (buf == null) { // no source attachment found
		fContentAccesses.put(method, null);
		return null;
	}

	ISourceRange javadocRange = method.getJavadocRange();
	if (javadocRange == null) {
		fContentAccesses.put(method, null);
		return null;
	}

	String rawJavadoc = buf.getText(javadocRange.getOffset(), javadocRange.getLength());
	Javadoc javadoc = getJavadocNode(method, rawJavadoc);
	if (javadoc == null) {
		fContentAccesses.put(method, null);
		return null;
	}

	JavadocContentAccess2 contentAccess = new JavadocContentAccess2(method, javadoc, rawJavadoc, this);
	fContentAccesses.put(method, contentAccess);
	return contentAccess;
}
 
Example #24
Source File: FindOccurrencesInFileAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void run(IStructuredSelection selection) {
	IMember member= getMember(selection);
	if (!ActionUtil.isProcessable(getShell(), member))
		return;
	FindOccurrencesEngine engine= FindOccurrencesEngine.create(new OccurrencesFinder());
	try {
		ISourceRange range= member.getNameRange();
		String result= engine.run(member.getTypeRoot(), range.getOffset(), range.getLength());
		if (result != null)
			showMessage(getShell(), fActionBars, result);
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}
}
 
Example #25
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param method the method
 * @return the Javadoc content access for the given method, or
 * 		<code>null</code> if no Javadoc could be found in source
 * @throws JavaModelException unexpected problem
 */
private JavadocContentAccess2 getJavadocContentAccess(IMethod method) throws JavaModelException {
	Object cached= fContentAccesses.get(method);
	if (cached != null)
		return (JavadocContentAccess2) cached;
	if (fContentAccesses.containsKey(method))
		return null;

	IBuffer buf= method.getOpenable().getBuffer();
	if (buf == null) { // no source attachment found
		fContentAccesses.put(method, null);
		return null;
	}

	ISourceRange javadocRange= method.getJavadocRange();
	if (javadocRange == null) {
		fContentAccesses.put(method, null);
		return null;
	}

	String rawJavadoc= buf.getText(javadocRange.getOffset(), javadocRange.getLength());
	Javadoc javadoc= getJavadocNode(method, rawJavadoc);
	if (javadoc == null) {
		fContentAccesses.put(method, null);
		return null;
	}

	JavadocContentAccess2 contentAccess= new JavadocContentAccess2(method, javadoc, rawJavadoc, this);
	fContentAccesses.put(method, contentAccess);
	return contentAccess;
}
 
Example #26
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 #27
Source File: RenameAnalyzeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ISourceRange getOldSourceRange(SearchMatch newMatch) {
	// cannot transfom offset in preview to offset in original -> just show enclosing method
	IJavaElement newMatchElement= (IJavaElement) newMatch.getElement();
	IJavaElement primaryElement= newMatchElement.getPrimaryElement();
	ISourceRange range= null;
	if (primaryElement.exists() && primaryElement instanceof ISourceReference) {
		try {
			range= ((ISourceReference) primaryElement).getSourceRange();
		} catch (JavaModelException e) {
			// can live without source range
		}
	}
	return range;
}
 
Example #28
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 #29
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 #30
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);
}