@Nullable private static IProperty getResolvedProperty(@NotNull final XmlAttributeValue codeValue) { return CachedValuesManager.getCachedValue(codeValue, KEY, () -> { List<IProperty> allProperties = new SmartList<>(); for (PsiReference nextRef : codeValue.getReferences()) { if (nextRef instanceof PsiPolyVariantReference) { Arrays.stream(((PsiPolyVariantReference) nextRef).multiResolve(false)) .filter(ResolveResult::isValidResult) .map(ResolveResult::getElement) .map(o -> ObjectUtils.tryCast(o, IProperty.class)) .filter(Objects::nonNull) .forEach(allProperties::add); } else { Optional.ofNullable(nextRef.resolve()) .map(o -> ObjectUtils.tryCast(o, IProperty.class)) .ifPresent(allProperties::add); } } IProperty theChosenOne = chooseForLocale(allProperties); return new CachedValueProvider.Result<>(theChosenOne, PsiModificationTracker.MODIFICATION_COUNT); }); }
@Nullable @Override public List<UsageInfo> findUsages( PsiFile psiFile, PsiDirectory newParent, boolean searchInComments, boolean searchInNonJavaFiles ) { Module mod = ModuleUtilCore.findModuleForPsiElement( psiFile ); ManModule module = ManProject.getModule( mod ); PsiClass psiClass = findPsiClass( psiFile ); if( psiClass == null ) { return Collections.emptyList(); } Query<PsiReference> search = ReferencesSearch.search( psiClass, GlobalSearchScope.moduleWithDependenciesAndLibrariesScope( module.getIjModule() ) ); List<UsageInfo> usages = new ArrayList<>(); for( PsiReference ref: search.findAll() ) { usages.add( new MoveRenameUsageInfo( ref.getElement(), ref, ref.getRangeInElement().getStartOffset(), ref.getRangeInElement().getEndOffset(), psiClass, ref.resolve() == null && !(ref instanceof PsiPolyVariantReference && ((PsiPolyVariantReference)ref).multiResolve( true ).length > 0) ) ); } return usages; }
private static List<UsageInfo> findUsages( PsiElement element, PsiElement ctx ) { // Module mod = ModuleUtilCore.findModuleForPsiElement( element ); // if( mod == null ) // { // return Collections.emptyList(); // } Query<PsiReference> search = ReferencesSearch.search( element, GlobalSearchScope.moduleScope( ModuleUtilCore.findModuleForPsiElement( ctx ) ) ); List<UsageInfo> usages = new ArrayList<>(); for( PsiReference ref : search.findAll() ) { MoveRenameUsageInfo usageInfo = new MoveRenameUsageInfo( ref.getElement(), ref, ref.getRangeInElement().getStartOffset(), ref.getRangeInElement().getEndOffset(), element, ref.resolve() == null && !(ref instanceof PsiPolyVariantReference && ((PsiPolyVariantReference)ref).multiResolve( true ).length > 0) ); usages.add( usageInfo ); } return usages; }
/** * Add local import statement * * @param name reference name in corresponding import element * @param qualifier if not {@code null} form {@code from qualifier import name} will be used, otherwise {@code import name} */ private void doAddLocalImport(@NotNull final String name, @Nullable final String qualifier) { myFixture.configureByFile(getTestName(true) + ".py"); WriteCommandAction.runWriteCommandAction(myFixture.getProject(), new Runnable() { @Override public void run() { final PsiPolyVariantReference reference = PyResolveTestCase.findReferenceByMarker(myFixture.getFile()); if (qualifier != null) { AddImportHelper.addLocalFromImportStatement(reference.getElement(), qualifier, name); } else { AddImportHelper.addLocalImportStatement(reference.getElement(), name); } } }); myFixture.checkResultByFile(getTestName(true) + ".after.py"); }
private void checkReferences(PsiReference[] references) { for (PsiReference reference : references) { if (!reference.isSoft()) { if (reference.resolve() == null) { if (reference instanceof PsiPolyVariantReference) { final PsiPolyVariantReference pvr = (PsiPolyVariantReference)reference; if (pvr.multiResolve(false).length == 0) { addError(reference); } } else { addError(reference); } } } } }
public void testResolveIncludedRef2() throws Throwable { myTestFixture.copyFileToProject("include.rng"); final PsiReference ref = myTestFixture.getReferenceAtCaretPositionWithAssertion("resolve-include-ref-2.rng"); assertTrue("PolyVariantRef", ref instanceof PsiPolyVariantReference); final PsiElement element = ref.resolve(); assertNull(element); final ResolveResult[] results = ((PsiPolyVariantReference)ref).multiResolve(false); assertEquals(2, results.length); for (ResolveResult result : results) { PsiElement e = result.getElement(); assertTrue(e instanceof XmlTag); final int contentLength = ((XmlTag)e).getSubTags().length; if (e.getContainingFile() == ref.getElement().getContainingFile()) { assertEquals(1, contentLength); } else { assertEquals(0, contentLength); } } }
@Nullable private static PsiElement resolveXsltReference(PsiReference reference) { final PsiElement element = reference.resolve(); if (element != null) { return element; } if (reference instanceof PsiPolyVariantReference) { final ResolveResult[] results = ((PsiPolyVariantReference)reference).multiResolve(false); for (ResolveResult result : results) { if (result.isValidResult() && result.getElement() instanceof XsltElement) { return result.getElement(); } } } return null; }
/** * Single resolve doesnt work if we have non unique class names in project context, * so try a multiResolve and use first matched method */ @Nullable public static Method getMultiResolvedMethod(PsiReference psiReference) { // class be unique in normal case, so try this first PsiElement resolvedReference = psiReference.resolve(); if (resolvedReference instanceof Method) { return (Method) resolvedReference; } // try multiResolve if class exists twice in project if(psiReference instanceof PsiPolyVariantReference) { for(ResolveResult resolveResult : ((PsiPolyVariantReference) psiReference).multiResolve(false)) { PsiElement element = resolveResult.getElement(); if(element instanceof Method) { return (Method) element; } } } return null; }
public static List<PsiElement> resolveTargetByName(@Nls String name, @NotNull Project project, GlobalSearchScope scope) { final PsiManager psiManager = PsiManager.getInstance(project); final ArrayList<PsiElement> result = new ArrayList<>(); final Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope); for (VirtualFile virtualFile : containingFiles) { final PsiFile psiFile = psiManager.findFile(virtualFile); if (psiFile instanceof PyFile) { final PyReferenceExpression referenceExpression = PantsPsiUtil.findTargetDefinitions((PyFile)psiFile).get(name); final PsiPolyVariantReference reference = referenceExpression != null ? referenceExpression.getReference() : null; final PsiElement definition = reference != null ? reference.resolve() : null; if (definition != null) { result.add(definition); } else if (referenceExpression != null) { // at least something result.add(referenceExpression); } } } return result; }
private void doTest(@NotNull String beforeText, String... expectedResolve) { myFixture.configureByText(GitFileType.INSTANCE, beforeText); PsiPolyVariantReference reference = ((PsiPolyVariantReference) myFixture.getReferenceAtCaretPosition()); assertNotNull(reference); final VirtualFile rootFile = myFixture.getFile().getContainingDirectory().getVirtualFile(); ResolveResult[] resolveResults = reference.multiResolve(true); List<String> actualResolve = ContainerUtil.map(resolveResults, new Function<ResolveResult, String>() { @Override public String fun(ResolveResult resolveResult) { PsiElement resolveResultElement = resolveResult.getElement(); assertNotNull(resolveResultElement); assertInstanceOf(resolveResultElement, PsiFileSystemItem.class); PsiFileSystemItem fileSystemItem = (PsiFileSystemItem) resolveResultElement; return VfsUtilCore.getRelativePath(fileSystemItem.getVirtualFile(), rootFile, '/'); } }); assertContainsElements(actualResolve, expectedResolve); }
private void storeTypeManifoldReferences( @NotNull PsiElement element ) { Module mod = ModuleUtilCore.findModuleForPsiElement( element ); if( mod == null ) { return; } ManModule module = ManProject.getModule( mod ); PsiClass psiClass = findPsiClass( (PsiFileSystemItem)element, module ); if( psiClass == null ) { return; } Query<PsiReference> search = ReferencesSearch.search( psiClass, GlobalSearchScope.projectScope( mod.getProject() ) ); List<UsageInfo> usages = new ArrayList<>(); for( PsiReference ref: search.findAll() ) { usages.add( new MoveRenameUsageInfo( ref.getElement(), ref, ref.getRangeInElement().getStartOffset(), ref.getRangeInElement().getEndOffset(), element, ref.resolve() == null && !(ref instanceof PsiPolyVariantReference && ((PsiPolyVariantReference)ref).multiResolve( true ).length > 0) ) ); } _usages = usages; if( psiClass instanceof ManifoldPsiClass ) { PsiElement fakeElement = ManGotoDeclarationHandler.find( psiClass, (ManifoldPsiClass)psiClass ); if( fakeElement instanceof PsiNamedElement && isTopLevelClassDeclaration( fakeElement ) ) { _classDeclElement = (PsiNamedElement)fakeElement; } } }
@NotNull private static PsiElement tryResolving(@NotNull PyExpression expression, @NotNull TypeEvalContext context) { if (expression instanceof PyReferenceExpression) { final PyReferenceExpression referenceExpr = (PyReferenceExpression)expression; final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context); final PsiPolyVariantReference reference = referenceExpr.getReference(resolveContext); final PsiElement element = reference.resolve(); if (element instanceof PyFunction) { final PyFunction function = (PyFunction)element; if (PyUtil.isInit(function)) { final PyClass cls = function.getContainingClass(); if (cls != null) { return cls; } } } else if (element instanceof PyTargetExpression) { final PyTargetExpression targetExpr = (PyTargetExpression)element; // XXX: Requires switching from stub to AST final PyExpression assignedValue = targetExpr.findAssignedValue(); if (assignedValue != null) { return assignedValue; } } if (element != null) { return element; } } return expression; }
@Override public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) { final PsiElement contextParent = context.getParent(); if (PyInjectionUtil.getLargestStringLiteral(context) == context && contextParent instanceof PyArgumentList) { final PyExpression[] args = ((PyArgumentList)contextParent).getArguments(); int index = ArrayUtil.indexOf(args, context); PyCallExpression call = PsiTreeUtil.getParentOfType(context, PyCallExpression.class); if (call != null) { final PyExpression callee = call.getCallee(); if (callee instanceof PyReferenceExpression && canBeRegexpCall(callee)) { final PsiPolyVariantReference ref = ((PyReferenceExpression)callee).getReference(PyResolveContext.noImplicits()); if (ref != null) { final PsiElement element = ref.resolve(); if (element != null && ScopeUtil.getScopeOwner(element) instanceof PyFile && element.getContainingFile().getName().equals("re.py") && isRegexpMethod(element, index)) { final Language language = isVerbose(call) ? PythonVerboseRegexpLanguage.INSTANCE : PythonRegexpLanguage.INSTANCE; registrar.startInjecting(language); final PyInjectionUtil.InjectionResult result = PyInjectionUtil.registerStringLiteralInjection(context, registrar); if (result.isInjected()) { registrar.doneInjecting(); if (!result.isStrict()) { final PsiFile file = getInjectedFile(registrar); if (file != null) { file.putUserData(InjectedLanguageUtil.FRANKENSTEIN_INJECTION, Boolean.TRUE); } } } } } } } } }
@NotNull @Override public PsiPolyVariantReference getReference(PyResolveContext context) { if (isQualified()) { return new PyQualifiedReference(this, context); } final PsiElement importParent = PsiTreeUtil.getParentOfType(this, PyImportElement.class, PyFromImportStatement.class); if (importParent != null) { return PyImportReference.forElement(this, importParent, context); } return new PyDocReference(this, context); }
private static boolean referencesTarget(@NotNull final PyExpression expression, @NotNull final PsiElement target) { final List<PsiElement> refs = new ArrayList<PsiElement>(); expression.accept(new PyRecursiveElementVisitor() { @Override public void visitPyReferenceExpression(PyReferenceExpression node) { super.visitPyReferenceExpression(node); final PsiPolyVariantReference ref = node.getReference(); if (ref.isReferenceTo(target)) { refs.add(node); } } }); return !refs.isEmpty(); }
@Override public void visitPyReferenceExpression(PyReferenceExpression node) { final PyExceptPart exceptPart = PsiTreeUtil.getParentOfType(node, PyExceptPart.class); if (exceptPart != null) { final PyExpression exceptClass = exceptPart.getExceptClass(); if (exceptClass != null && "ImportError".equals(exceptClass.getText())) return; } final PsiPolyVariantReference reference = node.getReference(getResolveContext()); if (reference == null) return; final PsiElement resolveResult = reference.resolve(); final PyFromImportStatement importStatement = PsiTreeUtil.getParentOfType(node, PyFromImportStatement.class); if (importStatement != null) { final PsiElement element = importStatement.resolveImportSource(); if (resolveResult != null && element != resolveResult.getContainingFile()) return; } String deprecationMessage = null; if (resolveResult instanceof PyFunction) { deprecationMessage = ((PyFunction) resolveResult).getDeprecationMessage(); } else if (resolveResult instanceof PyFile) { deprecationMessage = ((PyFile)resolveResult).getDeprecationMessage(); } if (deprecationMessage != null) { ASTNode nameElement = node.getNameElement(); registerProblem(nameElement == null ? node : nameElement.getPsi(), deprecationMessage, ProblemHighlightType.LIKE_DEPRECATED); } }
@Override public void visitPyRaiseStatement(PyRaiseStatement node) { PyExpression[] expressions = node.getExpressions(); if (expressions.length == 0) { return; } PyExpression expression = expressions[0]; if (expression instanceof PyCallExpression) { PyExpression callee = ((PyCallExpression)expression).getCallee(); if (callee instanceof PyReferenceExpression) { final PsiPolyVariantReference reference = ((PyReferenceExpression)callee).getReference(getResolveContext()); if (reference == null) return; PsiElement psiElement = reference.resolve(); if (psiElement instanceof PyClass) { PyClass aClass = (PyClass) psiElement; for (PyClassLikeType type : aClass.getAncestorTypes(myTypeEvalContext)) { if (type == null) { return; } final String name = type.getName(); if (name == null || "BaseException".equals(name) || "Exception".equals(name)) { return; } } registerProblem(expression, "Exception doesn't inherit from base \'Exception\' class", new PyAddExceptionSuperClassQuickFix()); } } } }
@NotNull @Override public PsiPolyVariantReference getReference(final PyResolveContext resolveContext) { if (isQualified()) { return new PyQualifiedReference(this, resolveContext); } return new PyTargetReference(this, resolveContext); }
@Nullable @Override public PyType getType(@NotNull TypeEvalContext context, @NotNull TypeEvalContext.Key key) { final PsiPolyVariantReference reference = getReference(PyResolveContext.noImplicits().withTypeEvalContext(context)); final List<PyType> members = new ArrayList<PyType>(); for (PsiElement resolved : PyUtil.multiResolveTopPriority(reference)) { PyType res = null; if (resolved instanceof PyCallable) { res = ((PyCallable)resolved).getCallType(context, this); } if (PyTypeChecker.isUnknown(res) || res instanceof PyNoneType) { final PyExpression indexExpression = getIndexExpression(); if (indexExpression != null) { final PyType type = context.getType(getOperand()); final PyClass cls = (type instanceof PyClassType) ? ((PyClassType)type).getPyClass() : null; if (cls != null && PyABCUtil.isSubclass(cls, PyNames.MAPPING)) { return res; } if (type instanceof PySubscriptableType) { res = ((PySubscriptableType)type).getElementType(indexExpression, context); } else if (type instanceof PyCollectionType) { // TODO: Select the parameter type that matches T in Iterable[T] final List<PyType> elementTypes = ((PyCollectionType)type).getElementTypes(context); res = elementTypes.isEmpty() ? null : elementTypes.get(0); } } } members.add(res); } return PyUnionType.union(members); }
private static boolean isNotAliasedInImportElement(@NotNull PsiReference reference) { if (reference instanceof PsiPolyVariantReference) { final ResolveResult[] results = ((PsiPolyVariantReference)reference).multiResolve(false); for (ResolveResult result : results) { final PsiElement resolved = result.getElement(); if (resolved instanceof PyImportElement && ((PyImportElement)resolved).getAsName() != null) { return false; } } } return true; }
@Override public void visitPyReferenceExpression(final PyReferenceExpression node) { final PsiPolyVariantReference reference = node.getReference(); if (reference.isReferenceTo(myElementToFind)) { myDependencyFound = true; return; } // TODO: This step is member-type specific. Move to MemberManagers? if (myElementToFind instanceof PyAssignmentStatement) { final PyExpression[] targets = ((PyAssignmentStatement)myElementToFind).getTargets(); if (targets.length != 1) { return; } final PyExpression expression = targets[0]; if (reference.isReferenceTo(expression)) { myDependencyFound = true; return; } if (node.getText().equals(expression.getText())) { // Check by name also myDependencyFound = true; } return; } final PsiElement declaration = reference.resolve(); myDependencyFound = PsiTreeUtil.findFirstParent(declaration, new PsiElementCondition()) != null; }
@NotNull @Override public PsiPolyVariantReference getReference(PyResolveContext context) { if (isQualified()) { return new PyQualifiedReference(this, context); } final PsiElement importParent = PsiTreeUtil.getParentOfType(this, PyImportElement.class, PyFromImportStatement.class); if (importParent != null) { return PyImportReference.forElement(this, importParent, context); } return new IpnbPyReference(this, context); }
private static boolean isResolvable(PsiReference ref) { if (ref.resolve() != null) { return true; } if (ref instanceof PsiPolyVariantReference) { return ((PsiPolyVariantReference)ref).multiResolve(false).length > 0; } return false; }
@Override public <T extends PsiPolyVariantReference> GroovyResolveResult[] multiResolve(@NotNull T ref, boolean incomplete, ResolveCache.PolyVariantResolver<T> resolver) { ResolveResult[] results = ResolveCache.getInstance(ref.getElement().getProject()).resolveWithCaching(ref, resolver, true, incomplete); return results.length == 0 ? GroovyResolveResult.EMPTY_ARRAY : (GroovyResolveResult[])results; }
@NotNull @Override public <T extends PsiPolyVariantReference> GroovyResolveResult[] multiResolve(@NotNull final T ref, final boolean incomplete, @NotNull final ResolveCache.PolyVariantResolver<T> resolver) { return _getCachedValue(ref.getElement(), new Computable<GroovyResolveResult[]>() { @Override public GroovyResolveResult[] compute() { return (GroovyResolveResult[])resolver.resolve(ref, incomplete); } }, Pair.create(incomplete, resolver.getClass())); }
/** * Single resolve doesnt work if we have non unique class names in project context, * so try a multiResolve */ @Nullable public static Method[] getMultiResolvedMethod(PsiReference psiReference) { // class be unique in normal case, so try this first PsiElement resolvedReference = psiReference.resolve(); if (resolvedReference instanceof Method) { return new Method[] { (Method) resolvedReference }; } // try multiResolve if class exists twice in project if(psiReference instanceof PsiPolyVariantReference) { Collection<Method> methods = new HashSet<>(); for(ResolveResult resolveResult : ((PsiPolyVariantReference) psiReference).multiResolve(false)) { PsiElement element = resolveResult.getElement(); if(element instanceof Method) { methods.add((Method) element); } } if(methods.size() > 0) { return methods.toArray(new Method[methods.size()]); } } return null; }
@RequiredReadAction public static boolean isReferenceTo(PsiPolyVariantReference referenceExpression, PsiElement element) { final ResolveResult firstValidResult = CSharpResolveUtil.findValidOrFirstMaybeResult(referenceExpression.multiResolve(false)); if(firstValidResult == null) { return false; } return isReferenceTo(firstValidResult, element); }
@Nullable public static PsiFile resolveFile(XmlAttribute location, PsiFile baseFile) { if (location == null) return null; final XmlAttributeValue valueElement = location.getValueElement(); if (valueElement == null) return null; // prefer direct relative path final String value = valueElement.getValue(); final PsiFile file = resolveFile(value, baseFile); if (file != baseFile && file instanceof XmlFile) { return file; } final PsiReference[] references = valueElement.getReferences(); for (PsiReference reference : references) { final PsiElement target = reference.resolve(); if (target == null && reference instanceof PsiPolyVariantReference) { final ResolveResult[] results = ((PsiPolyVariantReference)reference).multiResolve(false); for (ResolveResult result : results) { if (result.isValidResult()) { // TODO: how to weigh/prioritize the results? final PsiElement element = result.getElement(); if (element != baseFile && element instanceof XmlFile) { return (PsiFile)target; } } } } else if (target != baseFile && target instanceof XmlFile) { return (PsiFile)target; } } return null; }
public static boolean hasBadResolve(final PsiReference reference, boolean checkSoft) { if(!checkSoft && reference.isSoft()) { return false; } if(reference instanceof PsiPolyVariantReference) { return ((PsiPolyVariantReference) reference).multiResolve(false).length == 0; } return reference.resolve() == null; }
private Collection<PsiFileSystemItem> getContext(PsiElement element, PsiFile file) { XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class); if(!myAcceptSelf && tag != null) { tag = tag.getParentTag(); } while(tag != null) { XmlAttribute base = tag.getAttribute("base", XmlUtil.XML_NAMESPACE_URI); if(base != null) { XmlAttributeValue value = base.getValueElement(); if(value != null) { PsiReference reference = value.getReference(); if(reference instanceof PsiPolyVariantReference) { ResolveResult[] results = ((PsiPolyVariantReference) reference).multiResolve(false); return ContainerUtil.map(results, result -> (PsiFileSystemItem) result.getElement()); } } } tag = tag.getParentTag(); } PsiDirectory directory = file.getContainingDirectory(); return directory == null ? Collections.<PsiFileSystemItem>emptyList() : Collections.<PsiFileSystemItem>singletonList(directory); }