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);
}
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);
}
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);
}
}
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;
}
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;
}
Aggregations