use of org.python.pydev.core.docutils.PySelection.TddPossibleMatches in project Pydev by fabioz.
the class TddCodeGenerationQuickFixParticipant method getTddProps.
public List<ICompletionProposalHandle> getTddProps(PySelection ps, IImageCache imageCache, File f, IPythonNature nature, PyEdit edit, int offset, List<ICompletionProposalHandle> ret) {
if (ret == null) {
ret = new ArrayList<ICompletionProposalHandle>();
}
// Additional option: Generate markers for 'self.' accesses
int lineOfOffset = ps.getLineOfOffset(offset);
String lineContents = ps.getLine(lineOfOffset);
// Additional option: Generate methods for function calls
List<TddPossibleMatches> callsAtLine = ps.getTddPossibleMatchesAtLine();
if (callsAtLine.size() > 0) {
// Make sure we don't check the same thing twice.
Map<String, TddPossibleMatches> callsToCheck = new HashMap<String, TddPossibleMatches>();
for (TddPossibleMatches call : callsAtLine) {
String callString = call.initialPart + call.secondPart;
callsToCheck.put(callString, call);
}
CONTINUE_FOR: for (Map.Entry<String, TddPossibleMatches> entry : callsToCheck.entrySet()) {
// we have at least something as SomeClass(a=2,c=3) or self.bar or self.foo.bar() or just foo.bar, etc.
IPyRefactoring pyRefactoring = AbstractPyRefactoring.getPyRefactoring();
try {
TddPossibleMatches possibleMatch = entry.getValue();
String callWithoutParens = entry.getKey();
ItemPointer[] pointers = null;
PySelection callPs = null;
TddPossibleMatches lastPossibleMatchNotFound = possibleMatch;
for (int i = 0; i < 10; i++) {
// more than 10 attribute accesses in a line? No way!
lastPossibleMatchNotFound = possibleMatch;
if (i > 0) {
// We have to take 1 level out of the match... i.e.: if it was self.foo.get(), search now for self.foo.
String line = FullRepIterable.getWithoutLastPart(possibleMatch.full);
List<TddPossibleMatches> tddPossibleMatchesAtLine = ps.getTddPossibleMatchesAtLine(line);
if (tddPossibleMatchesAtLine.size() > 0) {
possibleMatch = tddPossibleMatchesAtLine.get(0);
callWithoutParens = possibleMatch.initialPart + possibleMatch.secondPart;
} else {
continue CONTINUE_FOR;
}
}
String full = possibleMatch.full;
int indexOf = lineContents.indexOf(full);
if (indexOf < 0) {
Log.log("Did not expect index < 0.");
continue CONTINUE_FOR;
}
callPs = new PySelection(ps.getDoc(), ps.getLineOffset() + indexOf + callWithoutParens.length());
RefactoringRequest request = new RefactoringRequest(f, callPs, null, nature, edit);
// Don't look in additional info.
request.setAdditionalInfo(RefactoringRequest.FIND_DEFINITION_IN_ADDITIONAL_INFO, false);
pointers = pyRefactoring.findDefinition(request);
if (((pointers != null && pointers.length > 0) || StringUtils.count(possibleMatch.full, '.') <= 1)) {
break;
}
}
if (pointers == null || callPs == null) {
continue CONTINUE_FOR;
}
if (lastPossibleMatchNotFound != null && lastPossibleMatchNotFound != possibleMatch && pointers.length >= 1) {
// Ok, as we were analyzing a string as self.bar.foo, we didn't find something in a pass
// i.e.: self.bar.foo, but we found it in a second pass
// as self.bar, so, this means we have to open the chance to create the 'foo' in self.bar.
String methodToCreate = FullRepIterable.getLastPart(lastPossibleMatchNotFound.secondPart);
int absoluteCursorOffset = callPs.getAbsoluteCursorOffset();
// +1 for the dot removed too.
absoluteCursorOffset = absoluteCursorOffset - (1 + methodToCreate.length());
PySelection newSelection = new PySelection(callPs.getDoc(), absoluteCursorOffset);
checkCreationBasedOnFoundPointers(edit, callPs, ret, possibleMatch, pointers, methodToCreate, newSelection, nature);
continue CONTINUE_FOR;
}
if (pointers.length >= 1) {
// the __init__ or something at the class level).
if (!checkInitCreation(edit, callPs, pointers, ret)) {
// This was called only when isCall == false
// Ok, if it's not a call and we found a field, it's still possible that we may want to create
// a field if it wasn't found in the __init__
boolean foundInInit = false;
for (ItemPointer p : pointers) {
Definition definition = p.definition;
try {
Object peek = definition.scope.getScopeStack().peek();
if (peek instanceof FunctionDef) {
FunctionDef functionDef = (FunctionDef) peek;
String rep = NodeUtils.getRepresentationString(functionDef);
if (rep != null && rep.equals("__init__")) {
foundInInit = true;
break;
}
}
} catch (Exception e) {
}
}
if (!foundInInit) {
checkMethodCreationAtClass(edit, pyRefactoring, callWithoutParens, callPs, ret, lineContents, possibleMatch, f, nature);
}
}
} else if (pointers.length == 0) {
checkMethodCreationAtClass(edit, pyRefactoring, callWithoutParens, callPs, ret, lineContents, possibleMatch, f, nature);
}
} catch (Exception e) {
if (onGetTddPropsError != null) {
onGetTddPropsError.call(e);
}
Log.log(e);
}
}
}
return ret;
}
use of org.python.pydev.core.docutils.PySelection.TddPossibleMatches in project Pydev by fabioz.
the class PySelectionTest method testGetFunctionCalls.
public void testGetFunctionCalls() throws Exception {
Document doc = new Document();
PySelection ps = new PySelection(doc);
assertEquals(0, ps.getTddPossibleMatchesAtLine().size());
doc.set("MyCall(aa, bb, 10, )");
List<TddPossibleMatches> calls = ps.getTddPossibleMatchesAtLine();
assertEquals(1, calls.size());
assertEquals("MyCall(", calls.get(0).toString());
doc.set("foo.MyCall(aa, bb, 10, )");
calls = ps.getTddPossibleMatchesAtLine();
assertEquals(1, calls.size());
assertEquals("foo.MyCall(", calls.get(0).toString());
doc.set("foo.MyCall1 (aa, bb, 10, )");
calls = ps.getTddPossibleMatchesAtLine();
assertEquals(1, calls.size());
assertEquals("foo.MyCall1 (", calls.get(0).toString());
doc.set("call1(aa, bar.call2(), 10, )");
calls = ps.getTddPossibleMatchesAtLine();
assertEquals(2, calls.size());
assertEquals("call1(", calls.get(0).toString());
assertEquals("bar.call2(", calls.get(1).toString());
doc.set("def m1(foo)");
calls = ps.getTddPossibleMatchesAtLine();
assertEquals(0, calls.size());
doc.set("class Bar(object):");
calls = ps.getTddPossibleMatchesAtLine();
assertEquals(0, calls.size());
doc.set("a = (1,3)");
calls = ps.getTddPossibleMatchesAtLine();
assertEquals(0, calls.size());
doc.set("self.a.b, my.call()");
calls = ps.getTddPossibleMatchesAtLine();
assertEquals(2, calls.size());
assertEquals("self.a.b", calls.get(0).toString());
assertEquals("my.call(", calls.get(1).toString());
doc.set("self.call().call2()");
calls = ps.getTddPossibleMatchesAtLine();
assertEquals(1, calls.size());
assertEquals("self.call(", calls.get(0).toString());
}
Aggregations