OSDN Git Service

Look up space width from settings instead of hardcoded to 4
authorTor Norbye <tnorbye@google.com>
Mon, 3 Oct 2011 21:31:11 +0000 (14:31 -0700)
committerTor Norbye <tnorbye@google.com>
Tue, 4 Oct 2011 00:08:24 +0000 (17:08 -0700)
Change-Id: Ib84a238d8586cc03915ea5dc394f2309f58d7052

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

index dba33c3..0702776 100644 (file)
@@ -19,6 +19,9 @@ import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs;
 import com.android.ide.eclipse.adt.internal.preferences.AttributeSortOrder;
 
 import org.eclipse.core.runtime.Preferences;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.ui.internal.editors.text.EditorsPlugin;
+import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
 import org.eclipse.wst.xml.core.internal.XMLCorePlugin;
 import org.eclipse.wst.xml.core.internal.preferences.XMLCorePreferenceNames;
 
@@ -50,6 +53,9 @@ public class XmlFormatPreferences {
     /** The string to insert for each indentation level */
     private String mOneIndentUnit = "    "; //$NON-NLS-1$
 
+    /** Tab width (number of spaces to display for a tab) */
+    private int mTabWidth = -1; // -1: uninitialized
+
     private XmlFormatPreferences() {
     }
 
@@ -105,4 +111,27 @@ public class XmlFormatPreferences {
 
         return mOneIndentUnit;
     }
+
+    /**
+     * Returns the number of spaces used to display a single tab character
+     *
+     * @return the number of spaces used to display a single tab character
+     */
+    @SuppressWarnings("restriction") // Editor settings
+    public int getTabWidth() {
+        if (mTabWidth == -1) {
+            String key = AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH;
+            try {
+                IPreferenceStore prefs = EditorsPlugin.getDefault().getPreferenceStore();
+                mTabWidth = prefs.getInt(key);
+            } catch (Throwable t) {
+                // Pass: We'll pick a suitable default instead below
+            }
+            if (mTabWidth <= 0) {
+                mTabWidth = 4;
+            }
+        }
+
+        return mTabWidth;
+    }
 }
index a884532..f473671 100644 (file)
@@ -451,7 +451,6 @@ public class XmlPrettyPrinter {
                 if (!startsWithNewline) {
                     Node previous = node.getPreviousSibling();
                     if (previous != null && previous.getNodeType() == Node.TEXT_NODE) {
-                        int TAB_SIZE = 4; // TODO: Look up Eclipse settings
                         String prevText = previous.getNodeValue();
                         int indentation = COMMENT_BEGIN.length();
                         for (int i = prevText.length() - 1; i >= 0; i--) {
@@ -459,7 +458,7 @@ public class XmlPrettyPrinter {
                             if (c == '\n') {
                                 break;
                             } else {
-                                indentation += (c == '\t') ? TAB_SIZE : 1;
+                                indentation += (c == '\t') ? mPrefs.getTabWidth() : 1;
                             }
                         }
 
@@ -494,7 +493,7 @@ public class XmlPrettyPrinter {
                                     }
                                     break;
                                 } else {
-                                    indent += (c == '\t') ? TAB_SIZE : 1;
+                                    indent += (c == '\t') ? mPrefs.getTabWidth() : 1;
                                 }
                             }
                         }
@@ -509,7 +508,7 @@ public class XmlPrettyPrinter {
                                 if (!Character.isWhitespace(c)) {
                                     break;
                                 } else {
-                                    indentation -= (c == '\t') ? TAB_SIZE : 1;
+                                    indentation -= (c == '\t') ? mPrefs.getTabWidth() : 1;
                                 }
                             }
                         }