use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.
the class RenameAnalyzeUtil method existsInNewOccurrences.
private static boolean existsInNewOccurrences(SearchMatch searchResult, SearchResultGroup[] newOccurrences, TextChangeManager manager) {
SearchResultGroup newGroup = findOccurrenceGroup(searchResult.getResource(), newOccurrences);
if (newGroup == null)
return false;
IRegion oldEditRange = getCorrespondingEditChangeRange(searchResult, manager);
if (oldEditRange == null)
return false;
SearchMatch[] newSearchResults = newGroup.getSearchResults();
int oldRangeOffset = oldEditRange.getOffset();
for (int i = 0; i < newSearchResults.length; i++) {
if (newSearchResults[i].getOffset() == oldRangeOffset)
return true;
}
return false;
}
use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.
the class RenameAnalyzeUtil method analyzeChanges.
private static void analyzeChanges(ICompilationUnit cu, TextChange change, SearchMatch[] oldMatches, SearchMatch[] newMatches, String newElementName, RefactoringStatus result) {
Map<Integer, SearchMatch> updatedOldOffsets = getUpdatedChangeOffsets(change, oldMatches);
for (int i = 0; i < newMatches.length; i++) {
SearchMatch newMatch = newMatches[i];
Integer offsetInNew = new Integer(newMatch.getOffset());
SearchMatch oldMatch = updatedOldOffsets.remove(offsetInNew);
if (oldMatch == null) {
addReferenceShadowedError(cu, newMatch, newElementName, result);
}
}
for (Iterator<SearchMatch> iter = updatedOldOffsets.values().iterator(); iter.hasNext(); ) {
// remaining old matches are not found any more -> they have been shadowed
SearchMatch oldMatch = iter.next();
addShadowsError(cu, oldMatch, result);
}
}
use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.
the class CreateCopyOfCompilationUnitChange method createChangeManager.
private static TextChangeManager createChangeManager(IProgressMonitor monitor, ICompilationUnit copy, String newName) throws CoreException {
TextChangeManager manager = new TextChangeManager();
SearchResultGroup refs = getReferences(copy, monitor);
if (refs == null)
return manager;
if (refs.getCompilationUnit() == null)
return manager;
String name = RefactoringCoreMessages.CopyRefactoring_update_ref;
SearchMatch[] results = refs.getSearchResults();
for (int j = 0; j < results.length; j++) {
SearchMatch searchResult = results[j];
if (searchResult.getAccuracy() == SearchMatch.A_INACCURATE)
continue;
int offset = searchResult.getOffset();
int length = searchResult.getLength();
TextChangeCompatibility.addTextEdit(manager.get(copy), name, new ReplaceEdit(offset, length, newName));
}
return manager;
}
use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.
the class CreateCopyOfCompilationUnitChange method getReferences.
private static SearchResultGroup getReferences(final ICompilationUnit copy, IProgressMonitor monitor) throws JavaModelException {
final ICompilationUnit[] copies = new ICompilationUnit[] { copy };
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(copies);
final IType type = copy.findPrimaryType();
if (type == null)
return null;
SearchPattern pattern = createSearchPattern(type);
final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(pattern);
engine.setScope(scope);
engine.setWorkingCopies(copies);
engine.setRequestor(new IRefactoringSearchRequestor() {
TypeOccurrenceCollector fTypeOccurrenceCollector = new TypeOccurrenceCollector(type);
public SearchMatch acceptSearchMatch(SearchMatch match) {
try {
return fTypeOccurrenceCollector.acceptSearchMatch2(copy, match);
} catch (CoreException e) {
JavaPlugin.log(e);
return null;
}
}
});
engine.searchPattern(monitor);
final Object[] results = engine.getResults();
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=106127)
for (int index = 0; index < results.length; index++) {
SearchResultGroup group = (SearchResultGroup) results[index];
if (group.getCompilationUnit().equals(copy))
return group;
}
return null;
}
use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.
the class MoveCuUpdateCreator method addReferenceUpdates.
private void addReferenceUpdates(TextChangeManager changeManager, ICompilationUnit movedUnit, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException, CoreException {
List<ICompilationUnit> cuList = Arrays.asList(fCus);
SearchResultGroup[] references = getReferences(movedUnit, pm, status);
for (int i = 0; i < references.length; i++) {
SearchResultGroup searchResultGroup = references[i];
ICompilationUnit referencingCu = searchResultGroup.getCompilationUnit();
if (referencingCu == null)
continue;
boolean simpleReferencesNeedNewImport = simpleReferencesNeedNewImport(movedUnit, referencingCu, cuList);
SearchMatch[] results = searchResultGroup.getSearchResults();
for (int j = 0; j < results.length; j++) {
// TODO: should update type references with results from addImport
TypeReference reference = (TypeReference) results[j];
if (reference.isImportDeclaration()) {
ImportRewrite rewrite = getImportRewrite(referencingCu);
IImportDeclaration importDecl = (IImportDeclaration) SearchUtils.getEnclosingJavaElement(results[j]);
if (Flags.isStatic(importDecl.getFlags())) {
rewrite.removeStaticImport(importDecl.getElementName());
addStaticImport(movedUnit, importDecl, rewrite);
} else {
rewrite.removeImport(importDecl.getElementName());
rewrite.addImport(createStringForNewImport(movedUnit, importDecl));
}
} else if (reference.isQualified()) {
TextChange textChange = changeManager.get(referencingCu);
String changeName = RefactoringCoreMessages.MoveCuUpdateCreator_update_references;
TextEdit replaceEdit = new ReplaceEdit(reference.getOffset(), reference.getSimpleNameStart() - reference.getOffset(), fNewPackage);
TextChangeCompatibility.addTextEdit(textChange, changeName, replaceEdit);
} else if (simpleReferencesNeedNewImport) {
ImportRewrite importEdit = getImportRewrite(referencingCu);
String typeName = reference.getSimpleName();
importEdit.addImport(getQualifiedType(fDestination.getElementName(), typeName));
}
}
}
}
Aggregations