From 523cbc6723b6d571635b036cec864aad91f22d1e Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 5 Jul 2017 14:20:57 +0300 Subject: [PATCH] Convert to expression body: insert new-line after = for long lines --- .../inspections/UseExpressionBodyInspection.kt | 14 +++++++++++++- .../useExpressionBody/veryVeryLong.kt | 5 +++++ .../useExpressionBody/veryVeryLong.kt.after | 4 ++++ .../basic/callableReferenceSelector.kt.after | 3 ++- .../localExtensionFunChainedCalls.kt.after | 3 ++- .../typeParametersAndConstraintsCombined1.kt.after | 3 ++- .../inspections/LocalInspectionTestGenerated.java | 6 ++++++ 7 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 idea/testData/inspectionsLocal/useExpressionBody/veryVeryLong.kt create mode 100644 idea/testData/inspectionsLocal/useExpressionBody/veryVeryLong.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UseExpressionBodyInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UseExpressionBodyInspection.kt index 011ef198cdb..52464061631 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UseExpressionBodyInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UseExpressionBodyInspection.kt @@ -174,9 +174,21 @@ class UseExpressionBodyInspection(private val convertEmptyToUnit: Boolean) : Abs val commentSaver = CommentSaver(body) - declaration.addBefore(KtPsiFactory(declaration).createEQ(), body) + val factory = KtPsiFactory(declaration) + declaration.addBefore(factory.createEQ(), body) val newBody = body.replaced(value) + val editor = declaration.findExistingEditor() + if (editor != null) { + val startOffset = newBody.startOffset + val document = editor.document + val startLine = document.getLineNumber(startOffset) + val rightMargin = editor.settings.getRightMargin(editor.project) + if (document.getLineEndOffset(startLine) - document.getLineStartOffset(startLine) >= rightMargin) { + declaration.addBefore(factory.createNewLine(), newBody) + } + } + commentSaver.restore(newBody) if (deleteTypeHandler != null && declaration is KtCallableDeclaration) { diff --git a/idea/testData/inspectionsLocal/useExpressionBody/veryVeryLong.kt b/idea/testData/inspectionsLocal/useExpressionBody/veryVeryLong.kt new file mode 100644 index 00000000000..580b81e8592 --- /dev/null +++ b/idea/testData/inspectionsLocal/useExpressionBody/veryVeryLong.kt @@ -0,0 +1,5 @@ +fun foo(s1: String, s2: String) = s1 + s2 + +fun bar(): String { + return foo("gjahkrgkjuyrtetyuairytoiuyareoihuyitouy897304hkdjgnba", "uiyiuye987130423toiuywtuiyrwityriwyhw") +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/useExpressionBody/veryVeryLong.kt.after b/idea/testData/inspectionsLocal/useExpressionBody/veryVeryLong.kt.after new file mode 100644 index 00000000000..aac8938fae3 --- /dev/null +++ b/idea/testData/inspectionsLocal/useExpressionBody/veryVeryLong.kt.after @@ -0,0 +1,4 @@ +fun foo(s1: String, s2: String) = s1 + s2 + +fun bar(): String = + foo("gjahkrgkjuyrtetyuairytoiuyareoihuyitouy897304hkdjgnba", "uiyiuye987130423toiuywtuiyrwityriwyhw") \ No newline at end of file diff --git a/idea/testData/refactoring/extractFunction/basic/callableReferenceSelector.kt.after b/idea/testData/refactoring/extractFunction/basic/callableReferenceSelector.kt.after index 88cf7a7f83b..010442d23ff 100644 --- a/idea/testData/refactoring/extractFunction/basic/callableReferenceSelector.kt.after +++ b/idea/testData/refactoring/extractFunction/basic/callableReferenceSelector.kt.after @@ -10,6 +10,7 @@ class Foo { arguments = map(arg) } - private fun map(arg: String) = mapOf(Foo::arguments.name to arg) + private fun map(arg: String) = + mapOf(Foo::arguments.name to arg) } } \ No newline at end of file diff --git a/idea/testData/refactoring/extractFunction/parameters/capturedFunctions/localExtensionFunChainedCalls.kt.after b/idea/testData/refactoring/extractFunction/parameters/capturedFunctions/localExtensionFunChainedCalls.kt.after index ea2f709cba5..6f14a88dbe9 100644 --- a/idea/testData/refactoring/extractFunction/parameters/capturedFunctions/localExtensionFunChainedCalls.kt.after +++ b/idea/testData/refactoring/extractFunction/parameters/capturedFunctions/localExtensionFunChainedCalls.kt.after @@ -13,4 +13,5 @@ fun foo(n: Int): Int { return i(n, Int::bar1, Int::bar2) } -private fun i(n: Int, bar1: Int.(m: Int) -> Int, bar2: Int.(m: Int) -> Int) = n.bar1(n + 1).bar2(n + 2) \ No newline at end of file +private fun i(n: Int, bar1: Int.(m: Int) -> Int, bar2: Int.(m: Int) -> Int) = + n.bar1(n + 1).bar2(n + 2) \ No newline at end of file diff --git a/idea/testData/refactoring/extractFunction/typeParameters/typeParametersAndConstraintsCombined1.kt.after b/idea/testData/refactoring/extractFunction/typeParameters/typeParametersAndConstraintsCombined1.kt.after index 8a514fdf410..ad0893c2a0c 100644 --- a/idea/testData/refactoring/extractFunction/typeParameters/typeParametersAndConstraintsCombined1.kt.after +++ b/idea/testData/refactoring/extractFunction/typeParameters/typeParametersAndConstraintsCombined1.kt.after @@ -17,4 +17,5 @@ class A(val t: T) where T: DataEx { } } -private fun i(a: A, b: A.B, v: V) where T : DataEx, U : DataExEx, V : DataEx = a.t.x + b.u.x + v.x \ No newline at end of file +private fun i(a: A, b: A.B, v: V) where T : DataEx, U : DataExEx, V : DataEx = + a.t.x + b.u.x + v.x \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 4521ce8b95f..28bff08c8d2 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -863,6 +863,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { doTest(fileName); } + @TestMetadata("veryVeryLong.kt") + public void testVeryVeryLong() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/useExpressionBody/veryVeryLong.kt"); + doTest(fileName); + } + @TestMetadata("when.kt") public void testWhen() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/useExpressionBody/when.kt");