use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class CompletionProposalCollector method acceptPotentialMethodDeclaration.
private void acceptPotentialMethodDeclaration(CompletionProposal proposal) {
try {
IJavaElement enclosingElement = null;
// if (getContext().isExtended()) {
// enclosingElement= getContext().getEnclosingElement();
// } else if (fCompilationUnit != null) {
// kept for backward compatibility: CU is not reconciled at this moment, information is missing (bug 70005)
enclosingElement = fCompilationUnit.getElementAt(proposal.getCompletionLocation() + 1);
// }
if (enclosingElement == null)
return;
IType type = (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
if (type != null) {
String prefix = String.valueOf(proposal.getName());
int completionStart = proposal.getReplaceStart();
int completionEnd = proposal.getReplaceEnd();
int relevance = computeRelevance(proposal);
GetterSetterCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance + 2, fSuggestedMethodNames, fJavaProposals);
MethodDeclarationCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance, fSuggestedMethodNames, fJavaProposals);
}
} catch (CoreException e) {
JavaPlugin.log(e);
}
}
use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class JavaElementLabelComposer method appendDeclarationLabel.
/**
* Appends the label for a import container, import or package declaration. Considers the D_* flags.
*
* @param declaration the element to render
* @param flags the rendering flags. Flags with names starting with 'D_' are considered.
*/
public void appendDeclarationLabel(IJavaElement declaration, long flags) {
if (getFlag(flags, JavaElementLabels.D_QUALIFIED)) {
IJavaElement openable = (IJavaElement) declaration.getOpenable();
if (openable != null) {
appendElementLabel(openable, JavaElementLabels.CF_QUALIFIED | JavaElementLabels.CU_QUALIFIED | (flags & QUALIFIER_FLAGS));
fBuffer.append('/');
}
}
if (declaration.getElementType() == IJavaElement.IMPORT_CONTAINER) {
fBuffer.append(JavaUIMessages.JavaElementLabels_import_container);
} else {
fBuffer.append(getElementName(declaration));
}
// post qualification
if (getFlag(flags, JavaElementLabels.D_POST_QUALIFIED)) {
int offset = fBuffer.length();
IJavaElement openable = (IJavaElement) declaration.getOpenable();
if (openable != null) {
fBuffer.append(JavaElementLabels.CONCAT_STRING);
appendElementLabel(openable, JavaElementLabels.CF_QUALIFIED | JavaElementLabels.CU_QUALIFIED | (flags & QUALIFIER_FLAGS));
}
if (getFlag(flags, JavaElementLabels.COLORIZE)) {
fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
}
}
}
use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class RefactoringService method createRenameRefactoring.
/**
* Create rename refactoring session.
*
* @param settings
* rename settings
* @return the rename refactoring session
* @throws CoreException
* when RenameSupport can't be created
* @throws RefactoringException
* when Java element was not found
*/
@POST
@Path("rename/create")
@Produces("application/json")
@Consumes("application/json")
public RenameRefactoringSession createRenameRefactoring(CreateRenameRefactoring settings) throws CoreException, RefactoringException {
IJavaProject javaProject = model.getJavaProject(settings.getProjectPath());
IJavaElement elementToRename;
ICompilationUnit cu = null;
switch(settings.getType()) {
case COMPILATION_UNIT:
elementToRename = javaProject.findType(settings.getPath()).getCompilationUnit();
break;
case PACKAGE:
elementToRename = javaProject.findPackageFragment(new org.eclipse.core.runtime.Path(settings.getPath()));
break;
case JAVA_ELEMENT:
cu = javaProject.findType(settings.getPath()).getCompilationUnit();
elementToRename = getSelectionElement(cu, settings.getOffset());
break;
default:
elementToRename = null;
}
if (elementToRename == null) {
throw new RefactoringException("Can't find java element to rename.");
}
return manager.createRenameRefactoring(elementToRename, cu, settings.getOffset(), settings.isRefactorLightweight());
}
use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class RefactoringService method createMoveRefactoring.
/**
* Create move refactoring session.
*
* @param cmr
* move settings, contains resource paths to move.
* @return refactoring session id.
* @throws JavaModelException
* when JavaModel has a failure
* @throws RefactoringException
* when impossible to create move refactoring session
*/
@POST
@Path("move/create")
@Consumes("application/json")
@Produces("text/plain")
public String createMoveRefactoring(CreateMoveRefactoring cmr) throws JavaModelException, RefactoringException {
IJavaProject javaProject = model.getJavaProject(cmr.getProjectPath());
IJavaElement[] javaElements;
try {
Function<ElementToMove, IJavaElement> map = javaElement -> {
try {
if (javaElement.isPack()) {
return javaProject.findPackageFragment(new org.eclipse.core.runtime.Path(javaElement.getPath()));
} else {
return javaProject.findType(javaElement.getPath()).getCompilationUnit();
}
} catch (JavaModelException e) {
throw new IllegalArgumentException(e);
}
};
javaElements = cmr.getElements().stream().map(map).toArray(IJavaElement[]::new);
} catch (IllegalArgumentException e) {
if (e.getCause() instanceof JavaModelException) {
throw (JavaModelException) e.getCause();
} else {
throw e;
}
}
if (RefactoringAvailabilityTester.isMoveAvailable(new IResource[0], javaElements)) {
return manager.createMoveRefactoringSession(javaElements);
}
throw new RefactoringException("Can't create move refactoring.");
}
use of org.eclipse.jdt.core.IJavaElement in project che by eclipse.
the class SearchManager method performFindUsageSearch.
private FindUsagesResponse performFindUsageSearch(IJavaElement element) throws JavaModelException, BadLocationException {
JavaSearchScopeFactory factory = JavaSearchScopeFactory.getInstance();
boolean isInsideJRE = factory.isInsideJRE(element);
JavaSearchQuery query = new JavaSearchQuery(new ElementQuerySpecification(element, IJavaSearchConstants.REFERENCES, factory.createWorkspaceScope(isInsideJRE), "workspace scope"));
NewSearchUI.runQueryInForeground(null, query);
ISearchResult result = query.getSearchResult();
JavaSearchResult javaResult = ((JavaSearchResult) result);
FindUsagesResponse response = DtoFactory.newDto(FindUsagesResponse.class);
Map<String, List<org.eclipse.che.ide.ext.java.shared.dto.search.Match>> mapMaches = new HashMap<>();
JavaElementToDtoConverter converter = new JavaElementToDtoConverter(javaResult);
for (Object o : javaResult.getElements()) {
IJavaElement javaElement = (IJavaElement) o;
IDocument document = null;
if (javaElement instanceof IMember) {
IMember member = ((IMember) javaElement);
if (member.isBinary()) {
if (member.getClassFile().getSource() != null) {
document = new Document(member.getClassFile().getSource());
}
} else {
document = getDocument(member.getCompilationUnit());
}
} else if (javaElement instanceof IPackageDeclaration) {
ICompilationUnit ancestor = (ICompilationUnit) (javaElement).getAncestor(IJavaElement.COMPILATION_UNIT);
document = getDocument(ancestor);
}
converter.addElementToProjectHierarchy(javaElement);
Match[] matches = javaResult.getMatches(o);
List<org.eclipse.che.ide.ext.java.shared.dto.search.Match> matchList = new ArrayList<>();
for (Match match : matches) {
org.eclipse.che.ide.ext.java.shared.dto.search.Match dtoMatch = DtoFactory.newDto(org.eclipse.che.ide.ext.java.shared.dto.search.Match.class);
if (document != null) {
IRegion lineInformation = document.getLineInformationOfOffset(match.getOffset());
int offsetInLine = match.getOffset() - lineInformation.getOffset();
Region matchInLine = DtoFactory.newDto(Region.class).withOffset(offsetInLine).withLength(match.getLength());
dtoMatch.setMatchInLine(matchInLine);
dtoMatch.setMatchLineNumber(document.getLineOfOffset(match.getOffset()));
dtoMatch.setMatchedLine(document.get(lineInformation.getOffset(), lineInformation.getLength()));
}
dtoMatch.setFileMatchRegion(DtoFactory.newDto(Region.class).withOffset(match.getOffset()).withLength(match.getLength()));
matchList.add(dtoMatch);
}
mapMaches.put(javaElement.getHandleIdentifier(), matchList);
}
List<JavaProject> projects = converter.getProjects();
response.setProjects(projects);
response.setMatches(mapMaches);
response.setSearchElementLabel(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT));
return response;
}
Aggregations