From 44edd94fea9cb1a5de0d78a54745b878920be635 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 12 Sep 2019 18:01:12 +0900 Subject: [PATCH] "Create member function" quick fix: do not add redundant semicolons after enum entry #KT-14899 Fixed --- .../idea/formatter/KotlinPreFormatProcessor.kt | 7 ++++--- .../createFunction/call/funOnEnumClass.kt | 8 ++++++++ .../createFunction/call/funOnEnumClass.kt.after | 12 ++++++++++++ .../createFunction/call/funOnEnumClass2.kt | 8 ++++++++ .../createFunction/call/funOnEnumClass2.kt.after | 12 ++++++++++++ .../kotlin/idea/quickfix/QuickFixTestGenerated.java | 10 ++++++++++ 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass.kt create mode 100644 idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass.kt.after create mode 100644 idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass2.kt create mode 100644 idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass2.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinPreFormatProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinPreFormatProcessor.kt index d974dfc03bd..0e4fd680053 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinPreFormatProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinPreFormatProcessor.kt @@ -60,10 +60,11 @@ private class Visitor(var range: TextRange) : KtTreeVisitorVoid() { prevEntry.add(comma) delta += comma.textLength } - } - else { + } else { val lastEntry = klass.declarations.lastIsInstanceOrNull() - if (lastEntry != null && lastEntry.containsToken(KtTokens.SEMICOLON)) return + if (lastEntry != null && + (lastEntry.containsToken(KtTokens.SEMICOLON) || lastEntry.nextSibling?.node?.elementType == KtTokens.SEMICOLON) + ) return if (lastEntry == null && classBody.containsToken(KtTokens.SEMICOLON)) return val semicolon = psiFactory.createSemicolon() diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass.kt b/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass.kt new file mode 100644 index 00000000000..1f6f963a579 --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass.kt @@ -0,0 +1,8 @@ +// "Create member function 'Bar.foo'" "true" +fun foo() { + Bar.BAZ.foo() +} + +enum class Bar { + BAZ +} \ No newline at end of file diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass.kt.after b/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass.kt.after new file mode 100644 index 00000000000..a16123e04c9 --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass.kt.after @@ -0,0 +1,12 @@ +// "Create member function 'Bar.foo'" "true" +fun foo() { + Bar.BAZ.foo() +} + +enum class Bar { + BAZ; + + fun foo() { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass2.kt b/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass2.kt new file mode 100644 index 00000000000..f10b0ad2a13 --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass2.kt @@ -0,0 +1,8 @@ +// "Create member function 'Bar.foo'" "true" +fun foo() { + Bar.BAZ.foo() +} + +enum class Bar { + BAZ; +} \ No newline at end of file diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass2.kt.after b/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass2.kt.after new file mode 100644 index 00000000000..a16123e04c9 --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass2.kt.after @@ -0,0 +1,12 @@ +// "Create member function 'Bar.foo'" "true" +fun foo() { + Bar.BAZ.foo() +} + +enum class Bar { + BAZ; + + fun foo() { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 62b7e414b5e..5c883acd6cf 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -3792,6 +3792,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { runTest("idea/testData/quickfix/createFromUsage/createFunction/call/funOnClassObject.kt"); } + @TestMetadata("funOnEnumClass.kt") + public void testFunOnEnumClass() throws Exception { + runTest("idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass.kt"); + } + + @TestMetadata("funOnEnumClass2.kt") + public void testFunOnEnumClass2() throws Exception { + runTest("idea/testData/quickfix/createFromUsage/createFunction/call/funOnEnumClass2.kt"); + } + @TestMetadata("funOnLibObject.kt") public void testFunOnLibObject() throws Exception { runTest("idea/testData/quickfix/createFromUsage/createFunction/call/funOnLibObject.kt");