OSDN Git Service

Formatting fixes for color lists
authorTor Norbye <tnorbye@google.com>
Tue, 6 Sep 2011 21:19:12 +0000 (14:19 -0700)
committerTor Norbye <tnorbye@google.com>
Tue, 6 Sep 2011 21:19:12 +0000 (14:19 -0700)
Color lists ended up getting formatted using the generic layout style
instead of the resource style (with multiple attributes packed into
the same line).

Furthermore, by convention the "color" attribute always logically
sorts to the end, after the various state flags, so update the
attribute sorting rules to reflect this.

Change-Id: Iaa88106f7eaf7c953b3a97dc203da5833a3bf085

eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/color/ColorDescriptors.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/AndroidXmlFormattingStrategy.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/uimodel/UiAttributeNode.java
eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java

index f6e50a8..4147fdd 100644 (file)
@@ -37,9 +37,12 @@ public class ColorDescriptors implements IDescriptorProvider {
     private static final String SDK_URL =
         "http://d.android.com/guide/topics/resources/color-list-resource.html"; //$NON-NLS-1$
 
+    public static final String SELECTOR_TAG = "selector";               //$NON-NLS-1$
+    public static final String ATTR_COLOR = "color";                    //$NON-NLS-1$
+
     /** The root element descriptor */
     private ElementDescriptor mDescriptor = new ElementDescriptor(
-            "selector", "Selector",
+            SELECTOR_TAG, "Selector",
             "Required. This must be the root element. Contains one or more <item> elements.",
             SDK_URL,
             new AttributeDescriptor[] { new XmlnsAttributeDescriptor(ANDROID_NS_NAME,
@@ -71,7 +74,7 @@ public class ColorDescriptors implements IDescriptorProvider {
                  + "its attributes. Must be a child of a <selector> element.",
             SDK_URL,
             new ReferenceAttributeDescriptor(
-                    ResourceType.COLOR, "color", "color", //$NON-NLS-1$ //$NON-NLS-2$
+                    ResourceType.COLOR, ATTR_COLOR, ATTR_COLOR,
                     SdkConstants.NS_RESOURCES,
                     "Hexadeximal color. Required. The color is specified with an RGB value and "
                         + "optional alpha channel.\n"
index ad0981b..4afa101 100644 (file)
@@ -17,6 +17,7 @@ package com.android.ide.eclipse.adt.internal.editors.formatting;
 
 import static com.android.ide.eclipse.adt.internal.editors.AndroidXmlAutoEditStrategy.findLineStart;
 import static com.android.ide.eclipse.adt.internal.editors.AndroidXmlAutoEditStrategy.findTextStart;
+import static com.android.ide.eclipse.adt.internal.editors.color.ColorDescriptors.SELECTOR_TAG;
 import static org.eclipse.jface.text.formatter.FormattingContextProperties.CONTEXT_MEDIUM;
 import static org.eclipse.jface.text.formatter.FormattingContextProperties.CONTEXT_PARTITION;
 import static org.eclipse.jface.text.formatter.FormattingContextProperties.CONTEXT_REGION;
@@ -55,6 +56,7 @@ import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
 import org.eclipse.wst.xml.ui.internal.XMLFormattingStrategy;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
@@ -506,9 +508,14 @@ public class AndroidXmlFormattingStrategy extends ContextBasedFormattingStrategy
         // The "resource" style is used for most value-based XML files:
         // strings, dimensions, booleans, colors, integers, plurals,
         // integer-arrays, string-arrays, and typed-arrays
-        if (domDocument.getDocumentElement() != null
-                && ResourcesDescriptors.ROOT_ELEMENT.equals(domDocument.getDocumentElement()
-                        .getTagName())) {
+        Element rootElement = domDocument.getDocumentElement();
+        if (rootElement != null
+                && ResourcesDescriptors.ROOT_ELEMENT.equals(rootElement.getTagName())) {
+            style = XmlFormatStyle.RESOURCE;
+        }
+
+        // Selectors are also used similar to resources
+        if (rootElement != null && SELECTOR_TAG.equals(rootElement.getTagName())) {
             style = XmlFormatStyle.RESOURCE;
         }
 
index 94cea34..a415e3c 100644 (file)
@@ -21,6 +21,7 @@ import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_HEIGHT;
 import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_PREFIX;
 import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_WIDTH;
 import static com.android.ide.common.layout.LayoutConstants.ATTR_STYLE;
+import static com.android.ide.eclipse.adt.internal.editors.color.ColorDescriptors.ATTR_COLOR;
 
 import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor;
 import com.android.ide.eclipse.adt.internal.editors.descriptors.AttributeDescriptor;
@@ -227,6 +228,11 @@ public abstract class UiAttributeNode implements Comparable<UiAttributeNode> {
             return 50;
         }
 
+        // "color" sorts to the end
+        if (ATTR_COLOR.equals(name)) {
+            return 100;
+        }
+
         return 60;
     }
 }
index 0aa4af3..0d6c19d 100644 (file)
@@ -329,18 +329,34 @@ public class XmlPrettyPrinterTest extends TestCase {
                 "<selector xmlns:android=\"http://schemas.android.com/apk/res/android\" >\n" +
                 "\n" +
                 "    <item\n" +
-                "        android:color=\"#ffff0000\"\n" +
-                "        android:state_pressed=\"true\"></item> <!-- pressed -->\n" +
+                "        android:state_pressed=\"true\"\n" +
+                "        android:color=\"#ffff0000\"></item> <!-- pressed -->\n" +
                 "\n" +
                 "    <item\n" +
-                "        android:color=\"#ff0000ff\"\n" +
-                "        android:state_focused=\"true\"></item> <!-- focused -->\n" +
+                "        android:state_focused=\"true\"\n" +
+                "        android:color=\"#ff0000ff\"></item> <!-- focused -->\n" +
                 "\n" +
                 "    <item android:color=\"#ff000000\"></item> <!-- default -->\n" +
                 "\n" +
                 "</selector>");
     }
 
+    public void testFormatColorList() throws Exception {
+        checkFormat(
+                XmlFormatStyle.RESOURCE,
+                "<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">\n" +
+                "<item android:state_activated=\"true\" android:color=\"#FFFFFF\"/>\n" +
+                "<item android:color=\"#777777\" /> <!-- not selected -->\n" +
+                "</selector>",
+
+                "<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">\n" +
+                "\n" +
+                "    <item android:state_activated=\"true\" android:color=\"#FFFFFF\"/>\n" +
+                "    <item android:color=\"#777777\"/> <!-- not selected -->\n" +
+                "\n" +
+                "</selector>");
+    }
+
     public void testPreserveNewlineAfterComment() throws Exception {
         checkFormat(
                 XmlFormatStyle.RESOURCE,