From 752a6cef0d8f71f32324ed803b7e37885258587c Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 29 Jul 2014 22:14:58 +0400 Subject: [PATCH] Completion: converted all weighers to Kotlin merging them into one file --- .../weigher/JetAccessibleWeigher.java | 52 -------- .../weigher/JetCompletionSorting.kt | 114 ++++++++++++++++-- .../JetDeclarationRemotenessWeigher.java | 85 ------------- .../completion/weigher/JetKindWeigher.java | 72 ----------- 4 files changed, 101 insertions(+), 222 deletions(-) delete mode 100644 idea/src/org/jetbrains/jet/plugin/completion/weigher/JetAccessibleWeigher.java delete mode 100644 idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java delete mode 100644 idea/src/org/jetbrains/jet/plugin/completion/weigher/JetKindWeigher.java diff --git a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetAccessibleWeigher.java b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetAccessibleWeigher.java deleted file mode 100644 index 09f2e53bf69..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetAccessibleWeigher.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.plugin.completion.weigher; - -import com.intellij.codeInsight.lookup.LookupElement; -import com.intellij.codeInsight.lookup.LookupElementWeigher; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; -import org.jetbrains.jet.plugin.completion.JetLookupObject; - -class JetAccessibleWeigher extends LookupElementWeigher { - JetAccessibleWeigher() { - super("JetAccessibleWeigher"); - } - - private enum MyResult { - normal, - deprecated - } - - @NotNull - @Override - public MyResult weigh(@NotNull LookupElement element) { - Object object = element.getObject(); - if (object instanceof JetLookupObject) { - JetLookupObject lookupObject = (JetLookupObject) object; - DeclarationDescriptor descriptor = lookupObject.getDescriptor(); - if (descriptor != null) { - if (KotlinBuiltIns.getInstance().isDeprecated(descriptor)) { - return MyResult.deprecated; - } - } - } - - return MyResult.normal; - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.kt b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.kt index 9dfdf7a68f4..656365366e4 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.kt +++ b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.kt @@ -24,29 +24,117 @@ import com.intellij.codeInsight.lookup.LookupElementWeigher import com.intellij.codeInsight.lookup.LookupElement import com.intellij.codeInsight.lookup.WeighingContext import org.jetbrains.jet.plugin.completion.* +import org.jetbrains.jet.lang.descriptors.impl.LocalVariableDescriptor +import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor +import org.jetbrains.jet.lang.descriptors.PropertyDescriptor +import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns +import org.jetbrains.jet.lang.resolve.DescriptorUtils +import org.jetbrains.jet.lang.resolve.name.isValidJavaFqName +import org.jetbrains.jet.lang.resolve.ImportPath +import org.jetbrains.jet.plugin.quickfix.ImportInsertHelper public fun CompletionResultSet.addJetSorting(parameters: CompletionParameters): CompletionResultSet { var sorter = CompletionSorter.defaultSorter(parameters, getPrefixMatcher())!! - object PriorityWeigher : LookupElementWeigher("priority", false, false) { - override fun weigh(element: LookupElement, context: WeighingContext) - = (element.getUserData(ITEM_PRIORITY_KEY) ?: ItemPriority.DEFAULT).ordinal() - } - - sorter = sorter.weighBefore("stats", PriorityWeigher, JetKindWeigher()) + sorter = sorter.weighBefore("stats", PriorityWeigher, KindWeigher) sorter = sorter.weighAfter( "stats", JetDeclarationRemotenessWeigher(parameters.getOriginalFile() as JetFile), - JetAccessibleWeigher()) + DeprecatedWeigher) - object PreferMatchingItemWeigher : LookupElementWeigher("preferMatching", false, true) { - override fun weigh(element: LookupElement, context: WeighingContext): Comparable { - val prefix = context.itemPattern(element) - return if (element.getLookupString() == prefix) 0 else 1 - } - } sorter = sorter.weighBefore("middleMatching", PreferMatchingItemWeigher) return withRelevanceSorter(sorter) } + +private object PriorityWeigher : LookupElementWeigher("kotlin.priority") { + override fun weigh(element: LookupElement, context: WeighingContext) + = (element.getUserData(ITEM_PRIORITY_KEY) ?: ItemPriority.DEFAULT).ordinal() +} + +private object KindWeigher : LookupElementWeigher("kotlin.kind") { + enum class Weight : Comparable { + override fun compareTo(other: Weight) = ordinal().compareTo(other.ordinal()) + + localOrParameter + property + probableKeyword + normal + packages + } + + override fun weigh(element: LookupElement): Weight { + val o = element.getObject() + return when (o) { + is JetLookupObject -> when (o.getDescriptor()) { + is LocalVariableDescriptor, is ValueParameterDescriptor -> Weight.localOrParameter + is PropertyDescriptor -> Weight.property + is PackageViewDescriptor -> Weight.packages + else -> Weight.normal + } + + is String -> Weight.probableKeyword + + else -> Weight.normal + } + } +} + +private object DeprecatedWeigher : LookupElementWeigher("kotlin.deprecated") { + override fun weigh(element: LookupElement): Int { + val o = element.getObject() + if (o is JetLookupObject) { + val descriptor = o.getDescriptor() + if (descriptor != null && KotlinBuiltIns.getInstance().isDeprecated(descriptor)) return 1 + } + + return 0 + } +} + +private object PreferMatchingItemWeigher : LookupElementWeigher("kotlin.preferMatching", false, true) { + override fun weigh(element: LookupElement, context: WeighingContext): Comparable { + val prefix = context.itemPattern(element) + return if (element.getLookupString() == prefix) 0 else 1 + } +} + +private class JetDeclarationRemotenessWeigher(private val file: JetFile) : LookupElementWeigher("kotlin.declarationRemoteness") { + private enum class Weight : Comparable { + override fun compareTo(other: Weight) = ordinal().compareTo(other.ordinal()) + + kotlinDefaultImport + thisFile + imported + normal + notImported + } + + override fun weigh(element: LookupElement): Weight { + val o = element.getObject() + if (o is JetLookupObject) { + val elementFile = o.getPsiElement()?.getContainingFile() + if (elementFile is JetFile && elementFile.getOriginalFile() == file) { + return Weight.thisFile + } + + val descriptor = o.getDescriptor() + if (descriptor != null) { + val fqName = DescriptorUtils.getFqName(descriptor).toString() + // Invalid name can be met for class object descriptor: Test.MyTest.A..testOther + if (isValidJavaFqName(fqName)) { + val importPath = ImportPath(fqName) + return when { + ImportInsertHelper.needImport(importPath, file) -> Weight.notImported + ImportInsertHelper.isImportedWithDefault(importPath, file) -> Weight.kotlinDefaultImport + else -> Weight.imported + } + } + } + } + + return Weight.normal + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java deleted file mode 100644 index 736e042b55f..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2010-2014 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.jet.plugin.completion.weigher; - -import com.intellij.codeInsight.lookup.LookupElement; -import com.intellij.codeInsight.lookup.LookupElementWeigher; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.jet.lang.resolve.DescriptorUtils; -import org.jetbrains.jet.lang.resolve.ImportPath; -import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe; -import org.jetbrains.jet.lang.resolve.name.NamePackage; -import org.jetbrains.jet.plugin.completion.JetLookupObject; -import org.jetbrains.jet.plugin.quickfix.ImportInsertHelper; - -public class JetDeclarationRemotenessWeigher extends LookupElementWeigher { - private final JetFile file; - - protected JetDeclarationRemotenessWeigher(JetFile file) { - super(JetDeclarationRemotenessWeigher.class.getSimpleName()); - this.file = file; - } - - private enum Weight { - kotlinDefaultImport, - thisFile, - imported, - normal, - notImported, - } - - @NotNull - @Override - public Comparable weigh(@NotNull LookupElement element) { - Object object = element.getObject(); - if (object instanceof JetLookupObject) { - JetLookupObject lookupObject = (JetLookupObject) object; - - PsiElement psiElement = lookupObject.getPsiElement(); - if (psiElement != null) { - PsiFile elementFile = psiElement.getContainingFile(); - if (elementFile instanceof JetFile && elementFile.getOriginalFile() == file) { - return Weight.thisFile; - } - } - - DeclarationDescriptor descriptor = lookupObject.getDescriptor(); - if (descriptor != null) { - FqNameUnsafe fqName = DescriptorUtils.getFqName(descriptor); - // Invalid name can be met for class object descriptor: Test.MyTest.A..testOther - if (NamePackage.isValidJavaFqName(fqName.toString())) { - ImportPath importPath = new ImportPath(fqName.toString()); - if (ImportInsertHelper.needImport(importPath, file)) { - return Weight.notImported; - } - else { - if (ImportInsertHelper.isImportedWithDefault(importPath, file)) { - return Weight.kotlinDefaultImport; - } - return Weight.imported; - } - } - } - } - - return Weight.normal; - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetKindWeigher.java b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetKindWeigher.java deleted file mode 100644 index d82a45dbf20..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetKindWeigher.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.plugin.completion.weigher; - -import com.intellij.codeInsight.lookup.LookupElement; -import com.intellij.codeInsight.lookup.LookupElementWeigher; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor; -import org.jetbrains.jet.lang.descriptors.PropertyDescriptor; -import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor; -import org.jetbrains.jet.lang.descriptors.impl.LocalVariableDescriptor; -import org.jetbrains.jet.plugin.completion.JetLookupObject; - -class JetKindWeigher extends LookupElementWeigher { - JetKindWeigher() { - super(JetKindWeigher.class.getSimpleName()); - } - - private enum Weight { - localOrParameter, - property, - probableKeyword, - normal, - packages - } - - @NotNull - @Override - public Weight weigh(@NotNull LookupElement element) { - Object object = element.getObject(); - if (object instanceof JetLookupObject) { - JetLookupObject lookupObject = (JetLookupObject) object; - DeclarationDescriptor descriptor = lookupObject.getDescriptor(); - if (descriptor != null) { - if (descriptor instanceof LocalVariableDescriptor || descriptor instanceof ValueParameterDescriptor) { - return Weight.localOrParameter; - } - else if (descriptor instanceof PropertyDescriptor) { - return Weight.property; - } - else if (descriptor instanceof PackageViewDescriptor) { - return Weight.packages; - } - } - } - else if (object instanceof String) { - return Weight.probableKeyword; - } - - return Weight.normal; - } - - @Override - public boolean isPrefixDependent() { - return false; - } -}