From a8642c8636476145d3e15ceebd153f2e626d5629 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 21 May 2014 18:35:27 +0400 Subject: [PATCH] Refatoring: rewrite in PsiTreeUtil style --- .../jetbrains/jet/lang/psi/JetPsiUtil.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java index bd829f69e3e..540517c5679 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * Copyright 2010-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.codeInsight.CommentUtilCore; import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.JetNodeTypes; @@ -387,22 +388,18 @@ public class JetPsiUtil { } @Nullable + @Contract("null, _ -> null") public static PsiElement getTopmostParentOfTypes(@Nullable PsiElement element, @NotNull Class... parentTypes) { - if (element == null) { - return null; + PsiElement answer = PsiTreeUtil.getParentOfType(element, parentTypes); + + do { + PsiElement next = PsiTreeUtil.getParentOfType(answer, parentTypes); + if (next == null) break; + answer = next; } + while (true); - PsiElement result = null; - PsiElement parent = element.getParent(); - while (parent != null) { - if (PsiTreeUtil.instanceOf(parent, parentTypes)) { - result = parent; - } - - parent = parent.getParent(); - } - - return result; + return answer; } public static boolean isNullConstant(@NotNull JetExpression expression) {