Search in sources :

Example 11 with RefactoringRequest

use of org.python.pydev.ast.refactoring.RefactoringRequest in project Pydev by fabioz.

the class SearchTest method testSearch6.

public void testSearch6() throws Exception {
    // from testlib.unittest import testcase as t
    String line = "class AnotherTest(t.TestCase):";
    // "from " < -- that's the cursor pos
    final File file = new File(TestDependent.TEST_PYSRC_TESTING_LOC + "testlib/unittest/anothertest.py");
    RefactoringRequest refactoringRequest = createRefactoringRequest(line, file);
    refactoringRequest.ps = new PySelection(refactoringRequest.getDoc(), 2, line.length() - 5);
    ItemPointer[] pointers = refactorer.findDefinition(refactoringRequest);
    assertEquals(1, pointers.length);
    assertEquals(new File(TestDependent.TEST_PYSRC_TESTING_LOC + "testlib/unittest/testcase.py"), pointers[0].file);
    // found the module
    assertEquals(8, pointers[0].start.line);
    assertEquals(6, pointers[0].start.column);
}
Also used : RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) PySelection(org.python.pydev.core.docutils.PySelection) File(java.io.File) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 12 with RefactoringRequest

use of org.python.pydev.ast.refactoring.RefactoringRequest in project Pydev by fabioz.

the class SearchTest method testSearch4.

public void testSearch4() throws Exception {
    String line = "from testlib.unittest import testcase as t";
    // "from testlib.unitt" < -- that's the cursor pos
    final File file = new File(TestDependent.TEST_PYSRC_TESTING_LOC + "testlib/unittest/anothertest.py");
    RefactoringRequest refactoringRequest = createRefactoringRequest(line, file);
    refactoringRequest.ps = new PySelection(refactoringRequest.getDoc(), line.length() - 24);
    ItemPointer[] pointers = refactorer.findDefinition(refactoringRequest);
    assertEquals(1, pointers.length);
    assertEquals(new File(TestDependent.TEST_PYSRC_TESTING_LOC + "testlib/unittest/__init__.py"), pointers[0].file);
    // found the module
    assertEquals(0, pointers[0].start.line);
    assertEquals(0, pointers[0].start.column);
}
Also used : RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) PySelection(org.python.pydev.core.docutils.PySelection) File(java.io.File) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 13 with RefactoringRequest

use of org.python.pydev.ast.refactoring.RefactoringRequest in project Pydev by fabioz.

the class RefactoringLocalTestBase method checkRename.

protected void checkRename(String strDoc, int line, int col, String initialName, boolean expectError, boolean onlyOnLocalScope, String newName) throws CoreException {
    Document doc = new Document(StringUtils.format(strDoc, getSame(initialName)));
    PySelection ps = new PySelection(doc, line, col);
    RefactoringRequest request = new RefactoringRequest(null, ps, nature);
    request.setAdditionalInfo(RefactoringRequest.FIND_REFERENCES_ONLY_IN_LOCAL_SCOPE, onlyOnLocalScope);
    request.moduleName = "foo";
    request.inputName = newName;
    request.fillActivationTokenAndQualifier();
    applyRenameRefactoring(request, expectError);
    String refactored = doc.get();
    if (DEBUG) {
        System.out.println(refactored);
    }
    if (!expectError) {
        assertEquals(initialName, request.qualifier);
        assertEquals(StringUtils.format(strDoc, getSame("bb")), refactored);
    } else {
        // cannot have changed
        assertEquals(StringUtils.format(strDoc, getSame(initialName)), refactored);
    }
}
Also used : RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) PyRefactoringRequest(org.python.pydev.ast.refactoring.PyRefactoringRequest) PySelection(org.python.pydev.core.docutils.PySelection) Document(org.eclipse.jface.text.Document)

Example 14 with RefactoringRequest

use of org.python.pydev.ast.refactoring.RefactoringRequest in project Pydev by fabioz.

the class PyGoToDefinition method findDefinitionsAndOpen.

public ItemPointer[] findDefinitionsAndOpen(boolean doOpenDefinition) {
    request = null;
    ps = PySelectionFromEditor.createPySelectionFromEditor(getTextEditor());
    final PyEdit pyEdit = getPyEdit();
    RefactoringRequest refactoringRequest;
    try {
        refactoringRequest = getRefactoringRequest();
    } catch (MisconfigurationException e1) {
        Log.log(e1);
        return new ItemPointer[0];
    }
    final Shell shell = EditorUtils.getShell();
    try {
        if (areRefactorPreconditionsOK(refactoringRequest)) {
            boolean acceptTypeshed = false;
            boolean findInAdditionalInfo = false;
            ItemPointer[] defs = findDefinition(pyEdit, acceptTypeshed, findInAdditionalInfo);
            boolean retry = defs == null || defs.length == 0;
            if (!retry) {
                if (defs.length == 1) {
                    if (defs[0].definition == null) {
                        retry = true;
                    } else if (defs[0].definition.module == null) {
                        retry = true;
                    } else if (defs[0].definition.module.getFile() == null) {
                        retry = true;
                    }
                }
            }
            if (retry) {
                acceptTypeshed = true;
                findInAdditionalInfo = true;
                defs = findDefinition(pyEdit, acceptTypeshed, findInAdditionalInfo);
            }
            if (doOpenDefinition) {
                openDefinition(defs, pyEdit, shell);
            }
            return defs;
        }
    } catch (Exception e) {
        Log.log(e);
        String msg = e.getMessage();
        if (msg == null) {
            msg = "Unable to get error msg (null)";
        }
        ErrorDialog.openError(shell, "Error", "Unable to do requested action", new Status(Status.ERROR, PydevPlugin.getPluginID(), 0, msg, e));
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Shell(org.eclipse.swt.widgets.Shell) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) MisconfigurationException(org.python.pydev.core.MisconfigurationException) PyEdit(org.python.pydev.editor.PyEdit) TooManyMatchesException(org.python.pydev.ast.refactoring.TooManyMatchesException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 15 with RefactoringRequest

use of org.python.pydev.ast.refactoring.RefactoringRequest 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)

Aggregations

RefactoringRequest (org.python.pydev.ast.refactoring.RefactoringRequest)59 PySelection (org.python.pydev.core.docutils.PySelection)30 ItemPointer (org.python.pydev.ast.item_pointer.ItemPointer)29 File (java.io.File)26 HierarchyNodeModel (org.python.pydev.ast.refactoring.HierarchyNodeModel)10 ArrayList (java.util.ArrayList)7 Document (org.eclipse.jface.text.Document)7 CoreException (org.eclipse.core.runtime.CoreException)6 BadLocationException (org.eclipse.jface.text.BadLocationException)6 MisconfigurationException (org.python.pydev.core.MisconfigurationException)6 HashSet (java.util.HashSet)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 IDocument (org.eclipse.jface.text.IDocument)5 IPyRefactoring (org.python.pydev.ast.refactoring.IPyRefactoring)5 PyRefactoringRequest (org.python.pydev.ast.refactoring.PyRefactoringRequest)5 Tuple (org.python.pydev.shared_core.structure.Tuple)5 IFile (org.eclipse.core.resources.IFile)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 Definition (org.python.pydev.ast.codecompletion.revisited.visitors.Definition)4 IPyRefactoringRequest (org.python.pydev.ast.refactoring.IPyRefactoringRequest)4