Minor: extract common logic from KotlinClassFileIndex, KotlinJavaScriptMetaFileIndex

This commit is contained in:
Michael Nedzelsky
2015-06-10 00:34:49 +03:00
parent 76e501c7ea
commit 12d7187394
@@ -29,86 +29,72 @@ import java.io.DataInput
import java.io.DataOutput import java.io.DataOutput
import java.util.Collections import java.util.Collections
private val KEY_DESCRIPTOR = object : KeyDescriptor<FqName> { abstract class KotlinFileIndexBase<T>(private val classOfIndex: Class<T>) : ScalarIndexExtension<FqName>() {
override fun save(output: DataOutput, value: FqName) = output.writeUTF(value.asString()) public val KEY: ID<FqName, Void> = ID.create(classOfIndex.getCanonicalName())
override fun read(input: DataInput) = FqName(input.readUTF()) private val KEY_DESCRIPTOR = object : KeyDescriptor<FqName> {
override fun save(output: DataOutput, value: FqName) = output.writeUTF(value.asString())
override fun getHashCode(value: FqName) = value.asString().hashCode() override fun read(input: DataInput) = FqName(input.readUTF())
override fun isEqual(val1: FqName?, val2: FqName?) = val1 == val2 override fun getHashCode(value: FqName) = value.asString().hashCode()
}
private fun indexer(log: Logger, f: (VirtualFile) -> FqName?) = override fun isEqual(val1: FqName?, val2: FqName?) = val1 == val2
DataIndexer<FqName, Void, FileContent> {
try {
val fqName = f(it.getFile())
if (fqName != null) {
Collections.singletonMap<FqName, Void>(fqName, null)
}
else {
emptyMap()
}
}
catch (e: Throwable) {
log.warn("Error while indexing file " + it.getFileName(), e)
emptyMap()
}
} }
public class KotlinClassFileIndex : ScalarIndexExtension<FqName>() { private val LOG = Logger.getInstance(classOfIndex)
override fun getName() = KEY override fun getName() = KEY
override fun getIndexer() = INDEXER
override fun getKeyDescriptor() = KEY_DESCRIPTOR
override fun getInputFilter() = INPUT_FILTER
override fun dependsOnFileContent() = true override fun dependsOnFileContent() = true
override fun getVersion() = VERSION override fun getKeyDescriptor() = KEY_DESCRIPTOR
companion object { protected fun indexer(f: (VirtualFile) -> FqName?): DataIndexer<FqName, Void, FileContent> =
public val KEY: ID<FqName, Void> = ID.create(javaClass<KotlinClassFileIndex>().getCanonicalName()) DataIndexer<FqName, Void, FileContent> {
try {
private val LOG = Logger.getInstance(javaClass<KotlinClassFileIndex>()) val fqName = f(it.getFile())
if (fqName != null) {
private val VERSION = 2 Collections.singletonMap<FqName, Void>(fqName, null)
}
private val INPUT_FILTER = FileBasedIndex.InputFilter { file -> file.getFileType() == JavaClassFileType.INSTANCE } else {
emptyMap()
private val INDEXER = indexer(LOG) { file -> }
val kotlinClass = KotlinBinaryClassCache.getKotlinBinaryClass(file) }
if (kotlinClass != null && kotlinClass.getClassHeader().isCompatibleAbiVersion) kotlinClass.getClassId().asSingleFqName() else null catch (e: Throwable) {
} LOG.warn("Error while indexing file " + it.getFileName(), e)
} emptyMap()
}
}
} }
public class KotlinJavaScriptMetaFileIndex : ScalarIndexExtension<FqName>() { public object KotlinClassFileIndex : KotlinFileIndexBase<KotlinClassFileIndex>(javaClass<KotlinClassFileIndex>()) {
override fun getName() = KEY
override fun getIndexer() = INDEXER override fun getIndexer() = INDEXER
override fun getKeyDescriptor() = KEY_DESCRIPTOR override fun getInputFilter() = FileBasedIndex.InputFilter { file -> file.getFileType() == JavaClassFileType.INSTANCE }
override fun getInputFilter() = INPUT_FILTER
override fun dependsOnFileContent() = true
override fun getVersion() = VERSION override fun getVersion() = VERSION
companion object { private val VERSION = 2
public val KEY: ID<FqName, Void> = ID.create(javaClass<KotlinJavaScriptMetaFileIndex>().getCanonicalName())
private val LOG = Logger.getInstance(javaClass<KotlinJavaScriptMetaFileIndex>()) private val INDEXER = indexer() { file ->
val kotlinClass = KotlinBinaryClassCache.getKotlinBinaryClass(file)
private val VERSION = 1 if (kotlinClass != null && kotlinClass.getClassHeader().isCompatibleAbiVersion) kotlinClass.getClassId().asSingleFqName() else null
}
private val INPUT_FILTER = FileBasedIndex.InputFilter { file -> file.getFileType() == KotlinJavaScriptMetaFileType } }
private val INDEXER = indexer(LOG) { file -> public object KotlinJavaScriptMetaFileIndex : KotlinFileIndexBase<KotlinJavaScriptMetaFileIndex>(javaClass<KotlinJavaScriptMetaFileIndex>()) {
if (file.getFileType() == KotlinJavaScriptMetaFileType) JsMetaFileUtils.getClassFqName(file) else null
} override fun getIndexer() = INDEXER
override fun getInputFilter() = FileBasedIndex.InputFilter { file -> file.getFileType() == KotlinJavaScriptMetaFileType }
override fun getVersion() = VERSION
private val VERSION = 1
private val INDEXER = indexer() { file ->
if (file.getFileType() == KotlinJavaScriptMetaFileType) JsMetaFileUtils.getClassFqName(file) else null
} }
} }