OSDN Git Service

Formatting fix: ensure no double blank new lines between elements
authorTor Norbye <tnorbye@google.com>
Tue, 4 Oct 2011 16:42:44 +0000 (09:42 -0700)
committerTor Norbye <tnorbye@google.com>
Tue, 4 Oct 2011 16:49:24 +0000 (09:49 -0700)
Change-Id: Id41a5543b4e4639eb8c9943463587168c90f9ff4

eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java
eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java

index f473671..e2c0780 100644 (file)
@@ -690,6 +690,10 @@ public class XmlPrettyPrinter {
     }
 
     private boolean newlineBeforeElementOpen(Element element, int depth) {
+        if (hasBlankLineAbove()) {
+            return false;
+        }
+
         if (mPrefs.removeEmptyLines || depth <= 0) {
             return false;
         }
@@ -797,6 +801,10 @@ public class XmlPrettyPrinter {
     }
 
     private boolean newlineAfterElementClose(Element element, int depth) {
+        if (hasBlankLineAbove()) {
+            return false;
+        }
+
         return element.getParentNode().getNodeType() == Node.ELEMENT_NODE
                 && !keepElementAsSingleLine(depth - 1, (Element) element.getParentNode());
     }
index 7436b2f..3664e70 100644 (file)
@@ -25,6 +25,7 @@ import org.w3c.dom.NodeList;
 
 import junit.framework.TestCase;
 
+@SuppressWarnings("javadoc")
 public class XmlPrettyPrinterTest extends TestCase {
     @Override
     protected void setUp() throws Exception {
@@ -533,4 +534,22 @@ public class XmlPrettyPrinterTest extends TestCase {
                 "\n" +
                 "</foo>");
     }
+
+    public void testCommentHandling2() throws Exception {
+        checkFormat(
+                XmlFormatPreferences.create(), XmlFormatStyle.LAYOUT,
+                "<foo >\n" +
+                "    <!-- multi -->\n" +
+                "\n" +
+                "    <bar />\n" +
+                "</foo>",
+
+                "<foo >\n" +
+                "\n" +
+                "    <!-- multi -->\n" +
+                "\n" +
+                "    <bar />\n" +
+                "\n" +
+                "</foo>");
+    }
 }