use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyInitReferenceSearchExecutor method processQuery.
public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiReference> consumer) {
PsiElement element = queryParameters.getElementToSearch();
if (!(element instanceof PyFunction)) {
return;
}
final AccessToken accessToken = ApplicationManager.getApplication().acquireReadActionLock();
String className;
SearchScope searchScope;
PyFunction function;
try {
function = (PyFunction) element;
if (!PyNames.INIT.equals(function.getName())) {
return;
}
final PyClass pyClass = function.getContainingClass();
if (pyClass == null) {
return;
}
className = pyClass.getName();
if (className == null) {
return;
}
searchScope = queryParameters.getEffectiveSearchScope();
if (searchScope instanceof GlobalSearchScope) {
searchScope = GlobalSearchScope.getScopeRestrictedByFileTypes((GlobalSearchScope) searchScope, PythonFileType.INSTANCE);
}
} finally {
accessToken.finish();
}
queryParameters.getOptimizer().searchWord(className, searchScope, UsageSearchContext.IN_CODE, true, function);
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyTargetReference method multiResolve.
@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
final ResolveResult[] results = super.multiResolve(incompleteCode);
boolean shadowed = false;
for (ResolveResult result : results) {
final PsiElement element = result.getElement();
if (element != null && (element.getContainingFile() != myElement.getContainingFile() || element instanceof PyFunction || element instanceof PyClass)) {
shadowed = true;
break;
}
}
if (results.length > 0 && !shadowed) {
return results;
}
// resolve to self if no other target found
return new ResolveResult[] { new PsiElementResolveResult(myElement) };
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyCallHierarchyProvider method getTarget.
@Nullable
@Override
public PsiElement getTarget(@NotNull DataContext dataContext) {
Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project == null)
return null;
PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
if (element == null) {
Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
if (editor != null) {
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (file == null)
return null;
element = TargetElementUtil.findTargetElement(editor, TargetElementUtil.ELEMENT_NAME_ACCEPTED | TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED | TargetElementUtil.LOOKUP_ITEM_ACCEPTED);
if (element instanceof PyFunction || element instanceof PyClass || element instanceof PyFile) {
return element;
}
element = file.findElementAt(editor.getCaretModel().getOffset());
}
}
return PsiTreeUtil.getNonStrictParentOfType(element, PyFunction.class, PyClass.class, PyFile.class);
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyCallHierarchyTreeStructureBase method buildChildren.
@NotNull
@Override
protected Object[] buildChildren(@NotNull HierarchyNodeDescriptor descriptor) {
final List<PyHierarchyNodeDescriptor> descriptors = new ArrayList<>();
if (descriptor instanceof PyHierarchyNodeDescriptor) {
final PyHierarchyNodeDescriptor pyDescriptor = (PyHierarchyNodeDescriptor) descriptor;
final PsiElement element = pyDescriptor.getPsiElement();
final boolean isCallable = element instanceof PyFunction || element instanceof PyClass || element instanceof PyFile;
HierarchyNodeDescriptor nodeDescriptor = getBaseDescriptor();
if (!(element instanceof PyElement) || !isCallable || nodeDescriptor == null) {
return ArrayUtil.EMPTY_OBJECT_ARRAY;
}
final List<PsiElement> children = getChildren((PyElement) element);
final HashMap<PsiElement, PyHierarchyNodeDescriptor> callerToDescriptorMap = new HashMap<>();
PsiElement baseClass = element instanceof PyFunction ? ((PyFunction) element).getContainingClass() : null;
for (PsiElement caller : children) {
if (isInScope(baseClass, caller, myScopeType)) {
PyHierarchyNodeDescriptor callerDescriptor = callerToDescriptorMap.get(caller);
if (callerDescriptor == null) {
callerDescriptor = new PyHierarchyNodeDescriptor(descriptor, caller, false);
callerToDescriptorMap.put(caller, callerDescriptor);
descriptors.add(callerDescriptor);
}
}
}
}
return ArrayUtil.toObjectArray(descriptors);
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class AbstractPythonLegacyTestRunConfiguration method getRefactoringElementListener.
@Override
public RefactoringElementListener getRefactoringElementListener(PsiElement element) {
if (element instanceof PsiDirectory) {
VirtualFile vFile = ((PsiDirectory) element).getVirtualFile();
if ((myTestType == TestType.TEST_FOLDER && pathsEqual(vFile, myFolderName)) || pathsEqual(vFile, getWorkingDirectory())) {
return new RefactoringElementAdapter() {
@Override
protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
String newPath = FileUtil.toSystemDependentName(((PsiDirectory) newElement).getVirtualFile().getPath());
setWorkingDirectory(newPath);
if (myTestType == TestType.TEST_FOLDER) {
myFolderName = newPath;
}
}
@Override
public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
final String systemDependant = FileUtil.toSystemDependentName(oldQualifiedName);
setWorkingDirectory(systemDependant);
if (myTestType == TestType.TEST_FOLDER) {
myFolderName = systemDependant;
}
}
};
}
return null;
}
if (myTestType == TestType.TEST_FOLDER) {
return null;
}
File scriptFile = new File(myScriptName);
if (!scriptFile.isAbsolute()) {
scriptFile = new File(getWorkingDirectory(), myScriptName);
}
PsiFile containingFile = element.getContainingFile();
VirtualFile vFile = containingFile == null ? null : containingFile.getVirtualFile();
if (vFile != null && Comparing.equal(new File(vFile.getPath()).getAbsolutePath(), scriptFile.getAbsolutePath())) {
if (element instanceof PsiFile) {
return new RefactoringElementAdapter() {
@Override
protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
VirtualFile virtualFile = ((PsiFile) newElement).getVirtualFile();
if (virtualFile != null) {
myScriptName = FileUtil.toSystemDependentName(virtualFile.getPath());
}
}
@Override
public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
myScriptName = FileUtil.toSystemDependentName(oldQualifiedName);
}
};
}
if (element instanceof PyClass && (myTestType == TestType.TEST_CLASS || myTestType == TestType.TEST_METHOD) && Comparing.equal(((PyClass) element).getName(), myClassName)) {
return new RefactoringElementAdapter() {
@Override
protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
myClassName = ((PyClass) newElement).getName();
}
@Override
public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
myClassName = oldQualifiedName;
}
};
}
if (element instanceof PyFunction && Comparing.equal(((PyFunction) element).getName(), myMethodName)) {
ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(element, ScopeOwner.class);
if ((myTestType == TestType.TEST_FUNCTION && scopeOwner instanceof PyFile) || (myTestType == TestType.TEST_METHOD && scopeOwner instanceof PyClass && Comparing.equal(scopeOwner.getName(), myClassName))) {
return new RefactoringElementAdapter() {
@Override
protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
myMethodName = ((PyFunction) newElement).getName();
}
@Override
public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
final int methodIdx = oldQualifiedName.indexOf("#") + 1;
if (methodIdx > 0 && methodIdx < oldQualifiedName.length()) {
myMethodName = oldQualifiedName.substring(methodIdx);
}
}
};
}
}
}
return null;
}
Aggregations