Search in sources :

Example 6 with SearchMatch

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;
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) IRegion(org.eclipse.jface.text.IRegion)

Example 7 with SearchMatch

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);
    }
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch)

Example 8 with SearchMatch

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;
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Example 9 with SearchMatch

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) IType(org.eclipse.jdt.core.IType) IRefactoringSearchRequestor(org.eclipse.jdt.internal.corext.refactoring.IRefactoringSearchRequestor) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) TypeOccurrenceCollector(org.eclipse.jdt.internal.corext.refactoring.rename.TypeOccurrenceCollector) RefactoringSearchEngine2(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2)

Example 10 with SearchMatch

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));
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) TextChange(org.eclipse.ltk.core.refactoring.TextChange) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Aggregations

SearchMatch (org.eclipse.jdt.core.search.SearchMatch)35 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)13 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)12 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)10 ArrayList (java.util.ArrayList)8 CoreException (org.eclipse.core.runtime.CoreException)8 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)8 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)8 IJavaElement (org.eclipse.jdt.core.IJavaElement)7 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)5 HashMap (java.util.HashMap)4 IResource (org.eclipse.core.resources.IResource)4 IMethod (org.eclipse.jdt.core.IMethod)4 RefactoringSearchEngine (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 HashSet (java.util.HashSet)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 IType (org.eclipse.jdt.core.IType)3