Search in sources :

Example 1 with FileMatch

use of com.python.pydev.refactoring.refactorer.search.copied.FileMatch in project Pydev by fabioz.

the class FindOccurrencesSearchQuery method run.

@Override
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
    try {
        monitor.beginTask("Searching...", 100);
        req.pushMonitor(monitor);
        FindOccurrencesSearchResult searchResult = (FindOccurrencesSearchResult) getSearchResult();
        Map<Tuple<String, File>, HashSet<ASTEntry>> occurrences;
        try {
            req.pushMonitor(new SubProgressMonitor(monitor, 80));
            occurrences = pyRefactoring.findAllOccurrences(req);
        } finally {
            req.popMonitor().done();
        }
        if (occurrences == null) {
            return Status.OK_STATUS;
        }
        int length = req.qualifier.length();
        HashSet<Integer> foundOffsets = new HashSet<Integer>();
        try {
            req.pushMonitor(new SubProgressMonitor(monitor, 20));
            Set<Entry<Tuple<String, File>, HashSet<ASTEntry>>> entrySet = occurrences.entrySet();
            req.getMonitor().beginTask("Resolving occurrences...", entrySet.size());
            for (Map.Entry<Tuple<String, File>, HashSet<ASTEntry>> o : entrySet) {
                foundOffsets.clear();
                IFile workspaceFile = null;
                try {
                    IProject project = null;
                    IPythonNature nature = req.nature;
                    if (nature != null) {
                        project = nature.getProject();
                    }
                    workspaceFile = FindWorkspaceFiles.getWorkspaceFile(o.getKey().o2, project);
                    if (workspaceFile == null) {
                        Log.logInfo(StringUtils.format("Ignoring: %s. " + "Unable to resolve to a file in the Eclipse workspace.", o.getKey().o2));
                        continue;
                    }
                } catch (IllegalStateException e) {
                    // this can happen on tests (but if not on tests, we want to re-throw it
                    String message = e.getMessage();
                    if (message == null || !message.equals("Workspace is closed.")) {
                        throw e;
                    }
                    // otherwise, let's just keep going in the test...
                    continue;
                }
                IDocument doc = FileUtilsFileBuffer.getDocFromResource(workspaceFile);
                req.getMonitor().setTaskName("Resolving occurrences... " + workspaceFile);
                for (ASTEntry entry : o.getValue()) {
                    int offset = AbstractRenameRefactorProcess.getOffset(doc, entry);
                    if (!foundOffsets.contains(offset)) {
                        foundOffsets.add(offset);
                        if (DebugFlags.DEBUG_FIND_REFERENCES) {
                            System.out.println("Adding match:" + workspaceFile);
                        }
                        PySelection ps = new PySelection(doc, offset);
                        int lineNumber = ps.getLineOfOffset();
                        String lineContents = ps.getLine(lineNumber);
                        int lineStartOffset = ps.getLineOffset(lineNumber);
                        LineElement element = new LineElement(workspaceFile, lineNumber, lineStartOffset, lineContents, offset - lineStartOffset);
                        searchResult.addMatch(new FileMatch(workspaceFile, offset, length, element));
                    }
                }
            }
        } finally {
            req.popMonitor().done();
        }
    } catch (CoreException e) {
        Log.log(e);
    } finally {
        req.popMonitor().done();
    }
    return Status.OK_STATUS;
}
Also used : IFile(org.eclipse.core.resources.IFile) IPythonNature(org.python.pydev.core.IPythonNature) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IProject(org.eclipse.core.resources.IProject) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) Entry(java.util.Map.Entry) CoreException(org.eclipse.core.runtime.CoreException) LineElement(com.python.pydev.refactoring.refactorer.search.copied.LineElement) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) PySelection(org.python.pydev.core.docutils.PySelection) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Map(java.util.Map) Tuple(org.python.pydev.shared_core.structure.Tuple) IDocument(org.eclipse.jface.text.IDocument) FileMatch(com.python.pydev.refactoring.refactorer.search.copied.FileMatch) HashSet(java.util.HashSet)

Aggregations

FileMatch (com.python.pydev.refactoring.refactorer.search.copied.FileMatch)1 LineElement (com.python.pydev.refactoring.refactorer.search.copied.LineElement)1 File (java.io.File)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1 IDocument (org.eclipse.jface.text.IDocument)1 IPythonNature (org.python.pydev.core.IPythonNature)1 PySelection (org.python.pydev.core.docutils.PySelection)1 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)1 Tuple (org.python.pydev.shared_core.structure.Tuple)1