From faa7f8fc6b3ee2a1b9aa083b938fc3c043a47066 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Fri, 4 Apr 2014 18:59:58 +0400 Subject: [PATCH] Add util class for stubbed PSI Implement methods to find parent by stub --- .../jet/lang/psi/JetStubbedPsiUtil.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/JetStubbedPsiUtil.java diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetStubbedPsiUtil.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetStubbedPsiUtil.java new file mode 100644 index 00000000000..329d716da32 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetStubbedPsiUtil.java @@ -0,0 +1,57 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.lang.psi; + +import com.intellij.psi.PsiElement; +import com.intellij.psi.stubs.StubElement; +import com.intellij.psi.util.PsiTreeUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class JetStubbedPsiUtil { + @Nullable + public static JetDeclaration getContainingDeclaration(@NotNull PsiElement element) { + return getContainingDeclaration(element, JetDeclaration.class, true); + } + + @Nullable + public static T getContainingDeclaration(@NotNull PsiElement element, @NotNull Class declarationClass) { + return getContainingDeclaration(element, declarationClass, true); + } + + @Nullable + public static T getContainingDeclaration( + @NotNull PsiElement element, + @NotNull Class declarationClass, + boolean strict + ) { + if (!strict && declarationClass.isInstance(element)) { + //noinspection unchecked + return (T) element; + } + if (element instanceof JetElementImplStub) { + StubElement stub = ((JetElementImplStub) element).getStub(); + if (stub != null) { + return stub.getParentStubOfType(declarationClass); + } + } + return PsiTreeUtil.getParentOfType(element, declarationClass, strict); + } + + private JetStubbedPsiUtil() { + } +}