Java Code Examples for com.intellij.psi.util.PsiTreeUtil#isAncestor()

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#isAncestor() . 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: SimpleDuplicatesFinder.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
protected SimpleMatch isDuplicateFragment(@Nonnull final PsiElement candidate) {
  if (!canReplace(myReplacement, candidate)) return null;
  for (PsiElement pattern : myPattern) {
    if (PsiTreeUtil.isAncestor(pattern, candidate, false)) return null;
  }
  PsiElement sibling = candidate;
  final ArrayList<PsiElement> candidates = new ArrayList<>();
  for (int i = 0; i != myPattern.size(); ++i) {
    if (sibling == null) return null;

    candidates.add(sibling);
    sibling = PsiTreeUtil.skipSiblingsForward(sibling, PsiWhiteSpace.class, PsiComment.class);
  }
  if (myPattern.size() != candidates.size()) return null;
  if (candidates.size() <= 0) return null;
  final SimpleMatch match = new SimpleMatch(candidates.get(0), candidates.get(candidates.size() - 1));
  for (int i = 0; i < myPattern.size(); i++) {
    if (!matchPattern(myPattern.get(i), candidates.get(i), match)) return null;
  }
  return match;
}
 
Example 2
Source File: CSharpFixedStatementImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean processDeclarations(@Nonnull PsiScopeProcessor processor, @Nonnull ResolveState state, PsiElement lastParent,
		@Nonnull PsiElement place)
{
	if(lastParent == null || !PsiTreeUtil.isAncestor(this, lastParent, false))
	{
		return true;
	}

	for(DotNetVariable dotNetVariable : getVariables())
	{
		if(!processor.execute(dotNetVariable, state))
		{
			return false;
		}
	}
	return true;
}
 
Example 3
Source File: CSharpLinqExpressionImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean processDeclarations(@Nonnull PsiScopeProcessor processor,
		@Nonnull ResolveState state,
		PsiElement lastParent,
		@Nonnull PsiElement place)
{
	if(lastParent == null || !PsiTreeUtil.isAncestor(this, lastParent, false))
	{
		return true;
	}

	for(PsiElement psiElement : getChildren())
	{
		if(!psiElement.processDeclarations(processor, state, lastParent, place))
		{
			return false;
		}
	}
	return super.processDeclarations(processor, state, lastParent, place);
}
 
Example 4
Source File: CSharpCatchStatementImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean processDeclarations(@Nonnull PsiScopeProcessor processor, @Nonnull ResolveState state, PsiElement lastParent, @Nonnull PsiElement place)
{
	if(lastParent == null || !PsiTreeUtil.isAncestor(this, lastParent, false))
	{
		return true;
	}
	CSharpLocalVariable variable = getVariable();
	if(variable != null && variable.getNameIdentifier() != null)   // variable can be without name
	{
		if(!processor.execute(variable, state))
		{
			return false;
		}
	}
	return true;
}
 
Example 5
Source File: CSharpForStatementImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean processDeclarations(@Nonnull PsiScopeProcessor processor, @Nonnull ResolveState state, PsiElement lastParent, @Nonnull PsiElement
		place)
{
	if(lastParent == null || !PsiTreeUtil.isAncestor(this, lastParent, false))
	{
		return true;
	}

	for(DotNetVariable dotNetVariable : getVariables())
	{
		if(!processor.execute(dotNetVariable, state))
		{
			return false;
		}
	}
	return true;
}
 
Example 6
Source File: CSharpUsingStatementImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean processDeclarations(@Nonnull PsiScopeProcessor processor, @Nonnull ResolveState state, PsiElement lastParent,
		@Nonnull PsiElement place)
{
	if(lastParent == null || !PsiTreeUtil.isAncestor(this, lastParent, false))
	{
		return true;
	}

	DotNetVariable variable = getVariable();
	if(variable != null)
	{
		if(!processor.execute(variable, state))
		{
			return false;
		}
	}
	return true;
}
 
Example 7
Source File: CSharpLinqQueryBodyImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean processDeclarations(@Nonnull PsiScopeProcessor processor,
		@Nonnull ResolveState state,
		PsiElement lastParent,
		@Nonnull PsiElement place)
{
	if(lastParent == null || !PsiTreeUtil.isAncestor(this, lastParent, false))
	{
		return true;
	}

	for(PsiElement psiElement : getChildren())
	{
		if(!psiElement.processDeclarations(processor, state, lastParent, place))
		{
			return false;
		}
	}
	return super.processDeclarations(processor, state, lastParent, place);
}
 
Example 8
Source File: CSharpForeachStatementImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean processDeclarations(@Nonnull PsiScopeProcessor processor, @Nonnull ResolveState state, PsiElement lastParent,
		@Nonnull PsiElement place)
{
	if(lastParent == null || !PsiTreeUtil.isAncestor(this, lastParent, false))
	{
		return true;
	}
	CSharpLocalVariable variable = getVariable();
	if(variable != null)
	{
		if(!processor.execute(variable, state))
		{
			return false;
		}
	}
	return true;
}
 
Example 9
Source File: PomModelImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private Pair<PomModelAspect, PomTransaction> getBlockingTransaction(final PomModelAspect aspect, PomTransaction transaction) {
  final List<PomModelAspect> allDependants = getAllDependants(aspect);
  for (final PomModelAspect pomModelAspect : allDependants) {
    Stack<Pair<PomModelAspect, PomTransaction>> blockedAspects = myBlockedAspects.get();
    ListIterator<Pair<PomModelAspect, PomTransaction>> blocksIterator = blockedAspects.listIterator(blockedAspects.size());
    while (blocksIterator.hasPrevious()) {
      final Pair<PomModelAspect, PomTransaction> pair = blocksIterator.previous();
      if (pomModelAspect == pair.getFirst() && // aspect dependence
          PsiTreeUtil.isAncestor(getContainingFileByTree(pair.getSecond().getChangeScope()), transaction.getChangeScope(), false) // same file
      ) {
        return pair;
      }
    }
  }
  return null;
}
 
Example 10
Source File: ConfigurationFromContext.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(ConfigurationFromContext configuration1, ConfigurationFromContext configuration2) {
  if (PsiTreeUtil.isAncestor(configuration1.getSourceElement(), configuration2.getSourceElement(), true)) {
    return 1;
  }
  if (PsiTreeUtil.isAncestor(configuration2.getSourceElement(), configuration1.getSourceElement(), true)) {
    return -1;
  }
  if (!configuration1.isPreferredTo(configuration2)) {
    return 1;
  }
  if (configuration2.shouldReplace(configuration1)) {
    return 1;
  }
  if (!configuration2.isPreferredTo(configuration1)) {
    return -1;
  }
  if (configuration1.shouldReplace(configuration2)) {
    return -1;
  }
  return 0;
}
 
Example 11
Source File: LocalSearchScope.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static PsiElement scopeElementsUnion(PsiElement element1, PsiElement element2) {
  if (PsiTreeUtil.isAncestor(element1, element2, false)) return element1;
  if (PsiTreeUtil.isAncestor(element2, element1, false)) return element2;
  PsiElement commonParent = PsiTreeUtil.findCommonParent(element1, element2);
  if (commonParent == null) return null;
  return commonParent;
}
 
Example 12
Source File: CopyFilesOrDirectoriesHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static PsiDirectory getCommonParentDirectory(PsiElement[] elements) {
  PsiDirectory result = null;

  for (PsiElement element : elements) {
    PsiDirectory directory;

    if (element instanceof PsiDirectory) {
      directory = (PsiDirectory)element;
      directory = directory.getParentDirectory();
    }
    else if (element instanceof PsiFile) {
      directory = PlatformPackageUtil.getDirectory(element);
    }
    else {
      throw new IllegalArgumentException("unexpected element " + element);
    }

    if (directory == null) continue;

    if (result == null) {
      result = directory;
    }
    else {
      if (PsiTreeUtil.isAncestor(directory, result, true)) {
        result = directory;
      }
    }
  }

  return result;
}
 
Example 13
Source File: CSharpRefactoringSupportProvider.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
public static boolean mayRenameInplace(@Nonnull PsiElement elementToRename, @Nullable final PsiElement nameSuggestionContext)
{
	if(nameSuggestionContext != null && nameSuggestionContext.getContainingFile() != elementToRename.getContainingFile())
	{
		return false;
	}
	if(elementToRename instanceof CSharpTupleVariable || elementToRename instanceof CSharpTupleElementImpl)
	{
		return true;
	}
	if(elementToRename instanceof DotNetNamespaceAsElement)
	{
		return true;
	}
	if(!(elementToRename instanceof CSharpLocalVariable) && !(elementToRename instanceof DotNetParameter) && !(elementToRename instanceof
			CSharpLambdaParameter))
	{
		return false;
	}
	SearchScope useScope = PsiSearchHelper.getInstance(elementToRename.getProject()).getUseScope(elementToRename);
	if(!(useScope instanceof LocalSearchScope))
	{
		return false;
	}
	PsiElement[] scopeElements = ((LocalSearchScope) useScope).getScope();
	if(scopeElements.length > 1)
	{
		return false;    // ... and badly scoped resource variables
	}
	PsiFile containingFile = elementToRename.getContainingFile();
	return PsiTreeUtil.isAncestor(containingFile, scopeElements[0], false);
}
 
Example 14
Source File: LocalSearchScope.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static PsiElement intersectScopeElements(PsiElement element1, PsiElement element2) {
  if (PsiTreeUtil.isContextAncestor(element1, element2, false)) return element2;
  if (PsiTreeUtil.isContextAncestor(element2, element1, false)) return element1;
  if (PsiTreeUtil.isAncestor(element1, element2, false)) return element2;
  if (PsiTreeUtil.isAncestor(element2, element1, false)) return element1;
  return null;
}
 
Example 15
Source File: CheckLevelHighlightInfoHolder.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean add(@Nullable HighlightInfo info) {
  if (info == null) return false;
  PsiElement psiElement = info.psiElement;
  if (psiElement != null && !PsiTreeUtil.isAncestor(myLevel, psiElement, false)) {
    throw new RuntimeException("Info: '" + info + "' reported for the element '" + psiElement + "'; but it was at the level " + myLevel);
  }
  return myHolder.add(info);
}
 
Example 16
Source File: PsiSearchScopeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isInScope(@Nonnull LocalSearchScope local, @Nonnull PsiElement element) {
  PsiElement[] scopeElements = local.getScope();
  for (final PsiElement scopeElement : scopeElements) {
    if (PsiTreeUtil.isAncestor(scopeElement, element, false)) return true;
  }
  return false;
}
 
Example 17
Source File: CommonRefactoringUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean isAncestor(@Nonnull PsiElement resolved, @Nonnull Collection<? extends PsiElement> scopes) {
  for (final PsiElement scope : scopes) {
    if (PsiTreeUtil.isAncestor(scope, resolved, false)) return true;
  }
  return false;
}
 
Example 18
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;
}
 
Example 19
Source File: HaxeExpressionUtil.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
public static boolean isOnAssignmentLeftHand(@NotNull HaxeExpression expr) {
  final PsiElement parent = PsiTreeUtil.skipParentsOfType(expr, HaxeParenthesizedExpression.class);
  return parent instanceof HaxeAssignExpression &&
         PsiTreeUtil.isAncestor(((HaxeAssignExpression)parent).getExpressionList().get(0), expr, false);
}
 
Example 20
Source File: HaxeExpressionUtil.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
public static boolean isAccessedForReading(@NotNull HaxeExpression expr) {
  final PsiElement parent = PsiTreeUtil.skipParentsOfType(expr, HaxeParenthesizedExpression.class);
  return !(parent instanceof HaxeAssignExpression) ||
         !PsiTreeUtil.isAncestor(((HaxeAssignExpression)parent).getExpressionList().get(0), expr, false) ||
         getAssignOperationElementType((HaxeAssignExpression)parent) != HaxeTokenTypes.OASSIGN;
}