From 48148fda6935744a41c379caec20764185a0e77f Mon Sep 17 00:00:00 2001 From: Mikhail Mazurkevich Date: Wed, 19 Feb 2020 19:47:15 +0300 Subject: [PATCH] [intellij-sh] Improve resolving by replace getPrevSibling to direct children iteration GitOrigin-RevId: 3a1067621348ce96c51719c5a376a3f67a33e999 --- .../ShFunctionDeclarationProcessor.java | 2 +- .../src/com/intellij/sh/psi/ResolveUtil.java | 22 ++++++++++--------- .../sh/src/com/intellij/sh/psi/ShFile.java | 2 +- .../psi/impl/ShLazyParseablePsiElement.java | 2 +- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/plugins/sh/src/com/intellij/sh/codeInsight/processor/ShFunctionDeclarationProcessor.java b/plugins/sh/src/com/intellij/sh/codeInsight/processor/ShFunctionDeclarationProcessor.java index 436c7964174b..fc6b03860192 100644 --- a/plugins/sh/src/com/intellij/sh/codeInsight/processor/ShFunctionDeclarationProcessor.java +++ b/plugins/sh/src/com/intellij/sh/codeInsight/processor/ShFunctionDeclarationProcessor.java @@ -13,7 +13,7 @@ public class ShFunctionDeclarationProcessor implements PsiScopeProcessor { private final String myFunctionName; private ShFunctionName myResult; - public ShFunctionDeclarationProcessor(String name) { + public ShFunctionDeclarationProcessor(@NotNull String name) { myFunctionName = name; } diff --git a/plugins/sh/src/com/intellij/sh/psi/ResolveUtil.java b/plugins/sh/src/com/intellij/sh/psi/ResolveUtil.java index 95f48f0fa928..2530ad3c272b 100644 --- a/plugins/sh/src/com/intellij/sh/psi/ResolveUtil.java +++ b/plugins/sh/src/com/intellij/sh/psi/ResolveUtil.java @@ -22,9 +22,13 @@ public class ResolveUtil { public static boolean processFunctionDeclarations(@Nullable PsiElement element, @NotNull TextRange lastParentTextRange, @NotNull PsiScopeProcessor processor, @NotNull ResolveState state) { if (element == null) return true; - for (PsiElement e = element; e != null; e = e.getPrevSibling()) { - if (violateTextRangeRestrictions(e.getTextRange(), lastParentTextRange)) continue; - if (!processor.execute(e, state) || (!violateRestrictions(e) && !processFunctionDeclarations(e.getLastChild(), lastParentTextRange, + if (!processor.execute(element, state)) return false; + PsiElement[] children = element.getChildren(); + if (children.length == 0) return true; + for (int i = children.length - 1; i >= 0; i--) { + PsiElement child = children[i]; + if (violateTextRangeRestrictions(child.getTextRange(), lastParentTextRange)) continue; + if (!processor.execute(child, state) || (!violateRestrictions(child) && !processFunctionDeclarations(child.getLastChild(), lastParentTextRange, processor, state))) { return false; } @@ -37,13 +41,11 @@ public class ResolveUtil { @NotNull ResolveState substitutor, @Nullable PsiElement lastParent, @NotNull PsiElement place) { - PsiElement run = lastParent == null ? element.getLastChild() : lastParent.getPrevSibling(); - - while (run != null) { - if (run instanceof ShCompositeElement && !run.processDeclarations(processor, substitutor, element, place)) { - return false; - } - run = run.getPrevSibling(); + PsiElement[] children = element.getChildren(); + if (children.length == 0) return true; + for (int i = children.length - 1; i >= 0; i--) { + PsiElement child = children[i]; + if (!child.processDeclarations(processor, substitutor, element, place)) return false; } return true; } diff --git a/plugins/sh/src/com/intellij/sh/psi/ShFile.java b/plugins/sh/src/com/intellij/sh/psi/ShFile.java index f310de94c5a9..4f9f073b98fc 100644 --- a/plugins/sh/src/com/intellij/sh/psi/ShFile.java +++ b/plugins/sh/src/com/intellij/sh/psi/ShFile.java @@ -32,7 +32,7 @@ public class ShFile extends PsiFileBase { @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) { - return ResolveUtil.processFunctionDeclarations(this.getLastChild(), place.getTextRange(), processor, state); + return ResolveUtil.processFunctionDeclarations(this, place.getTextRange(), processor, state); } @Nullable diff --git a/plugins/sh/src/com/intellij/sh/psi/impl/ShLazyParseablePsiElement.java b/plugins/sh/src/com/intellij/sh/psi/impl/ShLazyParseablePsiElement.java index 2f39b89601da..4bd0c89fdc4e 100644 --- a/plugins/sh/src/com/intellij/sh/psi/impl/ShLazyParseablePsiElement.java +++ b/plugins/sh/src/com/intellij/sh/psi/impl/ShLazyParseablePsiElement.java @@ -35,7 +35,7 @@ public abstract class ShLazyParseablePsiElement extends LazyParseablePsiElement @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) { - return ResolveUtil.processFunctionDeclarations(this.getLastChild(), place.getTextRange(), processor, state); + return ResolveUtil.processFunctionDeclarations(this, place.getTextRange(), processor, state); } @Override