diff --git a/idea/src/org/jetbrains/kotlin/idea/liveTemplates/macro/JetAnonymousSuperMacro.java b/idea/src/org/jetbrains/kotlin/idea/liveTemplates/macro/JetAnonymousSuperMacro.java deleted file mode 100644 index 53431c90e91..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/liveTemplates/macro/JetAnonymousSuperMacro.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * 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.liveTemplates.macro; - -import com.intellij.codeInsight.lookup.LookupElement; -import com.intellij.codeInsight.lookup.LookupElementBuilder; -import com.intellij.codeInsight.template.Expression; -import com.intellij.codeInsight.template.ExpressionContext; -import com.intellij.codeInsight.template.Macro; -import com.intellij.codeInsight.template.Result; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiDocumentManager; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiNamedElement; -import com.intellij.psi.util.PsiTreeUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.descriptors.ClassDescriptor; -import org.jetbrains.kotlin.descriptors.ClassKind; -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; -import org.jetbrains.kotlin.idea.JetBundle; -import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils; -import org.jetbrains.kotlin.psi.KtExpression; -import org.jetbrains.kotlin.psi.KtFile; -import org.jetbrains.kotlin.resolve.BindingContext; -import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils; -import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode; -import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter; -import org.jetbrains.kotlin.resolve.scopes.KtScope; - -import java.util.ArrayList; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; - -public class JetAnonymousSuperMacro extends Macro { - @Override - public String getName() { - return "anonymousSuper"; - } - - @Override - public String getPresentableName() { - return JetBundle.message("macro.fun.anonymousSuper"); - } - - @Override - public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) { - Editor editor = context.getEditor(); - if (editor != null) { - AnonymousTemplateEditingListener.registerListener(editor, context.getProject()); - } - - PsiNamedElement[] vars = getSupertypes(params, context); - if (vars == null || vars.length == 0) return null; - return new JetPsiElementResult(vars[0]); - } - - @Override - public LookupElement[] calculateLookupItems(@NotNull Expression[] params, ExpressionContext context) { - PsiNamedElement[] vars = getSupertypes(params, context); - if (vars == null || vars.length < 2) return null; - Set set = new LinkedHashSet(); - for (PsiNamedElement var : vars) { - set.add(LookupElementBuilder.create(var)); - } - return set.toArray(new LookupElement[set.size()]); - } - - @Nullable - private static PsiNamedElement[] getSupertypes(Expression[] params, ExpressionContext context) { - if (params.length != 0) return null; - - Project project = context.getProject(); - PsiDocumentManager.getInstance(project).commitAllDocuments(); - - PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument()); - if (!(psiFile instanceof KtFile)) return null; - - KtExpression expression = PsiTreeUtil.getParentOfType(psiFile.findElementAt(context.getStartOffset()), KtExpression.class); - if (expression == null) return null; - - BindingContext bc = ResolutionUtils.analyze(expression, BodyResolveMode.FULL); - KtScope scope = bc.get(BindingContext.RESOLUTION_SCOPE, expression); - if (scope == null) return null; - - List result = new ArrayList(); - - for (DeclarationDescriptor descriptor : scope.getDescriptors(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS, KtScope.Companion.getALL_NAME_FILTER())) { - if (!(descriptor instanceof ClassDescriptor)) continue; - ClassDescriptor classDescriptor = (ClassDescriptor) descriptor; - if (!classDescriptor.getModality().isOverridable()) continue; - ClassKind kind = classDescriptor.getKind(); - if (kind == ClassKind.INTERFACE || kind == ClassKind.CLASS) { - PsiElement declaration = DescriptorToSourceUtils.descriptorToDeclaration(descriptor); - if (declaration != null) { - result.add((PsiNamedElement) declaration); - } - } - } - - return result.toArray(new PsiNamedElement[result.size()]); - } -} diff --git a/idea/src/org/jetbrains/kotlin/idea/liveTemplates/macro/JetAnonymousSuperMacro.kt b/idea/src/org/jetbrains/kotlin/idea/liveTemplates/macro/JetAnonymousSuperMacro.kt new file mode 100644 index 00000000000..7ea2af7e0f0 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/liveTemplates/macro/JetAnonymousSuperMacro.kt @@ -0,0 +1,98 @@ +/* + * 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.liveTemplates.macro + +import com.intellij.codeInsight.lookup.LookupElement +import com.intellij.codeInsight.lookup.LookupElementBuilder +import com.intellij.codeInsight.template.Expression +import com.intellij.codeInsight.template.ExpressionContext +import com.intellij.codeInsight.template.Macro +import com.intellij.codeInsight.template.Result +import com.intellij.psi.PsiDocumentManager +import com.intellij.psi.PsiNamedElement +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.ClassKind +import org.jetbrains.kotlin.idea.JetBundle +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.psi.KtExpression +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter +import org.jetbrains.kotlin.utils.addIfNotNull +import java.util.* + +class JetAnonymousSuperMacro : Macro() { + override fun getName(): String { + return "anonymousSuper" + } + + override fun getPresentableName(): String { + return JetBundle.message("macro.fun.anonymousSuper") + } + + override fun calculateResult(params: Array, context: ExpressionContext): Result? { + val editor = context.editor + if (editor != null) { + AnonymousTemplateEditingListener.registerListener(editor, context.project) + } + + val vars = getSupertypes(params, context) + if (vars == null || vars.size() == 0) return null + return JetPsiElementResult(vars[0]) + } + + override fun calculateLookupItems(params: Array, context: ExpressionContext): Array? { + val vars = getSupertypes(params, context) + if (vars == null || vars.size < 2) return null + val set = LinkedHashSet() + for (`var` in vars) { + set.add(LookupElementBuilder.create(`var`)) + } + return set.toArray(arrayOfNulls(set.size)) + } + + private fun getSupertypes(params: Array, context: ExpressionContext): Array? { + if (params.size() != 0) return null + + val project = context.project + PsiDocumentManager.getInstance(project).commitAllDocuments() + + val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(context.editor!!.document) + if (psiFile !is KtFile) return null + + val expression = PsiTreeUtil.getParentOfType(psiFile.findElementAt(context.startOffset), KtExpression::class.java) ?: return null + + val bindingContext = expression.analyze(BodyResolveMode.FULL) + val scope = bindingContext.get(BindingContext.RESOLUTION_SCOPE, expression) ?: return null + + val result = ArrayList() + + for (descriptor in scope.getDescriptors(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS)) { + if (descriptor !is ClassDescriptor) continue + if (!descriptor.modality.isOverridable) continue + val kind = descriptor.kind + if (kind == ClassKind.INTERFACE || kind == ClassKind.CLASS) { + result.addIfNotNull(DescriptorToSourceUtils.descriptorToDeclaration(descriptor) as PsiNamedElement?) + } + } + + return result.toArray(arrayOfNulls(result.size)) + } +}