From ce851ed6582023e41790093ccd75bdd40944c0f8 Mon Sep 17 00:00:00 2001 From: Nick Badal Date: Sun, 19 Dec 2021 16:15:48 -0500 Subject: [PATCH] [kotlin] Ignore backticks when comparing imports This helps preserve their ordering when sorted lexicographically ^KTIJ-19394 Fixed closes https://github.com/JetBrains/intellij-community/pull/1879 GitOrigin-RevId: ebc5922726a030aff75903fe130a06791166b18c --- .../kotlin/idea/util/ImportPathComparator.kt | 5 ++++- .../imports/JsOptimizeImportsTestGenerated.java | 5 +++++ .../imports/JvmOptimizeImportsTestGenerated.java | 5 +++++ .../common/BacktickSort.dependency.kt | 5 +++++ .../editor/optimizeImports/common/BacktickSort.kt | 14 ++++++++++++++ .../optimizeImports/common/BacktickSort.kt.after | 14 ++++++++++++++ 6 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.dependency.kt create mode 100644 plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.kt create mode 100644 plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.kt.after diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/util/ImportPathComparator.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/util/ImportPathComparator.kt index a24572d30a291..0843dfb600e37 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/util/ImportPathComparator.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/util/ImportPathComparator.kt @@ -17,7 +17,10 @@ internal class ImportPathComparator(private val packageTable: KotlinPackageEntry import1, import2, { import -> bestEntryMatchIndex(import, ignoreAlias) }, - { import -> import.toString() } + { import -> + // Ignore backticks when comparing lexicographically + import.toString().replace("`", "") + } ) } diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/JsOptimizeImportsTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/JsOptimizeImportsTestGenerated.java index 6ec73e6cdbdf3..97b84cf435911 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/JsOptimizeImportsTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/JsOptimizeImportsTestGenerated.java @@ -82,6 +82,11 @@ public void testArrayAccessExpression() throws Exception { runTest("testData/editor/optimizeImports/common/ArrayAccessExpression.kt"); } + @TestMetadata("BacktickSort.kt") + public void testBacktickSort() throws Exception { + runTest("testData/editor/optimizeImports/common/BacktickSort.kt"); + } + @TestMetadata("ClassMemberImported.kt") public void testClassMemberImported() throws Exception { runTest("testData/editor/optimizeImports/common/ClassMemberImported.kt"); diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/JvmOptimizeImportsTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/JvmOptimizeImportsTestGenerated.java index 70ea821c994b1..23c87737270e6 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/JvmOptimizeImportsTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/JvmOptimizeImportsTestGenerated.java @@ -331,6 +331,11 @@ public void testArrayAccessExpression() throws Exception { runTest("testData/editor/optimizeImports/common/ArrayAccessExpression.kt"); } + @TestMetadata("BacktickSort.kt") + public void testBacktickSort() throws Exception { + runTest("testData/editor/optimizeImports/common/BacktickSort.kt"); + } + @TestMetadata("ClassMemberImported.kt") public void testClassMemberImported() throws Exception { runTest("testData/editor/optimizeImports/common/ClassMemberImported.kt"); diff --git a/plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.dependency.kt b/plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.dependency.kt new file mode 100644 index 0000000000000..b31e749b96f5d --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.dependency.kt @@ -0,0 +1,5 @@ +package b + +fun `fun`() {} +fun g() {} +fun `val`() {} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.kt b/plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.kt new file mode 100644 index 0000000000000..2917a8c14714a --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.kt @@ -0,0 +1,14 @@ +package a + +// Should be sorted, ignoring backticks +import b.g +import b.`fun` +import b.`val` + +class Foo( + fun bar() { + g() + `fun`() + `val`() + } +) \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.kt.after b/plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.kt.after new file mode 100644 index 0000000000000..ff6fbe6f577e4 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/editor/optimizeImports/common/BacktickSort.kt.after @@ -0,0 +1,14 @@ +package a + +// Should be sorted, ignoring backticks +import b.`fun` +import b.g +import b.`val` + +class Foo( + fun bar() { + g() + `fun`() + `val`() + } +) \ No newline at end of file