Better sorting: current package is preferred

This commit is contained in:
Valentin Kipyatkov
2015-08-21 14:20:23 +03:00
parent b4e71364a3
commit 68c0c83879
3 changed files with 27 additions and 16 deletions
@@ -0,0 +1,3 @@
package test
fun fooFromSamePackage() = 12
@@ -1,12 +1,19 @@
package test
import some.fooImported import some.fooImported
val fooVar = 12 val fooVar = 12
fun fooLocal() = 12 fun fooCurentFile() = 12
val some = foo<caret> val some = foo<caret>
// "foo" is before other elements because of exact prefix match // "foo" is before other elements because of exact prefix match
// ORDER: foo, fooVar, fooLocal, fooImported, fooNotImported // ORDER: foo
// ORDER: fooVar
// ORDER: fooCurentFile
// ORDER: fooFromSamePackage
// ORDER: fooImported
// ORDER: fooNotImported
// INVOCATION_COUNT: 2 // INVOCATION_COUNT: 2
// SELECTED: 0 // SELECTED: 0
@@ -31,7 +31,7 @@ class SymbolUsageProximityPrioritizer(private val file: JetFile) {
private val thisModule = ModuleUtilCore.findModuleForPsiElement(file) private val thisModule = ModuleUtilCore.findModuleForPsiElement(file)
private enum class PriorityBasedOnImports { private enum class PriorityBasedOnImports {
thisFile, thisPackage,
defaultImport, defaultImport,
preciseImport, preciseImport,
allUnderImport, allUnderImport,
@@ -41,13 +41,14 @@ class SymbolUsageProximityPrioritizer(private val file: JetFile) {
notToBeUsedInKotlin notToBeUsedInKotlin
} }
private enum class PriorityBasedOnModule { private enum class PriorityBasedOnLocation {
thisFile,
thisModule, thisModule,
project, project,
other other
} }
data class Priority(private val priority1: PriorityBasedOnImports, private val priority2: PriorityBasedOnModule) : Comparable<Priority> { data class Priority(private val priority1: PriorityBasedOnImports, private val priority2: PriorityBasedOnLocation) : Comparable<Priority> {
override fun compareTo(other: Priority): Int { override fun compareTo(other: Priority): Int {
val c1 = priority1.compareTo(other.priority1) val c1 = priority1.compareTo(other.priority1)
if (c1 != 0) return c1 if (c1 != 0) return c1
@@ -55,16 +56,14 @@ class SymbolUsageProximityPrioritizer(private val file: JetFile) {
} }
companion object { companion object {
val DEFAULT = Priority(PriorityBasedOnImports.default, PriorityBasedOnModule.other) val DEFAULT = Priority(PriorityBasedOnImports.default, PriorityBasedOnLocation.other)
} }
} }
fun priority(fqName: FqName, declaration: PsiElement?, isPackage: Boolean) fun priority(fqName: FqName, declaration: PsiElement?, isPackage: Boolean)
= Priority(priorityBasedOnImports(fqName, declaration, isPackage), priorityBasedOnModule(declaration)) = Priority(priorityBasedOnImports(fqName, isPackage), priorityBasedOnLocation(declaration))
private fun priorityBasedOnImports(fqName: FqName, declaration: PsiElement?, isPackage: Boolean): PriorityBasedOnImports {
if (declaration?.containingFile == file) return PriorityBasedOnImports.thisFile
private fun priorityBasedOnImports(fqName: FqName, isPackage: Boolean): PriorityBasedOnImports {
val importPath = ImportPath(fqName, false) val importPath = ImportPath(fqName, false)
if (isPackage) { if (isPackage) {
@@ -76,7 +75,8 @@ class SymbolUsageProximityPrioritizer(private val file: JetFile) {
return when { return when {
JavaToKotlinClassMap.INSTANCE.mapPlatformClass(fqName).isNotEmpty() -> PriorityBasedOnImports.notToBeUsedInKotlin JavaToKotlinClassMap.INSTANCE.mapPlatformClass(fqName).isNotEmpty() -> PriorityBasedOnImports.notToBeUsedInKotlin
ImportInsertHelper.getInstance(file.getProject()).isImportedWithDefault(importPath, file) -> PriorityBasedOnImports.defaultImport fqName.parent() == file.packageFqName -> PriorityBasedOnImports.thisPackage
ImportInsertHelper.getInstance(file.project).isImportedWithDefault(importPath, file) -> PriorityBasedOnImports.defaultImport
importCache.isImportedWithPreciseImport(fqName) -> PriorityBasedOnImports.preciseImport importCache.isImportedWithPreciseImport(fqName) -> PriorityBasedOnImports.preciseImport
importCache.isImportedWithAllUnderImport(fqName) -> PriorityBasedOnImports.allUnderImport importCache.isImportedWithAllUnderImport(fqName) -> PriorityBasedOnImports.allUnderImport
importCache.hasPreciseImportFromPackage(fqName.parent()) -> PriorityBasedOnImports.hasImportFromSamePackage importCache.hasPreciseImportFromPackage(fqName.parent()) -> PriorityBasedOnImports.hasImportFromSamePackage
@@ -84,12 +84,13 @@ class SymbolUsageProximityPrioritizer(private val file: JetFile) {
} }
} }
private fun priorityBasedOnModule(declaration: PsiElement?): PriorityBasedOnModule { private fun priorityBasedOnLocation(declaration: PsiElement?): PriorityBasedOnLocation {
return when { return when {
declaration == null -> PriorityBasedOnModule.other declaration == null -> PriorityBasedOnLocation.other
ModuleUtilCore.findModuleForPsiElement(declaration) == thisModule -> PriorityBasedOnModule.thisModule declaration.containingFile == file -> PriorityBasedOnLocation.thisFile
ProjectRootsUtil.isInProjectSource(declaration) -> PriorityBasedOnModule.project ModuleUtilCore.findModuleForPsiElement(declaration) == thisModule -> PriorityBasedOnLocation.thisModule
else -> PriorityBasedOnModule.other ProjectRootsUtil.isInProjectSource(declaration) -> PriorityBasedOnLocation.project
else -> PriorityBasedOnLocation.other
} }
} }