ScriptTemplatesClassRootsIndex: inc version and minor fixes
Content was changed, so we should force reindex. Minor changes: - There is no need to save a string, we can just save nothing by using the Unit value (see UnitKey.save and UnitKey.read) - Unit can be shared between all indexes, so we can extract UnitKey object - It would be good to extract abstract class FileListIndex: - for better readability: separate the logic of the specific index from the common FileListIndex implementation - someone can use it also - mapOf (Unit to null) will create singletonMap itself and more readable - ScriptTemplatesClassRootsIndex.KEY perhaps better to call NAME
This commit is contained in:
+2
-2
@@ -78,8 +78,8 @@ abstract class AbstractScriptTemplatesFromDependenciesTest : HeavyPlatformTestCa
|
|||||||
}
|
}
|
||||||
|
|
||||||
val roots: Collection<VirtualFile> = FileBasedIndex.getInstance().getContainingFiles(
|
val roots: Collection<VirtualFile> = FileBasedIndex.getInstance().getContainingFiles(
|
||||||
ScriptTemplatesClassRootsIndex.KEY,
|
ScriptTemplatesClassRootsIndex.NAME,
|
||||||
ScriptTemplatesClassRootsIndex.VALUE,
|
Unit,
|
||||||
GlobalSearchScope.allScope(project)
|
GlobalSearchScope.allScope(project)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -13,33 +13,38 @@ import org.jetbrains.kotlin.scripting.definitions.SCRIPT_DEFINITION_MARKERS_EXTE
|
|||||||
import org.jetbrains.kotlin.scripting.definitions.SCRIPT_DEFINITION_MARKERS_PATH
|
import org.jetbrains.kotlin.scripting.definitions.SCRIPT_DEFINITION_MARKERS_PATH
|
||||||
import java.io.DataInput
|
import java.io.DataInput
|
||||||
import java.io.DataOutput
|
import java.io.DataOutput
|
||||||
import java.util.*
|
|
||||||
|
object UnitKey : KeyDescriptor<Unit> {
|
||||||
|
override fun getHashCode(value: Unit) = 0
|
||||||
|
override fun isEqual(val1: Unit, val2: Unit) = true
|
||||||
|
override fun save(out: DataOutput, value: Unit?) = Unit
|
||||||
|
override fun read(`in`: DataInput) = Unit
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class FileListIndex :
|
||||||
|
ScalarIndexExtension<Unit>(),
|
||||||
|
DataIndexer<Unit, Void, FileContent> {
|
||||||
|
|
||||||
|
override fun getIndexer() = this
|
||||||
|
override fun getKeyDescriptor() = UnitKey
|
||||||
|
override fun dependsOnFileContent() = false
|
||||||
|
override fun map(inputData: FileContent) = mapOf(Unit to null)
|
||||||
|
}
|
||||||
|
|
||||||
class ScriptTemplatesClassRootsIndex :
|
class ScriptTemplatesClassRootsIndex :
|
||||||
ScalarIndexExtension<String>(),
|
FileListIndex(),
|
||||||
FileBasedIndex.InputFilter, KeyDescriptor<String>,
|
FileBasedIndex.InputFilter {
|
||||||
DataIndexer<String, Void, FileContent> {
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val KEY = ID.create<String, Void>(ScriptTemplatesClassRootsIndex::class.java.canonicalName)
|
val NAME = ID.create<Unit, Void>(ScriptTemplatesClassRootsIndex::class.java.canonicalName)
|
||||||
const val VALUE = "MY_VALUE"
|
const val VALUE = "MY_VALUE"
|
||||||
|
|
||||||
private val suffix = SCRIPT_DEFINITION_MARKERS_PATH.removeSuffix("/")
|
private val suffix = SCRIPT_DEFINITION_MARKERS_PATH.removeSuffix("/")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getName(): ID<String, Void> = KEY
|
override fun getName() = NAME
|
||||||
|
override fun getVersion(): Int = 3
|
||||||
override fun getIndexer(): DataIndexer<String, Void, FileContent> = this
|
|
||||||
|
|
||||||
override fun getKeyDescriptor(): KeyDescriptor<String> = this
|
|
||||||
|
|
||||||
override fun getInputFilter(): FileBasedIndex.InputFilter = this
|
|
||||||
|
|
||||||
override fun dependsOnFileContent() = false
|
|
||||||
|
|
||||||
override fun getVersion(): Int = 2
|
|
||||||
|
|
||||||
override fun indexDirectories(): Boolean = false
|
override fun indexDirectories(): Boolean = false
|
||||||
|
override fun getInputFilter(): FileBasedIndex.InputFilter = this
|
||||||
|
|
||||||
override fun acceptInput(file: VirtualFile): Boolean {
|
override fun acceptInput(file: VirtualFile): Boolean {
|
||||||
val parent = file.parent ?: return false
|
val parent = file.parent ?: return false
|
||||||
@@ -47,18 +52,4 @@ class ScriptTemplatesClassRootsIndex :
|
|||||||
&& parent.path.endsWith(suffix)
|
&& parent.path.endsWith(suffix)
|
||||||
&& file.path.endsWith(SCRIPT_DEFINITION_MARKERS_EXTENSION_WITH_DOT)
|
&& file.path.endsWith(SCRIPT_DEFINITION_MARKERS_EXTENSION_WITH_DOT)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun save(out: DataOutput, value: String) = IOUtil.writeUTF(out, value)
|
|
||||||
|
|
||||||
override fun read(input: DataInput): String? = IOUtil.readUTF(input)
|
|
||||||
|
|
||||||
override fun getHashCode(value: String): Int = value.hashCode()
|
|
||||||
|
|
||||||
override fun isEqual(val1: String?, val2: String?): Boolean {
|
|
||||||
return val1 == val2
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun map(inputData: FileContent): Map<String?, Void?> {
|
|
||||||
return Collections.singletonMap(VALUE, null)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+2
-2
@@ -106,8 +106,8 @@ class ScriptTemplatesFromDependenciesProvider(private val project: Project) : Sc
|
|||||||
ReadAction
|
ReadAction
|
||||||
.nonBlocking<List<VirtualFile>> {
|
.nonBlocking<List<VirtualFile>> {
|
||||||
FileBasedIndex.getInstance().getContainingFiles(
|
FileBasedIndex.getInstance().getContainingFiles(
|
||||||
ScriptTemplatesClassRootsIndex.KEY,
|
ScriptTemplatesClassRootsIndex.NAME,
|
||||||
ScriptTemplatesClassRootsIndex.VALUE,
|
Unit,
|
||||||
GlobalSearchScope.allScope(project)
|
GlobalSearchScope.allScope(project)
|
||||||
).filterNotNull()
|
).filterNotNull()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user