Java Code Examples for com.intellij.psi.PsiElement#getReference()

The following examples show how to use com.intellij.psi.PsiElement#getReference() . 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: ChangeSignatureAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static PsiElement findTargetMember(@Nullable PsiElement element) {
  if (element == null) return null;
  final ChangeSignatureHandler fileHandler = getChangeSignatureHandler(element.getLanguage());
  if (fileHandler != null) {
    final PsiElement targetMember = fileHandler.findTargetMember(element);
    if (targetMember != null) return targetMember;
  }
  PsiReference reference = element.getReference();
  if (reference == null && element instanceof PsiNameIdentifierOwner) {
    return element;
  }
  if (reference != null) {
    return reference.resolve();
  }
  return null;
}
 
Example 2
Source File: DocumentationProvider.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public PsiElement getCustomDocumentationElement(@NotNull Editor editor, @NotNull PsiFile file, @Nullable PsiElement contextElement) {
    // When quick doc inside empty parenthesis, we want to display the function doc (github #155)
    // functionName(<caret>) ==> functionName<caret>()
    if (contextElement != null && contextElement.getParent() instanceof PsiFunctionCallParams && contextElement.getLanguage() == RmlLanguage.INSTANCE) {
        PsiElement prevSibling = contextElement.getParent().getPrevSibling();
        if (prevSibling != null) {
            PsiReference reference = prevSibling.getReference();
            if (reference != null) {
                return reference.resolve();
            }
        }
    }
    return null;
}
 
Example 3
Source File: PhpElementsUtil.java    From idea-php-advanced-autocomplete with MIT License 6 votes vote down vote up
private static String getMethodName(PsiElement caller) {

        PsiReference psiReference = caller.getReference();

        if (psiReference == null) {
            return null;
        }

        PsiElement resolvedReference = psiReference.resolve();

        if (!(resolvedReference instanceof Method)) {
            return null;
        }

        Method method = (Method)resolvedReference;
        PhpClass methodClass = method.getContainingClass();

        if (methodClass == null) {
            return null;
        }

        String className = methodClass.getName();
        String methodName = method.getName();

        return className + "::" + methodName;
    }
 
Example 4
Source File: GrammarElementRefTest.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private PsiElement resolveRefAtCaret() {
	PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

	if (elementAtCaret != null) {
		PsiReference ref = elementAtCaret.getReference();

		if (ref != null) {
			return ref.resolve();
		} else {
			fail("No reference at caret");
		}
	} else {
		fail("No element at caret");
	}

	return null;
}
 
Example 5
Source File: LazyGetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static boolean isInitializedInConstructors(@NotNull PsiElement element) {
  if (!(element instanceof PsiIdentifier)) {
    return false;
  }
  PsiElement parent = element.getParent();
  if (!(parent instanceof PsiReferenceExpression)) {
    return false;
  }
  PsiElement qualifier = ((PsiReferenceExpression) parent).getQualifier();
  if (qualifier == null) {
    return false;
  }
  PsiReference reference = qualifier.getReference();
  if (reference == null) {
    return false;
  }
  PsiElement field = reference.resolve();
  if (!(field instanceof PsiField)) {
    return false;
  }
  PsiClass containingClass = ((PsiField) field).getContainingClass();
  if (containingClass == null) {
    return false;
  }
  return isInitializedInConstructors((PsiField) field, containingClass);
}
 
Example 6
Source File: PhpDocUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
private static PsiReference getElementReference(PsiElement element) {
    if (element instanceof NewExpression) {
        return ((NewExpression) element).getClassReference();
    } else if (element instanceof FunctionReference) {
        return element.getReference();
    } else {
        return null;
    }
}
 
Example 7
Source File: PhpDocUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
private static PsiReference getElementReference(PsiElement element) {
    if (element instanceof NewExpression) {
        return ((NewExpression) element).getClassReference();
    } else if (element instanceof FunctionReference) {
        return element.getReference();
    } else {
        return null;
    }
}
 
Example 8
Source File: RenameVariableTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
private void doRename(Runnable renameLogic, String... sourceFiles) {
    myFixture.setTestDataPath(getTestDataPath() + getTestName(true));

    List<String> filenames = Lists.newArrayList(sourceFiles);
    myFixture.configureByFiles(filenames.toArray(new String[filenames.size()]));

    renameLogic.run();

    for (String filename : filenames) {
        myFixture.checkResultByFile(filename, FileUtil.getNameWithoutExtension(filename) + "_after." + FileUtilRt.getExtension(filename), false);
    }

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    Assert.assertNotNull("caret element is null", psiElement);

    while (psiElement.getReference() == null) {
        if (psiElement.getParent() == null) {
            break;
        }

        psiElement = psiElement.getParent();
    }

    PsiReference psiReference = psiElement.getReference();
    Assert.assertNotNull("target reference wasn't found", psiReference);
    Assert.assertTrue("Renamed reference wasn't found in the canonical text", psiReference.getCanonicalText().contains("a_renamed"));

    PsiElement targetVariable = psiReference.resolve();
    if (!(psiElement instanceof BashVarDef)) {
        Assert.assertNotNull("target resolve result wasn't found", targetVariable);
        Assert.assertTrue("target is not a psi function definition", targetVariable instanceof BashVarDef);
    }
}
 
Example 9
Source File: PhpElementsUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Nullable
private static String getStringValue(@Nullable PsiElement psiElement, int depth) {
    if(psiElement == null || ++depth > 5) {
        return null;
    }

    if(psiElement instanceof StringLiteralExpression) {
        String resolvedString = ((StringLiteralExpression) psiElement).getContents();
        if(StringUtils.isEmpty(resolvedString)) {
            return null;
        }

        return resolvedString;
    } else if(psiElement instanceof Field) {
        return getStringValue(((Field) psiElement).getDefaultValue(), depth);
    } else if(psiElement instanceof ClassConstantReference && "class".equals(((ClassConstantReference) psiElement).getName())) {
        // Foobar::class
        return getClassConstantPhpFqn((ClassConstantReference) psiElement);
    } else if(psiElement instanceof PhpReference) {
        PsiReference psiReference = psiElement.getReference();
        if(psiReference == null) {
            return null;
        }

        PsiElement ref = psiReference.resolve();
        if(ref instanceof PhpReference) {
            return getStringValue(psiElement, depth);
        }

        if(ref instanceof Field) {
            return getStringValue(((Field) ref).getDefaultValue());
        }
    }

    return null;
}
 
Example 10
Source File: PhpElementsUtil.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
public static String getCanonicalFuncName(PsiElement caller) {
    if (caller.getReference() instanceof MethodReference) {
        return getMethodName(caller);
    }
    else if (caller.getReference() instanceof FunctionReference) {
        return getFuncName(caller);
    }
    else if (caller instanceof NewExpression) {
        return getClassConstructName(caller);
    }

    return null;
}
 
Example 11
Source File: JSGraphQLEndpointNamedTypePsiElement.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public boolean isEquivalentTo(PsiElement another) {
	if(this == another) {
		return true;
	}
	final PsiReference reference = another.getReference();
	if(reference != null) {
		return this.equals(reference.resolve());
	}
	return super.isEquivalentTo(another);
}
 
Example 12
Source File: JSGraphQLEndpointNamedPsiElement.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public boolean isEquivalentTo(PsiElement another) {
	if(another == null) {
		return false;
	}
	if(this == another) {
		return true;
	}
	final PsiReference reference = another.getReference();
	if(reference != null) {
		return this.equals(reference.resolve());
	}
	return super.isEquivalentTo(another);
}
 
Example 13
Source File: BlueprintJavaClassReferenceTest.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
public void testReference() {
    myFixture.configureByText("test.xml", REFERENCE);
    PsiElement element = TestReferenceUtil.getParentElementAtCaret(myFixture);
    PsiReference reference = element.getReference();
    assertNotNull(reference);
    assertTrue(reference instanceof JavaClassReference);
}
 
Example 14
Source File: ProtoErrorsAnnotator.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
private void checkReference(PsiElement element) {
    PsiReference ref = element.getReference();
    if (ref == null || ref.isSoft()) {
        return;
    }
    if (ref.resolve() == null) {
        String message = message("error.unresolved.reference");
        markError(element.getNode(), null, message);
    }
}
 
Example 15
Source File: PhpElementsUtil.java    From idea-php-toolbox with MIT License 4 votes vote down vote up
@Nullable
private static String getStringValue(@Nullable PsiElement psiElement, int depth) {

    if(psiElement == null || ++depth > 5) {
        return null;
    }

    if(psiElement instanceof StringLiteralExpression) {
        String resolvedString = ((StringLiteralExpression) psiElement).getContents();
        if(StringUtils.isEmpty(resolvedString)) {
            return null;
        }

        return resolvedString;
    }

    if(psiElement instanceof Field) {
        return getStringValue(((Field) psiElement).getDefaultValue(), depth);
    }

    if(psiElement instanceof PhpReference) {

        PsiReference psiReference = psiElement.getReference();
        if(psiReference == null) {
            return null;
        }

        PsiElement ref = psiReference.resolve();
        if(ref instanceof PhpReference) {
            return getStringValue(psiElement, depth);
        }

        if(ref instanceof Field) {
            PsiElement resolved = ((Field) ref).getDefaultValue();

            if(resolved instanceof StringLiteralExpression) {
                return ((StringLiteralExpression) resolved).getContents();
            }
        }

    }

    return null;

}
 
Example 16
Source File: PsiEquivalenceUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean areElementsEquivalent(@Nonnull PsiElement element1,
                                            @Nonnull PsiElement element2,
                                            @Nullable Comparator<PsiElement> resolvedElementsComparator,
                                            @Nullable Comparator<PsiElement> leafElementsComparator,
                                            @javax.annotation.Nullable Condition<PsiElement> isElementSignificantCondition,
                                            boolean areCommentsSignificant) {
  if(element1 == element2) return true;
  ASTNode node1 = element1.getNode();
  ASTNode node2 = element2.getNode();
  if (node1 == null || node2 == null) return false;
  if (node1.getElementType() != node2.getElementType()) return false;

  PsiElement[] children1 = getFilteredChildren(element1, isElementSignificantCondition, areCommentsSignificant);
  PsiElement[] children2 = getFilteredChildren(element2, isElementSignificantCondition, areCommentsSignificant);
  if (children1.length != children2.length) return false;

  for (int i = 0; i < children1.length; i++) {
    PsiElement child1 = children1[i];
    PsiElement child2 = children2[i];
    if (!areElementsEquivalent(child1, child2, resolvedElementsComparator,
                               leafElementsComparator, isElementSignificantCondition, areCommentsSignificant)) return false;
  }

  if (children1.length == 0) {
    if (leafElementsComparator != null) {
      if (leafElementsComparator.compare(element1, element2) != 0) return false;
    }
    else {
      if (!element1.textMatches(element2)) return false;
    }
  }

  PsiReference ref1 = element1.getReference();
  if (ref1 != null) {
    PsiReference ref2 = element2.getReference();
    if (ref2 == null) return false;
    PsiElement resolved1 = ref1.resolve();
    PsiElement resolved2 = ref2.resolve();
    if (!Comparing.equal(resolved1, resolved2)
        && (resolvedElementsComparator == null || resolvedElementsComparator.compare(resolved1, resolved2) != 0)) return false;
  }
  return true;

}
 
Example 17
Source File: XQueryAnnotator.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    IElementType elementType = element.getNode().getElementType();
    if (element instanceof XQueryFunctionName) {
        duplicateFunctionErrorCreator.createDuplicateFunctionDeclarationError(holder, (XQueryFunctionName) element, (XQueryFile) element.getContainingFile());
    }
    if (element instanceof PsiComment) {
        xQDocHighlighter.highlightXQDocTags((PsiComment) element, holder);
    }
    if (isPartOfMisplacedComment(elementType)) {
        misplacedCommentHighlighter.highlight(element, holder);
    }
    if (element instanceof XQueryVarRef) {
        unresolvedVariableChecker.check((XQueryVarRef) element, holder);
    }
    if (element instanceof XQueryFunctionInvocation) {
        unresolvedFunctionChecker.check((XQueryFunctionInvocation) element, holder);
    }
    if (element instanceof XQueryXmlTagNamespace) {
        unresolvedXmlNamespaceChecker.check((XQueryXmlTagNamespace) element, holder);
    }
    if (element instanceof XQueryItemType) {
        highlight(element, holder, XQuerySyntaxHighlighter.ITEM_TYPE);
    }
    PsiElement elementParent = element.getParent();
    if (element instanceof XQueryFunctionName && elementParent instanceof XQueryFunctionCall) {
        highlight(element, holder, XQuerySyntaxHighlighter.FUNCTION_CALL);
    }
    if (element instanceof XQueryFunctionName && elementParent instanceof XQueryFunctionDecl) {
        highlight(element, holder, XQuerySyntaxHighlighter.FUNCTION_DECLARATION);
    }
    if (element instanceof XQueryVarName) {
        if (elementParent instanceof XQueryVarRef) {
            PsiReference reference = elementParent.getReference();
            if (reference != null) {
                PsiElement resolvedReference = reference.resolve();
                if (resolvedReference != null) {
                    highlightVariable((XQueryVarName) element, holder, resolvedReference.getParent());
                }
            }
        } else {
            highlightVariable((XQueryVarName) element, holder, elementParent);
        }
    }
    if (element instanceof XQueryCompatibilityAnnotation || element instanceof XQueryMarklogicAnnotation) {
        highlight(element, holder, XQuerySyntaxHighlighter.ANNOTATION);
    }
    if (element instanceof XQueryAnnotation) {
        highlight(((XQueryAnnotation) element).getAnnotationName(), holder, XQuerySyntaxHighlighter.ANNOTATION);
        highlight(new TextRange(element.getTextRange().getStartOffset(), element.getTextRange().getStartOffset() + 1), holder, XQuerySyntaxHighlighter.ANNOTATION);
    }
}
 
Example 18
Source File: PhpElementsUtil.java    From idea-php-advanced-autocomplete with MIT License 4 votes vote down vote up
private static String getFuncName(PsiElement caller) {

        PsiReference psiReference = caller.getReference();

        if (psiReference == null) {
            return null;
        }

        PsiElement resolvedReference = psiReference.resolve();

        if (!(resolvedReference instanceof Function)) {
            return null;
        }

        Function function = (Function)resolvedReference;

        return function.getName();
    }
 
Example 19
Source File: PhpElementsUtil.java    From idea-php-toolbox with MIT License 4 votes vote down vote up
@Nullable
private static String getStringValue(@Nullable PsiElement psiElement, int depth) {

    if(psiElement == null || ++depth > 5) {
        return null;
    }

    if(psiElement instanceof StringLiteralExpression) {
        String resolvedString = ((StringLiteralExpression) psiElement).getContents();
        if(StringUtils.isEmpty(resolvedString)) {
            return null;
        }

        return resolvedString;
    }

    if(psiElement instanceof Field) {
        return getStringValue(((Field) psiElement).getDefaultValue(), depth);
    }

    if(psiElement instanceof PhpReference) {

        PsiReference psiReference = psiElement.getReference();
        if(psiReference == null) {
            return null;
        }

        PsiElement ref = psiReference.resolve();
        if(ref instanceof PhpReference) {
            return getStringValue(psiElement, depth);
        }

        if(ref instanceof Field) {
            PsiElement resolved = ((Field) ref).getDefaultValue();

            if(resolved instanceof StringLiteralExpression) {
                return ((StringLiteralExpression) resolved).getContents();
            }
        }

    }

    return null;

}
 
Example 20
Source File: TablesVisitor.java    From Thinkphp5-Plugin with MIT License 4 votes vote down vote up
@Override
    public void visitElement(PsiElement element) {
        if (element instanceof VariableImpl) {  //从模型变量收集
            PsiReference reference = element.getReference();
            if (reference == null) {
                super.visitElement(element);
            } else {
                Set<String> types = ((VariableImpl) reference).getType().getTypes();
                Project project = element.getProject();
                for (String item : types) {
                    if (item.contains("\\model\\")) { //model子类
                        Collection<PhpClass> classesByFQN = PhpIndex.getInstance(project).getClassesByFQN(item);
                        for (PhpClass cls : classesByFQN) {
                            String table = Util.getTableByClass(cls, project);
                            this.visitor.visit(table, null);
                        }
                    }
                }
            }
        } else if (element instanceof FunctionReference) {   //从table, join, db, name 收集
            PsiElement[] childrens = element.getChildren();
            for (PsiElement paramList : childrens) {
                if (paramList instanceof ParameterListImpl) {
                    if (paramList.getChildren().length > 0) {
                        PsiElement param = paramList.getChildren()[0];
//                        String methodName = ((FunctionReference) element).getName();
                        if (PsiElementUtil.isFunctionReference(param, "join", 0)) {
                            String text = param.getText().replace("'", "").replace("\"", "");
                            String[] s = text.split(" ");
                            this.visitor.visit(s[0], null);
                        } else if (PsiElementUtil.isFunctionReference(param, "table", 0)) {
                            addTable(param, 0);
                        } else if (PsiElementUtil.isFunctionReference(param, "db", 0) || PsiElementUtil.isFunctionReference(param, "name", 0)) {
                            addTable(param, 1);
                        }
                    }
                } else if (paramList instanceof FunctionReference) {  //链式调用方法
                    super.visitElement(element);
                }
            }
        } else {
            super.visitElement(element);
        }
    }