Treat method as simple if its code block doesn't contain any linefeeds (IDEA-136990)
authorYaroslav Lepenkin <yaroslav.lepenkin@jetbrains.com>
Thu, 26 Feb 2015 10:53:05 +0000 (13:53 +0300)
committerYaroslav Lepenkin <yaroslav.lepenkin@jetbrains.com>
Thu, 26 Feb 2015 11:12:03 +0000 (14:12 +0300)
java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java
java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterBracesTest.java

index f544a071997dcd55ed3e6c9734bb9269b81e0f5b..821460ad934369c1073ac353063fca37ed324db0 100644 (file)
@@ -368,18 +368,8 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor {
 
   private boolean shouldHandleAsSimpleMethod(@NotNull PsiMethod method) {
     if (!mySettings.KEEP_SIMPLE_METHODS_IN_ONE_LINE) return false;
-    
-    boolean skipElement = true;
-    for (PsiElement element : method.getChildren()) {
-      if (element instanceof PsiTypeElement) skipElement = false;
-      if (skipElement) continue;
-    
-      if (element.textContains('\n')) {
-        return false;
-      }
-    }
-
-    return true;
+    PsiCodeBlock body = method.getBody();
+    return body != null && !body.textContains('\n');
   }
 
   private static int getMethodHeaderStartOffset(@NotNull PsiMethod method) {
index 138e12cb649f93c6746e59d5919869fb7ac4d5e9..084727dda9f528d09833f2e02a49dcdf817ea082 100644 (file)
@@ -329,4 +329,19 @@ public class JavaFormatterBracesTest extends AbstractJavaFormatterTest {
       "}\n"
     );
   }
+
+  public void testMethodIsSimple_IfCodeBlockHasNoLinefeeds() {
+    getSettings().KEEP_SIMPLE_METHODS_IN_ONE_LINE = true;
+    doClassTest(
+      "public ModelAndView handleRequestInternalEmptyMulti(\n" +
+      "        final HttpServletRequest httpServletRequest,\n" +
+      "      final HttpServletResponse response)\n" +
+      "      throws IOException {}",
+      "public ModelAndView handleRequestInternalEmptyMulti(\n" +
+      "        final HttpServletRequest httpServletRequest,\n" +
+      "        final HttpServletResponse response)\n" +
+      "        throws IOException {}"
+    );
+  }
+
 }