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,7 +29,10 @@ 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>() {
public val KEY: ID<FqName, Void> = ID.create(classOfIndex.getCanonicalName())
private val KEY_DESCRIPTOR = object : KeyDescriptor<FqName> {
override fun save(output: DataOutput, value: FqName) = output.writeUTF(value.asString()) override fun save(output: DataOutput, value: FqName) = output.writeUTF(value.asString())
override fun read(input: DataInput) = FqName(input.readUTF()) override fun read(input: DataInput) = FqName(input.readUTF())
@@ -37,9 +40,17 @@ private val KEY_DESCRIPTOR = object : KeyDescriptor<FqName> {
override fun getHashCode(value: FqName) = value.asString().hashCode() override fun getHashCode(value: FqName) = value.asString().hashCode()
override fun isEqual(val1: FqName?, val2: FqName?) = val1 == val2 override fun isEqual(val1: FqName?, val2: FqName?) = val1 == val2
} }
private fun indexer(log: Logger, f: (VirtualFile) -> FqName?) = private val LOG = Logger.getInstance(classOfIndex)
override fun getName() = KEY
override fun dependsOnFileContent() = true
override fun getKeyDescriptor() = KEY_DESCRIPTOR
protected fun indexer(f: (VirtualFile) -> FqName?): DataIndexer<FqName, Void, FileContent> =
DataIndexer<FqName, Void, FileContent> { DataIndexer<FqName, Void, FileContent> {
try { try {
val fqName = f(it.getFile()) val fqName = f(it.getFile())
@@ -51,64 +62,39 @@ private fun indexer(log: Logger, f: (VirtualFile) -> FqName?) =
} }
} }
catch (e: Throwable) { catch (e: Throwable) {
log.warn("Error while indexing file " + it.getFileName(), e) LOG.warn("Error while indexing file " + it.getFileName(), e)
emptyMap() emptyMap()
} }
} }
}
public class KotlinClassFileIndex : 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 {
public val KEY: ID<FqName, Void> = ID.create(javaClass<KotlinClassFileIndex>().getCanonicalName())
private val LOG = Logger.getInstance(javaClass<KotlinClassFileIndex>())
private val VERSION = 2 private val VERSION = 2
private val INPUT_FILTER = FileBasedIndex.InputFilter { file -> file.getFileType() == JavaClassFileType.INSTANCE } private val INDEXER = indexer() { file ->
private val INDEXER = indexer(LOG) { file ->
val kotlinClass = KotlinBinaryClassCache.getKotlinBinaryClass(file) val kotlinClass = KotlinBinaryClassCache.getKotlinBinaryClass(file)
if (kotlinClass != null && kotlinClass.getClassHeader().isCompatibleAbiVersion) kotlinClass.getClassId().asSingleFqName() else null if (kotlinClass != null && kotlinClass.getClassHeader().isCompatibleAbiVersion) kotlinClass.getClassId().asSingleFqName() else null
} }
}
} }
public class KotlinJavaScriptMetaFileIndex : ScalarIndexExtension<FqName>() { public object KotlinJavaScriptMetaFileIndex : KotlinFileIndexBase<KotlinJavaScriptMetaFileIndex>(javaClass<KotlinJavaScriptMetaFileIndex>()) {
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() == KotlinJavaScriptMetaFileType }
override fun getInputFilter() = INPUT_FILTER
override fun dependsOnFileContent() = true
override fun getVersion() = VERSION override fun getVersion() = VERSION
companion object {
public val KEY: ID<FqName, Void> = ID.create(javaClass<KotlinJavaScriptMetaFileIndex>().getCanonicalName())
private val LOG = Logger.getInstance(javaClass<KotlinJavaScriptMetaFileIndex>())
private val VERSION = 1 private val VERSION = 1
private val INPUT_FILTER = FileBasedIndex.InputFilter { file -> file.getFileType() == KotlinJavaScriptMetaFileType } private val INDEXER = indexer() { file ->
private val INDEXER = indexer(LOG) { file ->
if (file.getFileType() == KotlinJavaScriptMetaFileType) JsMetaFileUtils.getClassFqName(file) else null if (file.getFileType() == KotlinJavaScriptMetaFileType) JsMetaFileUtils.getClassFqName(file) else null
} }
}
} }