KotlinStructureViewElement: J2K

This commit is contained in:
Dmitry Jemerov
2016-08-24 19:51:45 +02:00
parent 2ebacb6969
commit 50b1951746
@@ -14,159 +14,124 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.idea.structureView; package org.jetbrains.kotlin.idea.structureView
import com.intellij.ide.structureView.StructureViewTreeElement; import com.intellij.ide.structureView.StructureViewTreeElement
import com.intellij.ide.util.treeView.smartTree.TreeElement; import com.intellij.ide.util.treeView.smartTree.TreeElement
import com.intellij.navigation.ItemPresentation; import com.intellij.navigation.ItemPresentation
import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.DumbService
import com.intellij.openapi.ui.Queryable; import com.intellij.openapi.ui.Queryable
import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Computable
import com.intellij.psi.NavigatablePsiElement; import com.intellij.psi.NavigatablePsiElement
import com.intellij.util.ArrayUtil; import com.intellij.util.ArrayUtil
import com.intellij.util.Function; import com.intellij.util.Function
import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.ContainerUtil
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.TestOnly
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.annotations.TestOnly; import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils; import java.util.*
import org.jetbrains.kotlin.psi.*;
import java.util.ArrayList; class KotlinStructureViewElement : StructureViewTreeElement, Queryable {
import java.util.Collections; val element: NavigatablePsiElement
import java.util.List; val isInherited: Boolean
import java.util.Map;
public class KotlinStructureViewElement implements StructureViewTreeElement, Queryable { private var presentation: KotlinStructureElementPresentation? = null
private final NavigatablePsiElement element;
private final boolean isInherited;
private KotlinStructureElementPresentation presentation; constructor(element: NavigatablePsiElement, descriptor: DeclarationDescriptor, isInherited: Boolean) {
this.element = element
this.isInherited = isInherited
public KotlinStructureViewElement(@NotNull NavigatablePsiElement element, @NotNull DeclarationDescriptor descriptor, boolean isInherited) { if (element !is KtElement) {
this.element = element;
this.isInherited = isInherited;
if (!(element instanceof KtElement)) {
// Avoid storing descriptor in fields // Avoid storing descriptor in fields
presentation = new KotlinStructureElementPresentation(isInherited(), element, descriptor); presentation = KotlinStructureElementPresentation(isInherited, element, descriptor)
} }
} }
public KotlinStructureViewElement(@NotNull NavigatablePsiElement element, boolean isInherited) { constructor(element: NavigatablePsiElement, isInherited: Boolean) {
this.element = element; this.element = element
this.isInherited = isInherited; this.isInherited = isInherited
} }
public KotlinStructureViewElement(@NotNull KtFile fileElement) { constructor(fileElement: KtFile) {
element = fileElement; element = fileElement
isInherited = false; isInherited = false
} }
@NotNull override fun getValue(): Any {
public NavigatablePsiElement getElement() { return element
return element;
} }
@Override override fun navigate(requestFocus: Boolean) {
public Object getValue() { element.navigate(requestFocus)
return element;
} }
@Override override fun canNavigate(): Boolean {
public void navigate(boolean requestFocus) { return element.canNavigate()
element.navigate(requestFocus);
} }
@Override override fun canNavigateToSource(): Boolean {
public boolean canNavigate() { return element.canNavigateToSource()
return element.canNavigate();
} }
@Override override fun getPresentation(): ItemPresentation {
public boolean canNavigateToSource() {
return element.canNavigateToSource();
}
@NotNull
@Override
public ItemPresentation getPresentation() {
if (presentation == null) { if (presentation == null) {
presentation = new KotlinStructureElementPresentation(isInherited(), element, getDescriptor()); presentation = KotlinStructureElementPresentation(isInherited, element, descriptor)
} }
return presentation; return presentation!!
} }
@NotNull override fun getChildren(): Array<TreeElement> {
@Override val childrenDeclarations = childrenDeclarations
public TreeElement[] getChildren() { return ArrayUtil.toObjectArray(ContainerUtil.map(childrenDeclarations, Function<org.jetbrains.kotlin.psi.KtDeclaration, com.intellij.ide.util.treeView.smartTree.TreeElement> { declaration -> KotlinStructureViewElement(declaration, false) }), TreeElement::class.java)
List<KtDeclaration> childrenDeclarations = getChildrenDeclarations();
return ArrayUtil.toObjectArray(ContainerUtil.map(childrenDeclarations, new Function<KtDeclaration, TreeElement>() {
@Override
public TreeElement fun(KtDeclaration declaration) {
return new KotlinStructureViewElement(declaration, false);
}
}), TreeElement.class);
} }
@TestOnly @TestOnly
@Override override fun putInfo(info: MutableMap<String, String>) {
public void putInfo(@NotNull Map<String, String> info) { info.put("text", getPresentation().presentableText!!)
info.put("text", getPresentation().getPresentableText()); info.put("location", getPresentation().locationString!!)
info.put("location", getPresentation().getLocationString());
} }
public boolean isInherited() { private val descriptor: DeclarationDescriptor?
return isInherited; get() {
} if (!(element.isValid && element is KtDeclaration)) {
return null
}
@Nullable if (element is KtAnonymousInitializer) {
private DeclarationDescriptor getDescriptor() { return null
if (!(element.isValid() && element instanceof KtDeclaration)) { }
return null;
}
final KtDeclaration declaration = (KtDeclaration) element; return ApplicationManager.getApplication().runReadAction(Computable<org.jetbrains.kotlin.descriptors.DeclarationDescriptor> {
if (declaration instanceof KtAnonymousInitializer) {
return null;
}
return ApplicationManager.getApplication().runReadAction(new Computable<DeclarationDescriptor>() {
@Override
public DeclarationDescriptor compute() {
if (!DumbService.isDumb(element.getProject())) { if (!DumbService.isDumb(element.getProject())) {
return ResolutionUtils.resolveToDescriptor(declaration); return@Computable element.resolveToDescriptor()
} }
return null; null
} })
});
}
@NotNull
private List<KtDeclaration> getChildrenDeclarations() {
if (element instanceof KtFile) {
KtFile jetFile = (KtFile) element;
return jetFile.getDeclarations();
} }
else if (element instanceof KtClass) {
KtClass ktClass = (KtClass) element; private val childrenDeclarations: List<KtDeclaration>
List<KtDeclaration> declarations = new ArrayList<KtDeclaration>(); get() {
for (KtParameter parameter : ktClass.getPrimaryConstructorParameters()) { if (element is KtFile) {
if (parameter.hasValOrVar()) { return element.declarations
declarations.add(parameter); }
else if (element is KtClass) {
val declarations = ArrayList<KtDeclaration>()
for (parameter in element.getPrimaryConstructorParameters()) {
if (parameter.hasValOrVar()) {
declarations.add(parameter)
}
} }
declarations.addAll(element.declarations)
return declarations
}
else if (element is KtClassOrObject) {
return element.declarations
} }
declarations.addAll(ktClass.getDeclarations());
return declarations;
}
else if (element instanceof KtClassOrObject) {
return ((KtClassOrObject) element).getDeclarations();
}
return Collections.emptyList(); return emptyList()
} }
} }