From 1dd101f362d1316487983c87d9b119953e7906da Mon Sep 17 00:00:00 2001 From: Michael Nedzelsky Date: Tue, 9 Jun 2015 16:29:31 +0300 Subject: [PATCH] add KotlinJavaScriptAbiVersionIndex --- idea/src/META-INF/plugin.xml | 1 + .../KotlinJavaScriptAbiVersionIndex.kt | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/versions/KotlinJavaScriptAbiVersionIndex.kt diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index ccca40e7c8d..e5c1ab7a2b7 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -477,6 +477,7 @@ + diff --git a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinJavaScriptAbiVersionIndex.kt b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinJavaScriptAbiVersionIndex.kt new file mode 100644 index 00000000000..79b132155a5 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinJavaScriptAbiVersionIndex.kt @@ -0,0 +1,74 @@ +/* + * Copyright 2010-2015 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 org.jetbrains.kotlin.idea.versions + +import com.google.common.collect.Maps +import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.vfs.VfsUtilCore +import com.intellij.util.indexing.* +import com.intellij.util.io.ExternalIntegerKeyDescriptor +import org.jetbrains.kotlin.js.JavaScript +import org.jetbrains.kotlin.load.java.AbiVersionUtil +import org.jetbrains.kotlin.utils.KotlinJavascriptMetadata +import org.jetbrains.kotlin.utils.KotlinJavascriptMetadataUtils +import java.util.ArrayList + +/** + * Important! This is not a stub-based index. And it has its own version + */ +public object KotlinJavaScriptAbiVersionIndex : ScalarIndexExtension() { + + override fun getName() = ID.create(javaClass().getCanonicalName()) + + override fun getIndexer() = INDEXER + + override fun getKeyDescriptor() = ExternalIntegerKeyDescriptor() + + override fun getInputFilter() = FileBasedIndex.InputFilter() { file -> JavaScript.EXTENSION == file.getExtension() } + + override fun dependsOnFileContent() = true + + override fun getVersion() = VERSION + + private val VERSION = 1 + + private val LOG = Logger.getInstance(javaClass()) + + private val INDEXER = DataIndexer() { inputData: FileContent -> + val result = Maps.newHashMap() + + try { + val text = VfsUtilCore.loadText(inputData.getFile()) + val metadataList = ArrayList() + KotlinJavascriptMetadataUtils.parseMetadata(text, metadataList) + for (metadata in metadataList) { + if (KotlinJavascriptMetadataUtils.isAbiVersionCompatible(metadata.abiVersion)) { + result.put(metadata.abiVersion, null) + } + else { + // Version is set to something weird + result.put(AbiVersionUtil.INVALID_VERSION, null) + } + } + } + catch (e: Throwable) { + LOG.warn("Could not index ABI version for file " + inputData.getFile() + ": " + e.getMessage()) + } + + result + } +}