Java Code Examples for com.intellij.psi.PsiFile#isEquivalentTo()

The following examples show how to use com.intellij.psi.PsiFile#isEquivalentTo() . 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: BashRefactoringSupport.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isInplaceRenameAvailable(@NotNull PsiElement element, PsiElement context) {
    PsiFile fileContext = BashPsiUtils.findFileContext(element);
    if (context == null || fileContext == null || !fileContext.isEquivalentTo(BashPsiUtils.findFileContext(context))) {
        return false;
    }

    return (element instanceof BashVarDef) ||
            (element instanceof BashFunctionDef) ||
            (element instanceof BashVar) ||
            (element instanceof BashHereDocMarker) ||
            (element instanceof BashFileReference);
}
 
Example 2
Source File: BashResolveUtil.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
/**
 * @return true if the definition of this variable is not child of a conditional command or loop.
 */
public static boolean hasStaticVarDefPath(BashVar bashVar) {
    BashReference reference = bashVar.getNeighborhoodReference();
    if (reference == null) {
        return false;
    }

    PsiElement closestDef = reference.resolve();
    if (closestDef == null) {
        return false;
    }

    // if the closest def is in a different def scope, then we can't handle that
    // (e.g. var is top-level, def is in a function or var is in a function and def in another function, etc.)
    BashFunctionDef varScope = BashPsiUtils.findNextVarDefFunctionDefScope(bashVar);
    BashFunctionDef defScope = BashPsiUtils.findNextVarDefFunctionDefScope(closestDef);
    if (varScope == null && defScope != null) {
        return false;
    }

    // we can't handle different functions as scope
    if (varScope != null && !varScope.isEquivalentTo(defScope)) {
        return false;
    }

    // atm we can't handle different files
    PsiFile psiFile = bashVar.getContainingFile();
    if (varScope == null && !psiFile.isEquivalentTo(closestDef.getContainingFile())) {
        return false;
    }

    Collection<BashVarDef> allDefs = StubIndex.getElements(BashVarDefIndex.KEY, bashVar.getReferenceName(), bashVar.getProject(), GlobalSearchScope.fileScope(psiFile), BashVarDef.class);
    for (BashVarDef candidateDef : allDefs) {
        ProgressManager.checkCanceled();

        // skip var defs which are not in our own def scope
        BashFunctionDef scope = BashPsiUtils.findNextVarDefFunctionDefScope(candidateDef);
        if (varScope != null && !varScope.isEquivalentTo(scope)) {
            continue;
        }

        // it's not a static path if the var def is in a conditional block or loop and if our var is not
        PsiElement parent = PsiTreeUtil.findFirstParent(candidateDef, psi -> psi instanceof BashConditionalBlock || psi instanceof BashLoop);
        if (parent != null && !PsiTreeUtil.isAncestor(parent, bashVar, true)) {
            return false;
        }
    }

    return true;
}