Added firstOrNullIsInstance and firstIsInstance methods

This commit is contained in:
Valentin Kipyatkov
2014-11-19 22:33:24 +03:00
parent 46c393d2db
commit a0c9c0e7b2
6 changed files with 46 additions and 7 deletions
@@ -24,12 +24,12 @@ import org.jetbrains.jet.lang.psi.JetPsiFactory
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.PsiErrorElement
import org.jetbrains.jet.lang.psi.JetCallableDeclaration
import org.jetbrains.jet.utils.addToStdlib.firstOrNullIsInstance
fun getTypeReference(declaration: JetCallableDeclaration): JetTypeReference? {
return declaration.getFirstChild()!!.siblings(forward = true)
.dropWhile { it.getNode()!!.getElementType() != JetTokens.COLON }
.filterIsInstance(javaClass<JetTypeReference>())
.firstOrNull()
.firstOrNullIsInstance<JetTypeReference>()
}
fun setTypeReference(declaration: JetCallableDeclaration, addAfter: PsiElement?, typeRef: JetTypeReference?): JetTypeReference? {
@@ -32,9 +32,9 @@ import org.jetbrains.jet.lang.resolve.TypeResolver.FlexibleTypeCapabilitiesProvi
import kotlin.platform.platformStatic
import org.jetbrains.jet.storage.StorageManager
import org.jetbrains.jet.context.LazinessToken
import javax.inject.Inject
import org.jetbrains.jet.lang.resolve.lazy.LazyEntity
import org.jetbrains.jet.lang.resolve.lazy.ForceResolveUtil
import org.jetbrains.jet.utils.addToStdlib.firstOrNullIsInstance
public class TypeResolver(
private val annotationResolver: AnnotationResolver,
@@ -276,7 +276,7 @@ public class TypeResolver(
public fun resolveClass(scope: JetScope, userType: JetUserType, trace: BindingTrace): ClassifierDescriptor? {
val classifierDescriptor = qualifiedExpressionResolver.lookupDescriptorsForUserType(userType, scope, trace)
.filterIsInstance(javaClass<ClassifierDescriptor>()).firstOrNull()
.firstOrNullIsInstance<ClassifierDescriptor>()
if (classifierDescriptor != null) {
ImportsResolver.reportPlatformClassMappedToKotlin(moduleDescriptor, trace, userType, classifierDescriptor)
}
@@ -18,6 +18,7 @@ package org.jetbrains.jet.utils.addToStdlib
import java.util.HashMap
import java.util.Collections
import java.util.NoSuchElementException
deprecated("Replace with filterKeys when bootstrapped")
public fun <K, V> Map<K, V>.filterKeys_tmp(predicate: (K)->Boolean): Map<K, V> {
@@ -33,3 +34,39 @@ public fun <K, V> Map<K, V>.filterKeys_tmp(predicate: (K)->Boolean): Map<K, V> {
public fun <T: Any> T?.singletonOrEmptyList(): List<T> = if (this != null) Collections.singletonList(this) else Collections.emptyList()
public fun <T: Any> T?.singletonOrEmptySet(): Set<T> = if (this != null) Collections.singleton(this) else Collections.emptySet()
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T : Any> Stream<*>.firstOrNullIsInstance(): T? {
for (element in this) if (element is T) return element
return null
}
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T : Any> Iterable<*>.firstOrNullIsInstance(): T? {
for (element in this) if (element is T) return element
return null
}
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T : Any> Array<*>.firstOrNullIsInstance(): T? {
for (element in this) if (element is T) return element
return null
}
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T> Stream<*>.firstIsInstance(): T {
for (element in this) if (element is T) return element
throw NoSuchElementException("No element of given type found")
}
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T> Iterable<*>.firstIsInstance(): T {
for (element in this) if (element is T) return element
throw NoSuchElementException("No element of given type found")
}
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T> Array<*>.firstIsInstance(): T {
for (element in this) if (element is T) return element
throw NoSuchElementException("No element of given type found")
}
@@ -27,6 +27,7 @@ import com.intellij.psi.PsiElement
import org.jetbrains.jet.JetNodeTypes
import org.jetbrains.jet.lang.psi.psiUtil.isAncestor
import org.jetbrains.jet.lang.psi.psiUtil.isProbablyNothing
import org.jetbrains.jet.utils.addToStdlib.firstOrNullIsInstance
//TODO: do resolve anonymous object's body
@@ -402,6 +403,6 @@ class PartialBodyResolveFilter(
}
private fun JetBlockExpression.lastStatement(): JetExpression?
= getLastChild()?.siblings(forward = false)?.filterIsInstance<JetExpression>()?.firstOrNull()
= getLastChild()?.siblings(forward = false)?.firstOrNullIsInstance<JetExpression>()
}
@@ -21,14 +21,14 @@ import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiField
import com.intellij.psi.PsiClass
import org.jetbrains.jet.plugin.stubindex.PackageIndexUtil
import org.jetbrains.jet.utils.addToStdlib.firstIsInstance
// used in Upsource, what's why in idea-analysis module
public class JetShortNamesCache(private val project: com.intellij.openapi.project.Project) : com.intellij.psi.search.PsiShortNamesCache() {
class object {
public fun getKotlinInstance(project: com.intellij.openapi.project.Project): org.jetbrains.jet.plugin.caches.JetShortNamesCache
= com.intellij.openapi.extensions.Extensions.getArea(project).getExtensionPoint<com.intellij.psi.search.PsiShortNamesCache>(com.intellij.psi.search.PsiShortNamesCache.EP_NAME).getExtensions()
.filterIsInstance(javaClass<org.jetbrains.jet.plugin.caches.JetShortNamesCache>())
.first()
.firstIsInstance<JetShortNamesCache>()
}
/**
@@ -36,6 +36,7 @@ import org.jetbrains.jet.lang.resolve.java.descriptor.SamConstructorDescriptorKi
import org.jetbrains.jet.lang.resolve.scopes.DescriptorKindFilter
import org.jetbrains.jet.lang.resolve.calls.smartcasts.SmartCastUtils
import org.jetbrains.jet.lang.resolve.bindingContextUtil.getDataFlowInfo
import org.jetbrains.jet.utils.addToStdlib.firstOrNullIsInstance
class CompletionSessionConfiguration(
val completeNonImportedDeclarations: Boolean,