From 803e95364c99d547d3dc019c882501514acd22b4 Mon Sep 17 00:00:00 2001 From: Michael Nedzelsky Date: Thu, 5 Mar 2015 15:57:01 +0300 Subject: [PATCH] add KotlinJavascriptMetaFileDecompiler and support for .meta files --- .../kotlin/idea/decompiler/JetClsFile.kt | 39 +----------- .../idea/decompiler/KotlinClsFileBase.kt | 59 +++++++++++++++++++ .../decompiler/KotlinJavascriptMetaFile.kt | 27 +++++++++ .../KotlinJavascriptMetaFileDecompiler.java | 55 +++++++++++++++++ .../KotlinJavascriptMetaFileViewProvider.kt | 48 +++++++++++++++ idea/src/META-INF/plugin.xml | 1 + 6 files changed, 193 insertions(+), 36 deletions(-) create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinClsFileBase.kt create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFile.kt create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFileDecompiler.java create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFileViewProvider.kt diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/JetClsFile.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/JetClsFile.kt index 6dfd7a0347d..02afb1643ec 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/JetClsFile.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/JetClsFile.kt @@ -16,45 +16,12 @@ package org.jetbrains.kotlin.idea.decompiler -import com.intellij.openapi.util.TextRange -import com.intellij.psi.util.PsiTreeUtil -import org.jetbrains.annotations.TestOnly -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.psi.JetDeclaration import org.jetbrains.kotlin.idea.decompiler.textBuilder.buildDecompiledText -import org.jetbrains.kotlin.idea.decompiler.textBuilder.descriptorToKey +import org.jetbrains.kotlin.idea.decompiler.textBuilder.DecompiledText import org.jetbrains.kotlin.utils.concurrent.block.LockedClearableLazyValue -import org.jetbrains.kotlin.psi.JetFile -public class JetClsFile(val provider: JetClassFileViewProvider) : JetFile(provider, true) { - private val decompiledText = LockedClearableLazyValue(Any()) { +public class JetClsFile(val provider: JetClassFileViewProvider) : KotlinClsFileBase(provider) { + protected override val decompiledText: LockedClearableLazyValue = LockedClearableLazyValue(Any()) { buildDecompiledText(getVirtualFile()) } - - public fun getDeclarationForDescriptor(descriptor: DeclarationDescriptor): JetDeclaration? { - val key = descriptorToKey(descriptor.getOriginal()) - - val range = decompiledText.get().renderedDescriptorsToRange[key] - return if (range != null) { - PsiTreeUtil.findElementOfClassAtRange(this, range.getStartOffset(), range.getEndOffset(), javaClass()) - } - else { - null - } - } - - override fun getText(): String? { - return decompiledText.get().text - } - - override fun onContentReload() { - super.onContentReload() - - decompiledText.drop() - } - - TestOnly - fun getRenderedDescriptorsToRange(): Map { - return decompiledText.get().renderedDescriptorsToRange - } } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinClsFileBase.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinClsFileBase.kt new file mode 100644 index 00000000000..00665da80e0 --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinClsFileBase.kt @@ -0,0 +1,59 @@ +/* + * 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.decompiler + +import com.intellij.openapi.util.TextRange +import com.intellij.psi.FileViewProvider +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.annotations.TestOnly +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.idea.decompiler.textBuilder.DecompiledText +import org.jetbrains.kotlin.psi.JetDeclaration +import org.jetbrains.kotlin.idea.decompiler.textBuilder.descriptorToKey +import org.jetbrains.kotlin.utils.concurrent.block.LockedClearableLazyValue +import org.jetbrains.kotlin.psi.JetFile + +public abstract class KotlinClsFileBase(provider: FileViewProvider) : JetFile(provider, true) { + protected abstract val decompiledText: LockedClearableLazyValue + + public fun getDeclarationForDescriptor(descriptor: DeclarationDescriptor): JetDeclaration? { + val key = descriptorToKey(descriptor.getOriginal()) + + val range = decompiledText.get().renderedDescriptorsToRange[key] + return if (range != null) { + PsiTreeUtil.findElementOfClassAtRange(this, range.getStartOffset(), range.getEndOffset(), javaClass()) + } + else { + null + } + } + + override fun getText(): String? { + return decompiledText.get().text + } + + override fun onContentReload() { + super.onContentReload() + + decompiledText.drop() + } + + TestOnly + fun getRenderedDescriptorsToRange(): Map { + return decompiledText.get().renderedDescriptorsToRange + } +} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFile.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFile.kt new file mode 100644 index 00000000000..a74595f799a --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFile.kt @@ -0,0 +1,27 @@ +/* + * 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.decompiler + +import org.jetbrains.kotlin.idea.decompiler.textBuilder.buildDecompiledTextFromJsMetadata +import org.jetbrains.kotlin.idea.decompiler.textBuilder.DecompiledText +import org.jetbrains.kotlin.utils.concurrent.block.LockedClearableLazyValue + +public class KotlinJavascriptMetaFile(val provider: KotlinJavascriptMetaFileViewProvider) : KotlinClsFileBase(provider) { + protected override val decompiledText: LockedClearableLazyValue = LockedClearableLazyValue(Any()) { + buildDecompiledTextFromJsMetadata(getVirtualFile()) + } +} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFileDecompiler.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFileDecompiler.java new file mode 100644 index 00000000000..ca64482a9ca --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFileDecompiler.java @@ -0,0 +1,55 @@ +/* + * 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.decompiler; + +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.FileViewProvider; +import com.intellij.psi.PsiManager; +import com.intellij.psi.compiled.ClassFileDecompilers; +import com.intellij.psi.compiled.ClsStubBuilder; +import com.intellij.psi.stubs.PsiFileStub; +import com.intellij.util.indexing.FileContent; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.idea.decompiler.stubBuilder.KotlinClsStubBuilder; + +public class KotlinJavascriptMetaFileDecompiler extends ClassFileDecompilers.Full { + private final ClsStubBuilder stubBuilder = new KotlinClsStubBuilder() { + @Nullable + @Override + public PsiFileStub buildFileStub(@NotNull FileContent fileContent) { + return null; + } + }; + + @Override + public boolean accepts(@NotNull VirtualFile file) { + return file.getName().endsWith(".meta"); + } + + @NotNull + @Override + public ClsStubBuilder getStubBuilder() { + return stubBuilder; + } + + @NotNull + @Override + public FileViewProvider createFileViewProvider(@NotNull VirtualFile file, @NotNull PsiManager manager, boolean physical) { + return new KotlinJavascriptMetaFileViewProvider(manager, file, physical, DecompilerPackage.isKotlinInternalCompiledFile(file)); + } +} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFileViewProvider.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFileViewProvider.kt new file mode 100644 index 00000000000..09512d7cfd4 --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinJavascriptMetaFileViewProvider.kt @@ -0,0 +1,48 @@ +/* + * 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.decompiler + +import com.intellij.openapi.fileTypes.FileType +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.psi.PsiFile +import com.intellij.psi.PsiManager +import com.intellij.psi.SingleRootFileViewProvider +import org.jetbrains.kotlin.idea.JetLanguage +import kotlin.properties.Delegates + +public class KotlinJavascriptMetaFileViewProvider ( + manager: PsiManager, + val file: VirtualFile, + physical: Boolean, + val isInternal: Boolean) : SingleRootFileViewProvider(manager, file, physical, JetLanguage.INSTANCE) { + + val jetJsMetaFile by Delegates.blockingLazy(this) { + //TODO: check index that file is library file, as in ClassFileViewProvider + if (!isInternal) KotlinJavascriptMetaFile(this) else null + } + + override fun getContents(): CharSequence { + return jetJsMetaFile?.getText() ?: "" + } + + override fun createFile(project: Project, file: VirtualFile, fileType: FileType): PsiFile? = jetJsMetaFile + + override fun createCopy(copy: VirtualFile): SingleRootFileViewProvider { + return KotlinJavascriptMetaFileViewProvider(getManager(), copy, false, isInternal) + } +} diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 2d34fbbb9d7..da9d571a80b 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -432,6 +432,7 @@ +