From da76ef31ba81fd02391e59ff348cb66a5f9cdb7b Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Thu, 24 Aug 2017 19:49:00 +0300 Subject: [PATCH] add utility methods for Kotlin code --- .../src/com/intellij/psi/util/pointers.kt | 24 +++++++++++++++ .../src/com/intellij/psi/util/psiTreeUtil.kt | 30 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 java/java-impl/src/com/intellij/psi/util/pointers.kt create mode 100644 java/java-impl/src/com/intellij/psi/util/psiTreeUtil.kt diff --git a/java/java-impl/src/com/intellij/psi/util/pointers.kt b/java/java-impl/src/com/intellij/psi/util/pointers.kt new file mode 100644 index 000000000000..859a553fe4e2 --- /dev/null +++ b/java/java-impl/src/com/intellij/psi/util/pointers.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2000-2017 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 com.intellij.psi.util + +import com.intellij.psi.PsiElement +import com.intellij.psi.SmartPointerManager +import com.intellij.psi.SmartPsiElementPointer + +fun T.createSmartPointer(): SmartPsiElementPointer { + return SmartPointerManager.getInstance(project).createSmartPsiElementPointer(this) +} \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/psi/util/psiTreeUtil.kt b/java/java-impl/src/com/intellij/psi/util/psiTreeUtil.kt new file mode 100644 index 000000000000..a9221c950e37 --- /dev/null +++ b/java/java-impl/src/com/intellij/psi/util/psiTreeUtil.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2000-2017 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 com.intellij.psi.util + +import com.intellij.psi.PsiElement + +inline fun PsiElement.parentOfType(): T? { + return PsiTreeUtil.getParentOfType(this, T::class.java) +} + +inline fun PsiElement?.parentsOfType(): Sequence { + return parentsOfType(T::class.java) +} + +fun PsiElement?.parentsOfType(clazz: Class): Sequence { + return generateSequence(this) { it.parent }.filterIsInstance(clazz) +}