KotlinProjectViewProvider: J2K
This commit is contained in:
@@ -14,115 +14,106 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.idea.projectView;
|
package org.jetbrains.kotlin.idea.projectView
|
||||||
|
|
||||||
import com.intellij.ide.projectView.SelectableTreeStructureProvider;
|
import com.intellij.ide.projectView.SelectableTreeStructureProvider
|
||||||
import com.intellij.ide.projectView.ViewSettings;
|
import com.intellij.ide.projectView.ViewSettings
|
||||||
import com.intellij.ide.util.treeView.AbstractTreeNode;
|
import com.intellij.ide.util.treeView.AbstractTreeNode
|
||||||
import com.intellij.openapi.project.DumbAware;
|
import com.intellij.openapi.project.DumbAware
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.roots.ProjectFileIndex;
|
import com.intellij.openapi.roots.ProjectRootManager
|
||||||
import com.intellij.openapi.roots.ProjectRootManager;
|
import com.intellij.openapi.vfs.VirtualFile
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.PsiElement;
|
import org.jetbrains.kotlin.idea.KotlinIconProvider
|
||||||
import com.intellij.psi.PsiFile;
|
import org.jetbrains.kotlin.psi.KtClassBody
|
||||||
import org.jetbrains.kotlin.idea.KotlinIconProvider;
|
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||||
import org.jetbrains.kotlin.psi.KtClassBody;
|
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||||
import org.jetbrains.kotlin.psi.KtClassOrObject;
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.psi.KtDeclaration;
|
import java.util.*
|
||||||
import org.jetbrains.kotlin.psi.KtFile;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
class KotlinProjectViewProvider(private val myProject: Project) : SelectableTreeStructureProvider, DumbAware {
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class KotlinProjectViewProvider implements SelectableTreeStructureProvider, DumbAware {
|
override fun modify(parent: AbstractTreeNode<Any>,
|
||||||
private final Project myProject;
|
children: Collection<AbstractTreeNode<Any>>,
|
||||||
|
settings: ViewSettings): Collection<AbstractTreeNode<out Any>> {
|
||||||
|
val result = ArrayList<AbstractTreeNode<out Any>>()
|
||||||
|
|
||||||
public KotlinProjectViewProvider(Project project) {
|
for (child in children) {
|
||||||
myProject = project;
|
val childValue = child.value
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
if (childValue is KtFile) {
|
||||||
public Collection<AbstractTreeNode> modify(AbstractTreeNode parent, Collection<AbstractTreeNode> children, ViewSettings settings) {
|
val declarations = childValue.declarations
|
||||||
List<AbstractTreeNode> result = new ArrayList<AbstractTreeNode>();
|
|
||||||
|
|
||||||
for (AbstractTreeNode child : children) {
|
val mainClass = KotlinIconProvider.getMainClass(childValue)
|
||||||
Object childValue = child.getValue();
|
if (mainClass != null && declarations.size == 1) {
|
||||||
|
result.add(KtClassOrObjectTreeNode(childValue.project, mainClass, settings))
|
||||||
if (childValue instanceof KtFile) {
|
|
||||||
KtFile file = (KtFile) childValue;
|
|
||||||
List<KtDeclaration> declarations = file.getDeclarations();
|
|
||||||
|
|
||||||
KtClassOrObject mainClass = KotlinIconProvider.getMainClass(file);
|
|
||||||
if (mainClass != null && declarations.size() == 1) {
|
|
||||||
result.add(new KtClassOrObjectTreeNode(file.getProject(), mainClass, settings));
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result.add(new KtFileTreeNode(file.getProject(), file, settings));
|
result.add(KtFileTreeNode(childValue.project, childValue, settings))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result.add(child);
|
result.add(child)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun getData(selected: Collection<AbstractTreeNode<Any>>, dataName: String): Any? {
|
||||||
public Object getData(Collection<AbstractTreeNode> selected, String dataName) {
|
return null
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun getTopLevelElement(element: PsiElement): PsiElement? {
|
||||||
public PsiElement getTopLevelElement(PsiElement element) {
|
val file = element.containingFile
|
||||||
PsiFile file = element.getContainingFile();
|
if (file == null || file !is KtFile) return null
|
||||||
if (file == null || !(file instanceof KtFile)) return null;
|
|
||||||
|
|
||||||
VirtualFile virtualFile = file.getVirtualFile();
|
val virtualFile = file.virtualFile
|
||||||
if (!fileInRoots(virtualFile)) return file;
|
if (!fileInRoots(virtualFile)) return file
|
||||||
|
|
||||||
PsiElement current = element;
|
var current: PsiElement? = element
|
||||||
while (current != null) {
|
while (current != null) {
|
||||||
if (isSelectable(current)) break;
|
if (isSelectable(current)) break
|
||||||
current = current.getParent();
|
current = current.parent
|
||||||
}
|
}
|
||||||
|
|
||||||
if (current instanceof KtFile) {
|
if (current is KtFile) {
|
||||||
List<KtDeclaration> declarations = ((KtFile) current).getDeclarations();
|
val declarations = current.declarations
|
||||||
String nameWithoutExtension = virtualFile != null ? virtualFile.getNameWithoutExtension() : file.getName();
|
val nameWithoutExtension = if (virtualFile != null) virtualFile.nameWithoutExtension else file.name
|
||||||
if (declarations.size() == 1 && declarations.get(0) instanceof KtClassOrObject &&
|
if (declarations.size == 1 && declarations[0] is KtClassOrObject &&
|
||||||
nameWithoutExtension.equals(declarations.get(0).getName())) {
|
nameWithoutExtension == declarations[0].name) {
|
||||||
current = declarations.get(0);
|
current = declarations[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return current != null ? current : file;
|
return if (current != null) current else file
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isSelectable(PsiElement element) {
|
private fun isSelectable(element: PsiElement): Boolean {
|
||||||
if (element instanceof KtFile) return true;
|
if (element is KtFile) return true
|
||||||
if (element instanceof KtDeclaration) {
|
if (element is KtDeclaration) {
|
||||||
PsiElement parent = element.getParent();
|
var parent = element.getParent()
|
||||||
if (parent instanceof KtFile) {
|
if (parent is KtFile) {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
else if (parent instanceof KtClassBody) {
|
else if (parent is KtClassBody) {
|
||||||
parent = parent.getParent();
|
parent = parent.getParent()
|
||||||
if (parent instanceof KtClassOrObject) {
|
if (parent is KtClassOrObject) {
|
||||||
return isSelectable(parent);
|
return isSelectable(parent)
|
||||||
}
|
}
|
||||||
else return false;
|
else
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
else return false;
|
else
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
else return false;
|
else
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean fileInRoots(VirtualFile file) {
|
private fun fileInRoots(file: VirtualFile?): Boolean {
|
||||||
ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
|
val index = ProjectRootManager.getInstance(myProject).fileIndex
|
||||||
return file != null && (index.isInSourceContent(file) || index.isInLibraryClasses(file) || index.isInLibrarySource(file));
|
return file != null && (index.isInSourceContent(file) || index.isInLibraryClasses(file) || index.isInLibrarySource(file))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user