add KotlinJavascriptMetaFileDecompiler and support for .meta files

This commit is contained in:
Michael Nedzelsky
2015-03-05 15:57:01 +03:00
parent 5b6849225c
commit 803e95364c
6 changed files with 193 additions and 36 deletions
@@ -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<DecompiledText> = 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<JetDeclaration>())
}
else {
null
}
}
override fun getText(): String? {
return decompiledText.get().text
}
override fun onContentReload() {
super.onContentReload()
decompiledText.drop()
}
TestOnly
fun getRenderedDescriptorsToRange(): Map<String, TextRange> {
return decompiledText.get().renderedDescriptorsToRange
}
}
@@ -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<DecompiledText>
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<JetDeclaration>())
}
else {
null
}
}
override fun getText(): String? {
return decompiledText.get().text
}
override fun onContentReload() {
super.onContentReload()
decompiledText.drop()
}
TestOnly
fun getRenderedDescriptorsToRange(): Map<String, TextRange> {
return decompiledText.get().renderedDescriptorsToRange
}
}
@@ -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<DecompiledText> = LockedClearableLazyValue(Any()) {
buildDecompiledTextFromJsMetadata(getVirtualFile())
}
}
@@ -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));
}
}
@@ -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)
}
}
+1
View File
@@ -432,6 +432,7 @@
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.JetProbablyNothingPropertyShortNameIndex"/>
<psi.classFileDecompiler implementation="org.jetbrains.kotlin.idea.decompiler.JetClassFileDecompiler"/>
<psi.classFileDecompiler implementation="org.jetbrains.kotlin.idea.decompiler.KotlinJavascriptMetaFileDecompiler"/>
<fileBasedIndex implementation="org.jetbrains.kotlin.idea.versions.KotlinAbiVersionIndex"/>
<fileBasedIndex implementation="org.jetbrains.kotlin.idea.vfilefinder.KotlinClassFileIndex"/>