下面列出了com.intellij.psi.util.PsiTreeUtil#isAncestor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@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;
}
@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;
}
@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;
}
@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);
}
@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;
}
@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;
}
@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;
}
@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);
}
@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;
}
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;
}
@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;
}
@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);
}
@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;
}
@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);
}
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;
}
/**
* @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;
}
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;
}
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);
}
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;
}