From 4afd7c553b8cd8a07f94b6a001e137f333f4002b Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 28 Jan 2020 21:55:12 +0300 Subject: [PATCH] 201: AbstractTreeNode generified in ide.projectView package (bunched) --- .../KtDeclarationTreeNode.java.201 | 110 ++++++++++++++++++ .../idea/projectView/KtFileTreeNode.java.201 | 63 ++++++++++ 2 files changed, 173 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/projectView/KtDeclarationTreeNode.java.201 create mode 100644 idea/src/org/jetbrains/kotlin/idea/projectView/KtFileTreeNode.java.201 diff --git a/idea/src/org/jetbrains/kotlin/idea/projectView/KtDeclarationTreeNode.java.201 b/idea/src/org/jetbrains/kotlin/idea/projectView/KtDeclarationTreeNode.java.201 new file mode 100644 index 00000000000..bca4c7cdba5 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/projectView/KtDeclarationTreeNode.java.201 @@ -0,0 +1,110 @@ +/* + * 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.projectView; + +import com.intellij.application.options.CodeStyle; +import com.intellij.ide.projectView.PresentationData; +import com.intellij.ide.projectView.ViewSettings; +import com.intellij.ide.projectView.impl.nodes.AbstractPsiBasedNode; +import com.intellij.ide.util.treeView.AbstractTreeNode; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings; +import org.jetbrains.kotlin.psi.*; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +public class KtDeclarationTreeNode extends AbstractPsiBasedNode { + public static final String CLASS_INITIALIZER = ""; + + protected KtDeclarationTreeNode(Project project, KtDeclaration ktDeclaration, ViewSettings viewSettings) { + super(project, ktDeclaration, viewSettings); + } + + @Override + protected PsiElement extractPsiFromValue() { + return getValue(); + } + + @Override + protected Collection> getChildrenImpl() { + return Collections.emptyList(); + } + + @Override + protected void updateImpl(PresentationData data) { + KtDeclaration declaration = getValue(); + if (declaration != null) { + String text = declaration instanceof KtAnonymousInitializer ? CLASS_INITIALIZER : declaration.getName(); + if (text == null) return; + + KotlinCodeStyleSettings settings = CodeStyle.getSettings(getProject()) + .getCustomSettings(KotlinCodeStyleSettings.class); + + if (declaration instanceof KtProperty) { + KtProperty property = (KtProperty) declaration; + KtTypeReference ref = property.getTypeReference(); + if (ref != null) { + if (settings.SPACE_BEFORE_TYPE_COLON) text += " "; + text += ":"; + if (settings.SPACE_AFTER_TYPE_COLON) text += " "; + text += ref.getText(); + } + } + else if (declaration instanceof KtFunction) { + KtFunction function = (KtFunction) declaration; + KtTypeReference receiverTypeRef = function.getReceiverTypeReference(); + if (receiverTypeRef != null) { + text = receiverTypeRef.getText() + "." + text; + } + text += "("; + List parameters = function.getValueParameters(); + for (KtParameter parameter : parameters) { + if (parameter.getName() != null) { + text += parameter.getName(); + if (settings.SPACE_BEFORE_TYPE_COLON) text += " "; + text += ":"; + if (settings.SPACE_AFTER_TYPE_COLON) text += " "; + } + KtTypeReference typeReference = parameter.getTypeReference(); + if (typeReference != null) { + text += typeReference.getText(); + } + text += ", "; + } + if (parameters.size() > 0) text = text.substring(0, text.length() - 2); + text += ")"; + KtTypeReference typeReference = function.getTypeReference(); + if (typeReference != null) { + if (settings.SPACE_BEFORE_TYPE_COLON) text += " "; + text += ":"; + if (settings.SPACE_AFTER_TYPE_COLON) text += " "; + text += typeReference.getText(); + } + } + + data.setPresentableText(text); + } + } + + @Override + protected boolean isDeprecated() { + return KtPsiUtil.isDeprecated(getValue()); + } +} diff --git a/idea/src/org/jetbrains/kotlin/idea/projectView/KtFileTreeNode.java.201 b/idea/src/org/jetbrains/kotlin/idea/projectView/KtFileTreeNode.java.201 new file mode 100644 index 00000000000..07198c9e7cb --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/projectView/KtFileTreeNode.java.201 @@ -0,0 +1,63 @@ +/* + * 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.projectView; + +import com.intellij.ide.projectView.ViewSettings; +import com.intellij.ide.projectView.impl.nodes.PsiFileNode; +import com.intellij.ide.util.treeView.AbstractTreeNode; +import com.intellij.openapi.project.Project; +import org.jetbrains.kotlin.psi.KtClassOrObject; +import org.jetbrains.kotlin.psi.KtDeclaration; +import org.jetbrains.kotlin.psi.KtFile; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +public class KtFileTreeNode extends PsiFileNode { + public KtFileTreeNode(Project project, KtFile value, ViewSettings viewSettings) { + super(project, value, viewSettings); + } + + public final KtFile getKtFile() { + return (KtFile) getValue(); + } + + @Override + public Collection> getChildrenImpl() { + KtFile file = (KtFile) getValue(); + + if (file == null) return Collections.emptyList(); + ArrayList> result = new ArrayList<>(); + + if (getSettings().isShowMembers()) { + @SuppressWarnings("ConstantConditions") List declarations = (file.isScript() ? file.getScript() : file).getDeclarations(); + + for (KtDeclaration declaration : declarations) { + if (declaration instanceof KtClassOrObject) { + result.add(new KtClassOrObjectTreeNode(file.getProject(), (KtClassOrObject) declaration, getSettings())); + } + else if (getSettings().isShowMembers()) { + result.add(new KtDeclarationTreeNode(getProject(), declaration, getSettings())); + } + } + } + + return result; + } +}