Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -159,6 +159,10 @@ public class ShortenReferences(val options: (JetElement) -> Options = { Options.
|
|||||||
for (descriptor in descriptorsToImport) {
|
for (descriptor in descriptorsToImport) {
|
||||||
assert(descriptor !in failedToImportDescriptors)
|
assert(descriptor !in failedToImportDescriptors)
|
||||||
|
|
||||||
|
if (importInserter.optimizeImports()) {
|
||||||
|
anyChange = true
|
||||||
|
}
|
||||||
|
|
||||||
val result = importInserter.addImport(descriptor)
|
val result = importInserter.addImport(descriptor)
|
||||||
if (result != ImportInsertHelper.ImportDescriptorResult.ALREADY_IMPORTED) {
|
if (result != ImportInsertHelper.ImportDescriptorResult.ALREADY_IMPORTED) {
|
||||||
anyChange = true
|
anyChange = true
|
||||||
@@ -456,7 +460,6 @@ public class ShortenReferences(val options: (JetElement) -> Options = { Options.
|
|||||||
private val helper = ImportInsertHelper.getInstance(file.getProject())
|
private val helper = ImportInsertHelper.getInstance(file.getProject())
|
||||||
|
|
||||||
fun addImport(target: DeclarationDescriptor): ImportInsertHelper.ImportDescriptorResult {
|
fun addImport(target: DeclarationDescriptor): ImportInsertHelper.ImportDescriptorResult {
|
||||||
optimizeImports()
|
|
||||||
return helper.importDescriptor(file, target)
|
return helper.importDescriptor(file, target)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -151,7 +151,8 @@ public class KotlinImportOptimizer() : ImportOptimizer {
|
|||||||
|
|
||||||
val sortedImportsToGenerate = importsToGenerate.sortBy(importInsertHelper.importSortComparator)
|
val sortedImportsToGenerate = importsToGenerate.sortBy(importInsertHelper.importSortComparator)
|
||||||
|
|
||||||
//TODO: do not touch file if everything is already correct?
|
// check if no changes to imports required
|
||||||
|
if (oldImports.size() == sortedImportsToGenerate.size() && oldImports.map { it.getImportPath() } == sortedImportsToGenerate) return
|
||||||
|
|
||||||
ApplicationManager.getApplication()!!.runWriteAction(Runnable {
|
ApplicationManager.getApplication()!!.runWriteAction(Runnable {
|
||||||
val importList = file.getImportList()!!
|
val importList = file.getImportList()!!
|
||||||
|
|||||||
@@ -331,14 +331,14 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun detectNeededImports(importedDescriptors: Collection<DeclarationDescriptor>): Set<DeclarationDescriptor> {
|
private fun detectNeededImports(importedClasses: Collection<ClassifierDescriptor>): Set<ClassifierDescriptor> {
|
||||||
if (importedDescriptors.isEmpty()) return setOf()
|
if (importedClasses.isEmpty()) return setOf()
|
||||||
|
|
||||||
val descriptorsToCheck = importedDescriptors.map { it.getName() to it }.toMap().toLinkedMap()
|
val classesToCheck = importedClasses.map { it.getName() to it }.toMap().toLinkedMap()
|
||||||
val result = LinkedHashSet<DeclarationDescriptor>()
|
val result = LinkedHashSet<ClassifierDescriptor>()
|
||||||
file.accept(object : JetVisitorVoid() {
|
file.accept(object : JetVisitorVoid() {
|
||||||
override fun visitElement(element: PsiElement) {
|
override fun visitElement(element: PsiElement) {
|
||||||
if (descriptorsToCheck.isEmpty()) return
|
if (classesToCheck.isEmpty()) return
|
||||||
element.acceptChildren(this)
|
element.acceptChildren(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,11 +352,11 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
|
|||||||
if (JetPsiUtil.isSelectorInQualified(expression)) return
|
if (JetPsiUtil.isSelectorInQualified(expression)) return
|
||||||
|
|
||||||
val refName = expression.getReferencedNameAsName()
|
val refName = expression.getReferencedNameAsName()
|
||||||
val descriptor = descriptorsToCheck[refName]
|
val descriptor = classesToCheck[refName]
|
||||||
if (descriptor != null) {
|
if (descriptor != null) {
|
||||||
val targetFqName = targetFqName(expression)
|
val targetFqName = targetFqName(expression)
|
||||||
if (targetFqName != null && targetFqName == DescriptorUtils.getFqNameSafe(descriptor)) {
|
if (targetFqName != null && targetFqName == DescriptorUtils.getFqNameSafe(descriptor)) {
|
||||||
descriptorsToCheck.remove(refName)
|
classesToCheck.remove(refName)
|
||||||
result.add(descriptor)
|
result.add(descriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package foo.`val`
|
||||||
|
|
||||||
|
class `var`
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
// IMPORT: class: foo.val.var
|
||||||
|
// NAME_COUNT_TO_USE_STAR_IMPORT: 10
|
||||||
|
package p
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// IMPORT: class: foo.val.var
|
||||||
|
// NAME_COUNT_TO_USE_STAR_IMPORT: 10
|
||||||
|
package p
|
||||||
|
|
||||||
|
import foo.`val`.`var`
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import java.util.ArrayList
|
||||||
|
// some comment
|
||||||
|
import java.util.HashSet
|
||||||
|
|
||||||
|
val v: HashSet<ArrayList<String>>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import java.util.ArrayList
|
||||||
|
// some comment
|
||||||
|
import java.util.HashSet
|
||||||
|
|
||||||
|
val v: HashSet<ArrayList<String>>
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package foo.`val`
|
||||||
|
|
||||||
|
class `var`
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import foo.`val`.*
|
||||||
|
|
||||||
|
var v: `var`? = null
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import foo.`val`.`var`
|
||||||
|
|
||||||
|
var v: `var`? = null
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// OPTIMIZE_IMPORTS: true
|
||||||
|
// NAME_COUNT_TO_USE_STAR_IMPORT: 1
|
||||||
|
import java.util.HashMap
|
||||||
|
|
||||||
|
val v: HashMap<String, <selection>java.util.Date</selection>>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// OPTIMIZE_IMPORTS: true
|
||||||
|
// NAME_COUNT_TO_USE_STAR_IMPORT: 1
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
val v: HashMap<String, Date>
|
||||||
@@ -186,6 +186,12 @@ public class AddImportTestGenerated extends AbstractAddImportTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("KeywordNames.kt")
|
||||||
|
public void testKeywordNames() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/KeywordNames.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("NameCountForStarNotReached.kt")
|
@TestMetadata("NameCountForStarNotReached.kt")
|
||||||
public void testNameCountForStarNotReached() throws Exception {
|
public void testNameCountForStarNotReached() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/NameCountForStarNotReached.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/NameCountForStarNotReached.kt");
|
||||||
|
|||||||
@@ -81,6 +81,12 @@ public class OptimizeImportsTestGenerated extends AbstractOptimizeImportsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("DoNotTouchIfNoChanges.kt")
|
||||||
|
public void testDoNotTouchIfNoChanges() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/editor/optimizeImports/DoNotTouchIfNoChanges.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("DuplicatedImports.kt")
|
@TestMetadata("DuplicatedImports.kt")
|
||||||
public void testDuplicatedImports() throws Exception {
|
public void testDuplicatedImports() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/editor/optimizeImports/DuplicatedImports.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/editor/optimizeImports/DuplicatedImports.kt");
|
||||||
@@ -111,6 +117,12 @@ public class OptimizeImportsTestGenerated extends AbstractOptimizeImportsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("KeywordNames.kt")
|
||||||
|
public void testKeywordNames() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/editor/optimizeImports/KeywordNames.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("KotlinPackage.kt")
|
@TestMetadata("KotlinPackage.kt")
|
||||||
public void testKotlinPackage() throws Exception {
|
public void testKotlinPackage() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/editor/optimizeImports/KotlinPackage.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/editor/optimizeImports/KotlinPackage.kt");
|
||||||
|
|||||||
@@ -186,6 +186,12 @@ public class ShortenRefsTestGenerated extends AbstractShortenRefsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("ImportsOnTheFlyBug.kt")
|
||||||
|
public void testImportsOnTheFlyBug() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/shortenRefs/imports/ImportsOnTheFlyBug.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("leaveQualifiedConstructor.kt")
|
@TestMetadata("leaveQualifiedConstructor.kt")
|
||||||
public void testLeaveQualifiedConstructor() throws Exception {
|
public void testLeaveQualifiedConstructor() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/shortenRefs/imports/leaveQualifiedConstructor.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/shortenRefs/imports/leaveQualifiedConstructor.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user