Search in sources :

Example 6 with ASTEntry

use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.

the class MarkOccurrencesJob method getAnnotationsToAddAsMap.

/**
 * @param markOccurrencesRequest
 * @return true if the annotations were removed and added without any problems and false otherwise
 */
@Override
protected synchronized Map<Annotation, Position> getAnnotationsToAddAsMap(final BaseEditor baseEditor, IAnnotationModel annotationModel, MarkOccurrencesRequest markOccurrencesRequest, IProgressMonitor monitor) throws BadLocationException {
    PyEdit pyEdit = (PyEdit) baseEditor;
    PySourceViewer viewer = pyEdit.getPySourceViewer();
    if (viewer == null || monitor.isCanceled()) {
        return null;
    }
    if (monitor.isCanceled()) {
        return null;
    }
    if (markOccurrencesRequest instanceof TextBasedLocalMarkOccurrencesRequest) {
        TextBasedLocalMarkOccurrencesRequest textualMarkOccurrencesRequest = (TextBasedLocalMarkOccurrencesRequest) markOccurrencesRequest;
        PySelection pySelection = PySelection.fromTextSelection(ps);
        Tuple<Integer, Integer> startEndLines = pySelection.getCurrentMethodStartEndLines();
        int initialOffset = pySelection.getAbsoluteCursorOffset(startEndLines.o1, 0);
        int finalOffset = pySelection.getEndLineOffset(startEndLines.o2);
        List<IRegion> occurrences = ps.searchOccurrences(textualMarkOccurrencesRequest.currToken);
        if (occurrences.size() == 0) {
            return null;
        }
        Map<Annotation, Position> toAddAsMap = new HashMap<Annotation, Position>();
        for (Iterator<IRegion> it = occurrences.iterator(); it.hasNext(); ) {
            IRegion iRegion = it.next();
            if (iRegion.getOffset() < initialOffset || iRegion.getOffset() > finalOffset) {
                continue;
            }
            try {
                Annotation annotation = new Annotation(getOccurrenceAnnotationsType(), false, "occurrence");
                Position position = new Position(iRegion.getOffset(), iRegion.getLength());
                toAddAsMap.put(annotation, position);
            } catch (Exception e) {
                Log.log(e);
            }
        }
        return toAddAsMap;
    }
    PyMarkOccurrencesRequest pyMarkOccurrencesRequest = (PyMarkOccurrencesRequest) markOccurrencesRequest;
    Set<ASTEntry> occurrences = pyMarkOccurrencesRequest.getOccurrences();
    if (occurrences == null) {
        if (DEBUG) {
            System.out.println("Occurrences == null");
        }
        return null;
    }
    IDocument doc = pyEdit.getDocument();
    Map<Annotation, Position> toAddAsMap = new HashMap<Annotation, Position>();
    boolean markOccurrencesInStrings = MarkOccurrencesPreferencesPage.useMarkOccurrencesInStrings();
    // get the annotations to add
    for (ASTEntry entry : occurrences) {
        if (!markOccurrencesInStrings) {
            if (entry.node instanceof Name) {
                Name name = (Name) entry.node;
                if (name.ctx == Name.Artificial) {
                    continue;
                }
            }
        }
        SimpleNode node = entry.getNameNode();
        IRegion lineInformation = doc.getLineInformation(node.beginLine - 1);
        try {
            Annotation annotation = new Annotation(getOccurrenceAnnotationsType(), false, "occurrence");
            Position position = new Position(lineInformation.getOffset() + node.beginColumn - 1, pyMarkOccurrencesRequest.getInitialName().length());
            toAddAsMap.put(annotation, position);
        } catch (Exception e) {
            Log.log(e);
        }
    }
    return toAddAsMap;
}
Also used : Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) IRegion(org.eclipse.jface.text.IRegion) Annotation(org.eclipse.jface.text.source.Annotation) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) Name(org.python.pydev.parser.jython.ast.Name) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) PySelection(org.python.pydev.core.docutils.PySelection) PySourceViewer(org.python.pydev.editor.codefolding.PySourceViewer) PyEdit(org.python.pydev.editor.PyEdit) IDocument(org.eclipse.jface.text.IDocument)

Example 7 with ASTEntry

use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.

the class RefactoringRenameTestBase method asStr.

@SuppressWarnings("unchecked")
protected String asStr(Map<Tuple<String, File>, HashSet<ASTEntry>> referencesForModuleRename) throws Exception {
    Set<Entry<Tuple<String, File>, HashSet<ASTEntry>>> entrySet = referencesForModuleRename.entrySet();
    FastStringBuffer buf = new FastStringBuffer();
    ArrayList<Entry<Tuple<String, File>, HashSet<ASTEntry>>> lst = new ArrayList<>(entrySet);
    Comparator<Entry<Tuple<String, File>, HashSet<ASTEntry>>> c = new Comparator<Entry<Tuple<String, File>, HashSet<ASTEntry>>>() {

        @Override
        public int compare(Entry<Tuple<String, File>, HashSet<ASTEntry>> o1, Entry<Tuple<String, File>, HashSet<ASTEntry>> o2) {
            return o1.getKey().o1.compareTo(o2.getKey().o1);
        }
    };
    Collections.sort(lst, c);
    for (Entry<Tuple<String, File>, HashSet<ASTEntry>> entry : lst) {
        HashSet<ASTEntry> value = entry.getValue();
        if (value.size() > 0) {
            ArrayList<ASTEntry> lst2 = new ArrayList<>(value);
            Comparator<ASTEntry> c2 = new Comparator<ASTEntry>() {

                @Override
                public int compare(ASTEntry o1, ASTEntry o2) {
                    return o1.toString().compareTo(o2.toString());
                }
            };
            Collections.sort(lst2, c2);
            File f = entry.getKey().o2;
            String fileContents = FileUtils.getFileContents(f);
            Document initialDoc = new Document(fileContents);
            buf.append(entry.getKey().o1).append("\n");
            for (ASTEntry e : lst2) {
                buf.append("  ");
                buf.append(e.toString()).append("\n");
                List<TextEdit> edits = (List<TextEdit>) e.getAdditionalInfo(AstEntryScopeAnalysisConstants.AST_ENTRY_REPLACE_EDIT, null);
                if (edits == null) {
                    if (!(e instanceof ASTEntryWithSourceModule)) {
                        throw new AssertionError("Only ASTEntryWithSourceModule can have null edits. Found: " + e);
                    }
                } else {
                    Document changedDoc = new Document(fileContents);
                    for (TextEdit textEdit : edits) {
                        textEdit.apply(changedDoc);
                    }
                    List<String> changedLines = getChangedLines(initialDoc, changedDoc);
                    for (String i : changedLines) {
                        buf.append("    ");
                        buf.append(StringUtils.rightTrim(i)).append("\n");
                    }
                }
            }
            buf.append("\n");
        }
    }
    return buf.toString();
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) Comparator(java.util.Comparator) Entry(java.util.Map.Entry) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) TextEdit(org.eclipse.text.edits.TextEdit) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) ASTEntryWithSourceModule(org.python.pydev.ast.codecompletion.revisited.modules.ASTEntryWithSourceModule) List(java.util.List) ArrayList(java.util.ArrayList) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Example 8 with ASTEntry

use of org.python.pydev.parser.visitors.scope.ASTEntry 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)

Example 9 with ASTEntry

use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.

the class PyRenameInFileAction method fillWithOccurrences.

/**
 * Puts the found positions referente to the occurrences in the group
 *
 * @param document the document that will contain this positions
 * @param group the group that will contain this positions
 * @param ps the selection used
 * @return
 *
 * @throws BadLocationException
 * @throws OperationCanceledException
 * @throws CoreException
 * @throws MisconfigurationException
 */
private boolean fillWithOccurrences(IDocument document, LinkedPositionGroup group, IProgressMonitor monitor, PySelection ps) throws BadLocationException, OperationCanceledException, CoreException, MisconfigurationException {
    RefactoringRequest req = MarkOccurrencesJob.getRefactoringRequest(pyEdit, MarkOccurrencesJob.getRefactorAction(pyEdit), ps);
    if (monitor.isCanceled()) {
        return false;
    }
    PyRenameEntryPoint processor = new PyRenameEntryPoint(req);
    // process it to get what we need
    processor.checkInitialConditions(monitor);
    processor.checkFinalConditions(monitor, null);
    HashSet<ASTEntry> occurrences = processor.getOccurrences();
    if (monitor.isCanceled()) {
        return false;
    }
    // used so that we don't add duplicates
    Set<Tuple<Integer, Integer>> found = new HashSet<Tuple<Integer, Integer>>();
    List<ProposalPosition> groupPositions = new ArrayList<ProposalPosition>();
    if (occurrences != null) {
        // first, just sort by position (line, col)
        ArrayList<ASTEntry> sortedOccurrences = new ArrayList<ASTEntry>(occurrences);
        Collections.sort(sortedOccurrences, new Comparator<ASTEntry>() {

            @Override
            public int compare(ASTEntry o1, ASTEntry o2) {
                int thisVal = o1.node.beginLine;
                int anotherVal = o2.node.beginLine;
                int ret;
                if (thisVal == anotherVal) {
                    // if it's in the same line, let's sort by column
                    thisVal = o1.node.beginColumn;
                    anotherVal = o2.node.beginColumn;
                    ret = (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
                } else {
                    ret = (thisVal < anotherVal ? -1 : 1);
                }
                return ret;
            }
        });
        // now, gather positions to add to the group
        int i = 0;
        int firstPosition = -1;
        int absoluteCursorOffset = ps.getAbsoluteCursorOffset();
        for (ASTEntry entry : sortedOccurrences) {
            try {
                IRegion lineInformation = document.getLineInformation(entry.node.beginLine - 1);
                int colDef = NodeUtils.getClassOrFuncColDefinition(entry.node) - 1;
                int offset = lineInformation.getOffset() + colDef;
                int len = req.qualifier.length();
                Tuple<Integer, Integer> foundAt = new Tuple<Integer, Integer>(offset, len);
                if (!found.contains(foundAt)) {
                    i++;
                    ProposalPosition proposalPosition = new ProposalPosition(document, offset, len, i, new ICompletionProposal[0]);
                    found.add(foundAt);
                    groupPositions.add(proposalPosition);
                    if (offset <= absoluteCursorOffset && absoluteCursorOffset < offset + len) {
                        firstPosition = i;
                    }
                }
            } catch (Exception e) {
                Log.log(e);
                return false;
            }
        }
        if (firstPosition != -1) {
            ArrayList<ProposalPosition> newGroupPositions = new ArrayList<ProposalPosition>();
            // add from current to end
            for (i = firstPosition - 1; i < groupPositions.size(); i++) {
                newGroupPositions.add(groupPositions.get(i));
            }
            // and now from the start up to the current
            for (i = 0; i < firstPosition - 1; i++) {
                newGroupPositions.add(groupPositions.get(i));
            }
            groupPositions = newGroupPositions;
        }
        for (ProposalPosition proposalPosition : groupPositions) {
            group.addPosition(proposalPosition);
        }
    }
    return groupPositions.size() > 0;
}
Also used : RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ArrayList(java.util.ArrayList) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) IRegion(org.eclipse.jface.text.IRegion) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ProposalPosition(org.eclipse.jface.text.link.ProposalPosition) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Example 10 with ASTEntry

use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.

the class NodeUtils method isValidContextForSetNext.

/**
 * Identifies the context for both source and target line
 *
 * @param ASTEntry
 *            ast
 * @param int sourceLine: the line at which debugger is stopped currently
 *        (starts at 1)
 * @param int targetLine: the line at which we need to set next (starts at
 *        0)
 * @return
 */
public static boolean isValidContextForSetNext(SimpleNode ast, int sourceLine, int targetLine) {
    String sourceFunctionName = NodeUtils.getContextName((sourceLine - 1), ast);
    String targetFunctionName = NodeUtils.getContextName(targetLine, ast);
    if (compareMethodName(sourceFunctionName, targetFunctionName)) {
        ASTEntry sourceAST = NodeUtils.getLoopContextName(sourceLine, ast);
        ASTEntry targetAST = NodeUtils.getLoopContextName(targetLine + 1, ast);
        if (targetAST == null) {
            // Target line is not inside some loop
            return true;
        }
        if (isValidElseBlock(sourceAST, targetAST, sourceLine, targetLine)) {
            // Debug pointer can be set inside else block of
            return true;
        // for..else/while..else
        }
        if (sourceAST == null && targetAST != null) {
            // Source is outside loop and target is inside
            return false;
        // loop
        }
        if (sourceAST != null && targetAST != null) {
            // Both Source and Target is inside some loop
            if (sourceAST.equals(targetAST)) {
                return isValidInterLoopContext(sourceLine, targetLine, sourceAST, targetAST);
            } else {
                ASTEntry last = sourceAST;
                boolean retVal = false;
                while (last != null) {
                    ASTEntry parentAST = last.parent;
                    if (parentAST != null && parentAST.equals(targetAST)) {
                        retVal = true;
                        break;
                    }
                    last = parentAST;
                }
                return retVal;
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry)

Aggregations

ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)81 ArrayList (java.util.ArrayList)30 SimpleNode (org.python.pydev.parser.jython.SimpleNode)30 HashSet (java.util.HashSet)13 SequencialASTIteratorVisitor (org.python.pydev.parser.visitors.scope.SequencialASTIteratorVisitor)13 Tuple (org.python.pydev.shared_core.structure.Tuple)13 IToken (org.python.pydev.core.IToken)11 File (java.io.File)9 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)9 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)9 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)8 Str (org.python.pydev.parser.jython.ast.Str)8 CoreException (org.eclipse.core.runtime.CoreException)7 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)7 PySelection (org.python.pydev.core.docutils.PySelection)7 Name (org.python.pydev.parser.jython.ast.Name)7 Found (com.python.pydev.analysis.visitors.Found)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Attribute (org.python.pydev.parser.jython.ast.Attribute)6