Skip to content

Commit

Permalink
Term changes
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinkip committed Sep 30, 2016
1 parent 7699430 commit 57faa5a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ class OptimizedImportsBuilder(

private val importInsertHelper = ImportInsertHelper.getInstance(file.project)

private sealed class LockedImport {
private sealed class ImportRule {
// force presence of this import
data class Positive(val importPath: ImportPath) : LockedImport() {
override fun toString() = importPath.toString()
data class Add(val importPath: ImportPath) : ImportRule() {
override fun toString() = "+" + importPath.toString()
}

// force absence of this import
data class Negative(val importPath: ImportPath) : LockedImport() {
data class DoNotAdd(val importPath: ImportPath) : ImportRule() {
override fun toString() = "-" + importPath.toString()
}
}

private val lockedImports = HashSet<LockedImport>()
private val importRules = HashSet<ImportRule>()

fun buildOptimizedImports(): List<ImportPath>? {
// TODO: should we drop unused aliases?
Expand All @@ -93,13 +93,13 @@ class OptimizedImportsBuilder(
val aliasName = it.alias
aliasName != null && aliasName != it.fqnPart().shortName()
}
.mapTo(lockedImports) { LockedImport.Positive(it) }
.mapTo(importRules) { ImportRule.Add(it) }

while (true) {
val lockedImportsBefore = lockedImports.size
val importRulesBefore = importRules.size
val result = tryBuildOptimizedImports()
if (lockedImports.size == lockedImportsBefore) return result
testLog?.append("Trying to build import list again with locked imports: ${lockedImports.joinToString()}\n")
if (importRules.size == importRulesBefore) return result
testLog?.append("Trying to build import list again with import rules: ${importRules.joinToString()}\n")
}
}

Expand All @@ -116,8 +116,8 @@ class OptimizedImportsBuilder(

private fun tryBuildOptimizedImports(): List<ImportPath>? {
val importsToGenerate = HashSet<ImportPath>()
lockedImports
.filterIsInstance<LockedImport.Positive>()
importRules
.filterIsInstance<ImportRule.Add>()
.mapTo(importsToGenerate) { it.importPath }

val descriptorsByParentFqName = HashMap<FqName, MutableSet<DeclarationDescriptor>>()
Expand All @@ -129,7 +129,7 @@ class OptimizedImportsBuilder(

val parentFqName = fqName.parent()
val starImportPath = ImportPath(parentFqName, true)
if (canUseStarImport(descriptor, fqName) && !starImportPath.isNegativeLocked()) {
if (canUseStarImport(descriptor, fqName) && starImportPath.isAllowedByRules()) {
descriptorsByParentFqName.getOrPut(parentFqName) { HashSet() }.add(descriptor)
}
else {
Expand All @@ -147,7 +147,7 @@ class OptimizedImportsBuilder(
val fqNames = descriptors.map { it.importableFqName!! }.toSet()
val nameCountToUseStar = descriptors.first().nameCountToUseStar()
val useExplicitImports = fqNames.size < nameCountToUseStar && !options.isInPackagesToUseStarImport(parentFqName)
|| starImportPath.isNegativeLocked()
|| !starImportPath.isAllowedByRules()
if (useExplicitImports) {
fqNames
.filter { !isImportedByDefault(it) }
Expand Down Expand Up @@ -211,13 +211,13 @@ class OptimizedImportsBuilder(
val starImportPath = ImportPath(fqName.parent(), true)
val importPaths = file.importDirectives.map { it.importPath }
if (explicitImportPath in importPaths) {
lockedImports.add(LockedImport.Positive(explicitImportPath))
importRules.add(ImportRule.Add(explicitImportPath))
}
else if (starImportPath in importPaths) {
lockedImports.add(LockedImport.Positive(starImportPath))
importRules.add(ImportRule.Add(starImportPath))
}
else { // there is no import for this descriptor in the original import list, so do not allow to import it by star-import
lockedImports.add(LockedImport.Negative(starImportPath))
importRules.add(ImportRule.DoNotAdd(starImportPath))
}
}

Expand Down Expand Up @@ -322,5 +322,5 @@ class OptimizedImportsBuilder(
descriptors1.zip(descriptors2).all { it.first.importableFqName == it.second.importableFqName } //TODO: can have different order?
}

private fun ImportPath.isNegativeLocked(): Boolean = lockedImports.any { it is LockedImport.Negative && it.importPath == this }
private fun ImportPath.isAllowedByRules(): Boolean = importRules.none { it is ImportRule.DoNotAdd && it.importPath == this }
}
2 changes: 1 addition & 1 deletion idea/testData/editor/optimizeImports/KT11640.kt.log
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Additional checking of reference KtInvokeFunctionReference: MyFunction()
Additional checking of reference KtInvokeFunctionReference: foo()
Changed resolve of KtInvokeFunctionReference: foo()
Additional checking of reference KtInvokeFunctionReference: println("extension function")
Trying to build import list again with locked imports: bug.a.invoke
Trying to build import list again with import rules: +bug.a.invoke
2 changes: 1 addition & 1 deletion idea/testData/editor/optimizeImports/KT11640_1.kt.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Additional checking of reference KtInvokeFunctionReference: foo()
Changed resolve of KtInvokeFunctionReference: foo()
Trying to build import list again with locked imports: bug.a.*
Trying to build import list again with import rules: +bug.a.*
2 changes: 1 addition & 1 deletion idea/testData/editor/optimizeImports/Overloads.kt.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Additional checking of reference Getter: foo
Additional checking of reference KtSimpleNameReference: foo
Changed resolve of KtSimpleNameReference: foo
Additional checking of reference KtInvokeFunctionReference: foo("")
Trying to build import list again with locked imports: pack1.foo, pack2.*
Trying to build import list again with import rules: +pack1.foo, +pack2.*
Additional checking of reference Getter: foo
Additional checking of reference KtSimpleNameReference: foo
Additional checking of reference KtInvokeFunctionReference: foo("")
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Additional checking of reference KtSimpleNameReference: +=
Changed resolve of KtSimpleNameReference: +=
Trying to build import list again with locked imports: -p2.*, p1.plus
Trying to build import list again with import rules: -p2.*, +p1.plus
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Additional checking of reference KtSimpleNameReference: A
Changed resolve of KtSimpleNameReference: A
Additional checking of reference KtInvokeFunctionReference: A("")
Additional checking of reference KtInvokeFunctionReference: A(1)
Trying to build import list again with locked imports: p2.A, p1.*
Trying to build import list again with import rules: +p2.A, +p1.*

0 comments on commit 57faa5a

Please sign in to comment.