cleanup KotlinProjectViewProvider
This commit is contained in:
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.psi.KtClassBody
|
|||||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class KotlinProjectViewProvider(private val myProject: Project) : SelectableTreeStructureProvider, DumbAware {
|
class KotlinProjectViewProvider(private val myProject: Project) : SelectableTreeStructureProvider, DumbAware {
|
||||||
@@ -68,55 +69,33 @@ class KotlinProjectViewProvider(private val myProject: Project) : SelectableTree
|
|||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getData(selected: Collection<AbstractTreeNode<Any>>, dataName: String): Any? {
|
override fun getData(selected: Collection<AbstractTreeNode<Any>>, dataName: String): Any? = null
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getTopLevelElement(element: PsiElement): PsiElement? {
|
override fun getTopLevelElement(element: PsiElement): PsiElement? {
|
||||||
val file = element.containingFile
|
val file = element.containingFile as? KtFile ?: return null
|
||||||
if (file == null || file !is KtFile) return null
|
|
||||||
|
|
||||||
val virtualFile = file.virtualFile
|
val virtualFile = file.virtualFile
|
||||||
if (!fileInRoots(virtualFile)) return file
|
if (!fileInRoots(virtualFile)) return file
|
||||||
|
|
||||||
var current: PsiElement? = element
|
var current = element.parentsWithSelf.firstOrNull() { it.isSelectable() }
|
||||||
while (current != null) {
|
|
||||||
if (isSelectable(current)) break
|
|
||||||
current = current.parent
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current is KtFile) {
|
if (current is KtFile) {
|
||||||
val declarations = current.declarations
|
val declaration = current.declarations.singleOrNull()
|
||||||
val nameWithoutExtension = if (virtualFile != null) virtualFile.nameWithoutExtension else file.name
|
val nameWithoutExtension = if (virtualFile != null) virtualFile.nameWithoutExtension else file.name
|
||||||
if (declarations.size == 1 && declarations[0] is KtClassOrObject &&
|
if (declaration is KtClassOrObject && nameWithoutExtension == declaration.name) {
|
||||||
nameWithoutExtension == declarations[0].name) {
|
current = declaration
|
||||||
current = declarations[0]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return if (current != null) current else file
|
return current ?: file
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isSelectable(element: PsiElement): Boolean {
|
private fun PsiElement.isSelectable(): Boolean = when(this) {
|
||||||
if (element is KtFile) return true
|
is KtFile -> true
|
||||||
if (element is KtDeclaration) {
|
is KtDeclaration ->
|
||||||
var parent = element.getParent()
|
parent is KtFile ||
|
||||||
if (parent is KtFile) {
|
((parent as? KtClassBody)?.parent as? KtClassOrObject)?.isSelectable() ?: false
|
||||||
return true
|
else -> false
|
||||||
}
|
|
||||||
else if (parent is KtClassBody) {
|
|
||||||
parent = parent.getParent()
|
|
||||||
if (parent is KtClassOrObject) {
|
|
||||||
return isSelectable(parent)
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fileInRoots(file: VirtualFile?): Boolean {
|
private fun fileInRoots(file: VirtualFile?): Boolean {
|
||||||
|
|||||||
Reference in New Issue
Block a user