com.intellij.psi.PsiPolyVariantReference Java Examples

The following examples show how to use com.intellij.psi.PsiPolyVariantReference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Symfony2InterfacesUtil.java    From Thinkphp5-Plugin with MIT License 6 votes vote down vote up
/**
 * 根据方法引用获取方法
 * 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) {
    if (psiReference == null) return null;
    // 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;
}
 
Example #2
Source File: TestReferenceUtil.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
public static List<PsiElement> resolveReference(PsiElement element) {
    PsiReference[] references = element.getReferences();
    Assert.assertEquals(1, references.length);
    PsiReference reference = references[0];
    if (reference instanceof PsiPolyVariantReference) {
        ResolveResult[] results = ((PsiPolyVariantReference) reference).multiResolve(false);
        return Arrays.stream(results)
            .map(ResolveResult::getElement)
            .filter(Objects::nonNull)
            .map(TestReferenceUtil::getPsiElement)
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
    } else {
        return Stream.of(getPsiElement(reference.resolve()))
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
    }
}
 
Example #3
Source File: Symfony2InterfacesUtil.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: PantsTargetIndex.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #5
Source File: ResolvingTest.java    From idea-gitignore with MIT License 6 votes vote down vote up
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);
}
 
Example #6
Source File: Symfony2InterfacesUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #7
Source File: Symfony2InterfacesUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #8
Source File: CSharpReferenceExpressionImplUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #9
Source File: CamelBeanMethodReferenceTest.java    From camel-idea-plugin with Apache License 2.0 4 votes vote down vote up
public void testCamelNoMethodReference() {
    myFixture.configureByFiles("CompleteJavaBeanRoute4TestData.java");
    PsiElement element = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();
    assertEquals(0, ((PsiPolyVariantReference) element.getReferences()[0]).multiResolve(false).length);
}
 
Example #10
Source File: XQueryParameterInfoHandler.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
private ResolveResult[] getFunctionWithAllArities(XQueryFunctionCall functionCall) {
    PsiReference reference = functionCall.getReference();
    return ((PsiPolyVariantReference) reference).multiResolve(true);
}